4141import org .springframework .data .domain .OffsetScrollPosition ;
4242import org .springframework .data .domain .Page ;
4343import org .springframework .data .domain .PageRequest ;
44+ import org .springframework .data .domain .Range ;
4445import org .springframework .data .domain .ScrollPosition ;
4546import org .springframework .data .domain .Slice ;
4647import org .springframework .data .domain .Sort ;
4748import org .springframework .data .domain .Window ;
4849import org .springframework .data .geo .Box ;
4950import org .springframework .data .geo .Circle ;
5051import org .springframework .data .geo .Distance ;
52+ import org .springframework .data .geo .GeoPage ;
53+ import org .springframework .data .geo .GeoResult ;
5154import org .springframework .data .geo .GeoResults ;
5255import org .springframework .data .geo .Metrics ;
5356import org .springframework .data .geo .Point ;
@@ -96,7 +99,7 @@ MongoOperations mongoOperations() {
9699 @ BeforeAll
97100 static void beforeAll () {
98101 client .getDatabase (DB_NAME ).getCollection ("user" ).createIndex (new Document ("location.coordinates" , "2d" ),
99- new IndexOptions ());
102+ new IndexOptions ());
100103 }
101104
102105 @ BeforeEach
@@ -613,21 +616,21 @@ void testAggregationWithCollation() {
613616 void testNear () {
614617
615618 List <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99171 , 40.738868 ));
616- assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" );
619+ assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
617620 }
618621
619622 @ Test
620623 void testNearWithGeoJson () {
621624
622625 List <User > users = fragment .findByLocationCoordinatesNear (new GeoJsonPoint (-73.99171 , 40.738868 ));
623- assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" );
626+ assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
624627 }
625628
626629 @ Test
627630 void testGeoWithinCircle () {
628631
629632 List <User > users = fragment .findByLocationCoordinatesWithin (new Circle (-78.99171 , 45.738868 , 170 ));
630- assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" );
633+ assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
631634 }
632635
633636 @ Test
@@ -636,7 +639,7 @@ void testWithinBox() {
636639 Box box = new Box (new Point (-78.99171 , 35.738868 ), new Point (-68.99171 , 45.738868 ));
637640
638641 List <User > result = fragment .findByLocationCoordinatesWithin (box );
639- assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" );
642+ assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
640643 }
641644
642645 @ Test
@@ -648,18 +651,51 @@ void findsPeopleByLocationWithinPolygon() {
648651 Point fourth = new Point (-68.99171 , 35.738868 );
649652
650653 List <User > result = fragment .findByLocationCoordinatesWithin (new Polygon (first , second , third , fourth ));
651- assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" );
654+ assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
652655 }
653656
654-
655657 @ Test
656658 void testNearWithGeoResult () {
657659
658- GeoResults <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ), Distance .of (2000 , Metrics .KILOMETERS ));
659- System .out .println ("users: " + users );
660- assertThat (users ).isNotEmpty ();
660+ GeoResults <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
661+ Distance .of (5 , Metrics .KILOMETERS ));
662+ assertThat (users ).extracting (GeoResult ::getContent ).extracting (User ::getUsername ).containsExactly ("leia" );
663+ }
664+
665+ @ Test
666+ void testNearReturningListOfGeoResult () {
667+
668+ List <GeoResult <User >> users = fragment .findUserAsListByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
669+ Distance .of (5 , Metrics .KILOMETERS ));
670+ assertThat (users ).extracting (GeoResult ::getContent ).extracting (User ::getUsername ).containsExactly ("leia" );
671+ }
672+
673+ @ Test
674+ void testNearWithRange () {
675+
676+ Range <Distance > range = Distance .between (Distance .of (5 , Metrics .KILOMETERS ), Distance .of (2000 , Metrics .KILOMETERS ));
677+ GeoResults <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ), range );
678+
679+ assertThat (users ).extracting (GeoResult ::getContent ).extracting (User ::getUsername ).containsExactly ("vader" );
661680 }
662681
682+ @ Test
683+ void testNearReturningGeoPage () {
684+
685+ // TODO: still need to create the count and extract the total elements
686+ GeoPage <User > page1 = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
687+ Distance .of (2000 , Metrics .KILOMETERS ), PageRequest .of (0 , 1 ));
688+
689+ assertThat (page1 .hasNext ()).isTrue ();
690+
691+ GeoPage <User > page2 = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
692+ Distance .of (2000 , Metrics .KILOMETERS ), page1 .nextPageable ());
693+ assertThat (page2 .hasNext ()).isFalse ();
694+ }
695+
696+ /**
697+ * GeoResults<Person> results = repository.findPersonByLocationNear(new Point(-73.99, 40.73), range);
698+ */
663699 private static void initUsers () {
664700
665701 Document luke = Document .parse ("""
@@ -753,6 +789,12 @@ private static void initUsers() {
753789 "username": "vader",
754790 "first_name": "Anakin",
755791 "last_name": "Skywalker",
792+ "location" : {
793+ "planet" : "Death Star",
794+ "coordinates" : {
795+ "x" : -73.9, "y" : 40.7
796+ }
797+ },
756798 "visits" : 50,
757799 "posts": [
758800 {
0 commit comments