/
TestJsonFilter.java
143 lines (116 loc) · 4.76 KB
/
TestJsonFilter.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.fasterxml.jackson.databind.filter;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.*;
/**
* Tests for verifying that bean property filtering using JsonFilter
* works as expected.
*/
public class TestJsonFilter extends BaseMapTest
{
@JsonFilter("RootFilter")
static class Bean {
public String a = "a";
public String b = "b";
}
// [Issue#89]
static class Pod
{
protected String username;
// @JsonProperty(value = "user_password")
protected String userPassword;
public String getUsername() {
return username;
}
public void setUsername(String value) {
this.username = value;
}
@JsonIgnore
@JsonProperty(value = "user_password")
public java.lang.String getUserPassword() {
return userPassword;
}
@JsonProperty(value = "user_password")
public void setUserPassword(String value) {
this.userPassword = value;
}
}
// [Issue#306]: JsonFilter for properties, too!
@JsonPropertyOrder(alphabetic=true)
static class FilteredProps
{
// will default to using "RootFilter", only including 'a'
public Bean first = new Bean();
// but minimal includes 'b'
@JsonFilter("b")
public Bean second = new Bean();
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
private final ObjectMapper MAPPER = new ObjectMapper();
public void testSimpleInclusionFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter",
SimpleBeanPropertyFilter.filterOutAllExcept("a"));
assertEquals("{\"a\":\"a\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
// [JACKSON-504]: also verify it works via mapper
ObjectMapper mapper = new ObjectMapper();
mapper.setFilterProvider(prov);
assertEquals("{\"a\":\"a\"}", mapper.writeValueAsString(new Bean()));
}
public void testSimpleExclusionFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter",
SimpleBeanPropertyFilter.serializeAllExcept("a"));
assertEquals("{\"b\":\"b\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}
// should handle missing case gracefully
public void testMissingFilter() throws Exception
{
// First: default behavior should be to throw an exception
try {
MAPPER.writeValueAsString(new Bean());
fail("Should have failed without configured filter");
} catch (JsonMappingException e) { // should be resolved to a MappingException (internally may be something else)
verifyException(e, "Can not resolve PropertyFilter with id 'RootFilter'");
}
// but when changing behavior, should work difference
SimpleFilterProvider fp = new SimpleFilterProvider().setFailOnUnknownId(false);
ObjectMapper mapper = new ObjectMapper();
mapper.setFilterProvider(fp);
String json = mapper.writeValueAsString(new Bean());
assertEquals("{\"a\":\"a\",\"b\":\"b\"}", json);
}
// defaulting, as per [JACKSON-449]
public void testDefaultFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().setDefaultFilter(SimpleBeanPropertyFilter.filterOutAllExcept("b"));
assertEquals("{\"b\":\"b\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}
// [Issue#89] combining @JsonIgnore, @JsonProperty
public void testIssue89() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Pod pod = new Pod();
pod.username = "Bob";
pod.userPassword = "s3cr3t!";
String json = mapper.writeValueAsString(pod);
assertEquals("{\"username\":\"Bob\"}", json);
Pod pod2 = mapper.readValue("{\"username\":\"Bill\",\"user_password\":\"foo!\"}", Pod.class);
assertEquals("Bill", pod2.username);
assertEquals("foo!", pod2.userPassword);
}
// Wrt [Issue#306]
public void testFilterOnProperty() throws Exception
{
FilterProvider prov = new SimpleFilterProvider()
.addFilter("RootFilter", SimpleBeanPropertyFilter.filterOutAllExcept("a"))
.addFilter("b", SimpleBeanPropertyFilter.filterOutAllExcept("b"));
assertEquals("{\"first\":{\"a\":\"a\"},\"second\":{\"b\":\"b\"}}",
MAPPER.writer(prov).writeValueAsString(new FilteredProps()));
}
}