-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
TestFiltering.java
164 lines (138 loc) · 6.24 KB
/
TestFiltering.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.fasterxml.jackson.dataformat.csv.deser;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.FilterExceptFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvMappingException;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
public class TestFiltering extends ModuleTestBase
{
// Classes that represent views
static class ViewA { }
static class ViewAA extends ViewA { }
static class ViewB { }
static class ViewBB extends ViewB { }
@JsonPropertyOrder({ "a", "aa", "b" })
static class Bean
{
@JsonView({ ViewA.class, ViewB.class })
public String a = "1";
@JsonView({ViewAA.class })
public String aa = "2";
@JsonView(ViewB.class)
public String b = "3";
}
static final String COMPANY_FILTER = "COMPANY_FILTER";
@JsonPropertyOrder({ "id", "name", "ticker" })
@JsonFilter(COMPANY_FILTER)
public static class Company {
public int id;
public String name;
public String ticker;
Company() { }
Company(int id, String name, String ticker) {
this.id = id;
this.name = name;
this.ticker = ticker;
}
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
private final CsvMapper MAPPER = mapperForCsv();
public void testWithJsonView() throws Exception
{
CsvSchema schema = MAPPER.schemaFor(Bean.class).withLineSeparator("\n").withHeader();
String actual = MAPPER.writer(schema).withView(ViewB.class).writeValueAsString(new Bean());
// System.out.println(actual);
BufferedReader br = new BufferedReader(new StringReader(actual.trim()));
assertEquals("a,aa,b", br.readLine());
assertEquals("1,,3", br.readLine());
assertNull(br.readLine());
// plus read back?
final String INPUT = "a,aa,b\n5,6,7\n";
Bean result = MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(INPUT);
assertEquals("5", result.a);
// due to filtering, ought to use default
assertEquals("2", result.aa);
assertEquals("7", result.b);
}
public void testSchemaWithJsonViewSerialization() throws Exception
{
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
String actual = MAPPER.writer(schema).withView(ViewB.class).writeValueAsString(new Bean());
MAPPER.writer(schema).withView(ViewB.class);
BufferedReader br = new BufferedReader(new StringReader(actual.trim()));
assertEquals("a,b", br.readLine());
assertEquals("1,3", br.readLine());
assertNull(br.readLine());
}
public void testSchemaWithJsonViewDeserialization() throws Exception
{
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
final String input = "a,b\n5,7\n";
Bean result = MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input);
assertEquals("5", result.a);
// due to filtering, ought to use default
assertEquals("2", result.aa);
assertEquals("7", result.b);
}
public void testSchemaWithJsonViewDeserializationFail() throws Exception
{
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
final String input = "a,aa,b\n5,6,7\n";
try {
MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input);
fail();
} catch (CsvMappingException ignore) {
}
}
public void testWithJsonFilter() throws Exception
{
CsvSchema schema = MAPPER.schemaFor(Company.class).withLineSeparator("\n").withHeader();
SimpleFilterProvider filterProvider = new SimpleFilterProvider()
.addFilter(COMPANY_FILTER, FilterExceptFilter.filterOutAllExcept("name", "ticker"));
List<Company> companies = Arrays.asList(
new Company(1, "name1", "ticker1")
, new Company(2, "name2", "ticker2")
, new Company(3, "name3", "ticker3"));
String actual = MAPPER.writer(filterProvider).with(schema).writeValueAsString(companies);
// System.out.println(actual);
BufferedReader br = new BufferedReader(new StringReader(actual.trim()));
assertEquals("id,name,ticker", br.readLine());
assertEquals(",name1,ticker1", br.readLine());
assertEquals(",name2,ticker2", br.readLine());
assertEquals(",name3,ticker3", br.readLine());
assertNull(br.readLine());
}
public void testWithJsonFilterFieldSuppressed() throws Exception
{
final CsvSchema schema = new CsvSchema.Builder()
.addColumn("name")
.addColumn("ticker")
.setLineSeparator("\n").setUseHeader(true)
.build();
SimpleFilterProvider filterProvider = new SimpleFilterProvider()
.addFilter(COMPANY_FILTER, FilterExceptFilter.filterOutAllExcept("name", "ticker"));
List<Company> companies = Arrays.asList(
new Company(1, "name1", "ticker1")
, new Company(2, "name2", "ticker2")
, new Company(3, "name3", "ticker3"));
String actual = MAPPER.writer(filterProvider).with(schema).writeValueAsString(companies);
BufferedReader br = new BufferedReader(new StringReader(actual.trim()));
assertEquals("name,ticker", br.readLine());
assertEquals("name1,ticker1", br.readLine());
assertEquals("name2,ticker2", br.readLine());
assertEquals("name3,ticker3", br.readLine());
assertNull(br.readLine());
}
}