Skip to content

Commit

Permalink
Added more tests, but really just gotta make sure your Jackson versio…
Browse files Browse the repository at this point in the history
…n is 2.13 or above
  • Loading branch information
retrodaredevil committed Oct 31, 2021
1 parent c109c8e commit 514bccf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public JacksonJsonData deserialize(JsonParser p, DeserializationContext ctxt) th

@Override
public Object getAbsentValue(DeserializationContext ctxt) throws JsonMappingException {
// Note that this method is not available for Jackson versions below 2.13, so using 2.13 is the only thing that makes the deserialization of this work as expected
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import me.retrodaredevil.couchdbjava.json.JsonData;
import me.retrodaredevil.couchdbjava.json.jackson.JacksonJsonData;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SolarThingBulkGetTest {
@Test
void test() throws JsonProcessingException {
InputStream stream = getClass().getClassLoader().getResourceAsStream("bulk_get_data_2021-10-29.json");
requireNonNull(stream, "Could not get resource");
String data = new BufferedReader(new InputStreamReader(stream)).lines().parallel().collect(Collectors.joining("\n"));
ObjectMapper mapper = new ObjectMapper();
BulkGetResponse response = mapper.readValue(data, BulkGetResponse.class);
assertEquals(88, response.getResults().size());
for (String name : new String[] {
"bulk_get_data_2021-10-29.json",
"bulk_get_data_2021-10-30.json",
}) {
InputStream stream = getClass().getClassLoader().getResourceAsStream(name);
requireNonNull(stream, "Could not get resource: " + name);
String data = new BufferedReader(new InputStreamReader(stream)).lines().parallel().collect(Collectors.joining("\n"));
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(data, BulkGetResponse.class);
}
// This test relies on the correct deserialization of JacksonJsonData.
// This test was originally added because JSON undefined (not present), was being given to InnerResult's constructor has a JSON null, rather than a Java null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.retrodaredevil.couchdbjava.json.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -18,4 +20,44 @@ void test() throws JsonProcessingException {
ObjectNode objectNode = (ObjectNode) jsonData.getNode();
assertEquals(43, objectNode.get("test").asInt());
}
@Test
void testWithAbsent() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{}";
mapper.readValue(jsonString, ExpectingNullWhenAbsent.class);
}
@Test
void testWithNull() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"value\":null}";
mapper.readValue(jsonString, ExpectingJsonNullWhenNull.class);
}
@Test
void testWithEmptyString() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"value\":\"\"}";
mapper.readValue(jsonString, ExpectingEmptyStringWhenEmptyString.class);
}

private static class ExpectingNullWhenAbsent {

@JsonCreator
public ExpectingNullWhenAbsent(@JsonProperty("value") JsonData data) {
assertNull(data);
}
}
private static class ExpectingJsonNullWhenNull {

@JsonCreator
public ExpectingJsonNullWhenNull(@JsonProperty("value") JsonData data) {
assertEquals("null", data.getJson());
}
}
private static class ExpectingEmptyStringWhenEmptyString {

@JsonCreator
public ExpectingEmptyStringWhenEmptyString(@JsonProperty("value") JsonData data) {
assertEquals("\"\"", data.getJson());
}
}
}
1 change: 1 addition & 0 deletions couchdb/src/test/resources/bulk_get_data_2021-10-30.json

Large diffs are not rendered by default.

0 comments on commit 514bccf

Please sign in to comment.