41
41
import org .springframework .core .convert .converter .Converter ;
42
42
import org .springframework .data .annotation .Id ;
43
43
import org .springframework .data .convert .WritingConverter ;
44
+ import org .springframework .data .domain .Sort ;
45
+ import org .springframework .data .domain .Sort .Direction ;
46
+ import org .springframework .data .domain .Sort .Order ;
44
47
import org .springframework .data .mapping .model .MappingException ;
45
48
import org .springframework .data .mongodb .MongoDbFactory ;
46
49
import org .springframework .data .mongodb .core .DocumentTestUtils ;
60
63
* @author Christoph Strobl
61
64
* @author Thomas Darimont
62
65
* @author Mark Paluch
66
+ * @author Pavel Vodrazka
63
67
*/
64
68
@ RunWith (MockitoJUnitRunner .class )
65
69
public class UpdateMapperUnitTests {
@@ -398,13 +402,76 @@ public void updatePushEachWithSliceShouldRenderWhenUsingMultiplePushCorrectly()
398
402
Document key = getAsDocument (push , "key" );
399
403
400
404
assertThat (key .containsKey ("$slice" ), is (true ));
401
- assertThat (key .get ("$slice" ), is (5 ));
405
+ assertThat (( Integer ) key .get ("$slice" ), is (5 ));
402
406
assertThat (key .containsKey ("$each" ), is (true ));
403
407
404
408
Document key2 = getAsDocument (push , "key-2" );
405
409
406
410
assertThat (key2 .containsKey ("$slice" ), is (true ));
407
- assertThat (key2 .get ("$slice" ), is (-2 ));
411
+ assertThat ((Integer ) key2 .get ("$slice" ), is (-2 ));
412
+ assertThat (key2 .containsKey ("$each" ), is (true ));
413
+ }
414
+
415
+ /**
416
+ * @see DATAMONGO-1141
417
+ */
418
+ @ Test
419
+ public void updatePushEachWithValueSortShouldRenderCorrectly () {
420
+
421
+ Update update = new Update ().push ("scores" ).sort (Direction .DESC ).each (42 , 23 , 68 );
422
+
423
+ Document mappedObject = mapper .getMappedObject (update .getUpdateObject (), context .getPersistentEntity (Object .class ));
424
+
425
+ Document push = getAsDocument (mappedObject , "$push" );
426
+ Document key = getAsDocument (push , "scores" );
427
+
428
+ assertThat (key .containsKey ("$sort" ), is (true ));
429
+ assertThat ((Integer ) key .get ("$sort" ), is (-1 ));
430
+ assertThat (key .containsKey ("$each" ), is (true ));
431
+ }
432
+
433
+ /**
434
+ * @see DATAMONGO-1141
435
+ */
436
+ @ Test
437
+ public void updatePushEachWithDocumentSortShouldRenderCorrectly () {
438
+
439
+ Update update = new Update ().push ("names" )
440
+ .sort (new Sort (new Order (Direction .ASC , "last" ), new Order (Direction .ASC , "first" )))
441
+ .each (Collections .emptyList ());
442
+
443
+ Document mappedObject = mapper .getMappedObject (update .getUpdateObject (), context .getPersistentEntity (Object .class ));
444
+
445
+ Document push = getAsDocument (mappedObject , "$push" );
446
+ Document key = getAsDocument (push , "names" );
447
+
448
+ assertThat (key .containsKey ("$sort" ), is (true ));
449
+ assertThat ((Document ) key .get ("$sort" ), equalTo (new Document ("last" , 1 ).append ("first" , 1 )));
450
+ assertThat (key .containsKey ("$each" ), is (true ));
451
+ }
452
+
453
+ /**
454
+ * @see DATAMONGO-1141
455
+ */
456
+ @ Test
457
+ public void updatePushEachWithSortShouldRenderCorrectlyWhenUsingMultiplePush () {
458
+
459
+ Update update = new Update ().push ("authors" ).sort (Direction .ASC ).each ("Harry" ).push ("chapters" )
460
+ .sort (new Sort (Direction .ASC , "order" )).each (Collections .emptyList ());
461
+
462
+ Document mappedObject = mapper .getMappedObject (update .getUpdateObject (), context .getPersistentEntity (Object .class ));
463
+
464
+ Document push = getAsDocument (mappedObject , "$push" );
465
+ Document key1 = getAsDocument (push , "authors" );
466
+
467
+ assertThat (key1 .containsKey ("$sort" ), is (true ));
468
+ assertThat ((Integer ) key1 .get ("$sort" ), is (1 ));
469
+ assertThat (key1 .containsKey ("$each" ), is (true ));
470
+
471
+ Document key2 = getAsDocument (push , "chapters" );
472
+
473
+ assertThat (key2 .containsKey ("$sort" ), is (true ));
474
+ assertThat ((Document ) key2 .get ("$sort" ), equalTo (new Document ("order" , 1 )));
408
475
assertThat (key2 .containsKey ("$each" ), is (true ));
409
476
}
410
477
@@ -790,8 +857,7 @@ public void mappingShouldRetrainTypeInformationWhenValueTypeOfMapDoesNotMatchIts
790
857
@ Test
791
858
public void mappingShouldNotContainTypeInformationWhenValueTypeOfMapMatchesDeclaration () {
792
859
793
- Map <Object , NestedDocument > map = Collections .singletonMap ("jasnah" ,
794
- new NestedDocument ("kholin" ));
860
+ Map <Object , NestedDocument > map = Collections .singletonMap ("jasnah" , new NestedDocument ("kholin" ));
795
861
796
862
Update update = new Update ().set ("concreteMap" , map );
797
863
Document mappedUpdate = mapper .getMappedObject (update .getUpdateObject (),
@@ -808,8 +874,8 @@ public void mappingShouldNotContainTypeInformationWhenValueTypeOfMapMatchesDecla
808
874
@ SuppressWarnings ("unchecked" )
809
875
public void mapsUpdateWithBothReadingAndWritingConverterRegistered () {
810
876
811
- CustomConversions conversions = new CustomConversions (
812
- Arrays . asList ( ClassWithEnum .AllocationToStringConverter .INSTANCE , ClassWithEnum .StringToAllocationConverter .INSTANCE ));
877
+ CustomConversions conversions = new CustomConversions (Arrays . asList (
878
+ ClassWithEnum .AllocationToStringConverter .INSTANCE , ClassWithEnum .StringToAllocationConverter .INSTANCE ));
813
879
814
880
MongoMappingContext mappingContext = new MongoMappingContext ();
815
881
mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
@@ -953,8 +1019,8 @@ public void mapsMaxCorrectly() {
953
1019
@ SuppressWarnings ("unchecked" )
954
1020
public void mappingShouldConsiderCustomConvertersForEnumMapKeys () {
955
1021
956
- CustomConversions conversions = new CustomConversions (
957
- Arrays . asList ( ClassWithEnum .AllocationToStringConverter .INSTANCE , ClassWithEnum .StringToAllocationConverter .INSTANCE ));
1022
+ CustomConversions conversions = new CustomConversions (Arrays . asList (
1023
+ ClassWithEnum .AllocationToStringConverter .INSTANCE , ClassWithEnum .StringToAllocationConverter .INSTANCE ));
958
1024
959
1025
MongoMappingContext mappingContext = new MongoMappingContext ();
960
1026
mappingContext .setSimpleTypeHolder (conversions .getSimpleTypeHolder ());
0 commit comments