|
43 | 43 | import org.springframework.data.annotation.Id;
|
44 | 44 | import org.springframework.data.annotation.Version;
|
45 | 45 | import org.springframework.data.domain.Sort;
|
| 46 | +import org.springframework.data.geo.Point; |
46 | 47 | import org.springframework.data.mongodb.MongoDbFactory;
|
| 48 | +import org.springframework.data.mongodb.core.aggregation.Aggregation; |
47 | 49 | import org.springframework.data.mongodb.core.convert.CustomConversions;
|
48 | 50 | import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
|
49 | 51 | import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
|
52 | 54 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
53 | 55 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
54 | 56 | import org.springframework.data.mongodb.core.query.Criteria;
|
| 57 | +import org.springframework.data.mongodb.core.query.NearQuery; |
55 | 58 | import org.springframework.data.mongodb.core.query.Query;
|
56 | 59 | import org.springframework.data.mongodb.core.query.Update;
|
57 | 60 | import org.springframework.test.util.ReflectionTestUtils;
|
58 | 61 |
|
59 | 62 | import com.mongodb.BasicDBObject;
|
60 | 63 | import com.mongodb.BasicDBObjectBuilder;
|
| 64 | +import com.mongodb.CommandResult; |
61 | 65 | import com.mongodb.DB;
|
62 | 66 | import com.mongodb.DBCollection;
|
63 | 67 | import com.mongodb.DBCursor;
|
64 | 68 | import com.mongodb.DBObject;
|
65 | 69 | import com.mongodb.Mongo;
|
66 | 70 | import com.mongodb.MongoException;
|
| 71 | +import com.mongodb.ReadPreference; |
67 | 72 |
|
68 | 73 | /**
|
69 | 74 | * Unit tests for {@link MongoTemplate}.
|
@@ -352,6 +357,74 @@ public void processDocument(DBObject dbObject) throws MongoException, DataAccess
|
352 | 357 | assertThat(captor.getValue(), equalTo(new BasicDBObjectBuilder().add("foo", 1).get()));
|
353 | 358 | }
|
354 | 359 |
|
| 360 | + /** |
| 361 | + * @see DATAMONGO-1166 |
| 362 | + */ |
| 363 | + @Test |
| 364 | + public void aggregateShouldHonorReadPreferenceWhenSet() { |
| 365 | + |
| 366 | + CommandResult result = mock(CommandResult.class); |
| 367 | + |
| 368 | + when(result.get("result")).thenReturn(Collections.emptySet()); |
| 369 | + when(db.command(Mockito.any(DBObject.class), Mockito.any(ReadPreference.class))).thenReturn(result); |
| 370 | + when(db.command(Mockito.any(DBObject.class))).thenReturn(result); |
| 371 | + template.setReadPreference(ReadPreference.secondary()); |
| 372 | + |
| 373 | + template.aggregate(Aggregation.newAggregation(Aggregation.unwind("foo")), "collection-1", Wrapper.class); |
| 374 | + |
| 375 | + verify(this.db, times(1)).command(Mockito.any(DBObject.class), eq(ReadPreference.secondary())); |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * @see DATAMONGO-1166 |
| 380 | + */ |
| 381 | + @Test |
| 382 | + public void aggregateShouldIgnoreReadPreferenceWhenNotSet() { |
| 383 | + |
| 384 | + CommandResult result = mock(CommandResult.class); |
| 385 | + |
| 386 | + when(result.get("result")).thenReturn(Collections.emptySet()); |
| 387 | + when(db.command(Mockito.any(DBObject.class), Mockito.any(ReadPreference.class))).thenReturn(result); |
| 388 | + when(db.command(Mockito.any(DBObject.class))).thenReturn(result); |
| 389 | + |
| 390 | + template.aggregate(Aggregation.newAggregation(Aggregation.unwind("foo")), "collection-1", Wrapper.class); |
| 391 | + |
| 392 | + verify(this.db, times(1)).command(Mockito.any(DBObject.class)); |
| 393 | + } |
| 394 | + |
| 395 | + /** |
| 396 | + * @see DATAMONGO-1166 |
| 397 | + */ |
| 398 | + @Test |
| 399 | + public void geoNearShouldHonorReadPreferenceWhenSet() { |
| 400 | + |
| 401 | + when(db.command(Mockito.any(DBObject.class), Mockito.any(ReadPreference.class))) |
| 402 | + .thenReturn(mock(CommandResult.class)); |
| 403 | + when(db.command(Mockito.any(DBObject.class))).thenReturn(mock(CommandResult.class)); |
| 404 | + template.setReadPreference(ReadPreference.secondary()); |
| 405 | + |
| 406 | + NearQuery query = NearQuery.near(new Point(1, 1)); |
| 407 | + template.geoNear(query, Wrapper.class); |
| 408 | + |
| 409 | + verify(this.db, times(1)).command(Mockito.any(DBObject.class), eq(ReadPreference.secondary())); |
| 410 | + } |
| 411 | + |
| 412 | + /** |
| 413 | + * @see DATAMONGO-1166 |
| 414 | + */ |
| 415 | + @Test |
| 416 | + public void geoNearShouldIgnoreReadPreferenceWhenNotSet() { |
| 417 | + |
| 418 | + when(db.command(Mockito.any(DBObject.class), Mockito.any(ReadPreference.class))) |
| 419 | + .thenReturn(mock(CommandResult.class)); |
| 420 | + when(db.command(Mockito.any(DBObject.class))).thenReturn(mock(CommandResult.class)); |
| 421 | + |
| 422 | + NearQuery query = NearQuery.near(new Point(1, 1)); |
| 423 | + template.geoNear(query, Wrapper.class); |
| 424 | + |
| 425 | + verify(this.db, times(1)).command(Mockito.any(DBObject.class)); |
| 426 | + } |
| 427 | + |
355 | 428 | class AutogenerateableId {
|
356 | 429 |
|
357 | 430 | @Id BigInteger id;
|
|
0 commit comments