From 19e2fca9cb35bf69c5aa861a1a1d859999ebb7f2 Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 17 Dec 2014 21:39:25 +0200 Subject: [PATCH] Add JacksonExceptions Test --- .../org/baeldung/jackson/exception/User.java | 11 ++ .../jackson/exception/UserWithConflict.java | 25 +++ .../exception/UserWithPrivateFields.java | 16 ++ .../jackson/exception/UserWithRoot.java | 18 ++ .../org/baeldung/jackson/exception/Zoo.java | 19 ++ .../jackson/exception/ZooConfigured.java | 22 +++ .../jackson/test/JacksonExceptionsTest.java | 171 ++++++++++++++++++ 7 files changed, 282 insertions(+) create mode 100644 jackson/src/test/java/org/baeldung/jackson/exception/User.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/User.java b/jackson/src/test/java/org/baeldung/jackson/exception/User.java new file mode 100644 index 0000000000..764d5872ad --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/exception/User.java @@ -0,0 +1,11 @@ +package org.baeldung.jackson.exception; + +public class User { + public int id; + public String name; + + public User(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java b/jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java new file mode 100644 index 0000000000..79d4199e91 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/exception/UserWithConflict.java @@ -0,0 +1,25 @@ +package org.baeldung.jackson.exception; + +public class UserWithConflict { + public int id; + public String name; + boolean checked; + + public UserWithConflict() { + super(); + } + + public UserWithConflict(final int id, final String name, final boolean checked) { + this.id = id; + this.name = name; + this.checked = checked; + } + + public boolean getChecked() { + return checked; + } + + public boolean isChecked() { + return checked; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java b/jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java new file mode 100644 index 0000000000..707623bbb2 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/exception/UserWithPrivateFields.java @@ -0,0 +1,16 @@ +package org.baeldung.jackson.exception; + +public class UserWithPrivateFields { + int id; + String name; + + public UserWithPrivateFields() { + super(); + } + + public UserWithPrivateFields(final int id, final String name) { + this.id = id; + this.name = name; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java b/jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java new file mode 100644 index 0000000000..2cf78e0365 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/exception/UserWithRoot.java @@ -0,0 +1,18 @@ +package org.baeldung.jackson.exception; + +import com.fasterxml.jackson.annotation.JsonRootName; + +@JsonRootName(value = "user") +public class UserWithRoot { + public int id; + public String name; + + public UserWithRoot() { + super(); + } + + public UserWithRoot(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java b/jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java new file mode 100644 index 0000000000..be6d3965ba --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/exception/Zoo.java @@ -0,0 +1,19 @@ +package org.baeldung.jackson.exception; + +public class Zoo { + public Animal animal; +} + +abstract class Animal { + public String name; + + protected Animal() { + } +} + +class Cat extends Animal { + public int lives; + + public Cat() { + } +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java b/jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java new file mode 100644 index 0000000000..d76de5ffe2 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/exception/ZooConfigured.java @@ -0,0 +1,22 @@ +package org.baeldung.jackson.exception; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +public class ZooConfigured { + public AnimalConfigured animal; +} + +@JsonDeserialize(as = CatConfigured.class) +abstract class AnimalConfigured { + public String name; + + protected AnimalConfigured() { + } +} + +class CatConfigured extends AnimalConfigured { + public int lives; + + public CatConfigured() { + } +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java new file mode 100644 index 0000000000..147b733a2c --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonExceptionsTest.java @@ -0,0 +1,171 @@ +package org.baeldung.jackson.test; + +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.List; + +import org.baeldung.jackson.dtos.User; +import org.baeldung.jackson.exception.UserWithPrivateFields; +import org.baeldung.jackson.exception.UserWithRoot; +import org.baeldung.jackson.exception.Zoo; +import org.baeldung.jackson.exception.ZooConfigured; +import org.junit.Test; + +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; + +public class JacksonExceptionsTest { + + // JsonMappingException: Can not construct instance of + @Test(expected = JsonMappingException.class) + public void givenAbstractClass_whenDeserializing_thenException() throws IOException { + final String json = "{\"animal\":{\"name\":\"lacy\"}}"; + final ObjectMapper mapper = new ObjectMapper(); + + mapper.reader().withType(Zoo.class).readValue(json); + } + + @Test + public void givenAbstractClassConfigured_whenDeserializing_thenCorrect() throws IOException { + final String json = "{\"animal\":{\"name\":\"lacy\"}}"; + final ObjectMapper mapper = new ObjectMapper(); + + mapper.reader().withType(ZooConfigured.class).readValue(json); + } + + // JsonMappingException: No serializer found for class + @Test(expected = JsonMappingException.class) + public void givenClassWithPrivateFields_whenSerializing_thenException() throws IOException { + final UserWithPrivateFields user = new UserWithPrivateFields(1, "John"); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.writer().writeValueAsString(user); + } + + @Test + public void givenClassWithPrivateFields_whenConfigureSerializing_thenCorrect() throws IOException { + final UserWithPrivateFields user = new UserWithPrivateFields(1, "John"); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); + + final String result = mapper.writer().writeValueAsString(user); + assertThat(result, containsString("John")); + } + + // JsonMappingException: No suitable constructor found + @Test(expected = JsonMappingException.class) + public void givenNoDefaultConstructor_whenDeserializing_thenException() throws IOException { + final String json = "{\"id\":1,\"name\":\"John\"}"; + final ObjectMapper mapper = new ObjectMapper(); + + mapper.reader().withType(org.baeldung.jackson.exception.User.class).readValue(json); + } + + @Test + public void givenDefaultConstructor_whenDeserializing_thenCorrect() throws IOException { + final String json = "{\"id\":1,\"name\":\"John\"}"; + final ObjectMapper mapper = new ObjectMapper(); + + final User user = mapper.reader().withType(User.class).readValue(json); + assertEquals("John", user.name); + } + + // JsonMappingException: Root name does not match expected + @Test(expected = JsonMappingException.class) + public void givenWrappedJsonString_whenDeserializing_thenException() throws IOException { + final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}"; + + final ObjectMapper mapper = new ObjectMapper(); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + mapper.reader().withType(User.class).readValue(json); + } + + @Test + public void givenWrappedJsonStringAndConfigureClass_whenDeserializing_thenCorrect() throws IOException { + final String json = "{\"user\":{\"id\":1,\"name\":\"John\"}}"; + + final ObjectMapper mapper = new ObjectMapper(); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + final UserWithRoot user = mapper.reader().withType(UserWithRoot.class).readValue(json); + assertEquals("John", user.name); + } + + // JsonMappingException: Can not deserialize instance of + @Test(expected = JsonMappingException.class) + public void givenJsonOfArray_whenDeserializing_thenException() throws JsonProcessingException, IOException { + final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; + final ObjectMapper mapper = new ObjectMapper(); + + mapper.reader().withType(User.class).readValue(json); + } + + @Test + public void givenJsonOfArray_whenDeserializing_thenCorrect() throws JsonProcessingException, IOException { + final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]"; + final ObjectMapper mapper = new ObjectMapper(); + + final List users = mapper.reader().withType(new TypeReference>() { + }).readValue(json); + + assertEquals(2, users.size()); + } + + // UnrecognizedPropertyException + @Test(expected = UnrecognizedPropertyException.class) + public void givenJsonStringWithExtra_whenDeserializing_thenException() throws IOException { + final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}"; + + final ObjectMapper mapper = new ObjectMapper(); + mapper.reader().withType(User.class).readValue(json); + } + + @Test + public void givenJsonStringWithExtra_whenConfigureDeserializing_thenCorrect() throws IOException { + final String json = "{\"id\":1,\"name\":\"John\", \"checked\":true}"; + + final ObjectMapper mapper = new ObjectMapper(); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + + final User user = mapper.reader().withType(User.class).readValue(json); + assertEquals("John", user.name); + } + + // JsonParseException: Unexpected character (''' (code 39)) + @Test(expected = JsonParseException.class) + public void givenStringWithSingleQuotes_whenDeserializing_thenException() throws JsonProcessingException, IOException { + final String json = "{'id':1,'name':'John'}"; + final ObjectMapper mapper = new ObjectMapper(); + + mapper.reader().withType(User.class).readValue(json); + } + + @Test + public void givenStringWithSingleQuotes_whenConfigureDeserializing_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{'id':1,'name':'John'}"; + + final JsonFactory factory = new JsonFactory(); + factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); + final ObjectMapper mapper = new ObjectMapper(factory); + + final User user = mapper.reader().withType(User.class).readValue(json); + assertEquals("John", user.name); + } + + + +}