15
15
*/
16
16
package org .springframework .data .mongodb .core ;
17
17
18
- import static org .hamcrest .Matchers .*;
19
- import static org .junit .Assert .*;
18
+ import static org .assertj .core .api .Assertions .*;
20
19
21
20
import lombok .EqualsAndHashCode ;
22
21
import lombok .ToString ;
23
22
24
- import java .net .UnknownHostException ;
25
23
import java .util .List ;
26
24
27
25
import org .junit .Before ;
@@ -50,7 +48,7 @@ public class QueryByExampleTests {
50
48
Person p1 , p2 , p3 ;
51
49
52
50
@ Before
53
- public void setUp () throws UnknownHostException {
51
+ public void setUp () {
54
52
55
53
operations = new MongoTemplate (new MongoClient (), "query-by-example" );
56
54
operations .remove (new Query (), Person .class );
@@ -82,8 +80,7 @@ public void findByExampleShouldWorkForSimpleProperty() {
82
80
Query query = new Query (new Criteria ().alike (Example .of (sample )));
83
81
List <Person > result = operations .find (query , Person .class );
84
82
85
- assertThat (result , hasSize (2 ));
86
- assertThat (result , hasItems (p1 , p3 ));
83
+ assertThat (result ).containsExactlyInAnyOrder (p1 , p3 );
87
84
}
88
85
89
86
@ Test // DATAMONGO-1245
@@ -96,8 +93,7 @@ public void findByExampleShouldWorkForMultipleProperties() {
96
93
Query query = new Query (new Criteria ().alike (Example .of (sample )));
97
94
List <Person > result = operations .find (query , Person .class );
98
95
99
- assertThat (result , hasSize (1 ));
100
- assertThat (result , hasItem (p3 ));
96
+ assertThat (result ).containsExactly (p3 );
101
97
}
102
98
103
99
@ Test // DATAMONGO-1245
@@ -112,8 +108,7 @@ public void findByExampleShouldWorkForIdProperty() {
112
108
Query query = new Query (new Criteria ().alike (Example .of (sample )));
113
109
List <Person > result = operations .find (query , Person .class );
114
110
115
- assertThat (result , hasSize (1 ));
116
- assertThat (result , hasItem (p4 ));
111
+ assertThat (result ).containsExactly (p4 );
117
112
}
118
113
119
114
@ Test // DATAMONGO-1245
@@ -126,7 +121,7 @@ public void findByExampleShouldReturnEmptyListIfNotMatching() {
126
121
Query query = new Query (new Criteria ().alike (Example .of (sample )));
127
122
List <Person > result = operations .find (query , Person .class );
128
123
129
- assertThat (result , is ( empty ()) );
124
+ assertThat (result ). isEmpty ( );
130
125
}
131
126
132
127
@ Test // DATAMONGO-1245
@@ -137,8 +132,7 @@ public void findByExampleShouldReturnEverythingWhenSampleIsEmpty() {
137
132
Query query = new Query (new Criteria ().alike (Example .of (sample )));
138
133
List <Person > result = operations .find (query , Person .class );
139
134
140
- assertThat (result , hasSize (3 ));
141
- assertThat (result , hasItems (p1 , p2 , p3 ));
135
+ assertThat (result ).containsExactlyInAnyOrder (p1 , p2 , p3 );
142
136
}
143
137
144
138
@ Test // DATAMONGO-1245
@@ -150,7 +144,7 @@ public void findByExampleWithCriteria() {
150
144
Query query = new Query (new Criteria ().alike (Example .of (sample )).and ("firstname" ).regex ("^ary*" ));
151
145
152
146
List <Person > result = operations .find (query , Person .class );
153
- assertThat (result . size (), is ( 1 ) );
147
+ assertThat (result ). hasSize ( 1 );
154
148
}
155
149
156
150
@ Test // DATAMONGO-1459
@@ -163,8 +157,7 @@ public void findsExampleUsingAnyMatch() {
163
157
Query query = Query .query (Criteria .byExample (Example .of (probe , ExampleMatcher .matchingAny ())));
164
158
List <Person > result = operations .find (query , Person .class );
165
159
166
- assertThat (result , hasSize (2 ));
167
- assertThat (result , hasItems (p1 , p2 ));
160
+ assertThat (result ).containsExactlyInAnyOrder (p1 , p2 );
168
161
}
169
162
170
163
@ Test // DATAMONGO-1768
@@ -176,7 +169,7 @@ public void typedExampleMatchesNothingIfTypesDoNotMatch() {
176
169
Query query = new Query (new Criteria ().alike (Example .of (probe )));
177
170
List <Person > result = operations .find (query , Person .class );
178
171
179
- assertThat (result , hasSize ( 0 ) );
172
+ assertThat (result ). isEmpty ( );
180
173
}
181
174
182
175
@ Test // DATAMONGO-1768
@@ -189,8 +182,7 @@ public void exampleIgnoringClassTypeKeyMatchesCorrectly() {
189
182
new Criteria ().alike (Example .of (probe , ExampleMatcher .matching ().withIgnorePaths ("_class" ))));
190
183
List <Person > result = operations .find (query , Person .class );
191
184
192
- assertThat (result , hasSize (2 ));
193
- assertThat (result , hasItems (p1 , p3 ));
185
+ assertThat (result ).containsExactlyInAnyOrder (p1 , p3 );
194
186
}
195
187
196
188
@ Test // DATAMONGO-1768
@@ -202,8 +194,28 @@ public void untypedExampleMatchesCorrectly() {
202
194
Query query = new Query (new Criteria ().alike (Example .of (probe , UntypedExampleMatcher .matching ())));
203
195
List <Person > result = operations .find (query , Person .class );
204
196
205
- assertThat (result , hasSize (2 ));
206
- assertThat (result , hasItems (p1 , p3 ));
197
+ assertThat (result ).containsExactlyInAnyOrder (p1 , p3 );
198
+ }
199
+
200
+ @ Test // DATAMONGO-2314
201
+ public void alikeShouldWorkOnNestedProperties () {
202
+
203
+ PersonWrapper source1 = new PersonWrapper ();
204
+ source1 .id = "with-child-doc-1" ;
205
+ source1 .child = p1 ;
206
+
207
+ PersonWrapper source2 = new PersonWrapper ();
208
+ source2 .id = "with-child-doc-2" ;
209
+ source2 .child = p2 ;
210
+
211
+ operations .save (source1 );
212
+ operations .save (source2 );
213
+
214
+ Query query = new Query (
215
+ new Criteria ("child" ).alike (Example .of (p1 , ExampleMatcher .matching ().withIgnorePaths ("_class" ))));
216
+ List <PersonWrapper > result = operations .find (query , PersonWrapper .class );
217
+
218
+ assertThat (result ).containsExactly (source1 );
207
219
}
208
220
209
221
@ Document ("dramatis-personae" )
@@ -223,4 +235,13 @@ static class NotAPersonButStillMatchingFields {
223
235
String firstname , middlename ;
224
236
@ Field ("last_name" ) String lastname ;
225
237
}
238
+
239
+ @ Document ("dramatis-personae" )
240
+ @ EqualsAndHashCode
241
+ @ ToString
242
+ static class PersonWrapper {
243
+
244
+ @ Id String id ;
245
+ Person child ;
246
+ }
226
247
}
0 commit comments