|
1 | 1 | package org.baeldung.jackson; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.containsString; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.hamcrest.Matchers.not; |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertThat; |
| 8 | + |
3 | 9 | import java.io.IOException; |
| 10 | +import java.util.List; |
4 | 11 |
|
5 | 12 | import org.junit.Test; |
6 | 13 |
|
7 | 14 | import com.fasterxml.jackson.core.JsonParseException; |
| 15 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 16 | +import com.fasterxml.jackson.databind.JsonMappingException; |
8 | 17 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 19 | +import com.google.common.collect.Lists; |
9 | 20 |
|
10 | 21 | public class JacksonUnitTest { |
11 | 22 |
|
12 | | - // tests |
| 23 | + // tests - single entity to json |
13 | 24 |
|
14 | 25 | @Test |
15 | | - public final void whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException { |
| 26 | + public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException { |
16 | 27 | final ObjectMapper mapper = new ObjectMapper(); |
17 | | - final String fooDtoAsString = mapper.writeValueAsString(new FooDto()); |
18 | | - System.out.println(fooDtoAsString); |
| 28 | + final String dtoAsString = mapper.writeValueAsString(new FooDto()); |
| 29 | + |
| 30 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 31 | + System.out.println(dtoAsString); |
19 | 32 | } |
20 | 33 |
|
21 | 34 | @Test |
22 | | - public final void whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException { |
| 35 | + public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException { |
23 | 36 | final ObjectMapper mapper = new ObjectMapper(); |
24 | 37 | final FooDto dtoObject = new FooDto(); |
25 | 38 | dtoObject.setBooleanValue(true); |
26 | 39 |
|
27 | | - final String fooDtoAsString = mapper.writeValueAsString(dtoObject); |
28 | | - System.out.println(fooDtoAsString); |
| 40 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 41 | + |
| 42 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 43 | + System.out.println(dtoAsString); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 48 | + final ObjectMapper mapper = new ObjectMapper(); |
| 49 | + final BarDto dtoObject = new BarDto(); |
| 50 | + dtoObject.setBooleanValue(true); |
| 51 | + |
| 52 | + final String dtoAsString = mapper.writeValueAsString(dtoObject); |
| 53 | + |
| 54 | + assertThat(dtoAsString, not(containsString("intValue"))); |
| 55 | + assertThat(dtoAsString, containsString("booleanValue")); |
| 56 | + System.out.println(dtoAsString); |
| 57 | + } |
| 58 | + |
| 59 | + // tests - multiple entities to json |
| 60 | + |
| 61 | + @Test |
| 62 | + public final void whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException { |
| 63 | + final List<MyDto> listOfDtos = Lists.newArrayList(new MyDto("a", 1, true), new MyDto("bc", 3, false)); |
| 64 | + |
| 65 | + final ObjectMapper mapper = new ObjectMapper(); |
| 66 | + final String dtosAsString = mapper.writeValueAsString(listOfDtos); |
| 67 | + |
| 68 | + System.out.println(dtosAsString); |
29 | 69 | } |
30 | 70 |
|
| 71 | + // tests - json to single entity |
| 72 | + |
| 73 | + @Test |
| 74 | + public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 75 | + final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true}"; |
| 76 | + final ObjectMapper mapper = new ObjectMapper(); |
| 77 | + |
| 78 | + final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); |
| 79 | + |
| 80 | + assertNotNull(readValue); |
| 81 | + assertThat(readValue.getStringValue(), equalTo("a")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test(expected = UnrecognizedPropertyException.class) |
| 85 | + public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException { |
| 86 | + final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; |
| 87 | + final ObjectMapper mapper = new ObjectMapper(); |
| 88 | + |
| 89 | + final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); |
| 90 | + |
| 91 | + assertNotNull(readValue); |
| 92 | + assertThat(readValue.getStringValue(), equalTo("a")); |
| 93 | + assertThat(readValue.isBooleanValue(), equalTo(true)); |
| 94 | + assertThat(readValue.getIntValue(), equalTo(1)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public final void givenJsonHasUnkownValuesButJacksonIsIgnoringUnkownFields_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 99 | + final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; |
| 100 | + final ObjectMapper mapper = new ObjectMapper(); |
| 101 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 102 | + |
| 103 | + final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class); |
| 104 | + |
| 105 | + assertNotNull(readValue); |
| 106 | + assertThat(readValue.getStringValue(), equalTo("a")); |
| 107 | + assertThat(readValue.isBooleanValue(), equalTo(true)); |
| 108 | + assertThat(readValue.getIntValue(), equalTo(1)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public final void givenJsonHasUnkownValuesButUnkownFieldsAreIgnoredOnClass_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { |
| 113 | + final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}"; |
| 114 | + final ObjectMapper mapper = new ObjectMapper(); |
| 115 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 116 | + |
| 117 | + final MyDtoIgnoreUnkown readValue = mapper.readValue(jsonAsString, MyDtoIgnoreUnkown.class); |
| 118 | + |
| 119 | + assertNotNull(readValue); |
| 120 | + assertThat(readValue.getStringValue(), equalTo("a")); |
| 121 | + assertThat(readValue.isBooleanValue(), equalTo(true)); |
| 122 | + assertThat(readValue.getIntValue(), equalTo(1)); |
| 123 | + } |
| 124 | + |
| 125 | + // tests - json to multiple entities |
| 126 | + |
31 | 127 | } |
32 | 128 |
|
33 | 129 | /* |
34 | 130 | Article Ideas: |
35 | 131 | - Deserializing with a custom JsonParser |
36 | | -
|
| 132 | +- Jackson Ignore: ignore specific fields at field level, at class level, |
37 | 133 | */ |
0 commit comments