import
static
org.junit.matchers.JUnitMatchers.*;
import
static
org.junit.Assert.assertEquals;
import
static
org.junit.Assert.assertThat;
import
java.io.IOException;
import
java.util.List;
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;
import
com.gfg.exceptions.Author;
import
com.gfg.exceptions.AuthorWithNoDefaultConstructor;
import
com.gfg.exceptions.AuthorWithPrivateFields;
import
com.gfg.exceptions.AuthorWithRoot;
import
com.gfg.exceptions.AnimalLives;
import
com.gfg.exceptions.AnimalLivesConfigured;
public
class
JacksonExceptionsUnitTestExample {
@Test
(expected = JsonMappingException.
class
)
public
void
givenAbstractClass_whenDeserializing_thenException()
throws
IOException {
final
String json =
"{\"animal\":{\"name\":\"rocky\"}}"
;
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.reader()
.forType(AnimalLives.
class
)
.readValue(json);
}
@Test
public
void
givenAbstractClassConfigured_whenDeserializing_thenCorrect()
throws
IOException {
final
String json =
"{\"animal\":{\"name\":\"bruce\"}}"
;
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.reader()
.forType(AnimalLivesConfigured.
class
)
.readValue(json);
}
@Test
(expected = JsonMappingException.
class
)
public
void
givenClassWithPrivateFields_whenSerializing_thenException()
throws
IOException {
final
AuthorWithPrivateFields user =
new
AuthorWithPrivateFields(
1
,
"Rachel"
);
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.writer()
.writeValueAsString(user);
}
@Test
public
void
givenClassWithPrivateFields_whenConfigureSerializing_thenCorrect()
throws
IOException {
final
AuthorWithPrivateFields user =
new
AuthorWithPrivateFields(
1
,
"Ross"
);
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
final
String result = mapper.writer()
.writeValueAsString(user);
assertThat(result, containsString(
"Ross"
));
}
@Test
(expected = JsonMappingException.
class
)
public
void
givenNoDefaultConstructor_whenDeserializing_thenException()
throws
IOException {
final
String json =
"{\"id\":1,\"name\":\"Chandler\"}"
;
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.reader()
.forType(AuthorWithNoDefaultConstructor.
class
)
.readValue(json);
}
@Test
public
void
givenDefaultConstructor_whenDeserializing_thenCorrect()
throws
IOException {
final
String json =
"{\"id\":1,\"name\":\"Joey\"}"
;
final
ObjectMapper mapper =
new
ObjectMapper();
final
Author user = mapper.reader()
.forType(Author.
class
)
.readValue(json);
assertEquals(
"Joey"
, user.name);
}
@Test
(expected = JsonMappingException.
class
)
public
void
givenWrappedJsonString_whenDeserializing_thenException()
throws
IOException {
final
String json =
"{\"user\":{\"id\":1,\"name\":\"Monica\"}}"
;
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.reader()
.forType(Author.
class
)
.readValue(json);
}
@Test
public
void
givenWrappedJsonStringAndConfigureClass_whenDeserializing_thenCorrect()
throws
IOException {
final
String json =
"{\"user\":{\"id\":1,\"name\":\"Phoebe\"}}"
;
final
ObjectMapper mapper =
new
ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
final
AuthorWithRoot user = mapper.reader()
.forType(AuthorWithRoot.
class
)
.readValue(json);
assertEquals(
"Phoebe"
, user.name);
}
}