Skip to content

Commit

Permalink
refactor: add parameterized tests for Serialization utils
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Nov 11, 2022
1 parent 965caac commit b726498
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@

class SerializationSingleDocumentUnmarshalTest {
@ParameterizedTest(name = "#{index} - unmarshal {0}")
@ValueSource(strings = {"document-with-trailing-document-delimiter.yml", "document-with-leading-document-delimiter.yml", "document-with-leading-and-trailing-document-delimiter.yml", "document-with-no-document-delimiter.yml"})
@ValueSource(strings = {
"document-with-trailing-document-delimiter.yml",
"document-with-leading-document-delimiter.yml",
"document-with-leading-and-trailing-document-delimiter.yml",
"document-with-no-document-delimiter.yml"
})
void unmarshalWithSingleDocumentWithDocumentDelimiterShouldReturnKubernetesResource(String arg) {
// When
final KubernetesResource result = Serialization.unmarshal(
SerializationTest.class.getResourceAsStream(String.format("/serialization/%s", arg)),
Collections.emptyMap()
);
SerializationTest.class.getResourceAsStream(String.format("/serialization/%s", arg)),
Collections.emptyMap());
// Then
assertThat(result)
.asInstanceOf(InstanceOfAssertFactories.type(Service.class))
.hasFieldOrPropertyWithValue("apiVersion", "v1")
.hasFieldOrPropertyWithValue("kind", "Service")
.hasFieldOrPropertyWithValue("metadata.name", "redis-master")
.hasFieldOrPropertyWithValue("metadata.labels.app", "redis")
.hasFieldOrPropertyWithValue("metadata.labels.tier", "backend")
.hasFieldOrPropertyWithValue("metadata.labels.role", "master")
.hasFieldOrPropertyWithValue("spec.selector.app", "redis")
.hasFieldOrPropertyWithValue("spec.selector.tier", "backend")
.hasFieldOrPropertyWithValue("spec.selector.role", "master");
.asInstanceOf(InstanceOfAssertFactories.type(Service.class))
.hasFieldOrPropertyWithValue("apiVersion", "v1")
.hasFieldOrPropertyWithValue("kind", "Service")
.hasFieldOrPropertyWithValue("metadata.name", "redis-master")
.hasFieldOrPropertyWithValue("metadata.labels.app", "redis")
.hasFieldOrPropertyWithValue("metadata.labels.tier", "backend")
.hasFieldOrPropertyWithValue("metadata.labels.role", "master")
.hasFieldOrPropertyWithValue("spec.selector.app", "redis")
.hasFieldOrPropertyWithValue("spec.selector.tier", "backend")
.hasFieldOrPropertyWithValue("spec.selector.role", "master");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;
import java.io.IOException;
Expand All @@ -65,10 +68,12 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class SerializationTest {

Expand Down Expand Up @@ -195,7 +200,7 @@ void testSerializeYamlWithAlias() {
}

@Test
void testClone() {
void cloneKubernetesResourceReturnsDifferentInstance() {
// Given
Pod pod = new PodBuilder().withNewMetadata().withName("pod").endMetadata().build();
// When
Expand All @@ -208,7 +213,7 @@ void testClone() {
}

@Test
void testCloneNonResource() {
void cloneNonResourceReturnsDifferentInstance() {
// Given
Map<String, String> value = Collections.singletonMap("key", "value");
// When
Expand Down Expand Up @@ -237,7 +242,7 @@ void unmarshalWithValidCustomResourceShouldReturnGenericCustomResource() {
}

@Test
void unmarshalWithInvalidYamlShouldThrowException() {
void unmarshalWithInvalidYamlShouldReturnRawExtension() {
// Given
final InputStream is = SerializationTest.class.getResourceAsStream("/serialization/invalid-yaml.yml");
// When
Expand All @@ -247,29 +252,62 @@ void unmarshalWithInvalidYamlShouldThrowException() {
}

@Test
void unmarshalArrays() {
void unmarshalYamlArrayShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> Serialization.unmarshal("- 1\n- 2"))
.withMessageStartingWith("Cannot parse a nested array containing non-object resource");
}

@Test
void unmarshalJsonArrayShouldThrowException() {
assertThatExceptionOfType(KubernetesClientException.class)
.isThrownBy(() -> Serialization.unmarshal("[1, 2]"))
.withMessage("An error has occurred.")
.havingCause()
.withMessageStartingWith("Cannot parse a nested array containing non-object resource");
}

@Test
void unmarshalYamlArrayWithProvidedTypeShouldDeserialize() {
// not valid as KubernetesResource - we'd have to look ahead to know if the array values
// were not hasmetadata
assertThrows(KubernetesClientException.class, () -> Serialization.unmarshal("[1, 2]"));
assertThrows(IllegalArgumentException.class, () -> Serialization.unmarshal("- 1\n- 2"));

assertEquals(Arrays.asList(1, 2), Serialization.unmarshal("[1, 2]", List.class));
assertEquals(Arrays.asList(1, 2), Serialization.unmarshal("- 1\n- 2", List.class));
}

@Test
void unmarshalPrimitives() {
// as json
RawExtension raw = Serialization.unmarshal("\"a\"");
assertEquals("a", raw.getValue());
// as yaml
raw = Serialization.unmarshal("a");
assertEquals("a", raw.getValue());
void unmarshalJsonArrayWithProvidedTypeShouldDeserialize() {
// not valid as KubernetesResource - we'd have to look ahead to know if the array values
// were not hasmetadata
assertEquals(Arrays.asList(1, 2), Serialization.unmarshal("[1, 2]", List.class));
}

@ParameterizedTest(name = "''{0}'' should be deserialized as ''{1}''")
@MethodSource("unmarshalPrimitivesInput")
void unmarshalPrimitives(String input, Object expected) {
assertThat(Serialization.<RawExtension>unmarshal(input))
.extracting(RawExtension::getValue)
.isEqualTo(expected);
assertEquals("a", Serialization.unmarshal("\"a\"", String.class));
assertEquals("a", Serialization.unmarshal("a", String.class));
}

@ParameterizedTest(name = "''{0}'' and ''{2}'' target type should be deserialized as ''{1}''")
@MethodSource("unmarshalPrimitivesInput")
void unmarshalPrimitivesWithType(String input, Object expected, Class<?> targetType) {
assertThat(Serialization.unmarshal(input, targetType))
.isEqualTo(expected);
}

static Stream<Arguments> unmarshalPrimitivesInput() {
return Stream.of(
Arguments.arguments("\"a\"", "a", String.class), // JSON
Arguments.arguments("a", "a", String.class), // YAML
Arguments.arguments("1", 1, Integer.class),
Arguments.arguments("true", true, Boolean.class),
Arguments.arguments("1.2", 1.2, Double.class)
);
}

@Test
void unmarshalRawResource() {
InputStream is = SerializationTest.class.getResourceAsStream("/serialization/invalid-resource.yml");
Expand Down

0 comments on commit b726498

Please sign in to comment.