forked from JFulgoni/Java-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultSerializeTest.java
More file actions
110 lines (88 loc) · 3.47 KB
/
Copy pathDefaultSerializeTest.java
File metadata and controls
110 lines (88 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
/**
* Both of these unit tests will fail due to a bug in FasterXML.Jackson-Databind
*/
public class DefaultSerializeTest {
/**
* Here we are testing to see that the resulting JSON includes defaultOfTrue when it gets set to false,
* and that defaultOfFalse is not included because of JSONInclude(Include.NON_DEFAULT)
* @throws JsonProcessingException
*/
@Test
public void testDefaultOfTrue() throws JsonProcessingException{
/** Verify that the default value of DefaultOfTrue is True */
DefaultPOJO myPOJO = new DefaultPOJO();
assertTrue(myPOJO.getDefaultOfTrue());
/** Verify that the new value of DefaultOfTrue is False */
myPOJO.setDefaultOfTrueToFalse();
assertFalse(myPOJO.getDefaultOfTrue());
String expected = "{\"myString\":\"Hello World!\",\"defaultOfTrue\":false}";
String actual = new ObjectMapper().writeValueAsString(myPOJO);
assertEquals(expected, actual);
/** The actual JSON returned here is:
* < {"myString":"Hello World!"} >
* When we are expecting the non default value of defaultOfTrue to be included in the JSON
*/
}
/**
* Here we are testing to see that the resulting JSON includes defaultOfFalse when it gets set to true,
* and that defaultOfTrue is not included because of JSONInclude(Include.NON_DEFAULT)
* @throws JsonProcessingException
*/
@Test
public void testDefaultOfFalse() throws JsonProcessingException{
/** Verify that the default value of DefaultOfFalse is False */
DefaultPOJO myPOJO = new DefaultPOJO();
assertFalse(myPOJO.getDefaultOfFalse());
/**Verify that the new value of DefaultOfFalse is True */
myPOJO.setDefaultOfFalseToTrue();
assertTrue(myPOJO.getDefaultOfFalse());
String expected = "{\"myString\":\"Hello World!\",\"defaultOfFalse\":true}";
String actual = new ObjectMapper().writeValueAsString(myPOJO);
assertEquals(expected, actual);
/**
* The actual JSON returned here is:
* < {"myString":"Hello World!","defaultOfTrue":true,"defaultOfFalse":true} >
* When we are expecting the defaultOfTrue to not be included, since it's default value is true
*/
}
@JsonPropertyOrder({"myString", "defaultOfTrue", "defaultOfFalse"})
public static class DefaultPOJO {
@JsonProperty
@JsonInclude(Include.NON_DEFAULT)
private boolean defaultOfTrue = true;
@JsonProperty
@JsonInclude(Include.NON_DEFAULT)
private boolean defaultOfFalse = false;
@JsonProperty
private String myString;
@JsonCreator
public DefaultPOJO() {
myString = "Hello World!";
}
public DefaultPOJO setDefaultOfTrueToFalse(){
this.defaultOfTrue = false;
return this;
}
public DefaultPOJO setDefaultOfFalseToTrue(){
this.defaultOfFalse = true;
return this;
}
public boolean getDefaultOfTrue(){
return this.defaultOfTrue;
}
public boolean getDefaultOfFalse(){
return this.defaultOfFalse;
}
}
}