Skip to content

Commit

Permalink
Test builder pattern on CR
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP authored and manusa committed Apr 12, 2022
1 parent 165dd37 commit 227baa4
Show file tree
Hide file tree
Showing 11 changed files with 14,578 additions and 14 deletions.
28 changes: 28 additions & 0 deletions java-generator/it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
</dependency>
<dependency>
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-client</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 1 addition & 2 deletions java-generator/it/src/it/enum-ser-deser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<artifactId>java-generator-integration-tests</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -94,7 +94,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<useFile>false</useFile>
<trimStackTrace>false</trimStackTrace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,21 @@
package io.fabric8.it.certmanager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import io.cert_manager.v1.CertificateRequest;
import io.cert_manager.v1.CertificateRequestSpec;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.zjsonpatch.JsonDiff;
import org.junit.jupiter.api.Test;
import io.fabric8.java.generator.testing.KubernetesResourceDiff;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestEnumSerialization {

ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));

@Test
void testDeserialization() {
// Arrange
Expand All @@ -60,13 +53,11 @@ void testSerialization() throws Exception {
// Arrange
Path resPath = Paths.get(getClass().getResource("/sample1.yaml").toURI());
String yamlContent = new String(Files.readAllBytes(resPath), "UTF8");
JsonNode originalCRJson = yamlMapper.readTree(yamlContent);
CertificateRequest sample = Serialization.unmarshal(yamlContent, CertificateRequest.class);
KubernetesResourceDiff diff = new KubernetesResourceDiff(yamlContent, Serialization.asYaml(sample));

// Act
JsonNode resultCRJson = yamlMapper.readTree(Serialization.asYaml(sample));
JsonNode diff = JsonDiff.asJson(originalCRJson, resultCRJson);
List<JsonNode> aggregatedDiffs = StreamSupport.stream(diff.spliterator(), false).collect(Collectors.toList());
List<JsonNode> aggregatedDiffs = diff.getListOfDiffs();

// Assert
assertEquals(0, aggregatedDiffs.size());
Expand Down
17 changes: 17 additions & 0 deletions java-generator/it/src/it/extensions/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# 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.
#

invoker.goals=test
124 changes: 124 additions & 0 deletions java-generator/it/src/it/extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project>

<modelVersion>4.0.0</modelVersion>

<artifactId>extensions-tests</artifactId>
<groupId>io.fabric8.it</groupId>
<version>0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>@maven.compiler.source@</maven.compiler.source>
<maven.compiler.target>@maven.compiler.target@</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>sundr-adapter-reflect</artifactId>
<version>@sundrio.version@</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<version>@sundrio.version@</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>@lombok.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>@validation-api.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-integration-tests</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>@validation-api.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<version>@sundrio.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>@lombok.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<source>src/test/resources/camel-k.crds.1.8.1.yaml</source>
<extraAnnotations>true</extraAnnotations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useFile>false</useFile>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.it.certmanager;

import com.fasterxml.jackson.databind.JsonNode;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.Test;
import io.fabric8.java.generator.testing.KubernetesResourceDiff;
import io.fabric8.java.generator.testing.extensions.camlek.ExtensionCamelK;
import io.fabric8.it.extensions.camelk.JavaGeneratedCamelK;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestExtensionsWireCompat {

@Test
void testCamelKCatalogCR() {
// Arrange
io.fabric8.camelk.v1.CamelCatalog extensionCamelCatalog = ExtensionCamelK.dummyCamelKCatalog();
org.apache.camel.v1.CamelCatalog javaGenCamelCatalog = JavaGeneratedCamelK.dummyCamelKCatalog();
KubernetesResourceDiff diff = new KubernetesResourceDiff(Serialization.asYaml(extensionCamelCatalog), Serialization.asYaml(javaGenCamelCatalog));

// Act
List<JsonNode> aggregatedDiffs = diff.getListOfDiffs();

// Assert
assertEquals(0, aggregatedDiffs.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.it.extensions.camelk;

import org.apache.camel.v1.CamelCatalog;
import org.apache.camel.v1.CamelCatalogBuilder;
import org.apache.camel.v1.CamelCatalogSpecBuilder;
import org.apache.camel.v1.camelcatalogspec.ArtifactsBuilder;
import org.apache.camel.v1.camelcatalogspec.RuntimeBuilder;
import org.apache.camel.v1.camelcatalogspec.artifacts.dependencies.ExclusionsBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;

public class JavaGeneratedCamelK {

// Code generated with
// ./java-generator/cli/target/java-gen -t="java-generator/it/src/main/java" -s="java-generator/it/src/it/extensions/src/test/resources/camel-k.crds.1.8.1.yaml" --add-extra-annotations

public static CamelCatalog dummyCamelKCatalog() {
CamelCatalog camelCatalog = new CamelCatalogBuilder()
.withNewMetadata()
.withName("dummy")
.endMetadata()
.withNewSpec()
.addToArtifacts("artifact1",
new ArtifactsBuilder()
.withArtifactId("artifact1ID")
.withDataformats("dataformat1")
.withDependencies(
new org.apache.camel.v1.camelcatalogspec.artifacts.DependenciesBuilder()
.withArtifactId("artifact2")
.withGroupId("mygroupid")
.withVersion("1.0.0")
.withExclusions(
new ExclusionsBuilder()
.withArtifactId("artifact3")
.build()
)
.build()
)
.withGroupId("mygroupid")
.withJavaTypes("javatype1", "javatype2")
.withLanguages("java", "kotlin")
.build()
)
.withRuntime(
new RuntimeBuilder()
.withVersion("2.0.0")
.withProvider("myprovider")
.withDependencies(
new org.apache.camel.v1.camelcatalogspec.runtime.DependenciesBuilder()
.withArtifactId("aid")
.withGroupId("gid")
.withVersion("v")
.build()
)
.build()
)
.endSpec()
.build();

return camelCatalog;
}

}
Loading

0 comments on commit 227baa4

Please sign in to comment.