Skip to content

Commit 4db2daa

Browse files
committed
Add class level @JSONVIEW tests
Issue: SPR-14925
1 parent 722e230 commit 4db2daa

File tree

4 files changed

+155
-128
lines changed

4 files changed

+155
-128
lines changed

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package org.springframework.http.codec.json;
1818

19-
import java.util.Arrays;
20-
import java.util.Collections;
2119
import java.util.List;
2220
import java.util.Map;
2321

24-
import com.fasterxml.jackson.annotation.JsonView;
22+
import static java.util.Arrays.asList;
23+
import static java.util.Collections.*;
2524
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.*;
2628
import reactor.core.publisher.Flux;
2729
import reactor.core.publisher.Mono;
2830
import reactor.test.StepVerifier;
@@ -31,7 +33,6 @@
3133
import org.springframework.core.codec.CodecException;
3234
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
3335
import org.springframework.core.io.buffer.DataBuffer;
34-
import org.springframework.http.MediaType;
3536
import org.springframework.http.codec.Pojo;
3637

3738
import static org.junit.Assert.assertFalse;
@@ -50,30 +51,29 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
5051
public void canDecode() {
5152
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
5253
ResolvableType type = ResolvableType.forClass(Pojo.class);
53-
assertTrue(decoder.canDecode(type, MediaType.APPLICATION_JSON));
54+
assertTrue(decoder.canDecode(type, APPLICATION_JSON));
5455
assertTrue(decoder.canDecode(type, null));
55-
assertFalse(decoder.canDecode(type, MediaType.APPLICATION_XML));
56+
assertFalse(decoder.canDecode(type, APPLICATION_XML));
5657
}
5758

5859
@Test
5960
public void decodePojo() throws Exception {
6061
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
6162
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
6263
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
63-
Collections.emptyMap());
64+
emptyMap());
6465

6566
StepVerifier.create(flux)
6667
.expectNext(new Pojo("foofoo", "barbar"))
67-
.expectComplete()
68-
.verify();
68+
.verifyComplete();
6969
}
7070

7171
@Test
7272
public void decodePojoWithError() throws Exception {
7373
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\":}"));
7474
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
7575
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
76-
Collections.emptyMap());
76+
emptyMap());
7777

7878
StepVerifier.create(flux).verifyError(CodecException.class);
7979
}
@@ -85,10 +85,10 @@ public void decodeToList() throws Exception {
8585

8686
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
8787
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType,
88-
null, Collections.emptyMap());
88+
null, emptyMap());
8989

9090
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")))
9292
.expectComplete()
9393
.verify();
9494
}
@@ -100,21 +100,20 @@ public void decodeToFlux() throws Exception {
100100

101101
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
102102
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
103-
Collections.emptyMap());
103+
emptyMap());
104104

105105
StepVerifier.create(flux)
106106
.expectNext(new Pojo("f1", "b1"))
107107
.expectNext(new Pojo("f2", "b2"))
108-
.expectComplete()
109-
.verify();
108+
.verifyComplete();
110109
}
111110

112111
@Test
113-
public void jsonView() throws Exception {
112+
public void fieldLevelJsonView() throws Exception {
114113
Flux<DataBuffer> source = Flux.just(
115114
stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
116115
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);
118117
Flux<JacksonViewBean> flux = new Jackson2JsonDecoder()
119118
.decode(source, elementType, null, hints).cast(JacksonViewBean.class);
120119

@@ -124,63 +123,37 @@ public void jsonView() throws Exception {
124123
assertNull(b.getWithView2());
125124
assertNull(b.getWithoutView());
126125
})
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();
129145
}
130146

131147
@Test
132148
public void decodeEmptyBodyToMono() throws Exception {
133149
Flux<DataBuffer> source = Flux.empty();
134150
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
135151
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType,
136-
null, Collections.emptyMap());
152+
null, emptyMap());
137153

138154
StepVerifier.create(mono)
139155
.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();
184157
}
185158

186159
}

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@
1616

1717
package org.springframework.http.codec.json;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
2120

2221
import com.fasterxml.jackson.annotation.JsonTypeInfo;
2322
import com.fasterxml.jackson.annotation.JsonTypeName;
24-
import com.fasterxml.jackson.annotation.JsonView;
23+
import static java.util.Collections.*;
2524
import org.junit.Test;
25+
import static org.springframework.http.MediaType.*;
2626
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
27+
import static org.springframework.http.codec.json.Jackson2JsonEncoder.*;
28+
import static org.springframework.http.codec.json.JacksonViewBean.*;
2729
import reactor.core.publisher.Flux;
2830
import reactor.core.publisher.Mono;
2931
import reactor.test.StepVerifier;
3032

3133
import org.springframework.core.ResolvableType;
3234
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
3335
import org.springframework.core.io.buffer.DataBuffer;
34-
import org.springframework.http.MediaType;
3536
import org.springframework.http.codec.Pojo;
3637
import org.springframework.http.codec.ServerSentEvent;
3738

@@ -49,11 +50,11 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
4950
@Test
5051
public void canEncode() {
5152
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
52-
assertTrue(this.encoder.canEncode(pojoType, MediaType.APPLICATION_JSON));
53+
assertTrue(this.encoder.canEncode(pojoType, APPLICATION_JSON));
5354
assertTrue(this.encoder.canEncode(pojoType, null));
54-
assertFalse(this.encoder.canEncode(pojoType, MediaType.APPLICATION_XML));
55+
assertFalse(this.encoder.canEncode(pojoType, APPLICATION_XML));
5556
ResolvableType sseType = ResolvableType.forClass(ServerSentEvent.class);
56-
assertFalse(this.encoder.canEncode(sseType, MediaType.APPLICATION_JSON));
57+
assertFalse(this.encoder.canEncode(sseType, APPLICATION_JSON));
5758
}
5859

5960
@Test
@@ -64,24 +65,22 @@ public void encode() throws Exception {
6465
new Pojo("foofoofoo", "barbarbar")
6566
);
6667
ResolvableType type = ResolvableType.forClass(Pojo.class);
67-
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
68+
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, emptyMap());
6869

6970
StepVerifier.create(output)
7071
.consumeNextWith(stringConsumer("[{\"foo\":\"foo\",\"bar\":\"bar\"},{\"foo\":\"foofoo\",\"bar\":\"barbar\"},{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]"))
71-
.expectComplete()
72-
.verify();
72+
.verifyComplete();
7373
}
7474

7575
@Test
7676
public void encodeWithType() throws Exception {
7777
Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
7878
ResolvableType type = ResolvableType.forClass(ParentClass.class);
79-
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
79+
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, emptyMap());
8080

8181
StepVerifier.create(output)
8282
.consumeNextWith(stringConsumer("[{\"type\":\"foo\"},{\"type\":\"bar\"}]"))
83-
.expectComplete()
84-
.verify();
83+
.verifyComplete();
8584
}
8685

8786
@Test
@@ -92,31 +91,45 @@ public void encodeAsStream() throws Exception {
9291
new Pojo("foofoofoo", "barbarbar")
9392
);
9493
ResolvableType type = ResolvableType.forClass(Pojo.class);
95-
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, APPLICATION_STREAM_JSON, Collections.emptyMap());
94+
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, APPLICATION_STREAM_JSON, emptyMap());
9695

9796
StepVerifier.create(output)
9897
.consumeNextWith(stringConsumer("{\"foo\":\"foo\",\"bar\":\"bar\"}\n"))
9998
.consumeNextWith(stringConsumer("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n"))
10099
.consumeNextWith(stringConsumer("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n"))
101-
.expectComplete()
102-
.verify();
100+
.verifyComplete();
103101
}
104102

105103
@Test
106-
public void jsonView() throws Exception {
104+
public void fieldLevelJsonView() throws Exception {
107105
JacksonViewBean bean = new JacksonViewBean();
108106
bean.setWithView1("with");
109107
bean.setWithView2("with");
110108
bean.setWithoutView("without");
111109

112110
ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
113-
Map<String, Object> hints = Collections.singletonMap(Jackson2JsonEncoder.JSON_VIEW_HINT, MyJacksonView1.class);
111+
Map<String, Object> hints = singletonMap(JSON_VIEW_HINT, MyJacksonView1.class);
114112
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null, hints);
115113

116114
StepVerifier.create(output)
117115
.consumeNextWith(stringConsumer("{\"withView1\":\"with\"}"))
118-
.expectComplete()
119-
.verify();
116+
.verifyComplete();
117+
}
118+
119+
@Test
120+
public void classLevelJsonView() throws Exception {
121+
JacksonViewBean bean = new JacksonViewBean();
122+
bean.setWithView1("with");
123+
bean.setWithView2("with");
124+
bean.setWithoutView("without");
125+
126+
ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
127+
Map<String, Object> hints = singletonMap(JSON_VIEW_HINT, MyJacksonView3.class);
128+
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null, hints);
129+
130+
StepVerifier.create(output)
131+
.consumeNextWith(stringConsumer("{\"withoutView\":\"without\"}"))
132+
.verifyComplete();
120133
}
121134

122135

@@ -132,46 +145,4 @@ private static class Foo extends ParentClass {
132145
private static class Bar extends ParentClass {
133146
}
134147

135-
136-
private interface MyJacksonView1 {}
137-
138-
private interface MyJacksonView2 {}
139-
140-
141-
@SuppressWarnings("unused")
142-
private static class JacksonViewBean {
143-
144-
@JsonView(MyJacksonView1.class)
145-
private String withView1;
146-
147-
@JsonView(MyJacksonView2.class)
148-
private String withView2;
149-
150-
private String withoutView;
151-
152-
public String getWithView1() {
153-
return withView1;
154-
}
155-
156-
public void setWithView1(String withView1) {
157-
this.withView1 = withView1;
158-
}
159-
160-
public String getWithView2() {
161-
return withView2;
162-
}
163-
164-
public void setWithView2(String withView2) {
165-
this.withView2 = withView2;
166-
}
167-
168-
public String getWithoutView() {
169-
return withoutView;
170-
}
171-
172-
public void setWithoutView(String withoutView) {
173-
this.withoutView = withoutView;
174-
}
175-
}
176-
177148
}

0 commit comments

Comments
 (0)