Skip to content

Commit

Permalink
format: spotless
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed May 16, 2022
1 parent 7310622 commit 49b51e4
Showing 1 changed file with 74 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ void unmarshalCRDWithSchema() throws Exception {
final CustomResourceDefinition crd = Serialization.unmarshal(input, CustomResourceDefinition.class);
// When
JSONSchemaProps spec = crd.getSpec()
.getValidation()
.getOpenAPIV3Schema()
.getProperties()
.get("spec");
.getValidation()
.getOpenAPIV3Schema()
.getProperties()
.get("spec");
// Then
assertThat(spec)
.hasFieldOrPropertyWithValue("properties.builderName.example", new TextNode("builder-example"))
.hasFieldOrPropertyWithValue("properties.hollow.default", BooleanNode.FALSE)
.hasFieldOrPropertyWithValue("properties.dimensions.properties.x.default", new IntNode(10))
.extracting(JSONSchemaProps::getRequired).asList()
.containsExactly("builderName", "edges", "dimensions");
.hasFieldOrPropertyWithValue("properties.builderName.example", new TextNode("builder-example"))
.hasFieldOrPropertyWithValue("properties.hollow.default", BooleanNode.FALSE)
.hasFieldOrPropertyWithValue("properties.dimensions.properties.x.default", new IntNode(10))
.extracting(JSONSchemaProps::getRequired).asList()
.containsExactly("builderName", "edges", "dimensions");
}

@Test
Expand All @@ -92,19 +92,19 @@ void unmarshalListWithWindowsLineSeparatorsString() throws Exception {
final KubernetesResource result = Serialization.unmarshal(crlfFile, KubernetesResource.class);
// Then
assertThat(result)
.asInstanceOf(InstanceOfAssertFactories.type(KubernetesList.class))
.extracting(KubernetesList::getItems).asList()
.hasSize(2)
.hasAtLeastOneElementOfType(Service.class)
.hasAtLeastOneElementOfType(Deployment.class);
.asInstanceOf(InstanceOfAssertFactories.type(KubernetesList.class))
.extracting(KubernetesList::getItems).asList()
.hasSize(2)
.hasAtLeastOneElementOfType(Service.class)
.hasAtLeastOneElementOfType(Deployment.class);
}

private String readYamlToString(String path) throws IOException {
return Files.readAllLines(
new File(SerializationTest.class.getResource(path).getFile()).toPath(), StandardCharsets.UTF_8)
.stream()
.filter(line -> !line.startsWith("#"))
.collect(Collectors.joining("\n"));
new File(SerializationTest.class.getResource(path).getFile()).toPath(), StandardCharsets.UTF_8)
.stream()
.filter(line -> !line.startsWith("#"))
.collect(Collectors.joining("\n"));
}

@Test
Expand Down Expand Up @@ -159,19 +159,18 @@ void testSerializeYamlWithAlias() {
Pod pod = Serialization.unmarshal(fileInputStream);
// Then
assertThat(pod)
.isNotNull()
.hasFieldOrPropertyWithValue("metadata.name", "test-pod-with-alias")
.hasFieldOrPropertyWithValue("spec.nodeSelector.workload", "build")
.returns(1, p -> p.getSpec().getTolerations().size())
.hasFieldOrPropertyWithValue("spec.securityContext.runAsGroup", 1000L)
.hasFieldOrPropertyWithValue("spec.securityContext.runAsUser", 1000L)
.extracting(Pod::getSpec).extracting(PodSpec::getContainers).asList()
.hasSize(2)
.extracting("name", "image", "resources.requests.cpu")
.containsExactly(
new Tuple("ubuntu", "ubuntu:bionic", new Quantity("100m")),
new Tuple("python3", "python:3.7", new Quantity("100m"))
);
.isNotNull()
.hasFieldOrPropertyWithValue("metadata.name", "test-pod-with-alias")
.hasFieldOrPropertyWithValue("spec.nodeSelector.workload", "build")
.returns(1, p -> p.getSpec().getTolerations().size())
.hasFieldOrPropertyWithValue("spec.securityContext.runAsGroup", 1000L)
.hasFieldOrPropertyWithValue("spec.securityContext.runAsUser", 1000L)
.extracting(Pod::getSpec).extracting(PodSpec::getContainers).asList()
.hasSize(2)
.extracting("name", "image", "resources.requests.cpu")
.containsExactly(
new Tuple("ubuntu", "ubuntu:bionic", new Quantity("100m")),
new Tuple("python3", "python:3.7", new Quantity("100m")));
}

@Test
Expand All @@ -182,26 +181,26 @@ void testClone() {
Pod clonePod = Serialization.clone(pod);
// Then
assertThat(clonePod)
.isEqualTo(pod)
.isNotSameAs(pod)
.hasFieldOrPropertyWithValue("metadata.name", "pod");
.isEqualTo(pod)
.isNotSameAs(pod)
.hasFieldOrPropertyWithValue("metadata.name", "pod");
}

@Test
@DisplayName("unmarshal, with valid YAML custom resource, should return GenericKubernetesResource instance")
void unmarshalWithValidCustomResourceShouldReturnGenericCustomResource() {
// When
final KubernetesResource result = Serialization.unmarshal(
SerializationTest.class.getResourceAsStream("/serialization/custom-resource.yml"));
SerializationTest.class.getResourceAsStream("/serialization/custom-resource.yml"));
// Then
assertThat(result)
.isNotNull()
.isInstanceOf(GenericKubernetesResource.class)
.hasFieldOrPropertyWithValue("apiVersion", "the-cr.example.com/v1")
.hasFieldOrPropertyWithValue("Kind", "SomeCustomResource")
.hasFieldOrPropertyWithValue("metadata.name", "custom-resource-example")
.hasFieldOrPropertyWithValue("additionalProperties.spec.field", "value")
.hasFieldOrPropertyWithValue("additionalProperties.status.reconciled", true);
.isNotNull()
.isInstanceOf(GenericKubernetesResource.class)
.hasFieldOrPropertyWithValue("apiVersion", "the-cr.example.com/v1")
.hasFieldOrPropertyWithValue("Kind", "SomeCustomResource")
.hasFieldOrPropertyWithValue("metadata.name", "custom-resource-example")
.hasFieldOrPropertyWithValue("additionalProperties.spec.field", "value")
.hasFieldOrPropertyWithValue("additionalProperties.status.reconciled", true);
}

@Test
Expand All @@ -213,16 +212,15 @@ void unmarshalWithInvalidYamlShouldThrowException() {
final ClassCastException result = assertThrows(ClassCastException.class, () -> Serialization.unmarshal(is));
// Then
assertThat(result)
.hasMessageContainingAll("java.lang.String", "cannot be cast to", "java.util.Map");
.hasMessageContainingAll("java.lang.String", "cannot be cast to", "java.util.Map");
}

@Test
@DisplayName("unmarshal, with invalid YAML resource, should return null")
void unmarshalWithInvalidResourceShouldReturnNull() {
// When
final KubernetesResource result = Serialization.unmarshal(
SerializationTest.class.getResourceAsStream("/serialization/invalid-resource.yml")
);
SerializationTest.class.getResourceAsStream("/serialization/invalid-resource.yml"));
// Then
assertThat(result).isNull();
}
Expand All @@ -232,66 +230,58 @@ void unmarshalWithInvalidResourceShouldReturnNull() {
void unmarshalWithValidListShouldReturnKubernetesList() {
// When
final KubernetesResource result = Serialization.unmarshal(
SerializationTest.class.getResourceAsStream("/serialization/kubernetes-list.yml")
);
SerializationTest.class.getResourceAsStream("/serialization/kubernetes-list.yml"));
// Then
assertThat(result)
.asInstanceOf(InstanceOfAssertFactories.type(KubernetesList.class))
.extracting(KubernetesList::getItems).asList()
.hasSize(3)
.extracting("class", "apiVersion", "kind", "metadata.name")
.containsExactlyInAnyOrder(
new Tuple(GenericKubernetesResource.class, "custom.resource.example.com/v1", "Example", "a-custom-resource"),
new Tuple(Namespace.class, "v1", "Namespace", "a-namespace"),
new Tuple(Pod.class, "v1", "Pod", "a-pod")
);
.asInstanceOf(InstanceOfAssertFactories.type(KubernetesList.class))
.extracting(KubernetesList::getItems).asList()
.hasSize(3)
.extracting("class", "apiVersion", "kind", "metadata.name")
.containsExactlyInAnyOrder(
new Tuple(GenericKubernetesResource.class, "custom.resource.example.com/v1", "Example", "a-custom-resource"),
new Tuple(Namespace.class, "v1", "Namespace", "a-namespace"),
new Tuple(Pod.class, "v1", "Pod", "a-pod"));
}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type"
)
@JsonSubTypes(
@JsonSubTypes.Type(value = Typed.class, name = "x")
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes(@JsonSubTypes.Type(value = Typed.class, name = "x"))
public interface Typeable {

String getType();
String getType();

}

public static class Typed implements Typeable {

@Override
public String getType() {
return "x";
}
@Override
public String getType() {
return "x";
}

}

public static class Root {

private Typeable typeable;
private Typeable typeable;

public Typeable getTypeable() {
return typeable;
}
public Typeable getTypeable() {
return typeable;
}

public void setTypeable(Typeable typeable) {
this.typeable = typeable;
}
public void setTypeable(Typeable typeable) {
this.typeable = typeable;
}
}

@Test
void testSerializeYamlWithJsonSubTypes() {
Root root = new Root();
root.setTypeable(new Typed());
final String result = Serialization.asYaml(root);
assertThat(result)
Root root = new Root();
root.setTypeable(new Typed());
final String result = Serialization.asYaml(root);
assertThat(result)
.isEqualTo("---\n"
+ "typeable:\n"
+ " type: \"x\"\n", Serialization.asYaml(root));
+ "typeable:\n"
+ " type: \"x\"\n", Serialization.asYaml(root));
}

@Group("serialization.fabric8.io")
Expand All @@ -308,8 +298,8 @@ void nullValueShouldNotBeOutput() {
MyCR cr = new MyCR();
final String s = Serialization.asYaml(cr);
assertThat(s)
.doesNotContain("status")
.contains("spec: \"foo\"");
.doesNotContain("status")
.contains("spec: \"foo\"");
}

@Test
Expand Down

0 comments on commit 49b51e4

Please sign in to comment.