From 2adcc555f5ec44e85fed5aa18cbe60d5a3043426 Mon Sep 17 00:00:00 2001 From: Ethan Uberseder Date: Fri, 22 Dec 2023 15:15:35 -0500 Subject: [PATCH] add tests for generics --- .../fabric8/crd/example/generic/Generic.java | 5 ++ .../crd/example/generic/NestedGeneric.java | 8 ++++ .../example/generic/ResourceWithGeneric.java | 6 +++ .../generic/ResourceWithGenericSpec.java | 7 +++ .../crd/generator/v1/JsonSchemaTest.java | 47 +++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 crd-generator/api/src/test/java/io/fabric8/crd/example/generic/Generic.java create mode 100644 crd-generator/api/src/test/java/io/fabric8/crd/example/generic/NestedGeneric.java create mode 100644 crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGeneric.java create mode 100644 crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGenericSpec.java diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/Generic.java b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/Generic.java new file mode 100644 index 00000000000..49a9633d727 --- /dev/null +++ b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/Generic.java @@ -0,0 +1,5 @@ +package io.fabric8.crd.example.generic; + +public class Generic { + T bar; +} diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/NestedGeneric.java b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/NestedGeneric.java new file mode 100644 index 00000000000..6dad0c9e742 --- /dev/null +++ b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/NestedGeneric.java @@ -0,0 +1,8 @@ +package io.fabric8.crd.example.generic; + +import java.util.List; + +public class NestedGeneric

{ + Generic

quux; + List

corge; +} diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGeneric.java b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGeneric.java new file mode 100644 index 00000000000..7c08b5d5a80 --- /dev/null +++ b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGeneric.java @@ -0,0 +1,6 @@ +package io.fabric8.crd.example.generic; + +import io.fabric8.kubernetes.client.CustomResource; + +public class ResourceWithGeneric extends CustomResource { +} diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGenericSpec.java b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGenericSpec.java new file mode 100644 index 00000000000..5f93bfadcdb --- /dev/null +++ b/crd-generator/api/src/test/java/io/fabric8/crd/example/generic/ResourceWithGenericSpec.java @@ -0,0 +1,7 @@ +package io.fabric8.crd.example.generic; + +public class ResourceWithGenericSpec { + Generic foo; + Generic baz; + NestedGeneric qux; +} diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java b/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java index 57f7c071990..8d487b77a1b 100644 --- a/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java +++ b/crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java @@ -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; @@ -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 properties = schema.getProperties(); + assertEquals(2, properties.size()); + + final JSONSchemaProps specSchema = properties.get("spec"); + Map spec = specSchema.getProperties(); + assertEquals(3, spec.size()); + + JSONSchemaProps foo = spec.get("foo"); + assertNotNull(foo); + Map fooProps = foo.getProperties(); + assertNotNull(fooProps); + assertEquals("string", fooProps.get("bar").getType()); + + JSONSchemaProps baz = spec.get("baz"); + assertNotNull(baz); + Map bazProps = baz.getProperties(); + assertNotNull(bazProps); + assertEquals("integer", bazProps.get("bar").getType()); + + JSONSchemaProps qux = spec.get("qux"); + assertNotNull(qux); + Map quxProps = qux.getProperties(); + assertEquals(2, quxProps.size()); + + JSONSchemaProps quux = quxProps.get("quux"); + assertNotNull(quux); + Map 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);