Skip to content

Commit

Permalink
Added tests w/wo additionalProps + negative test, simplified test crd
Browse files Browse the repository at this point in the history
  • Loading branch information
Javatar81 committed Aug 30, 2024
1 parent d78072a commit c73ceeb
Show file tree
Hide file tree
Showing 17 changed files with 963 additions and 5,774 deletions.
89 changes: 89 additions & 0 deletions java-generator/it/src/it/ignore-unknown-toplevel-objects/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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>ignore-unknown-toplevel-objects</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.fabric8</groupId>
<artifactId>java-generator-integration-tests</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>generator-annotations</artifactId>
<version>@project.version@</version>
</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/application-crd.yml</source>
<generatedAnnotations>false</generatedAnnotations>
<alwaysPreserveUnknown>false</alwaysPreserveUnknown>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>@maven.surefire.plugin.version@</version>
<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,97 @@
/*
* 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.top;

import com.fasterxml.jackson.databind.JsonNode;
import io.fabric8.kubernetes.api.model.AnyType;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.Test;
import io.fabric8.java.generator.testing.KubernetesResourceDiff;
import io.argoproj.v1alpha1.Application;
import io.argoproj.v1alpha1.ApplicationSpec;
import io.argoproj.v1alpha1.applicationspec.Destination;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

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

class TestIgnoreUnknownTopleveObjsFromConfig {

@Test
void testDeserialization() {
try {
Application.class.getMethod("getAdditionalProperties");
fail("No getAdditionalProperties method expected");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

private Application createSampleApplicationNoAdditionalFields(String name) throws Exception {
Application application = new Application();
ObjectMeta meta = new ObjectMeta();
meta.setName(name);
meta.setNamespace("mynamespace");
application.setMetadata(meta);
ApplicationSpec spec = new ApplicationSpec();
application.setSpec(spec);
spec.setProject("default");
Destination des = new Destination();
des.setNamespace("targetnamespace");
des.setServer("https://kubernetes.default.svc");
spec.setDestination(des);
return application;
}

@Test
void testAgainstSampleNoAdditionalFields() throws Exception {
// Arrange
Path resPath = Paths.get(getClass().getResource("/sample-no-addprops.yaml").toURI());
String yamlContent = new String(Files.readAllBytes(resPath), "UTF8");
Application sample = createSampleApplicationNoAdditionalFields("testapp-no-addprops");
KubernetesResourceDiff diff = new KubernetesResourceDiff(yamlContent, Serialization.asYaml(sample));

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

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

@Test
void testAgainstSampleWithAdditionalFields() throws Exception {
// Arrange
Path resPath = Paths.get(getClass().getResource("/sample-addprops.yaml").toURI());
String yamlContent = new String(Files.readAllBytes(resPath), "UTF8");
Application sample = createSampleApplicationNoAdditionalFields(" testapp-addprops");
KubernetesResourceDiff diff = new KubernetesResourceDiff(yamlContent, Serialization.asYaml(sample));

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

// Assert
// We expect 6 additional fields that are not available in the sample object
assertEquals(6, aggregatedDiffs.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.top;

import com.fasterxml.jackson.databind.JsonNode;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.utils.Serialization;
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 io.argoproj.v1alpha1.Application;
import io.argoproj.v1alpha1.ApplicationSpec;
import io.argoproj.v1alpha1.applicationspec.Destination;
import io.argoproj.v1alpha1.application.Operation;
import io.argoproj.v1alpha1.application.operation.InitiatedBy;

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

class TestToplevelFields {

private Application createSampleApplication() throws Exception {
Application application = new Application();
ObjectMeta meta = new ObjectMeta();
meta.setName("testapp-tl-operation");
meta.setNamespace("mynamespace");
application.setMetadata(meta);
ApplicationSpec spec = new ApplicationSpec();
application.setSpec(spec);
spec.setProject("default");
Destination des = new Destination();
des.setNamespace("targetnamespace");
des.setServer("https://kubernetes.default.svc");
spec.setDestination(des);
Operation op = new Operation();
InitiatedBy initiatedBy = new InitiatedBy();
initiatedBy.setUsername("me");
op.setInitiatedBy(initiatedBy);
application.setOperation(op);
return application;
}
@Test
void testAgainstSample() throws Exception {
// Arrange
Path resPath = Paths.get(getClass().getResource("/sample-tl-operation.yaml").toURI());
String yamlContent = new String(Files.readAllBytes(resPath), "UTF8");
Application sample = createSampleApplication();
KubernetesResourceDiff diff = new KubernetesResourceDiff(yamlContent, Serialization.asYaml(sample));

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

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

}
Loading

0 comments on commit c73ceeb

Please sign in to comment.