Skip to content

Commit

Permalink
fix #3625: test corrections related to having non-null values
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed May 23, 2022
1 parent 7746090 commit 2629db3
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Collections;

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

Expand All @@ -40,8 +42,7 @@ void tearDown() {
@DisplayName("unmarshal, with unknown fields, values are set in additionalProperties map")
void unmarshalWithUnknownFields() {
// Given
final String marshalled =
"{\"kind\": \"ConfigMap\"," +
final String marshalled = "{\"kind\": \"ConfigMap\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\":{\"name\":\"the-name\"}," +
"\"data\":{\"key\":\"value\"}," +
Expand All @@ -50,39 +51,37 @@ void unmarshalWithUnknownFields() {
final KubernetesResource result = Serialization.unmarshal(marshalled);
// Then
assertThat(result)
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("data.key", "value")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("data.key", "value")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
}

@Test
@DisplayName("unmarshal, with unmatched type fields, should throw Exception")
void unmarshalWithUnmatchedTypeFieldsAndDefaults() {
// Given
final String marshalled =
"{\"kind\": \"ConfigMap\"," +
final String marshalled = "{\"kind\": \"ConfigMap\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\":{\"name\":\"the-name\"}," +
"\"data\":{\"key\":\"value\"}," +
"\"immutable\":\"${immutable}\"}";
// When
final KubernetesClientException result = assertThrows(KubernetesClientException.class, () ->
Serialization.unmarshal(marshalled));
final KubernetesClientException result = assertThrows(KubernetesClientException.class,
() -> Serialization.unmarshal(marshalled));
// Then
assertThat(result)
.getCause()
.isInstanceOf(InvalidFormatException.class)
.hasMessageStartingWith("Cannot deserialize value of type `java.lang.Boolean` from String \"${immutable}\"");
.getCause()
.isInstanceOf(InvalidFormatException.class)
.hasMessageStartingWith("Cannot deserialize value of type `java.lang.Boolean` from String \"${immutable}\"");
}

@Test
@DisplayName("unmarshal, with unmatched type fields and unrestricted, values are set in additionalProperties map")
void unmarshalWithUnmatchedTypeFields() {
// Given
Serialization.UNMATCHED_FIELD_TYPE_MODULE.setRestrictToTemplates(false);
final String marshalled =
"{\"kind\": \"ConfigMap\"," +
final String marshalled = "{\"kind\": \"ConfigMap\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\":{\"name\":\"the-name\"}," +
"\"data\":{\"key\":\"value\"}," +
Expand All @@ -92,20 +91,19 @@ void unmarshalWithUnmatchedTypeFields() {
final KubernetesResource result = Serialization.unmarshal(marshalled);
// Then
assertThat(result)
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("immutable", null)
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue")
.hasFieldOrPropertyWithValue("additionalProperties.immutable", "${immutable}");
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("immutable", null)
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue")
.hasFieldOrPropertyWithValue("additionalProperties.immutable", "${immutable}");
}

@Test
@DisplayName("unmarshal, with unmatched type nested fields and unrestricted, values are set in additionalProperties map")
void unmarshalWithUnmatchedTypeNestedFields() {
// Given
Serialization.UNMATCHED_FIELD_TYPE_MODULE.setRestrictToTemplates(false);
final String marshalled =
"{\"kind\": \"Deployment\"," +
final String marshalled = "{\"kind\": \"Deployment\"," +
"\"apiVersion\": \"apps/v1\"," +
"\"metadata\":{\"name\":\"deployment\", \"annotations\": \"${annotations}\"}," +
"\"spec\":{\"replicas\":\"${replicas}\",\"paused\":true}," +
Expand All @@ -114,37 +112,37 @@ void unmarshalWithUnmatchedTypeNestedFields() {
final KubernetesResource result = Serialization.unmarshal(marshalled);
// Then
assertThat(result)
.isInstanceOf(Deployment.class)
.hasFieldOrPropertyWithValue("metadata.name", "deployment")
.hasFieldOrPropertyWithValue("metadata.annotations", null)
.hasFieldOrPropertyWithValue("metadata.additionalProperties.annotations", "${annotations}")
.hasFieldOrPropertyWithValue("spec.paused", true)
.hasFieldOrPropertyWithValue("spec.replicas", null)
.hasFieldOrPropertyWithValue("spec.additionalProperties.replicas", "${replicas}")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
.isInstanceOf(Deployment.class)
.hasFieldOrPropertyWithValue("metadata.name", "deployment")
.hasFieldOrPropertyWithValue("metadata.annotations", Collections.emptyMap())
.hasFieldOrPropertyWithValue("metadata.additionalProperties.annotations", "${annotations}")
.hasFieldOrPropertyWithValue("spec.paused", true)
.hasFieldOrPropertyWithValue("spec.replicas", null)
.hasFieldOrPropertyWithValue("spec.additionalProperties.replicas", "${replicas}")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
}

@Test
@DisplayName("marshal, with unmatched type fields, values are set in additionalProperties map")
void marshalWithAdditionalPropertiesOverridingFields() {
// Given
final ConfigMap configMap = new ConfigMapBuilder()
.withNewMetadata().withName("name").addToAnnotations("key", "value").endMetadata()
.withImmutable(true)
.build();
.withNewMetadata().withName("name").addToAnnotations("key", "value").endMetadata()
.withImmutable(true)
.build();
configMap.getAdditionalProperties().put("immutable", "${immutable}");
configMap.getAdditionalProperties().put("unknownField", "unknownValue");
configMap.getMetadata().getAdditionalProperties().put("annotations", "${annotations}");
// When
final String result = Serialization.asJson(configMap);
// Then
assertThat(result).isEqualTo("{" +
"\"apiVersion\":\"v1\"," +
"\"kind\":\"ConfigMap\"," +
"\"metadata\":{\"name\":\"name\",\"annotations\":\"${annotations}\"}," +
"\"immutable\":\"${immutable}\"," +
"\"unknownField\":\"unknownValue\"" +
"}");
"\"apiVersion\":\"v1\"," +
"\"kind\":\"ConfigMap\"," +
"\"metadata\":{\"name\":\"name\",\"annotations\":\"${annotations}\"}," +
"\"immutable\":\"${immutable}\"," +
"\"unknownField\":\"unknownValue\"" +
"}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -55,8 +57,7 @@ void setUp() {
@DisplayName("readValue, with unknown fields, values are set in additionalProperties map")
void readValueWithUnknownFields() throws JsonProcessingException {
// Given
final String json =
"{\"kind\": \"ConfigMap\"," +
final String json = "{\"kind\": \"ConfigMap\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\":{\"name\":\"the-name\"}," +
"\"data\":{\"key\":\"value\"}," +
Expand All @@ -65,18 +66,17 @@ void readValueWithUnknownFields() throws JsonProcessingException {
final KubernetesResource result = objectMapper.readValue(json, KubernetesResource.class);
// Then
assertThat(result)
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("data.key", "value")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("data.key", "value")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
}

@Test
@DisplayName("readValue, with unmatched type fields, values are set in additionalProperties map")
void readValueWithUnmatchedTypeFields() throws JsonProcessingException {
// Given
final String json =
"{\"kind\": \"ConfigMap\"," +
final String json = "{\"kind\": \"ConfigMap\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\":{\"name\":\"the-name\"}," +
"\"data\":{\"key\":\"value\"}," +
Expand All @@ -86,19 +86,18 @@ void readValueWithUnmatchedTypeFields() throws JsonProcessingException {
final KubernetesResource result = objectMapper.readValue(json, KubernetesResource.class);
// Then
assertThat(result)
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("immutable", null)
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue")
.hasFieldOrPropertyWithValue("additionalProperties.immutable", "${immutable}");
.isInstanceOf(ConfigMap.class)
.hasFieldOrPropertyWithValue("metadata.name", "the-name")
.hasFieldOrPropertyWithValue("immutable", null)
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue")
.hasFieldOrPropertyWithValue("additionalProperties.immutable", "${immutable}");
}

@Test
@DisplayName("readValue, with unmatched type nested fields, values are set in additionalProperties map")
void readValueWithUnmatchedTypeNestedFields() throws JsonProcessingException {
// Given
final String json =
"{\"kind\": \"Deployment\"," +
final String json = "{\"kind\": \"Deployment\"," +
"\"apiVersion\": \"apps/v1\"," +
"\"metadata\":{\"name\":\"deployment\", \"annotations\": \"${annotations}\"}," +
"\"spec\":{\"replicas\":\"${replicas}\",\"paused\":true}," +
Expand All @@ -107,24 +106,24 @@ void readValueWithUnmatchedTypeNestedFields() throws JsonProcessingException {
final KubernetesResource result = objectMapper.readValue(json, KubernetesResource.class);
// Then
assertThat(result)
.isInstanceOf(Deployment.class)
.hasFieldOrPropertyWithValue("metadata.name", "deployment")
.hasFieldOrPropertyWithValue("metadata.annotations", null)
.hasFieldOrPropertyWithValue("metadata.additionalProperties.annotations", "${annotations}")
.hasFieldOrPropertyWithValue("spec.paused", true)
.hasFieldOrPropertyWithValue("spec.replicas", null)
.hasFieldOrPropertyWithValue("spec.additionalProperties.replicas", "${replicas}")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
.isInstanceOf(Deployment.class)
.hasFieldOrPropertyWithValue("metadata.name", "deployment")
.hasFieldOrPropertyWithValue("metadata.annotations", Collections.emptyMap())
.hasFieldOrPropertyWithValue("metadata.additionalProperties.annotations", "${annotations}")
.hasFieldOrPropertyWithValue("spec.paused", true)
.hasFieldOrPropertyWithValue("spec.replicas", null)
.hasFieldOrPropertyWithValue("spec.additionalProperties.replicas", "${replicas}")
.hasFieldOrPropertyWithValue("additionalProperties.unknownField", "unknownValue");
}

@Test
@DisplayName("readValue, with no anySetter, should throw Exception")
void readValueWithNoAnySetterShouldThrowException() {
void readValueWithNoAnySetterShouldThrowException() {
// Given
final String json = "{\"value\": false}";
// When
final MismatchedInputException result = assertThrows(MismatchedInputException.class, () ->
objectMapper.readValue(json, Example.class));
final MismatchedInputException result = assertThrows(MismatchedInputException.class,
() -> objectMapper.readValue(json, Example.class));
// Then
assertThat(result).hasMessageStartingWith("Cannot deserialize value of type `int` from Boolean value");
}
Expand All @@ -134,15 +133,14 @@ void readValueWithNoAnySetterShouldThrowException() {
void readValueWithRestrictToTemplatesAndUnmatchedTypeFields() {
// Given
unmatchedFieldTypeModule.setRestrictToTemplates(true);
final String json =
"{\"kind\": \"ConfigMap\"," +
final String json = "{\"kind\": \"ConfigMap\"," +
"\"apiVersion\": \"v1\"," +
"\"metadata\":{\"name\":\"the-name\"}," +
"\"data\":{\"key\":\"value\"}," +
"\"immutable\":\"${immutable}\"}";
// When
final MismatchedInputException result = assertThrows(MismatchedInputException.class, () ->
objectMapper.readValue(json, KubernetesResource.class));
final MismatchedInputException result = assertThrows(MismatchedInputException.class,
() -> objectMapper.readValue(json, KubernetesResource.class));
// Then
assertThat(result).hasMessageStartingWith("Cannot deserialize value of type `java.lang.Boolean` from String");
}
Expand All @@ -152,22 +150,22 @@ void readValueWithRestrictToTemplatesAndUnmatchedTypeFields() {
void writeValueAsStringWithAdditionalPropertiesOverridingFields() throws JsonProcessingException {
// Given
final ConfigMap configMap = new ConfigMapBuilder()
.withNewMetadata().withName("name").addToAnnotations("key", "ignored").addToLabels("lKey", "value").endMetadata()
.withImmutable(true)
.build();
.withNewMetadata().withName("name").addToAnnotations("key", "ignored").addToLabels("lKey", "value").endMetadata()
.withImmutable(true)
.build();
configMap.getAdditionalProperties().put("immutable", "${immutable}");
configMap.getAdditionalProperties().put("unknownField", "unknownValue");
configMap.getMetadata().getAdditionalProperties().put("annotations", "${annotations}");
// When
final String result = objectMapper.writeValueAsString(configMap);
// Then
assertThat(result).isEqualTo("{" +
"\"apiVersion\":\"v1\"," +
"\"kind\":\"ConfigMap\"," +
"\"metadata\":{\"labels\":{\"lKey\":\"value\"},\"name\":\"name\",\"annotations\":\"${annotations}\"}," +
"\"immutable\":\"${immutable}\"," +
"\"unknownField\":\"unknownValue\"" +
"}");
"\"apiVersion\":\"v1\"," +
"\"kind\":\"ConfigMap\"," +
"\"metadata\":{\"labels\":{\"lKey\":\"value\"},\"name\":\"name\",\"annotations\":\"${annotations}\"}," +
"\"immutable\":\"${immutable}\"," +
"\"unknownField\":\"unknownValue\"" +
"}");
}

@Test
Expand All @@ -177,18 +175,18 @@ void writeValueAsStringWithAdditionalPropertiesOverridingFieldsShouldLogWarning(
// Given
unmatchedFieldTypeModule.setLogWarnings(true);
final ConfigMap configMap = new ConfigMapBuilder()
.withNewMetadata().withName("name").endMetadata()
.withImmutable(true)
.build();
.withNewMetadata().withName("name").endMetadata()
.withImmutable(true)
.build();
configMap.getAdditionalProperties().put("immutable", "I'll trigger a warning");
final Logger mockLogger = mock(Logger.class, RETURNS_DEEP_STUBS);
lfMock.when(() -> LoggerFactory.getLogger(any(Class.class))).thenReturn(mockLogger);
// When
objectMapper.writeValueAsString(configMap);
// Then
verify(mockLogger, times(1))
.warn("Value in field '{}' ignored in favor of value in additionalProperties ({}) for {}",
"immutable", "I'll trigger a warning", "io.fabric8.kubernetes.api.model.ConfigMap");
.warn("Value in field '{}' ignored in favor of value in additionalProperties ({}) for {}",
"immutable", "I'll trigger a warning", "io.fabric8.kubernetes.api.model.ConfigMap");
}
}

Expand All @@ -202,18 +200,18 @@ void writeValueAsStringWithAdditionalPropertiesOverridingFieldsShouldNotLogWarni
om.registerModule(module);
module.setLogWarnings(false);
final ConfigMap configMap = new ConfigMapBuilder()
.withNewMetadata().withName("name").endMetadata()
.withImmutable(true)
.build();
.withNewMetadata().withName("name").endMetadata()
.withImmutable(true)
.build();
configMap.getAdditionalProperties().put("immutable", "I'll trigger a warning");
final Logger mockLogger = mock(Logger.class, RETURNS_DEEP_STUBS);
lfMock.when(() -> LoggerFactory.getLogger(any(Class.class))).thenReturn(mockLogger);
// When
om.writeValueAsString(configMap);
// Then
verify(mockLogger, never())
.warn("Value in field '{}' ignored in favor of value in additionalProperties ({}) for {}",
"immutable", "I'll trigger a warning", "io.fabric8.kubernetes.api.model.ConfigMap");
.warn("Value in field '{}' ignored in favor of value in additionalProperties ({}) for {}",
"immutable", "I'll trigger a warning", "io.fabric8.kubernetes.api.model.ConfigMap");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void createOrReplace() {
assertNotNull(replacedConfigMap.getMetadata().getCreationTimestamp());
assertNotNull(replacedConfigMap.getMetadata().getUid());
ConfigMap configMapQueriedFromServer = client.configMaps().withName(name).get();
assertNull(configMapQueriedFromServer.getMetadata().getLabels());
assertTrue(configMapQueriedFromServer.getMetadata().getLabels().isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void testJsonMergePatch() {

// Then
assertThat(patchedConfigMap).isNotNull();
assertThat(patchedConfigMap.getMetadata().getAnnotations()).isNull();
assertThat(patchedConfigMap.getMetadata().getAnnotations()).isNullOrEmpty();
}

@Test
Expand Down
Loading

0 comments on commit 2629db3

Please sign in to comment.