|
27 | 27 | import java.util.HashSet;
|
28 | 28 | import java.util.List;
|
29 | 29 | import java.util.Optional;
|
| 30 | +import java.util.Set; |
30 | 31 | import java.util.UUID;
|
31 | 32 | import java.util.regex.Pattern;
|
32 | 33 | import java.util.stream.Collectors;
|
| 34 | +import java.util.stream.IntStream; |
33 | 35 | import java.util.stream.Stream;
|
34 | 36 |
|
35 | 37 | import org.hamcrest.Matchers;
|
36 | 38 | import org.junit.Before;
|
| 39 | +import org.junit.Ignore; |
37 | 40 | import org.junit.Rule;
|
38 | 41 | import org.junit.Test;
|
39 | 42 | import org.junit.rules.ExpectedException;
|
|
60 | 63 | import org.springframework.data.mongodb.core.MongoOperations;
|
61 | 64 | import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
|
62 | 65 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
| 66 | +import org.springframework.data.mongodb.core.query.Query; |
63 | 67 | import org.springframework.data.mongodb.repository.Person.Sex;
|
64 | 68 | import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
|
65 | 69 | import org.springframework.data.querydsl.QSort;
|
@@ -1231,4 +1235,71 @@ public void findByRegexWithPatternAndOptions() {
|
1231 | 1235 | assertThat(repository.findByFirstnameRegex(Pattern.compile(fn))).hasSize(0);
|
1232 | 1236 | assertThat(repository.findByFirstnameRegex(Pattern.compile(fn, Pattern.CASE_INSENSITIVE))).hasSize(1);
|
1233 | 1237 | }
|
| 1238 | + |
| 1239 | + @Test // DATAMONGO-2149 |
| 1240 | + public void annotatedQueryShouldAllowSliceInFieldsProjectionWithDbRef() { |
| 1241 | + |
| 1242 | + operations.remove(new Query(), User.class); |
| 1243 | + |
| 1244 | + List<User> users = IntStream.range(0, 10).mapToObj(it -> { |
| 1245 | + |
| 1246 | + User user = new User(); |
| 1247 | + user.id = "id" + it; |
| 1248 | + user.username = "user" + it; |
| 1249 | + |
| 1250 | + return user; |
| 1251 | + }).collect(Collectors.toList()); |
| 1252 | + |
| 1253 | + users.forEach(operations::save); |
| 1254 | + |
| 1255 | + alicia.fans = new ArrayList<>(users); |
| 1256 | + operations.save(alicia); |
| 1257 | + |
| 1258 | + Person target = repository.findWithSliceInProjection(alicia.getId(), 0, 5); |
| 1259 | + assertThat(target.getFans().size()).isEqualTo(5); |
| 1260 | + } |
| 1261 | + |
| 1262 | + @Test // DATAMONGO-2149 |
| 1263 | + public void annotatedQueryShouldAllowPositionalParameterInFieldsProjection() { |
| 1264 | + |
| 1265 | + Set<Address> addressList = IntStream.range(0, 10).mapToObj(it -> new Address("street-" + it, "zip", "lnz")) |
| 1266 | + .collect(Collectors.toSet()); |
| 1267 | + |
| 1268 | + alicia.setShippingAddresses(addressList); |
| 1269 | + operations.save(alicia); |
| 1270 | + |
| 1271 | + Person target = repository.findWithArrayPositionInProjection(1); |
| 1272 | + |
| 1273 | + assertThat(target).isNotNull(); |
| 1274 | + assertThat(target.getShippingAddresses()).hasSize(1); |
| 1275 | + } |
| 1276 | + |
| 1277 | + @Test // DATAMONGO-2149 |
| 1278 | + @Ignore("This one fails due to Json parse exception within MongoDB") |
| 1279 | + public void annotatedQueryShouldAllowPositionalParameterInFieldsProjectionWithDbRef() { |
| 1280 | + |
| 1281 | + // the following needs to be added to PersonRepository. |
| 1282 | + |
| 1283 | + // @Query(value = "{ 'fans' : { '$elemMatch' : { '$ref' : 'user' } } }", fields = "{ 'fans.$': ?0 }") |
| 1284 | + // Person findWithArrayPositionInProjectionWithDbRef(int position); |
| 1285 | + |
| 1286 | + List<User> userList = IntStream.range(0, 10).mapToObj(it -> { |
| 1287 | + |
| 1288 | + User user = new User(); |
| 1289 | + user.id = "" + it; |
| 1290 | + user.username = "user" + it; |
| 1291 | + |
| 1292 | + return user; |
| 1293 | + }).collect(Collectors.toList()); |
| 1294 | + |
| 1295 | + userList.forEach(operations::save); |
| 1296 | + |
| 1297 | + alicia.setFans(userList); |
| 1298 | + operations.save(alicia); |
| 1299 | + |
| 1300 | + // Person target = repository.findWithArrayPositionInProjectionWithDbRef(1); |
| 1301 | + // |
| 1302 | + // assertThat(target).isNotNull(); |
| 1303 | + // assertThat(target.getShippingAddresses()).hasSize(1); |
| 1304 | + } |
1234 | 1305 | }
|
0 commit comments