Skip to content

Commit

Permalink
Add deserialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trickl committed Sep 9, 2019
1 parent be4d955 commit 5344e7b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public boolean serializeAsNameValue(
if (prop.willSuppressNulls()) {
return false;
} else {
gen.writeRaw(name);
return true;
}
provider.findNullValueSerializer(prop).serialize(propValue, valueGenerator, provider);
}
} else if (prop.getType().isTypeOrSubTypeOf(Collection.class)
|| prop.getType().isArrayType()) {
Collection<?> collection = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand All @@ -18,6 +20,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

public class JavaTimePropertyTest {

private static ObjectMapper objectMapper;
Expand All @@ -33,20 +39,27 @@ public static void setup() {
}

@HttpQuery
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
private static class JsonFormatQuery {
@JsonProperty("param")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "UTC")
private Instant value;

public JsonFormatQuery(Instant value) {
this.value = value;
}
private LocalDate value;
}

@Test
public void testStringParamSerialization() throws JsonProcessingException {
assertEquals("?param=2013-01-31",
objectMapper.writeValueAsString(new JsonFormatQuery(
Instant.parse("2013-01-31T10:15:00.00Z"))));
LocalDate.parse("2013-01-31"))));
}

@Test
public void testStringParamDeserialization() throws IOException {
assertEquals(new JsonFormatQuery(
LocalDate.parse("2013-01-31")),
objectMapper.readValue("\"?param=2013-01-31\"",
JsonFormatQuery.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand All @@ -14,6 +15,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

public class JsonFormatPropertyTest {

private static ObjectMapper objectMapper;
Expand All @@ -25,21 +30,28 @@ public static void setup() {
}

@HttpQuery
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
private static class JsonFormatQuery {
@JsonProperty("param")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date value;

public JsonFormatQuery(Date value) {
this.value = value;
}
}

@Test
public void testStringParamSerialization() throws JsonProcessingException {
Calendar calendar = new GregorianCalendar(2013,0,31);
Calendar calendar = new GregorianCalendar(2013, 0, 31);
assertEquals("?param=2013-01-31",
objectMapper.writeValueAsString(new JsonFormatQuery(
calendar.getTime())));
objectMapper.writeValueAsString(new JsonFormatQuery(calendar.getTime())));
}

@Test
public void testStringParamDeserialization() throws IOException {
Calendar calendar = new GregorianCalendar(2013,0,31);
assertEquals(new JsonFormatQuery(
calendar.getTime()),
objectMapper.readValue("\"?param=2013-01-31\"",
JsonFormatQuery.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -11,6 +13,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

public class JsonIgnoreTest {

private static ObjectMapper objectMapper;
Expand All @@ -22,7 +28,10 @@ public static void setup() {
}

@HttpQuery
private static class MultiPropertyQuery {
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
private static class MultiPropertyQuery {
@JsonProperty("paramA")
private String valueA;

Expand All @@ -32,18 +41,18 @@ private static class MultiPropertyQuery {

@JsonProperty("paramC")
private int valueC;

public MultiPropertyQuery(String valueA, String valueB, int valueC) {
this.valueA = valueA;
this.valueB = valueB;
this.valueC = valueC;
}
}

@Test
public void testIgnoreParamSerialization() throws JsonProcessingException {
assertEquals("?paramA=valueA&paramC=123",
objectMapper.writeValueAsString(
new MultiPropertyQuery("valueA", "valueB", 123)));
objectMapper.writeValueAsString(new MultiPropertyQuery("valueA", "valueB", 123)));
}

@Test
public void testIgnoreParamDeserialization() throws IOException {
assertEquals(new MultiPropertyQuery("valueA", null, 123),
objectMapper.readValue("\"?paramA=valueA&paramC=123\"",
MultiPropertyQuery.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -12,7 +14,11 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class JsonIncludeTest {
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

public class JsonNullsTest {

private static ObjectMapper objectMapper;

Expand All @@ -23,31 +29,43 @@ public static void setup() {
}

@HttpQuery
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
private static class MultiPropertyQuery {
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("paramA")
private String valueA;

@JsonProperty("paramB")
private String valueB;

public MultiPropertyQuery(String valueA, String valueB) {
this.valueA = valueA;
this.valueB = valueB;
}
private Integer valueB;
}

@Test
public void testExcludedNullParamSerialization() throws JsonProcessingException {
assertEquals("?paramB=valueB",
assertEquals("?paramB=123",
objectMapper.writeValueAsString(
new MultiPropertyQuery(null, "valueB")));
new MultiPropertyQuery(null, 123)));
}

@Test
public void testIncludedNullAsEmptyParamSerialization() throws JsonProcessingException {
assertEquals("?paramA=valueA&paramB",
assertEquals("?paramA=valueA&paramB=null",
objectMapper.writeValueAsString(
new MultiPropertyQuery("valueA", null)));
}

@Test
public void testExcludedNullParamDeserialization() throws IOException {
assertEquals(new MultiPropertyQuery(null, 123),
objectMapper.readValue("\"?paramB=123\"",
MultiPropertyQuery.class));
}

@Test
public void testIncludeNullAsEmptyParamDeserialization() throws IOException {
assertEquals(new MultiPropertyQuery("valueA", null),
objectMapper.readValue("\"?paramA=valueA&paramB=null\"",
MultiPropertyQuery.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import lombok.AllArgsConstructor;

public class JsonPropertyTest {

private static ObjectMapper objectMapper;
Expand All @@ -21,25 +23,18 @@ public static void setup() {
objectMapper.registerModule(new HttpQueryModule());
}

@AllArgsConstructor
private static class ObjectProperty {
private String valueA;

private String valueB;

public ObjectProperty(String valueA, String valueB) {
this.valueA = valueA;
this.valueB = valueB;
}
}

@HttpQuery
@AllArgsConstructor
private static class JsonPropertyQuery {
@JsonProperty("param")
private ObjectProperty value;

public JsonPropertyQuery(ObjectProperty value) {
this.value = value;
}
}

@Test
Expand Down

0 comments on commit 5344e7b

Please sign in to comment.