From 75d30c7c7acb175a6548957565369ee81a537b7e Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Tue, 29 Oct 2024 17:30:08 +0800 Subject: [PATCH] test(openapi): comprehensive tests for openapi-model-generator-maven-plugin Signed-off-by: Marc Nuri --- .../openapi/maven-plugin/pom.xml | 5 + ...EnvoyConfigObjectMatchObjectTypes.expected | 27 +++ .../IsLoadBalancerSettingsLbPolicy.expected | 26 +++ .../expected/LoadBalancerSettings.expected | 147 +++++++++++++ ...oadBalancerSettingsConsistentHash.expected | 116 ++++++++++ .../LoadBalancerSettingsSimple.expected | 116 ++++++++++ .../LoadBalancerSettingsSimpleLB.expected | 41 ++++ .../src/it/istio/invoker.properties | 16 ++ .../openapi/maven-plugin/src/it/istio/pom.xml | 80 +++++++ .../maven-plugin/src/it/istio/verify.groovy | 35 +++ .../schema/generator/schema/SchemaUtils.java | 20 +- .../generator/model/ClassInformationTest.java | 202 ++++++++++++++++++ .../{ => schema}/SchemaUtilsTest.java | 142 +++++++++++- 13 files changed, 956 insertions(+), 17 deletions(-) create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsEnvoyFilterEnvoyConfigObjectMatchObjectTypes.expected create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsLoadBalancerSettingsLbPolicy.expected create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettings.expected create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsConsistentHash.expected create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimple.expected create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimpleLB.expected create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/invoker.properties create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/pom.xml create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/it/istio/verify.groovy create mode 100644 kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/ClassInformationTest.java rename kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/{ => schema}/SchemaUtilsTest.java (72%) diff --git a/kubernetes-model-generator/openapi/maven-plugin/pom.xml b/kubernetes-model-generator/openapi/maven-plugin/pom.xml index 17e9a8c7500..71cfaf7559b 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/pom.xml +++ b/kubernetes-model-generator/openapi/maven-plugin/pom.xml @@ -67,6 +67,11 @@ swagger-parser 2.1.23 + + org.assertj + assertj-core + test + diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsEnvoyFilterEnvoyConfigObjectMatchObjectTypes.expected b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsEnvoyFilterEnvoyConfigObjectMatchObjectTypes.expected new file mode 100644 index 00000000000..1010824d9c9 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsEnvoyFilterEnvoyConfigObjectMatchObjectTypes.expected @@ -0,0 +1,27 @@ + +package io.fabric8.istio.api.api.networking.v1alpha3; + +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonTypeResolver; +import io.fabric8.kubernetes.api.model.KubernetesResource; + +@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonTypeResolver(io.fabric8.kubernetes.model.jackson.UnwrappedTypeResolverBuilder.class) +@JsonSubTypes({ + @JsonSubTypes.Type(EnvoyFilterEnvoyConfigObjectMatchCluster.class), + @JsonSubTypes.Type(EnvoyFilterEnvoyConfigObjectMatchListener.class), + @JsonSubTypes.Type(EnvoyFilterEnvoyConfigObjectMatchRouteConfiguration.class), +}) +@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION) +@Generated("jsonschema2pojo") +public interface IsEnvoyFilterEnvoyConfigObjectMatchObjectTypes extends KubernetesResource +{ + + + +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsLoadBalancerSettingsLbPolicy.expected b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsLoadBalancerSettingsLbPolicy.expected new file mode 100644 index 00000000000..275e5033ab4 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/IsLoadBalancerSettingsLbPolicy.expected @@ -0,0 +1,26 @@ + +package io.fabric8.istio.api.api.networking.v1alpha3; + +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonTypeResolver; +import io.fabric8.kubernetes.api.model.KubernetesResource; + +@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonTypeResolver(io.fabric8.kubernetes.model.jackson.UnwrappedTypeResolverBuilder.class) +@JsonSubTypes({ + @JsonSubTypes.Type(LoadBalancerSettingsConsistentHash.class), + @JsonSubTypes.Type(LoadBalancerSettingsSimple.class), +}) +@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION) +@Generated("jsonschema2pojo") +public interface IsLoadBalancerSettingsLbPolicy extends KubernetesResource +{ + + + +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettings.expected b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettings.expected new file mode 100644 index 00000000000..0822673c548 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettings.expected @@ -0,0 +1,147 @@ + +package io.fabric8.istio.api.api.networking.v1alpha3; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.fabric8.kubernetes.api.builder.Editable; +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.ContainerPort; +import io.fabric8.kubernetes.api.model.EnvVar; +import io.fabric8.kubernetes.api.model.IntOrString; +import io.fabric8.kubernetes.api.model.KubernetesResource; +import io.fabric8.kubernetes.api.model.LabelSelector; +import io.fabric8.kubernetes.api.model.LocalObjectReference; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.ObjectReference; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; +import io.fabric8.kubernetes.api.model.PodTemplateSpec; +import io.fabric8.kubernetes.api.model.ResourceRequirements; +import io.fabric8.kubernetes.api.model.Volume; +import io.fabric8.kubernetes.api.model.VolumeMount; +import io.sundr.builder.annotations.Buildable; +import io.sundr.builder.annotations.BuildableReference; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@JsonDeserialize(using = io.fabric8.kubernetes.model.jackson.JsonUnwrappedDeserializer.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "LbPolicy", + "localityLbSetting", + "warmupDurationSecs" +}) +@ToString +@EqualsAndHashCode +@Accessors(prefix = { + "_", + "" +}) +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = { + @BuildableReference(ObjectMeta.class), + @BuildableReference(LabelSelector.class), + @BuildableReference(Container.class), + @BuildableReference(PodTemplateSpec.class), + @BuildableReference(ResourceRequirements.class), + @BuildableReference(IntOrString.class), + @BuildableReference(ObjectReference.class), + @BuildableReference(LocalObjectReference.class), + @BuildableReference(PersistentVolumeClaim.class), + @BuildableReference(EnvVar.class), + @BuildableReference(ContainerPort.class), + @BuildableReference(Volume.class), + @BuildableReference(VolumeMount.class) +}) +@Generated("jsonschema2pojo") +public class LoadBalancerSettings implements Editable , KubernetesResource +{ + + @JsonProperty("LbPolicy") + @JsonUnwrapped + private IsLoadBalancerSettingsLbPolicy lbPolicy; + @JsonProperty("localityLbSetting") + private LocalityLoadBalancerSetting localityLbSetting; + @JsonProperty("warmupDurationSecs") + private String warmupDurationSecs; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + /** + * No args constructor for use in serialization + * + */ + public LoadBalancerSettings() { + } + + public LoadBalancerSettings(IsLoadBalancerSettingsLbPolicy lbPolicy, LocalityLoadBalancerSetting localityLbSetting, String warmupDurationSecs) { + super(); + this.lbPolicy = lbPolicy; + this.localityLbSetting = localityLbSetting; + this.warmupDurationSecs = warmupDurationSecs; + } + + @JsonProperty("LbPolicy") + @JsonUnwrapped + public IsLoadBalancerSettingsLbPolicy getLbPolicy() { + return lbPolicy; + } + + @JsonProperty("LbPolicy") + public void setLbPolicy(IsLoadBalancerSettingsLbPolicy lbPolicy) { + this.lbPolicy = lbPolicy; + } + + @JsonProperty("localityLbSetting") + public LocalityLoadBalancerSetting getLocalityLbSetting() { + return localityLbSetting; + } + + @JsonProperty("localityLbSetting") + public void setLocalityLbSetting(LocalityLoadBalancerSetting localityLbSetting) { + this.localityLbSetting = localityLbSetting; + } + + @JsonProperty("warmupDurationSecs") + public String getWarmupDurationSecs() { + return warmupDurationSecs; + } + + @JsonProperty("warmupDurationSecs") + public void setWarmupDurationSecs(String warmupDurationSecs) { + this.warmupDurationSecs = warmupDurationSecs; + } + + @JsonIgnore + public LoadBalancerSettingsBuilder edit() { + return new LoadBalancerSettingsBuilder(this); + } + + @JsonIgnore + public LoadBalancerSettingsBuilder toBuilder() { + return edit(); + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public void setAdditionalProperties(Map additionalProperties) { + this.additionalProperties = additionalProperties; + } + +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsConsistentHash.expected b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsConsistentHash.expected new file mode 100644 index 00000000000..3a5d1e8c086 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsConsistentHash.expected @@ -0,0 +1,116 @@ + +package io.fabric8.istio.api.api.networking.v1alpha3; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.fabric8.kubernetes.api.builder.Editable; +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.ContainerPort; +import io.fabric8.kubernetes.api.model.EnvVar; +import io.fabric8.kubernetes.api.model.IntOrString; +import io.fabric8.kubernetes.api.model.KubernetesResource; +import io.fabric8.kubernetes.api.model.LabelSelector; +import io.fabric8.kubernetes.api.model.LocalObjectReference; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.ObjectReference; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; +import io.fabric8.kubernetes.api.model.PodTemplateSpec; +import io.fabric8.kubernetes.api.model.ResourceRequirements; +import io.fabric8.kubernetes.api.model.Volume; +import io.fabric8.kubernetes.api.model.VolumeMount; +import io.sundr.builder.annotations.Buildable; +import io.sundr.builder.annotations.BuildableReference; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "consistentHash" +}) +@ToString +@EqualsAndHashCode +@Accessors(prefix = { + "_", + "" +}) +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = { + @BuildableReference(ObjectMeta.class), + @BuildableReference(LabelSelector.class), + @BuildableReference(Container.class), + @BuildableReference(PodTemplateSpec.class), + @BuildableReference(ResourceRequirements.class), + @BuildableReference(IntOrString.class), + @BuildableReference(ObjectReference.class), + @BuildableReference(LocalObjectReference.class), + @BuildableReference(PersistentVolumeClaim.class), + @BuildableReference(EnvVar.class), + @BuildableReference(ContainerPort.class), + @BuildableReference(Volume.class), + @BuildableReference(VolumeMount.class) +}) +@Generated("jsonschema2pojo") +public class LoadBalancerSettingsConsistentHash implements IsLoadBalancerSettingsLbPolicy, Editable , KubernetesResource +{ + + @JsonProperty("consistentHash") + private LoadBalancerSettingsConsistentHashLB consistentHash; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + /** + * No args constructor for use in serialization + * + */ + public LoadBalancerSettingsConsistentHash() { + } + + public LoadBalancerSettingsConsistentHash(LoadBalancerSettingsConsistentHashLB consistentHash) { + super(); + this.consistentHash = consistentHash; + } + + @JsonProperty("consistentHash") + public LoadBalancerSettingsConsistentHashLB getConsistentHash() { + return consistentHash; + } + + @JsonProperty("consistentHash") + public void setConsistentHash(LoadBalancerSettingsConsistentHashLB consistentHash) { + this.consistentHash = consistentHash; + } + + @JsonIgnore + public LoadBalancerSettingsConsistentHashBuilder edit() { + return new LoadBalancerSettingsConsistentHashBuilder(this); + } + + @JsonIgnore + public LoadBalancerSettingsConsistentHashBuilder toBuilder() { + return edit(); + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public void setAdditionalProperties(Map additionalProperties) { + this.additionalProperties = additionalProperties; + } + +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimple.expected b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimple.expected new file mode 100644 index 00000000000..26bacc85ae1 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimple.expected @@ -0,0 +1,116 @@ + +package io.fabric8.istio.api.api.networking.v1alpha3; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.fabric8.kubernetes.api.builder.Editable; +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.ContainerPort; +import io.fabric8.kubernetes.api.model.EnvVar; +import io.fabric8.kubernetes.api.model.IntOrString; +import io.fabric8.kubernetes.api.model.KubernetesResource; +import io.fabric8.kubernetes.api.model.LabelSelector; +import io.fabric8.kubernetes.api.model.LocalObjectReference; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.ObjectReference; +import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; +import io.fabric8.kubernetes.api.model.PodTemplateSpec; +import io.fabric8.kubernetes.api.model.ResourceRequirements; +import io.fabric8.kubernetes.api.model.Volume; +import io.fabric8.kubernetes.api.model.VolumeMount; +import io.sundr.builder.annotations.Buildable; +import io.sundr.builder.annotations.BuildableReference; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "simple" +}) +@ToString +@EqualsAndHashCode +@Accessors(prefix = { + "_", + "" +}) +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = { + @BuildableReference(ObjectMeta.class), + @BuildableReference(LabelSelector.class), + @BuildableReference(Container.class), + @BuildableReference(PodTemplateSpec.class), + @BuildableReference(ResourceRequirements.class), + @BuildableReference(IntOrString.class), + @BuildableReference(ObjectReference.class), + @BuildableReference(LocalObjectReference.class), + @BuildableReference(PersistentVolumeClaim.class), + @BuildableReference(EnvVar.class), + @BuildableReference(ContainerPort.class), + @BuildableReference(Volume.class), + @BuildableReference(VolumeMount.class) +}) +@Generated("jsonschema2pojo") +public class LoadBalancerSettingsSimple implements IsLoadBalancerSettingsLbPolicy, Editable , KubernetesResource +{ + + @JsonProperty("simple") + private LoadBalancerSettingsSimpleLB simple; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + /** + * No args constructor for use in serialization + * + */ + public LoadBalancerSettingsSimple() { + } + + public LoadBalancerSettingsSimple(LoadBalancerSettingsSimpleLB simple) { + super(); + this.simple = simple; + } + + @JsonProperty("simple") + public LoadBalancerSettingsSimpleLB getSimple() { + return simple; + } + + @JsonProperty("simple") + public void setSimple(LoadBalancerSettingsSimpleLB simple) { + this.simple = simple; + } + + @JsonIgnore + public LoadBalancerSettingsSimpleBuilder edit() { + return new LoadBalancerSettingsSimpleBuilder(this); + } + + @JsonIgnore + public LoadBalancerSettingsSimpleBuilder toBuilder() { + return edit(); + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + public void setAdditionalProperties(Map additionalProperties) { + this.additionalProperties = additionalProperties; + } + +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimpleLB.expected b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimpleLB.expected new file mode 100644 index 00000000000..6d8332ca241 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/expected/LoadBalancerSettingsSimpleLB.expected @@ -0,0 +1,41 @@ + +package io.fabric8.istio.api.api.networking.v1alpha3; + +import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonCreator; + +@Generated("jsonschema2pojo") +public enum LoadBalancerSettingsSimpleLB +{ + LEAST_CONN(1), + LEAST_REQUEST(5), + PASSTHROUGH(3), + RANDOM(2), + ROUND_ROBIN(4), + UNSPECIFIED(0); + private final int value; + private LoadBalancerSettingsSimpleLB(int value) { + this.value = value; + } + + @JsonCreator + public static LoadBalancerSettingsSimpleLB fromValue(Object value) { + if (value instanceof String) { + for (LoadBalancerSettingsSimpleLB e : LoadBalancerSettingsSimpleLB.values()) { + if (e.name().equalsIgnoreCase(value.toString())) { + return e; + } + } + } else if (value instanceof Integer) { + for (LoadBalancerSettingsSimpleLB e : LoadBalancerSettingsSimpleLB.values()) { + if (value.equals(e.value)) { + return e; + } + } + } + return null; + } + + + +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/invoker.properties b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/invoker.properties new file mode 100644 index 00000000000..166b9682bb0 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/invoker.properties @@ -0,0 +1,16 @@ +# +# 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. +# + diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/pom.xml b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/pom.xml new file mode 100644 index 00000000000..42a8101292c --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/pom.xml @@ -0,0 +1,80 @@ + + + + + 4.0.0 + + io.fabric8.it + istio + 0.0-SNAPSHOT + jar + + + @maven.compiler.source@ + @maven.compiler.target@ + + + + + + + + + io.fabric8 + openapi-model-generator-maven-plugin + @project.version@ + + + + generate-model + + + + + + ${project.basedir} + + @project.basedir@/../schemas/io.istio.json + + io.fabric8.kubernetes.api.model + io.fabric8.kubernetes.api.builder + true + false + + io.fabric8.istio.api + io.fabric8.kubernetes.api.model + io.fabric8.kubernetes.api.model + io.fabric8.kubernetes.api.model + + + ^io\.istio\..*$ + + + + #/components/schemas/org.golang.google.protobuf.types.known.durationpb.Duration + java.lang.String + + + + + + + + + diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/verify.groovy b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/verify.groovy new file mode 100644 index 00000000000..296b1449c0c --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/it/istio/verify.groovy @@ -0,0 +1,35 @@ +/* + * 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. + */ +import static org.junit.jupiter.api.Assertions.assertEquals +import static org.junit.jupiter.api.Assertions.assertTrue + + +new File(basedir, "/expected").eachFile { it -> + var javaFileName = it.name.replaceAll("\\.expected\$", ".java") + var javaFile = new File(basedir, sprintf("/src/generated/java/io/fabric8/istio/api/api/networking/v1alpha3/%s", javaFileName)) + assertTrue(javaFile.exists(), sprintf("File %s does not exist", javaFile)) + assertEquals( + it + .getText("UTF-8") + .replace("\r\n", "\n") + .replaceAll(" +\n", "\n") + .trim(), + javaFile.getText("UTF-8").replace("\r\n", "\n").replaceAll(" +\n", "\n").trim(), + sprintf("File %s does not match expected content", it) + ) +} + +true diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtils.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtils.java index 16b680b43c4..b75ada2a508 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtils.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtils.java @@ -178,7 +178,7 @@ public SchemaUtils(GeneratorSettings settings) { * @return the simple class name associated to the provided Schema reference. */ public String refToClassName(String ref) { - final String cleanRef = ref.replaceAll("^#/components/schemas/", ""); + final String cleanRef = ref.replaceAll("^#/((components/schemas)|(definitions))/", ""); if (settings.getRefToClassNameMappings().containsKey(cleanRef)) { return settings.getRefToClassNameMappings().getProperty(cleanRef); } @@ -262,7 +262,7 @@ public String schemaToClassName(ImportManager imports, Schema schema) { } public static String classType(Schema schema) { - if (schema.getExtensions() != null && schema.getExtensions().get("x-kubernetes-fabric8-type") != null) { + if (hasExtensionStringValue(schema, "x-kubernetes-fabric8-type")) { return schema.getExtensions().get("x-kubernetes-fabric8-type").toString(); } return "class"; @@ -273,7 +273,7 @@ public static boolean isEnum(Schema schema) { } public static Collection enumValues(Schema schema) { - if (isEnum(schema) && schema.getExtensions().containsKey("x-kubernetes-fabric8-enum-values")) { + if (isEnum(schema) && hasExtensionStringValue(schema, "x-kubernetes-fabric8-enum-values")) { return Stream.of(schema.getExtensions().get("x-kubernetes-fabric8-enum-values").toString().split(",")) .sorted() .collect(Collectors.toList()); @@ -286,8 +286,7 @@ public static boolean isInterface(Schema schema) { } public static boolean hasInterfaceFields(Schema schema) { - return schema.getExtensions() != null - && schema.getExtensions().containsKey("x-kubernetes-fabric8-interface-fields"); + return hasExtensionStringValue(schema, "x-kubernetes-fabric8-interface-fields"); } public static Set interfaceFields(Schema schema) { @@ -298,7 +297,7 @@ public static Set interfaceFields(Schema schema) { } public List interfaceImplementation(Schema schema) { - if (schema.getExtensions() != null && schema.getExtensions().containsKey("x-kubernetes-fabric8-implementation")) { + if (hasExtensionStringValue(schema, "x-kubernetes-fabric8-implementation")) { return Stream.of(schema.getExtensions().get("x-kubernetes-fabric8-implementation").toString().split(",")) .map(this::refToClassName) .sorted() @@ -308,7 +307,7 @@ public List interfaceImplementation(Schema schema) { } public String interfaceImplemented(Schema schema) { - if (schema.getExtensions() != null && schema.getExtensions().containsKey("x-kubernetes-fabric8-implements")) { + if (hasExtensionStringValue(schema, "x-kubernetes-fabric8-implements")) { return refToClassName(schema.getExtensions().get("x-kubernetes-fabric8-implements").toString()); } return null; @@ -560,4 +559,11 @@ public static String uncapitalize(String string) { } return string.substring(0, 1).toLowerCase(Locale.ROOT) + string.substring(1); } + + private static boolean hasExtensionStringValue(Schema schema, String key) { + return schema.getExtensions() != null + && schema.getExtensions().containsKey(key) + && schema.getExtensions().get(key) instanceof String + && !schema.getExtensions().get(key).toString().isEmpty(); + } } diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/ClassInformationTest.java b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/ClassInformationTest.java new file mode 100644 index 00000000000..50c8b3dd363 --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/model/ClassInformationTest.java @@ -0,0 +1,202 @@ +/* + * 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.kubernetes.schema.generator.model; + +import io.fabric8.kubernetes.schema.generator.GeneratorSettings; +import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils; +import io.swagger.v3.oas.models.media.ObjectSchema; +import io.swagger.v3.oas.models.media.Schema; +import org.assertj.core.api.InstanceOfAssertFactories; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ClassInformationTest { + + private ClassInformation classInformation; + + @Nested + class Interface { + + @BeforeEach + void setUp() { + final var schemaUtils = new SchemaUtils(GeneratorSettings.builder().build()); + final var schema = new ObjectSchema(); + schema.addExtension("x-kubernetes-fabric8-type", "interface"); + schema.addExtension("x-kubernetes-fabric8-implementation", "InterfaceImplementationOne,InterfaceImplementationTwo"); + final Map.Entry> clazz = Map.entry("io.fabric8.kubernetes.Interface", schema); + classInformation = new ClassInformation(schemaUtils, clazz); + } + + @Test + void getKubernetesListType() { + assertThat(classInformation.getKubernetesListType()).isNull(); + } + + @Test + void getPackageName() { + assertThat(classInformation.getPackageName()).isEqualTo("io.fabric8.kubernetes"); + } + + @Test + void isInterface() { + assertThat(classInformation.isInterface()).isTrue(); + } + + @Test + void isEnum() { + assertThat(classInformation.isEnum()).isFalse(); + } + + @Test + void isHasMetadata() { + assertThat(classInformation.isHasMetadata()).isFalse(); + } + + @Test + void isNamespaced() { + assertThat(classInformation.isNamespaced()).isFalse(); + } + + @Test + void isEditable() { + assertThat(classInformation.isEditable()).isFalse(); + } + + @Test + void classType() { + assertThat(classInformation.getClassType()).isEqualTo("interface"); + } + + @Test + void getClassSimpleName() { + assertThat(classInformation.getClassSimpleName()).isEqualTo("Interface"); + } + + @Test + void getClassName() { + assertThat(classInformation.getClassName()).isEqualTo("io.fabric8.kubernetes.Interface"); + } + + @Test + void getImplementsExtends() { + assertThat(classInformation.getImplementsExtends()).isEqualTo("extends KubernetesResource"); + } + + @Test + void getJsonSubTypes() { + assertThat(classInformation.getJsonSubTypes()) + .extracting(JsonSubTypes::getSubTypes) + .asInstanceOf(InstanceOfAssertFactories.list(String.class)) + .containsExactly("InterfaceImplementationOne", "InterfaceImplementationTwo"); + } + + @Test + void getImports() { + assertThat(classInformation.getImports()) + .containsExactly( + "com.fasterxml.jackson.annotation.JsonSubTypes", + "com.fasterxml.jackson.annotation.JsonTypeInfo", + "com.fasterxml.jackson.databind.annotation.JsonTypeResolver", + "io.fabric8.kubernetes.api.model.KubernetesResource"); + } + } + + @Nested + class Enum { + + @BeforeEach + void setUp() { + final var schemaUtils = new SchemaUtils(GeneratorSettings.builder().build()); + final var schema = new ObjectSchema(); + schema.addExtension("x-kubernetes-fabric8-type", "enum"); + schema.addExtension("x-kubernetes-fabric8-enum-values", "ACTIVE(0),INACTIVE(1),AUTO(2)"); + final Map.Entry> clazz = Map.entry("io.fabric8.kubernetes.Mode", schema); + classInformation = new ClassInformation(schemaUtils, clazz); + } + + @Test + void getKubernetesListType() { + assertThat(classInformation.getKubernetesListType()).isNull(); + } + + @Test + void getPackageName() { + assertThat(classInformation.getPackageName()).isEqualTo("io.fabric8.kubernetes"); + } + + @Test + void isInterface() { + assertThat(classInformation.isInterface()).isFalse(); + } + + @Test + void isEnum() { + assertThat(classInformation.isEnum()).isTrue(); + } + + @Test + void isHasMetadata() { + assertThat(classInformation.isHasMetadata()).isFalse(); + } + + @Test + void isNamespaced() { + assertThat(classInformation.isNamespaced()).isFalse(); + } + + @Test + void isEditable() { + assertThat(classInformation.isEditable()).isFalse(); + } + + @Test + void classType() { + assertThat(classInformation.getClassType()).isEqualTo("enum"); + } + + @Test + void getClassSimpleName() { + assertThat(classInformation.getClassSimpleName()).isEqualTo("Mode"); + } + + @Test + void getClassName() { + assertThat(classInformation.getClassName()).isEqualTo("io.fabric8.kubernetes.Mode"); + } + + @Test + void getImplementsExtends() { + assertThat(classInformation.getImplementsExtends()).isEmpty(); + } + + @Test + void getJsonSubTypes() { + assertThat(classInformation.getJsonSubTypes()).isNull(); + } + + @Test + void getImports() { + assertThat(classInformation.getImports()) + .containsExactly( + "com.fasterxml.jackson.annotation.JsonCreator"); + } + } +} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtilsTest.java similarity index 72% rename from kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java rename to kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtilsTest.java index 5d104e8b5ea..52e585da7d2 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/SchemaUtilsTest.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/test/java/io/fabric8/kubernetes/schema/generator/schema/SchemaUtilsTest.java @@ -13,9 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.fabric8.kubernetes.schema.generator; +package io.fabric8.kubernetes.schema.generator.schema; -import io.fabric8.kubernetes.schema.generator.schema.SchemaUtils; +import io.fabric8.kubernetes.schema.generator.GeneratorSettings; +import io.fabric8.kubernetes.schema.generator.ImportManager; +import io.fabric8.kubernetes.schema.generator.ImportOrderComparator; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.DateSchema; import io.swagger.v3.oas.models.media.DateTimeSchema; @@ -27,18 +29,25 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; import java.io.File; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import java.util.TreeSet; +import java.util.stream.Stream; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -56,11 +65,17 @@ void setUp(@TempDir File tempDir) { @CsvSource({ "#/definitions/io.k8s.api.core.v1.Pod, Pod", "#/definitions/io.k8s.api.core.v1.PodList, PodList", - "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta, ListMeta" + "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta, ListMeta", + "#/components/schemas/io.k8s.api.core.v1.toCapitalize, ToCapitalize", + "io.k8s.api.core.v1.ToBeMapped, Mapped", + "#/components/schemas/io.k8s.api.core.v1.ToBeMapped, Mapped", + "#/definitions/io.k8s.api.core.v1.ToBeMapped, Mapped" }) void refToClassName(String ref, String expected) { - final String result = new SchemaUtils(generatorSettingsBuilder.build()).refToClassName(ref); - assertEquals(expected, result); + final var properties = new Properties(); + properties.put("io.k8s.api.core.v1.ToBeMapped", "Mapped"); + final var schemaUtils = new SchemaUtils(generatorSettingsBuilder.refToClassNameMappings(properties).build()); + assertEquals(expected, schemaUtils.refToClassName(ref)); } @ParameterizedTest @@ -135,11 +150,118 @@ void serializerForJavaClass(String javaClass, String expected) { assertEquals(expected, SchemaUtils.serializerForJavaClass(javaClass)); } - @Test - void isInterface() { - final ObjectSchema schema = new ObjectSchema(); - schema.setExtensions(Map.of("x-kubernetes-fabric8-type", "interface")); - assertTrue(SchemaUtils.isInterface(schema)); + @Nested + @TestInstance(TestInstance.Lifecycle.PER_CLASS) + class InterfaceSchemas { + + @Test + void classType() { + final ObjectSchema schema = new ObjectSchema(); + schema.setExtensions(Map.of("x-kubernetes-fabric8-type", "interface")); + assertThat(SchemaUtils.classType(schema)).isEqualTo("interface"); + } + + @Test + void isInterface() { + final ObjectSchema schema = new ObjectSchema(); + schema.setExtensions(Map.of("x-kubernetes-fabric8-type", "interface")); + assertTrue(SchemaUtils.isInterface(schema)); + } + + @Test + void hasInterfaceFields() { + final var schema = new ObjectSchema(); + schema.setExtensions(Map.of("x-kubernetes-fabric8-interface-fields", "field1,field2")); + assertTrue(SchemaUtils.hasInterfaceFields(schema)); + } + + @ParameterizedTest(name = "{index}: x-kubernetes-fabric8-interface-fields={0}") + @MethodSource("interfaceFieldsData") + void interfaceFields(String extensionValue, String... expected) { + final var schema = new ObjectSchema(); + schema.setExtensions(Collections.singletonMap("x-kubernetes-fabric8-interface-fields", extensionValue)); + assertThat(SchemaUtils.interfaceFields(schema)) + .containsExactlyInAnyOrder(expected); + } + + Stream interfaceFieldsData() { + return Stream.of( + Arguments.of("field1,field2", new String[] { "field1", "field2" }), + Arguments.of("field2,field1", new String[] { "field1", "field2" }), + Arguments.of("", new String[0]), + Arguments.of(null, new String[0])); + } + + @ParameterizedTest(name = "{index}: x-kubernetes-fabric8-interface-implementation={0}") + @MethodSource("interfaceImplementationData") + void interfaceImplementation(String extensionValue, String... expected) { + final var schema = new ObjectSchema(); + schema.setExtensions(new HashMap<>()); + schema.getExtensions().put("x-kubernetes-fabric8-type", "interface"); + schema.getExtensions().put("x-kubernetes-fabric8-implementation", extensionValue); + assertThat(new SchemaUtils(generatorSettingsBuilder.build()).interfaceImplementation(schema)) + .containsExactly(expected); + } + + Stream interfaceImplementationData() { + return Stream.of( + Arguments.of("InterfaceImpl,AnotherInterfaceImpl", new String[] { "AnotherInterfaceImpl", "InterfaceImpl" }), + Arguments.of("AnotherInterfaceImpl,InterfaceImpl", new String[] { "AnotherInterfaceImpl", "InterfaceImpl" }), + Arguments.of("", new String[0]), + Arguments.of(null, new String[0])); + } + + @ParameterizedTest(name = "{index}: x-kubernetes-fabric8-interface-implements={0}") + @CsvSource({ + "InterfaceClass, InterfaceClass", + ", ", + }) + void interfaceImplemented(String extensionValue, String expected) { + final var schema = new ObjectSchema(); + schema.setExtensions(Collections.singletonMap("x-kubernetes-fabric8-implements", extensionValue)); + assertThat(new SchemaUtils(generatorSettingsBuilder.build()).interfaceImplemented(schema)) + .isEqualTo(expected); + } + + } + + @Nested + @TestInstance(TestInstance.Lifecycle.PER_CLASS) + class EnumSchemas { + + @Test + void classType() { + final ObjectSchema schema = new ObjectSchema(); + schema.setExtensions(Map.of("x-kubernetes-fabric8-type", "enum")); + assertThat(SchemaUtils.classType(schema)).isEqualTo("enum"); + } + + @Test + void isEnum() { + final ObjectSchema schema = new ObjectSchema(); + schema.setExtensions(Map.of("x-kubernetes-fabric8-type", "enum")); + assertTrue(SchemaUtils.isEnum(schema)); + } + + @ParameterizedTest(name = "{index}: x-kubernetes-fabric8-enum-values={0}") + @MethodSource("enumValuesData") + void enumValues(String extensionValue, String... expected) { + final var schema = new ObjectSchema(); + schema.setExtensions(new HashMap<>()); + schema.getExtensions().put("x-kubernetes-fabric8-type", "enum"); + schema.getExtensions().put("x-kubernetes-fabric8-enum-values", extensionValue); + assertThat(SchemaUtils.enumValues(schema)) + .containsExactly(expected); + } + + Stream enumValuesData() { + return Stream.of( + Arguments.of("ONE(0),TWO(1),THREE(3)", new String[] { "ONE(0)", "THREE(3)", "TWO(1)" }), + Arguments.of("A,B,C,Z", new String[] { "A", "B", "C", "Z" }), + Arguments.of("", new String[0]), + Arguments.of(null, new String[0])); + } + } @Nested