Merge pull request #108 from Doha2012/master
Add jackson bidirectional relationship test
This commit is contained in:
		
						commit
						208ed4a8ee
					
				| @ -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<List<ItemWithSerializer>> { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public List<ItemWithSerializer> deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { | ||||||
|  |         return new ArrayList<ItemWithSerializer>(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -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<List<ItemWithSerializer>> { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void serialize(final List<ItemWithSerializer> items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { | ||||||
|  |         final List<Integer> ids = new ArrayList<Integer>(); | ||||||
|  |         for (final ItemWithSerializer item : items) { | ||||||
|  |             ids.add(item.id); | ||||||
|  |         } | ||||||
|  |         generator.writeObject(ids); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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<Item> userItems; | ||||||
|  | 
 | ||||||
|  |     public User() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public User(final int id, final String name) { | ||||||
|  |         this.id = id; | ||||||
|  |         this.name = name; | ||||||
|  |         userItems = new ArrayList<Item>(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void addItem(final Item item) { | ||||||
|  |         userItems.add(item); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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<ItemWithIdentity> userItems; | ||||||
|  | 
 | ||||||
|  |     public UserWithIdentity() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public UserWithIdentity(final int id, final String name) { | ||||||
|  |         this.id = id; | ||||||
|  |         this.name = name; | ||||||
|  |         userItems = new ArrayList<ItemWithIdentity>(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void addItem(final ItemWithIdentity item) { | ||||||
|  |         userItems.add(item); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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<ItemWithIgnore> userItems; | ||||||
|  | 
 | ||||||
|  |     public UserWithIgnore() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public UserWithIgnore(final int id, final String name) { | ||||||
|  |         this.id = id; | ||||||
|  |         this.name = name; | ||||||
|  |         userItems = new ArrayList<ItemWithIgnore>(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void addItem(final ItemWithIgnore item) { | ||||||
|  |         userItems.add(item); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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<ItemWithRef> userItems; | ||||||
|  | 
 | ||||||
|  |     public UserWithRef() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public UserWithRef(final int id, final String name) { | ||||||
|  |         this.id = id; | ||||||
|  |         this.name = name; | ||||||
|  |         userItems = new ArrayList<ItemWithRef>(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void addItem(final ItemWithRef item) { | ||||||
|  |         userItems.add(item); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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<ItemWithSerializer> userItems; | ||||||
|  | 
 | ||||||
|  |     public UserWithSerializer() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public UserWithSerializer(final int id, final String name) { | ||||||
|  |         this.id = id; | ||||||
|  |         this.name = name; | ||||||
|  |         userItems = new ArrayList<ItemWithSerializer>(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void addItem(final ItemWithSerializer item) { | ||||||
|  |         userItems.add(item); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user