|
16 | 16 | package org.springframework.data.mongodb.core.convert;
|
17 | 17 |
|
18 | 18 | import static org.hamcrest.CoreMatchers.*;
|
| 19 | +import static org.hamcrest.Matchers.equalTo; |
19 | 20 | import static org.hamcrest.collection.IsMapContaining.*;
|
20 | 21 | import static org.junit.Assert.*;
|
21 | 22 | import static org.mockito.Mockito.*;
|
|
42 | 43 | import org.springframework.core.convert.converter.Converter;
|
43 | 44 | import org.springframework.data.annotation.Id;
|
44 | 45 | import org.springframework.data.convert.WritingConverter;
|
| 46 | +import org.springframework.data.domain.Sort; |
| 47 | +import org.springframework.data.domain.Sort.Direction; |
| 48 | +import org.springframework.data.domain.Sort.Order; |
45 | 49 | import org.springframework.data.mapping.model.MappingException;
|
46 | 50 | import org.springframework.data.mongodb.MongoDbFactory;
|
47 | 51 | import org.springframework.data.mongodb.core.DBObjectTestUtils;
|
|
68 | 72 | * @author Christoph Strobl
|
69 | 73 | * @author Thomas Darimont
|
70 | 74 | * @author Mark Paluch
|
| 75 | + * @author Pavel Vodrazka |
71 | 76 | */
|
72 | 77 | @RunWith(MockitoJUnitRunner.class)
|
73 | 78 | public class UpdateMapperUnitTests {
|
@@ -416,6 +421,68 @@ public void updatePushEachWithSliceShouldRenderWhenUsingMultiplePushCorrectly()
|
416 | 421 | assertThat(key2.containsField("$each"), is(true));
|
417 | 422 | }
|
418 | 423 |
|
| 424 | + /** |
| 425 | + * @see DATAMONGO-1141 |
| 426 | + */ |
| 427 | + @Test |
| 428 | + public void updatePushEachWithValueSortShouldRenderCorrectly() { |
| 429 | + |
| 430 | + Update update = new Update().push("scores").sort(Direction.DESC).each(42, 23, 68); |
| 431 | + |
| 432 | + DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class)); |
| 433 | + |
| 434 | + DBObject push = getAsDBObject(mappedObject, "$push"); |
| 435 | + DBObject key = getAsDBObject(push, "scores"); |
| 436 | + |
| 437 | + assertThat(key.containsField("$sort"), is(true)); |
| 438 | + assertThat((Integer) key.get("$sort"), is(-1)); |
| 439 | + assertThat(key.containsField("$each"), is(true)); |
| 440 | + } |
| 441 | + |
| 442 | + /** |
| 443 | + * @see DATAMONGO-1141 |
| 444 | + */ |
| 445 | + @Test |
| 446 | + public void updatePushEachWithDocumentSortShouldRenderCorrectly() { |
| 447 | + |
| 448 | + Update update = new Update().push("names").sort(new Sort(new Order(Direction.ASC, "last"), new Order(Direction.ASC, "first"))) |
| 449 | + .each(Collections.emptyList()); |
| 450 | + |
| 451 | + DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class)); |
| 452 | + |
| 453 | + DBObject push = getAsDBObject(mappedObject, "$push"); |
| 454 | + DBObject key = getAsDBObject(push, "names"); |
| 455 | + |
| 456 | + assertThat(key.containsField("$sort"), is(true)); |
| 457 | + assertThat((DBObject) key.get("$sort"), equalTo(new BasicDBObjectBuilder().add("last", 1).add("first", 1).get())); |
| 458 | + assertThat(key.containsField("$each"), is(true)); |
| 459 | + } |
| 460 | + |
| 461 | + /** |
| 462 | + * @see DATAMONGO-1141 |
| 463 | + */ |
| 464 | + @Test |
| 465 | + public void updatePushEachWithSortShouldRenderCorrectlyWhenUsingMultiplePush() { |
| 466 | + |
| 467 | + Update update = new Update().push("authors").sort(Direction.ASC).each("Harry") |
| 468 | + .push("chapters").sort(new Sort(Direction.ASC, "order")).each(Collections.emptyList()); |
| 469 | + |
| 470 | + DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class)); |
| 471 | + |
| 472 | + DBObject push = getAsDBObject(mappedObject, "$push"); |
| 473 | + DBObject key1 = getAsDBObject(push, "authors"); |
| 474 | + |
| 475 | + assertThat(key1.containsField("$sort"), is(true)); |
| 476 | + assertThat((Integer) key1.get("$sort"), is(1)); |
| 477 | + assertThat(key1.containsField("$each"), is(true)); |
| 478 | + |
| 479 | + DBObject key2 = getAsDBObject(push, "chapters"); |
| 480 | + |
| 481 | + assertThat(key2.containsField("$sort"), is(true)); |
| 482 | + assertThat((DBObject) key2.get("$sort"), equalTo(new BasicDBObjectBuilder().add("order", 1).get())); |
| 483 | + assertThat(key2.containsField("$each"), is(true)); |
| 484 | + } |
| 485 | + |
419 | 486 | /**
|
420 | 487 | * @see DATAMONGO-410
|
421 | 488 | */
|
|
0 commit comments