diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/MultipleSchemaSwaps.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/MultipleSchemaSwaps.java new file mode 100644 index 00000000000..4e78b23e87f --- /dev/null +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/MultipleSchemaSwaps.java @@ -0,0 +1,29 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.crd.generator.schemaswaps; + +import io.fabric8.crd.generator.annotation.SchemaSwap; +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Version("v1") +@Group("acme.com") +@SchemaSwap(originalType = SchemaSwapSpec.SomeObject.class, fieldName = "shouldBeString", targetType = String.class) +@SchemaSwap(originalType = SchemaSwapSpec.AnotherObject.class, fieldName = "shouldBeInt", targetType = Integer.class) +public class MultipleSchemaSwaps extends CustomResource { + +} diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/SchemaSwapCRDTest.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/SchemaSwapCRDTest.java new file mode 100644 index 00000000000..2819b0cf134 --- /dev/null +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/SchemaSwapCRDTest.java @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.crd.generator.schemaswaps; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition; +import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion; +import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; +import io.fabric8.kubernetes.client.utils.Serialization; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SchemaSwapCRDTest { + + @Test + void testCrd() { + CustomResourceDefinition d = Serialization.unmarshal(getClass().getClassLoader() + .getResourceAsStream("META-INF/fabric8/multipleschemaswaps.acme.com-v1.yml"), + CustomResourceDefinition.class); + assertNotNull(d); + + CustomResourceDefinitionVersion v1 = d.getSpec().getVersions().get(0); + assertNotNull(v1); + assertEquals("v1", v1.getName()); + Map spec = v1.getSchema().getOpenAPIV3Schema().getProperties().get("spec").getProperties(); + assertNotNull(spec); + + // 'first' is replaced by SchemaSwap from int to string + JSONSchemaProps first = spec.get("first"); + Map firstProps = first.getProperties(); + assertNotNull(firstProps); + JSONSchemaProps firstProperty = firstProps.get("shouldBeString"); + assertEquals("string", firstProperty.getType()); + + // 'second' is replaced by the same SchemaSwap that is applied multiple times + JSONSchemaProps second = spec.get("second"); + Map secondProps = second.getProperties(); + assertNotNull(secondProps); + JSONSchemaProps secondProperty = secondProps.get("shouldBeString"); + assertEquals("string", secondProperty.getType()); + + // 'third' is replaced by the another SchemaSwap + JSONSchemaProps third = spec.get("third"); + Map thirdProps = third.getProperties(); + assertNotNull(thirdProps); + JSONSchemaProps thirdProperty = thirdProps.get("shouldBeInt"); + assertEquals("integer", thirdProperty.getType()); + } + +} diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/SchemaSwapSpec.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/SchemaSwapSpec.java new file mode 100644 index 00000000000..80dfab67d59 --- /dev/null +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/schemaswaps/SchemaSwapSpec.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.crd.generator.schemaswaps; + +public class SchemaSwapSpec { + private SomeObject first; + private SomeObject second; + private AnotherObject third; + + static class SomeObject { + private int shouldBeString; + } + + static class AnotherObject { + private String shouldBeInt; + } +}