Modify jackson bidirectional relationship test

This commit is contained in:
DOHA 2015-01-02 06:45:29 +02:00
parent f4758569a7
commit eb28e071ed
1 changed files with 14 additions and 22 deletions

View File

@ -26,23 +26,21 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonBidirectionRelationTest { public class JacksonBidirectionRelationTest {
@Test(expected = JsonMappingException.class) @Test(expected = JsonMappingException.class)
public void givenBidirectionRelation_whenSerialize_thenException() throws JsonProcessingException { public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException {
final User user = new User(1, "John"); final User user = new User(1, "John");
final Item item = new Item(2, "book", user); final Item item = new Item(2, "book", user);
user.addItem(item); user.addItem(item);
final ObjectMapper mapper = new ObjectMapper(); new ObjectMapper().writeValueAsString(item);
mapper.writeValueAsString(item);
} }
@Test @Test
public void givenBidirectionRelation_whenUseJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException { public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
final UserWithRef user = new UserWithRef(1, "John"); final UserWithRef user = new UserWithRef(1, "John");
final ItemWithRef item = new ItemWithRef(2, "book", user); final ItemWithRef item = new ItemWithRef(2, "book", user);
user.addItem(item); user.addItem(item);
final ObjectMapper mapper = new ObjectMapper(); final String result = new ObjectMapper().writeValueAsString(item);
final String result = mapper.writeValueAsString(item);
assertThat(result, containsString("book")); assertThat(result, containsString("book"));
assertThat(result, containsString("John")); assertThat(result, containsString("John"));
@ -50,13 +48,12 @@ public class JacksonBidirectionRelationTest {
} }
@Test @Test
public void givenBidirectionRelation_whenUseJsonIdentityInfo_thenCorrect() throws JsonProcessingException { public void givenBidirectionRelation_whenUsingJsonIdentityInfo_thenCorrect() throws JsonProcessingException {
final UserWithIdentity user = new UserWithIdentity(1, "John"); final UserWithIdentity user = new UserWithIdentity(1, "John");
final ItemWithIdentity item = new ItemWithIdentity(2, "book", user); final ItemWithIdentity item = new ItemWithIdentity(2, "book", user);
user.addItem(item); user.addItem(item);
final ObjectMapper mapper = new ObjectMapper(); final String result = new ObjectMapper().writeValueAsString(item);
final String result = mapper.writeValueAsString(item);
assertThat(result, containsString("book")); assertThat(result, containsString("book"));
assertThat(result, containsString("John")); assertThat(result, containsString("John"));
@ -64,13 +61,12 @@ public class JacksonBidirectionRelationTest {
} }
@Test @Test
public void givenBidirectionRelation_whenUseJsonIgnore_thenCorrect() throws JsonProcessingException { public void givenBidirectionRelation_whenUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
final UserWithIgnore user = new UserWithIgnore(1, "John"); final UserWithIgnore user = new UserWithIgnore(1, "John");
final ItemWithIgnore item = new ItemWithIgnore(2, "book", user); final ItemWithIgnore item = new ItemWithIgnore(2, "book", user);
user.addItem(item); user.addItem(item);
final ObjectMapper mapper = new ObjectMapper(); final String result = new ObjectMapper().writeValueAsString(item);
final String result = mapper.writeValueAsString(item);
assertThat(result, containsString("book")); assertThat(result, containsString("book"));
assertThat(result, containsString("John")); assertThat(result, containsString("John"));
@ -78,13 +74,12 @@ public class JacksonBidirectionRelationTest {
} }
@Test @Test
public void givenBidirectionRelation_whenUseCustomSerializer_thenCorrect() throws JsonProcessingException { public void givenBidirectionRelation_whenUsingCustomSerializer_thenCorrect() throws JsonProcessingException {
final UserWithSerializer user = new UserWithSerializer(1, "John"); final UserWithSerializer user = new UserWithSerializer(1, "John");
final ItemWithSerializer item = new ItemWithSerializer(2, "book", user); final ItemWithSerializer item = new ItemWithSerializer(2, "book", user);
user.addItem(item); user.addItem(item);
final ObjectMapper mapper = new ObjectMapper(); final String result = new ObjectMapper().writeValueAsString(item);
final String result = mapper.writeValueAsString(item);
assertThat(result, containsString("book")); assertThat(result, containsString("book"));
assertThat(result, containsString("John")); assertThat(result, containsString("John"));
@ -92,24 +87,21 @@ public class JacksonBidirectionRelationTest {
} }
@Test @Test
public void givenBidirectionRelation_whenDeserializeUsingIdentity_thenCorrect() throws JsonProcessingException, IOException { public void givenBidirectionRelation_whenDeserializingUsingIdentity_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}"; final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
final ObjectMapper mapper = new ObjectMapper(); final ItemWithIdentity item = new ObjectMapper().reader(ItemWithIdentity.class).readValue(json);
final ItemWithIdentity item = mapper.reader(ItemWithIdentity.class).readValue(json);
assertEquals(2, item.id); assertEquals(2, item.id);
assertEquals("book", item.itemName); assertEquals("book", item.itemName);
assertEquals("John", item.owner.name); assertEquals("John", item.owner.name);
} }
@Test @Test
public void givenBidirectionRelation_whenUseCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException { public void givenBidirectionRelation_whenUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException {
final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}"; final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}";
final ObjectMapper mapper = new ObjectMapper(); final ItemWithSerializer item = new ObjectMapper().reader(ItemWithSerializer.class).readValue(json);
final ItemWithSerializer item = mapper.reader(ItemWithSerializer.class).readValue(json);
assertEquals(2, item.id); assertEquals(2, item.id);
assertEquals("book", item.itemName); assertEquals("book", item.itemName);
assertEquals("John", item.owner.name); assertEquals("John", item.owner.name);