22
22
import lombok .NoArgsConstructor ;
23
23
24
24
import java .io .ByteArrayInputStream ;
25
+ import java .nio .charset .StandardCharsets ;
25
26
import java .util .List ;
26
27
import java .util .Set ;
27
28
38
39
* Unit tests for {@link JsonProjectingMethodInterceptorFactory}.
39
40
*
40
41
* @author Oliver Gierke
42
+ * @author Mark Paluch
41
43
* @since 1.13
42
44
* @soundtrack Richard Spaven - Assemble (Whole Other*)
43
45
*/
@@ -51,7 +53,8 @@ void setUp() {
51
53
52
54
String json = "{\" firstname\" : \" Dave\" , " //
53
55
+ "\" address\" : { \" zipCode\" : \" 01097\" , \" city\" : \" Dresden\" }," //
54
- + "\" addresses\" : [ { \" zipCode\" : \" 01097\" , \" city\" : \" Dresden\" }]" + " }" ;
56
+ + "\" addresses\" : [ { \" zipCode\" : \" 01097\" , \" city\" : \" Dresden\" }, { \" zipCode\" : \" 69469\" , \" city\" : \" Weinheim\" }]"
57
+ + " }" ;
55
58
56
59
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory ();
57
60
@@ -97,6 +100,13 @@ void accessPropertyOnNestedProjection() {
97
100
assertThat (customer .getAddressProjections ().get (0 ).getZipCode ()).isEqualTo ("01097" );
98
101
}
99
102
103
+ @ Test // gh-2270
104
+ void nestedProjectionCollectionShouldContainMultipleElements () {
105
+ assertThat (customer .getAddressProjections ()).hasSize (2 );
106
+ assertThat (customer .getAddressProjections ().get (0 ).getZipCode ()).isEqualTo ("01097" );
107
+ assertThat (customer .getAddressProjections ().get (1 ).getZipCode ()).isEqualTo ("69469" );
108
+ }
109
+
100
110
@ Test // DATCMNS-885
101
111
void accessPropertyThatUsesJsonPathProjectionInTurn () {
102
112
assertThat (customer .getAnotherAddressProjection ().getZipCodeButNotCity ()).isEqualTo ("01097" );
@@ -107,7 +117,7 @@ void accessCollectionPropertyThatUsesJsonPathProjectionInTurn() {
107
117
108
118
List <AnotherAddressProjection > projections = customer .getAnotherAddressProjections ();
109
119
110
- assertThat (projections ).hasSize (1 );
120
+ assertThat (projections ).hasSize (2 );
111
121
assertThat (projections .get (0 ).getZipCodeButNotCity ()).isEqualTo ("01097" );
112
122
}
113
123
@@ -133,7 +143,7 @@ void accessNestedPropertyButStayOnRootLevel() {
133
143
void accessNestedFields () {
134
144
135
145
assertThat (customer .getNestedCity ()).isEqualTo ("Dresden" );
136
- assertThat (customer .getNestedCities ()).hasSize (2 );
146
+ assertThat (customer .getNestedCities ()).hasSize (3 );
137
147
}
138
148
139
149
@ Test // DATACMNS-1144
@@ -146,6 +156,18 @@ void triesMultipleDeclaredPathsIfNotAvailable() {
146
156
assertThat (customer .getName ().getSomeName ()).isEqualTo (customer .getName ().getFirstname ());
147
157
}
148
158
159
+ @ Test // gh-2270
160
+ void shouldProjectOnArray () {
161
+
162
+ String json = "[ { \" creationDate\" : 1610111331413, \" changeDate\" : 1610111332160, \" person\" : { \" caption\" : \" Test2 TEST2\" , \" firstName\" : \" Test2\" , \" lastName\" : \" Test2\" } }, "
163
+ + "{ \" creationDate\" : 1609775450502, \" changeDate\" : 1609775451333, \" person\" : { \" caption\" : \" Test TEST\" , \" firstName\" : \" Test\" , \" lastName\" : \" Test\" } }]" ;
164
+
165
+ UserPayload projection = projectionFactory .createProjection (UserPayload .class ,
166
+ new ByteArrayInputStream (json .getBytes (StandardCharsets .UTF_8 )));
167
+
168
+ assertThat (projection .users ()).hasSize (2 );
169
+ }
170
+
149
171
interface Customer {
150
172
151
173
String getFirstname ();
@@ -218,4 +240,18 @@ interface AnotherAddressProjection {
218
240
static class Address {
219
241
private String zipCode , city ;
220
242
}
243
+
244
+ @ ProjectedPayload
245
+ interface UserPayload {
246
+
247
+ @ JsonPath ("$..person" )
248
+ List <Users > users ();
249
+
250
+ interface Users {
251
+
252
+ public String getFirstName ();
253
+
254
+ public String getLastName ();
255
+ }
256
+ }
221
257
}
0 commit comments