From a04ca3d267a7128a9836b3f0cb4956929d8e2fb2 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Tue, 7 May 2024 15:27:20 +0200 Subject: [PATCH 1/2] test(crd-generator): approval tests for crd-generator-api-v2 Signed-off-by: Marc Nuri --- crd-generator/test/pom.xml | 4 ++ .../CRDGeneratorApprovalTest.java | 66 ++++++++++++++----- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/crd-generator/test/pom.xml b/crd-generator/test/pom.xml index c00c3847d1e..f6517b92546 100644 --- a/crd-generator/test/pom.xml +++ b/crd-generator/test/pom.xml @@ -38,6 +38,10 @@ io.fabric8 crd-generator-api + + io.fabric8 + crd-generator-api-v2 + org.projectlombok lombok diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java index 8d05c1cb7de..e2727a7cb10 100644 --- a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.java @@ -72,7 +72,7 @@ void tearDown() { @ParameterizedTest(name = "{1}.{2} parallel={3}") @MethodSource("crdApprovalTests") @DisplayName("CRD Generator V1 Approval Tests") - void v1ApprovalTest( + void apiV1ApprovalTest( Class>[] crClasses, String expectedCrd, String version, boolean parallel) { final Map> result = new CRDGenerator() .withParallelGenerationEnabled(parallel) @@ -94,23 +94,58 @@ void v1ApprovalTest( new Namer(expectedCrd, version)); } + @ParameterizedTest(name = "{1}.{2} parallel={3}") + @MethodSource("crdV1ApprovalTests") + @DisplayName("CRD Generator V2 Approval Tests") + void apiV2ApprovalTest( + Class>[] crClasses, String expectedCrd, String version, boolean parallel) { + Approvals.settings().allowMultipleVerifyCallsForThisMethod(); + final Map> result = new io.fabric8.crdv2.generator.CRDGenerator() + .withParallelGenerationEnabled(parallel) + .inOutputDir(tempDir) + .customResourceClasses(crClasses) + .forCRDVersions(version) + .detailedGenerate() + .getCRDDetailsPerNameAndVersion(); + + assertThat(result) + .withFailMessage(() -> "Could not find expected CRD " + expectedCrd + + " in results. Found instead: " + result.keySet()) + .containsKey(expectedCrd) + .extractingByKey(expectedCrd) + .isNotNull(); + + Approvals.verify( + new FileApprovalWriter(new File(result.get(expectedCrd).get(version).getFilePath())), + new Namer(expectedCrd, version)); + } + static Stream crdApprovalTests() { + return Stream.concat( + crdApprovalBaseCases("v1"), + crdApprovalBaseCases("v1beta1")).map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel)); + } + + static Stream crdV1ApprovalTests() { + return crdApprovalBaseCases("v1") + .map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel)); + } + + static Stream crdApprovalBaseCases(String crdVersion) { final List cases = new ArrayList<>(); - for (String crdVersion : new String[] { "v1", "v1beta1" }) { - for (boolean parallel : new boolean[] { false, true }) { - cases.add(new TestCase("annotateds.samples.fabric8.io", crdVersion, parallel, Annotated.class)); - cases.add(new TestCase("complexkinds.samples.fabric8.io", crdVersion, parallel, Complex.class)); - cases.add(new TestCase("children.sample.fabric8.io", crdVersion, parallel, Child.class)); - cases.add(new TestCase("containingjsons.sample.fabric8.io", crdVersion, parallel, ContainingJson.class)); - cases.add(new TestCase("k8svalidations.samples.fabric8.io", crdVersion, parallel, K8sValidation.class)); - cases.add(new TestCase("containingmaps.sample.fabric8.io", crdVersion, parallel, ContainingMaps.class)); - cases.add(new TestCase("multiples.sample.fabric8.io", crdVersion, parallel, - io.fabric8.crd.generator.approvaltests.multipleversions.v1.Multiple.class, - io.fabric8.crd.generator.approvaltests.multipleversions.v2.Multiple.class)); - cases.add(new TestCase("nocyclics.sample.fabric8.io", crdVersion, parallel, NoCyclic.class)); - } + for (boolean parallel : new boolean[] { false, true }) { + cases.add(new TestCase("annotateds.samples.fabric8.io", crdVersion, parallel, Annotated.class)); + cases.add(new TestCase("complexkinds.samples.fabric8.io", crdVersion, parallel, Complex.class)); + cases.add(new TestCase("children.sample.fabric8.io", crdVersion, parallel, Child.class)); + cases.add(new TestCase("containingjsons.sample.fabric8.io", crdVersion, parallel, ContainingJson.class)); + cases.add(new TestCase("k8svalidations.samples.fabric8.io", crdVersion, parallel, K8sValidation.class)); + cases.add(new TestCase("containingmaps.sample.fabric8.io", crdVersion, parallel, ContainingMaps.class)); + cases.add(new TestCase("multiples.sample.fabric8.io", crdVersion, parallel, + io.fabric8.crd.generator.approvaltests.multipleversions.v1.Multiple.class, + io.fabric8.crd.generator.approvaltests.multipleversions.v2.Multiple.class)); + cases.add(new TestCase("nocyclics.sample.fabric8.io", crdVersion, parallel, NoCyclic.class)); } - return cases.stream().map(tc -> Arguments.of(tc.crClasses, tc.expectedCrd, tc.version, tc.parallel)); + return cases.stream(); } private static final class TestCase { @@ -119,6 +154,7 @@ private static final class TestCase { private final String version; private final boolean parallel; + @SafeVarargs public TestCase(String expectedCrd, String version, boolean parallel, Class>... crClasses) { this.expectedCrd = expectedCrd; this.version = version; From a05e4d13d39cdd4e7edaf32b1db1198143027500 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Wed, 8 May 2024 10:41:28 +0200 Subject: [PATCH 2/2] test(crd-generator): approval tests, remove unsupported v1 json (any getter/setter) Signed-off-by: Marc Nuri --- .../json/ContainingJsonSpec.java | 2 - .../crd/generator/approvaltests/json/Foo.java | 38 ------------------- ...ingjsons.sample.fabric8.io.v1.approved.yml | 9 ----- ...ons.sample.fabric8.io.v1beta1.approved.yml | 9 ----- 4 files changed, 58 deletions(-) delete mode 100644 crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/Foo.java diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/ContainingJsonSpec.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/ContainingJsonSpec.java index 84183a495dd..97ad29c2fb7 100644 --- a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/ContainingJsonSpec.java +++ b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/ContainingJsonSpec.java @@ -25,6 +25,4 @@ public class ContainingJsonSpec { private JsonNode free; - private Foo foo; - } diff --git a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/Foo.java b/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/Foo.java deleted file mode 100644 index 3e27b063f26..00000000000 --- a/crd-generator/test/src/test/java/io/fabric8/crd/generator/approvaltests/json/Foo.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.approvaltests.json; - -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; - -import java.util.HashMap; -import java.util.Map; - -public class Foo { - - private Map configAsMap = new HashMap<>(); - - @JsonAnyGetter - public Map getConfigAsMap() { - return configAsMap; - } - - @JsonAnySetter - public void setConfigAsMap(String name, Object value) { - this.configAsMap.put(name, value); - } - -} diff --git a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1.approved.yml b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1.approved.yml index 85145a74cde..7f4c6472191 100644 --- a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1.approved.yml +++ b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1.approved.yml @@ -19,15 +19,6 @@ spec: properties: field: type: "integer" - foo: - properties: - configAsMap: - additionalProperties: - type: "object" - type: "object" - x-kubernetes-preserve-unknown-fields: true - type: "object" - x-kubernetes-preserve-unknown-fields: true free: x-kubernetes-preserve-unknown-fields: true type: "object" diff --git a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1beta1.approved.yml b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1beta1.approved.yml index 28a31277c02..56c46ec041e 100644 --- a/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1beta1.approved.yml +++ b/crd-generator/test/src/test/resources/io/fabric8/crd/generator/approvaltests/CRDGeneratorApprovalTest.approvalTest.containingjsons.sample.fabric8.io.v1beta1.approved.yml @@ -17,15 +17,6 @@ spec: properties: field: type: "integer" - foo: - properties: - configAsMap: - additionalProperties: - type: "object" - type: "object" - x-kubernetes-preserve-unknown-fields: true - type: "object" - x-kubernetes-preserve-unknown-fields: true free: x-kubernetes-preserve-unknown-fields: true type: "object"