Skip to content

Commit

Permalink
add tests for generics
Browse files Browse the repository at this point in the history
  • Loading branch information
euberseder-hubspot committed Dec 22, 2023
1 parent bbc0b7f commit 2adcc55
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.fabric8.crd.example.generic;

public class Generic<T> {
T bar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.fabric8.crd.example.generic;

import java.util.List;

public class NestedGeneric<P> {
Generic<P> quux;
List<P> corge;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.fabric8.crd.example.generic;

import io.fabric8.kubernetes.client.CustomResource;

public class ResourceWithGeneric extends CustomResource<ResourceWithGenericSpec, Void> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.fabric8.crd.example.generic;

public class ResourceWithGenericSpec {
Generic<String> foo;
Generic<Integer> baz;
NestedGeneric<String> qux;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import io.fabric8.crd.example.extraction.Extraction;
import io.fabric8.crd.example.extraction.IncorrectExtraction;
import io.fabric8.crd.example.extraction.IncorrectExtraction2;
import io.fabric8.crd.example.generic.ResourceWithGeneric;
import io.fabric8.crd.example.json.ContainingJson;
import io.fabric8.crd.example.person.Person;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrArray;
import io.sundr.model.TypeDef;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -222,6 +224,51 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
assertNull(barProps.get("baz"));
}

@Test
void shouldProcessGenericClasses() {
TypeDef resourceWithGeneric = Types.typeDefFrom(ResourceWithGeneric.class);
JSONSchemaProps schema = JsonSchema.from(resourceWithGeneric);
assertNotNull(schema);

Map<String, JSONSchemaProps> properties = schema.getProperties();
assertEquals(2, properties.size());

final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = specSchema.getProperties();
assertEquals(3, spec.size());

JSONSchemaProps foo = spec.get("foo");
assertNotNull(foo);
Map<String, JSONSchemaProps> fooProps = foo.getProperties();
assertNotNull(fooProps);
assertEquals("string", fooProps.get("bar").getType());

JSONSchemaProps baz = spec.get("baz");
assertNotNull(baz);
Map<String, JSONSchemaProps> bazProps = baz.getProperties();
assertNotNull(bazProps);
assertEquals("integer", bazProps.get("bar").getType());

JSONSchemaProps qux = spec.get("qux");
assertNotNull(qux);
Map<String, JSONSchemaProps> quxProps = qux.getProperties();
assertEquals(2, quxProps.size());

JSONSchemaProps quux = quxProps.get("quux");
assertNotNull(quux);
Map<String, JSONSchemaProps> quuxProps = quux.getProperties();
assertNotNull(quuxProps);
assertEquals("string", quuxProps.get("bar").getType());

JSONSchemaProps corge = quxProps.get("corge");
assertNotNull(corge);
JSONSchemaPropsOrArray corgeItems = corge.getItems();
assertNotNull(corgeItems);
JSONSchemaProps corgeItemsProps = corgeItems.getSchema();
assertNotNull(corgeItemsProps);
assertEquals("string", corgeItemsProps.getType());
}

@Test
void shouldThrowIfSchemaSwapHasUnmatchedField() {
TypeDef incorrectExtraction = Types.typeDefFrom(IncorrectExtraction.class);
Expand Down

0 comments on commit 2adcc55

Please sign in to comment.