From 4cee202068d7f4f3b3f612e8e4a05d60d9ed3f3f Mon Sep 17 00:00:00 2001 From: DOHA Date: Tue, 30 Dec 2014 21:44:39 +0200 Subject: [PATCH] Add jackson bidirectional relationship test --- .../bidirection/CustomListDeserializer.java | 19 +++ .../bidirection/CustomListSerializer.java | 22 ++++ .../baeldung/jackson/bidirection/Item.java | 18 +++ .../jackson/bidirection/ItemWithIdentity.java | 21 ++++ .../jackson/bidirection/ItemWithIgnore.java | 18 +++ .../jackson/bidirection/ItemWithRef.java | 21 ++++ .../bidirection/ItemWithSerializer.java | 18 +++ .../baeldung/jackson/bidirection/User.java | 24 ++++ .../jackson/bidirection/UserWithIdentity.java | 28 +++++ .../jackson/bidirection/UserWithIgnore.java | 28 +++++ .../jackson/bidirection/UserWithRef.java | 28 +++++ .../bidirection/UserWithSerializer.java | 30 +++++ .../test/JacksonBidirectionRelationTest.java | 119 ++++++++++++++++++ 13 files changed, 394 insertions(+) create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/User.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java create mode 100644 jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java new file mode 100644 index 0000000000..cc4e1d5589 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java @@ -0,0 +1,19 @@ +package org.baeldung.jackson.bidirection; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +public class CustomListDeserializer extends JsonDeserializer> { + + @Override + public List deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { + return new ArrayList(); + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java new file mode 100644 index 0000000000..545f7e18c1 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java @@ -0,0 +1,22 @@ +package org.baeldung.jackson.bidirection; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +public class CustomListSerializer extends JsonSerializer> { + + @Override + public void serialize(final List items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { + final List ids = new ArrayList(); + for (final ItemWithSerializer item : items) { + ids.add(item.id); + } + generator.writeObject(ids); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java new file mode 100644 index 0000000000..506ee8f709 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java @@ -0,0 +1,18 @@ +package org.baeldung.jackson.bidirection; + + +public class Item { + public int id; + public String itemName; + public User owner; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final User owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java new file mode 100644 index 0000000000..2091ec6e40 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java @@ -0,0 +1,21 @@ +package org.baeldung.jackson.bidirection; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +public class ItemWithIdentity { + public int id; + public String itemName; + public UserWithIdentity owner; + + public ItemWithIdentity() { + super(); + } + + public ItemWithIdentity(final int id, final String itemName, final UserWithIdentity owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java new file mode 100644 index 0000000000..ab21593c1b --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java @@ -0,0 +1,18 @@ +package org.baeldung.jackson.bidirection; + + +public class ItemWithIgnore { + public int id; + public String itemName; + public UserWithIgnore owner; + + public ItemWithIgnore() { + super(); + } + + public ItemWithIgnore(final int id, final String itemName, final UserWithIgnore owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java new file mode 100644 index 0000000000..295ec9580d --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java @@ -0,0 +1,21 @@ +package org.baeldung.jackson.bidirection; + +import com.fasterxml.jackson.annotation.JsonManagedReference; + +public class ItemWithRef { + public int id; + public String itemName; + + @JsonManagedReference + public UserWithRef owner; + + public ItemWithRef() { + super(); + } + + public ItemWithRef(final int id, final String itemName, final UserWithRef owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java new file mode 100644 index 0000000000..d69efcce87 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java @@ -0,0 +1,18 @@ +package org.baeldung.jackson.bidirection; + + +public class ItemWithSerializer { + public int id; + public String itemName; + public UserWithSerializer owner; + + public ItemWithSerializer() { + super(); + } + + public ItemWithSerializer(final int id, final String itemName, final UserWithSerializer owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/User.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/User.java new file mode 100644 index 0000000000..a92dff4052 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/User.java @@ -0,0 +1,24 @@ +package org.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +public class User { + public int id; + public String name; + public List userItems; + + public User() { + super(); + } + + public User(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final Item item) { + userItems.add(item); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java new file mode 100644 index 0000000000..52fa22c32d --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java @@ -0,0 +1,28 @@ +package org.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +public class UserWithIdentity { + public int id; + public String name; + public List userItems; + + public UserWithIdentity() { + super(); + } + + public UserWithIdentity(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithIdentity item) { + userItems.add(item); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java new file mode 100644 index 0000000000..7714487f76 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java @@ -0,0 +1,28 @@ +package org.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +public class UserWithIgnore { + public int id; + public String name; + + @JsonIgnore + public List userItems; + + public UserWithIgnore() { + super(); + } + + public UserWithIgnore(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithIgnore item) { + userItems.add(item); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java new file mode 100644 index 0000000000..406b87f78c --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java @@ -0,0 +1,28 @@ +package org.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonBackReference; + +public class UserWithRef { + public int id; + public String name; + + @JsonBackReference + public List userItems; + + public UserWithRef() { + super(); + } + + public UserWithRef(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithRef item) { + userItems.add(item); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java new file mode 100644 index 0000000000..25c202a9d3 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java @@ -0,0 +1,30 @@ +package org.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +public class UserWithSerializer { + public int id; + public String name; + + @JsonSerialize(using = CustomListSerializer.class) + @JsonDeserialize(using = CustomListDeserializer.class) + public List userItems; + + public UserWithSerializer() { + super(); + } + + public UserWithSerializer(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithSerializer item) { + userItems.add(item); + } +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java new file mode 100644 index 0000000000..195142b246 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonBidirectionRelationTest.java @@ -0,0 +1,119 @@ +package org.baeldung.jackson.test; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import org.baeldung.jackson.bidirection.Item; +import org.baeldung.jackson.bidirection.ItemWithIdentity; +import org.baeldung.jackson.bidirection.ItemWithIgnore; +import org.baeldung.jackson.bidirection.ItemWithRef; +import org.baeldung.jackson.bidirection.ItemWithSerializer; +import org.baeldung.jackson.bidirection.User; +import org.baeldung.jackson.bidirection.UserWithIdentity; +import org.baeldung.jackson.bidirection.UserWithIgnore; +import org.baeldung.jackson.bidirection.UserWithRef; +import org.baeldung.jackson.bidirection.UserWithSerializer; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JacksonBidirectionRelationTest { + + @Test(expected = JsonMappingException.class) + public void givenBidirectionRelation_whenSerialize_thenException() throws JsonProcessingException { + final User user = new User(1, "John"); + final Item item = new Item(2, "book", user); + user.addItem(item); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.writeValueAsString(item); + } + + @Test + public void givenBidirectionRelation_whenUseJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException { + final UserWithRef user = new UserWithRef(1, "John"); + final ItemWithRef item = new ItemWithRef(2, "book", user); + user.addItem(item); + + final ObjectMapper mapper = new ObjectMapper(); + final String result = mapper.writeValueAsString(item); + + assertThat(result, containsString("book")); + assertThat(result, containsString("John")); + assertThat(result, not(containsString("userItems"))); + } + + @Test + public void givenBidirectionRelation_whenUseJsonIdentityInfo_thenCorrect() throws JsonProcessingException { + final UserWithIdentity user = new UserWithIdentity(1, "John"); + final ItemWithIdentity item = new ItemWithIdentity(2, "book", user); + user.addItem(item); + + final ObjectMapper mapper = new ObjectMapper(); + final String result = mapper.writeValueAsString(item); + + assertThat(result, containsString("book")); + assertThat(result, containsString("John")); + assertThat(result, containsString("userItems")); + } + + @Test + public void givenBidirectionRelation_whenUseJsonIgnore_thenCorrect() throws JsonProcessingException { + final UserWithIgnore user = new UserWithIgnore(1, "John"); + final ItemWithIgnore item = new ItemWithIgnore(2, "book", user); + user.addItem(item); + + final ObjectMapper mapper = new ObjectMapper(); + final String result = mapper.writeValueAsString(item); + + assertThat(result, containsString("book")); + assertThat(result, containsString("John")); + assertThat(result, not(containsString("userItems"))); + } + + @Test + public void givenBidirectionRelation_whenUseCustomSerializer_thenCorrect() throws JsonProcessingException { + final UserWithSerializer user = new UserWithSerializer(1, "John"); + final ItemWithSerializer item = new ItemWithSerializer(2, "book", user); + user.addItem(item); + + final ObjectMapper mapper = new ObjectMapper(); + final String result = mapper.writeValueAsString(item); + + assertThat(result, containsString("book")); + assertThat(result, containsString("John")); + assertThat(result, containsString("userItems")); + } + + @Test + public void givenBidirectionRelation_whenDeserializeUsingIdentity_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}"; + + final ObjectMapper mapper = new ObjectMapper(); + + final ItemWithIdentity item = mapper.reader(ItemWithIdentity.class).readValue(json); + assertEquals(2, item.id); + assertEquals("book", item.itemName); + assertEquals("John", item.owner.name); + } + + @Test + public void givenBidirectionRelation_whenUseCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException { + final String json = "{\"id\":2,\"itemName\":\"book\",\"owner\":{\"id\":1,\"name\":\"John\",\"userItems\":[2]}}"; + + final ObjectMapper mapper = new ObjectMapper(); + + final ItemWithSerializer item = mapper.reader(ItemWithSerializer.class).readValue(json); + assertEquals(2, item.id); + assertEquals("book", item.itemName); + assertEquals("John", item.owner.name); + } + +} +