16
16
17
17
package org .springframework .http .codec .json ;
18
18
19
- import java .util .Arrays ;
20
- import java .util .Collections ;
21
19
import java .util .List ;
22
20
import java .util .Map ;
23
21
24
- import com .fasterxml .jackson .annotation .JsonView ;
22
+ import static java .util .Arrays .asList ;
23
+ import static java .util .Collections .*;
25
24
import org .junit .Test ;
25
+ import static org .springframework .http .MediaType .*;
26
+ import static org .springframework .http .codec .json .Jackson2JsonDecoder .*;
27
+ import static org .springframework .http .codec .json .JacksonViewBean .*;
26
28
import reactor .core .publisher .Flux ;
27
29
import reactor .core .publisher .Mono ;
28
30
import reactor .test .StepVerifier ;
31
33
import org .springframework .core .codec .CodecException ;
32
34
import org .springframework .core .io .buffer .AbstractDataBufferAllocatingTestCase ;
33
35
import org .springframework .core .io .buffer .DataBuffer ;
34
- import org .springframework .http .MediaType ;
35
36
import org .springframework .http .codec .Pojo ;
36
37
37
38
import static org .junit .Assert .assertFalse ;
@@ -50,30 +51,29 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
50
51
public void canDecode () {
51
52
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder ();
52
53
ResolvableType type = ResolvableType .forClass (Pojo .class );
53
- assertTrue (decoder .canDecode (type , MediaType . APPLICATION_JSON ));
54
+ assertTrue (decoder .canDecode (type , APPLICATION_JSON ));
54
55
assertTrue (decoder .canDecode (type , null ));
55
- assertFalse (decoder .canDecode (type , MediaType . APPLICATION_XML ));
56
+ assertFalse (decoder .canDecode (type , APPLICATION_XML ));
56
57
}
57
58
58
59
@ Test
59
60
public void decodePojo () throws Exception {
60
61
Flux <DataBuffer > source = Flux .just (stringBuffer ("{\" foo\" : \" foofoo\" , \" bar\" : \" barbar\" }" ));
61
62
ResolvableType elementType = ResolvableType .forClass (Pojo .class );
62
63
Flux <Object > flux = new Jackson2JsonDecoder ().decode (source , elementType , null ,
63
- Collections . emptyMap ());
64
+ emptyMap ());
64
65
65
66
StepVerifier .create (flux )
66
67
.expectNext (new Pojo ("foofoo" , "barbar" ))
67
- .expectComplete ()
68
- .verify ();
68
+ .verifyComplete ();
69
69
}
70
70
71
71
@ Test
72
72
public void decodePojoWithError () throws Exception {
73
73
Flux <DataBuffer > source = Flux .just (stringBuffer ("{\" foo\" :}" ));
74
74
ResolvableType elementType = ResolvableType .forClass (Pojo .class );
75
75
Flux <Object > flux = new Jackson2JsonDecoder ().decode (source , elementType , null ,
76
- Collections . emptyMap ());
76
+ emptyMap ());
77
77
78
78
StepVerifier .create (flux ).verifyError (CodecException .class );
79
79
}
@@ -85,10 +85,10 @@ public void decodeToList() throws Exception {
85
85
86
86
ResolvableType elementType = ResolvableType .forClassWithGenerics (List .class , Pojo .class );
87
87
Mono <Object > mono = new Jackson2JsonDecoder ().decodeToMono (source , elementType ,
88
- null , Collections . emptyMap ());
88
+ null , emptyMap ());
89
89
90
90
StepVerifier .create (mono )
91
- .expectNext (Arrays . asList (new Pojo ("f1" , "b1" ), new Pojo ("f2" , "b2" )))
91
+ .expectNext (asList (new Pojo ("f1" , "b1" ), new Pojo ("f2" , "b2" )))
92
92
.expectComplete ()
93
93
.verify ();
94
94
}
@@ -100,21 +100,20 @@ public void decodeToFlux() throws Exception {
100
100
101
101
ResolvableType elementType = ResolvableType .forClass (Pojo .class );
102
102
Flux <Object > flux = new Jackson2JsonDecoder ().decode (source , elementType , null ,
103
- Collections . emptyMap ());
103
+ emptyMap ());
104
104
105
105
StepVerifier .create (flux )
106
106
.expectNext (new Pojo ("f1" , "b1" ))
107
107
.expectNext (new Pojo ("f2" , "b2" ))
108
- .expectComplete ()
109
- .verify ();
108
+ .verifyComplete ();
110
109
}
111
110
112
111
@ Test
113
- public void jsonView () throws Exception {
112
+ public void fieldLevelJsonView () throws Exception {
114
113
Flux <DataBuffer > source = Flux .just (
115
114
stringBuffer ("{\" withView1\" : \" with\" , \" withView2\" : \" with\" , \" withoutView\" : \" without\" }" ));
116
115
ResolvableType elementType = ResolvableType .forClass (JacksonViewBean .class );
117
- Map <String , Object > hints = Collections . singletonMap (Jackson2JsonDecoder . JSON_VIEW_HINT , MyJacksonView1 .class );
116
+ Map <String , Object > hints = singletonMap (JSON_VIEW_HINT , MyJacksonView1 .class );
118
117
Flux <JacksonViewBean > flux = new Jackson2JsonDecoder ()
119
118
.decode (source , elementType , null , hints ).cast (JacksonViewBean .class );
120
119
@@ -124,63 +123,37 @@ public void jsonView() throws Exception {
124
123
assertNull (b .getWithView2 ());
125
124
assertNull (b .getWithoutView ());
126
125
})
127
- .expectComplete ()
128
- .verify ();
126
+ .verifyComplete ();
127
+ }
128
+
129
+ @ Test
130
+ public void classLevelJsonView () throws Exception {
131
+ Flux <DataBuffer > source = Flux .just (stringBuffer (
132
+ "{\" withView1\" : \" with\" , \" withView2\" : \" with\" , \" withoutView\" : \" without\" }" ));
133
+ ResolvableType elementType = ResolvableType .forClass (JacksonViewBean .class );
134
+ Map <String , Object > hints = singletonMap (JSON_VIEW_HINT , MyJacksonView3 .class );
135
+ Flux <JacksonViewBean > flux = new Jackson2JsonDecoder ()
136
+ .decode (source , elementType , null , hints ).cast (JacksonViewBean .class );
137
+
138
+ StepVerifier .create (flux )
139
+ .consumeNextWith (b -> {
140
+ assertNull (b .getWithView1 ());
141
+ assertNull (b .getWithView2 ());
142
+ assertTrue (b .getWithoutView ().equals ("without" ));
143
+ })
144
+ .verifyComplete ();
129
145
}
130
146
131
147
@ Test
132
148
public void decodeEmptyBodyToMono () throws Exception {
133
149
Flux <DataBuffer > source = Flux .empty ();
134
150
ResolvableType elementType = ResolvableType .forClass (Pojo .class );
135
151
Mono <Object > mono = new Jackson2JsonDecoder ().decodeToMono (source , elementType ,
136
- null , Collections . emptyMap ());
152
+ null , emptyMap ());
137
153
138
154
StepVerifier .create (mono )
139
155
.expectNextCount (0 )
140
- .expectComplete ()
141
- .verify ();
142
- }
143
-
144
-
145
- private interface MyJacksonView1 {}
146
-
147
- private interface MyJacksonView2 {}
148
-
149
-
150
- @ SuppressWarnings ("unused" )
151
- private static class JacksonViewBean {
152
-
153
- @ JsonView (MyJacksonView1 .class )
154
- private String withView1 ;
155
-
156
- @ JsonView (MyJacksonView2 .class )
157
- private String withView2 ;
158
-
159
- private String withoutView ;
160
-
161
- public String getWithView1 () {
162
- return withView1 ;
163
- }
164
-
165
- public void setWithView1 (String withView1 ) {
166
- this .withView1 = withView1 ;
167
- }
168
-
169
- public String getWithView2 () {
170
- return withView2 ;
171
- }
172
-
173
- public void setWithView2 (String withView2 ) {
174
- this .withView2 = withView2 ;
175
- }
176
-
177
- public String getWithoutView () {
178
- return withoutView ;
179
- }
180
-
181
- public void setWithoutView (String withoutView ) {
182
- this .withoutView = withoutView ;
183
- }
156
+ .verifyComplete ();
184
157
}
185
158
186
159
}
0 commit comments