Skip to content

Commit

Permalink
review: template deserializer and UnmatchedFieldTypeModule
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Aug 23, 2022
1 parent e19e8cd commit 3393442
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 87 deletions.
16 changes: 16 additions & 0 deletions kubernetes-model-generator/kubernetes-model-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,30 @@
*/
package io.fabric8.kubernetes.model.jackson;

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

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.junit.jupiter.api.Test;

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

public class JsonUnwrappedDeserializerTest {
class JsonUnwrappedDeserializerTest {

private static final String EXPECTED_VALUE_A = "Value A";
private static final String EXPECTED_VALUE_B = "Value B";
private static final String EXPECTED_VALUE_C = "Value C";

@Test
public void shouldDeserializeInterfacesWithJsonWrapped() throws JsonProcessingException {
void shouldDeserializeInterfacesWithJsonWrapped() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
RootClass instance = mapper.readValue("{ \"stringField\": \"" + EXPECTED_VALUE_A + "\", "
+ "\"extendedField\": \"" + EXPECTED_VALUE_B + "\", "
+ "\"nestedField\": \"" + EXPECTED_VALUE_C + "\" }", RootClass.class);
+ "\"extendedField\": \"" + EXPECTED_VALUE_B + "\", "
+ "\"nestedField\": \"" + EXPECTED_VALUE_C + "\" }", RootClass.class);
// Verify normal fields works along to the json-wrapped fields
assertEquals(EXPECTED_VALUE_A, instance.stringField);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
Expand All @@ -42,14 +43,14 @@ class SettableBeanPropertyDelegateTest {
private SettableBeanProperty delegateMock;
private SettableAnyProperty anySetterMock;
private SettableBeanPropertyDelegate settableBeanPropertyDelegate;
private AtomicBoolean useAnySetter = new AtomicBoolean();
private AtomicBoolean useAnySetter;

@BeforeEach
void setUp() {
delegateMock = mock(SettableBeanProperty.class, RETURNS_DEEP_STUBS);
anySetterMock = mock(SettableAnyProperty.class);
useAnySetter.set(false);
settableBeanPropertyDelegate = new SettableBeanPropertyDelegate(delegateMock, anySetterMock, () -> useAnySetter.get());
useAnySetter = new AtomicBoolean(false);
settableBeanPropertyDelegate = new SettableBeanPropertyDelegate(delegateMock, anySetterMock, useAnySetter::get);
}

@Test
Expand Down Expand Up @@ -183,14 +184,14 @@ void deserializeSetAndReturn() throws IOException {
}

@Test
@DisplayName("deserializeSetAndReturn, throws Exception, should try anySetter or rethrow")
void deserializeSetAndReturnWithException() throws IOException {
@DisplayName("deserializeSetAndReturn, with anySetter enabled and throws Exception, should use anySetter")
void deserializeSetAndReturnWithExceptionUsingAnySetter() throws IOException {
// Given
final Object instance = new Object();
when(delegateMock.getName()).thenReturn("the-property");
when(delegateMock.deserializeSetAndReturn(any(), any(), eq(instance)))
.thenThrow(MismatchedInputException.from(null, Integer.class, "The Mocked Exception"));
doThrow(MismatchedInputException.from(null, Integer.class, "The Mocked Exception"))
doThrow(MismatchedInputException.from(null, Integer.class, "The delegate deserializeAndSet Exception"))
.when(delegateMock).deserializeAndSet(any(), any(), eq(instance));
useAnySetter.set(true);
// When
Expand All @@ -199,4 +200,22 @@ void deserializeSetAndReturnWithException() throws IOException {
assertThat(result).isNull();
verify(anySetterMock, times(1)).set(eq(instance), eq("the-property"), any());
}

@Test
@DisplayName("deserializeSetAndReturn, with anySetter disabled and throws Exception, should throw Exception")
void deserializeSetAndReturnWithExceptionNotUsingAnySetter() throws IOException {
// Given
final Object instance = new Object();
when(delegateMock.getName()).thenReturn("the-property");
when(delegateMock.deserializeSetAndReturn(any(), any(), eq(instance)))
.thenThrow(MismatchedInputException.from(null, Integer.class, "The Mocked Exception"));
doThrow(MismatchedInputException.from(null, Integer.class, "The delegate deserializeAndSet Exception"))
.when(delegateMock).deserializeAndSet(any(), any(), eq(instance));
// When
final MismatchedInputException result = assertThrows(MismatchedInputException.class,
() -> settableBeanPropertyDelegate.deserializeSetAndReturn(mock(JsonParser.class), null, instance));
// Then
assertThat(result)
.hasMessage("The delegate deserializeAndSet Exception");
}
}

0 comments on commit 3393442

Please sign in to comment.