Skip to content

Commit eeb06cd

Browse files
Update issue reference & add missing test for geojson
1 parent 0fb84ba commit eeb06cd

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

spring-data-mongodb/src/test/java/example/aot/UserRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.geo.Point;
4343
import org.springframework.data.geo.Polygon;
4444
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
45+
import org.springframework.data.mongodb.core.geo.GeoJson;
4546
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
4647
import org.springframework.data.mongodb.core.geo.Sphere;
4748
import org.springframework.data.mongodb.repository.Aggregation;
@@ -127,6 +128,8 @@ public interface UserRepository extends CrudRepository<User, String> {
127128

128129
List<User> findByLocationCoordinatesWithin(GeoJsonPolygon polygon);
129130

131+
List<User> findUserByLocationCoordinatesWithin(GeoJson<?> geoJson);
132+
130133
GeoResults<User> findByLocationCoordinatesNear(Point point, Distance maxDistance);
131134

132135
GeoResults<User> findByLocationCoordinatesNearAndLastname(Point point, Distance maxDistance, String lastname);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/MongoRepositoryContributorTests.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -613,28 +613,28 @@ void testAggregationWithCollation() {
613613
.withMessageContaining("'locale' is invalid");
614614
}
615615

616-
@Test
616+
@Test // GH-5004
617617
void testNear() {
618618

619619
List<User> users = fragment.findByLocationCoordinatesNear(new Point(-73.99171, 40.738868));
620620
assertThat(users).extracting(User::getUsername).containsExactly("leia", "vader");
621621
}
622622

623-
@Test
623+
@Test // GH-5004
624624
void testNearWithGeoJson() {
625625

626626
List<User> users = fragment.findByLocationCoordinatesNear(new GeoJsonPoint(-73.99171, 40.738868));
627627
assertThat(users).extracting(User::getUsername).containsExactly("leia", "vader");
628628
}
629629

630-
@Test
630+
@Test // GH-5004
631631
void testGeoWithinCircle() {
632632

633633
List<User> users = fragment.findByLocationCoordinatesWithin(new Circle(-78.99171, 45.738868, 170));
634634
assertThat(users).extracting(User::getUsername).containsExactly("leia", "vader");
635635
}
636636

637-
@Test
637+
@Test // GH-5004
638638
void testWithinBox() {
639639

640640
Box box = new Box(new Point(-78.99171, 35.738868), new Point(-68.99171, 45.738868));
@@ -643,7 +643,7 @@ void testWithinBox() {
643643
assertThat(result).extracting(User::getUsername).containsExactly("leia", "vader");
644644
}
645645

646-
@Test
646+
@Test // GH-5004
647647
void findsPeopleByLocationWithinPolygon() {
648648

649649
Point first = new Point(-78.99171, 35.738868);
@@ -655,7 +655,7 @@ void findsPeopleByLocationWithinPolygon() {
655655
assertThat(result).extracting(User::getUsername).containsExactly("leia", "vader");
656656
}
657657

658-
@Test
658+
@Test // GH-5004
659659
void findsPeopleByLocationWithinGeoJsonPolygon() {
660660

661661
Point first = new Point(-78.99171, 35.738868);
@@ -668,31 +668,44 @@ void findsPeopleByLocationWithinGeoJsonPolygon() {
668668
assertThat(result).extracting(User::getUsername).containsExactly("leia", "vader");
669669
}
670670

671-
@Test
671+
@Test // GH-5004
672+
void findsPeopleByLocationWithinSomeGenericGeoJsonObject() {
673+
674+
Point first = new Point(-78.99171, 35.738868);
675+
Point second = new Point(-78.99171, 45.738868);
676+
Point third = new Point(-68.99171, 45.738868);
677+
Point fourth = new Point(-68.99171, 35.738868);
678+
679+
List<User> result = fragment
680+
.findUserByLocationCoordinatesWithin(new GeoJsonPolygon(first, second, third, fourth, first));
681+
assertThat(result).extracting(User::getUsername).containsExactly("leia", "vader");
682+
}
683+
684+
@Test // GH-5004
672685
void testNearWithGeoResult() {
673686

674687
GeoResults<User> users = fragment.findByLocationCoordinatesNear(new Point(-73.99, 40.73),
675688
Distance.of(5, Metrics.KILOMETERS));
676689
assertThat(users).extracting(GeoResult::getContent).extracting(User::getUsername).containsExactly("leia");
677690
}
678691

679-
@Test
692+
@Test // GH-5004
680693
void testNearWithAdditionalFilterQueryAsGeoResult() {
681694

682695
GeoResults<User> users = fragment.findByLocationCoordinatesNearAndLastname(new Point(-73.99, 40.73),
683696
Distance.of(50, Metrics.KILOMETERS), "Organa");
684697
assertThat(users).extracting(GeoResult::getContent).extracting(User::getUsername).containsExactly("leia");
685698
}
686699

687-
@Test
700+
@Test // GH-5004
688701
void testNearReturningListOfGeoResult() {
689702

690703
List<GeoResult<User>> users = fragment.findUserAsListByLocationCoordinatesNear(new Point(-73.99, 40.73),
691704
Distance.of(5, Metrics.KILOMETERS));
692705
assertThat(users).extracting(GeoResult::getContent).extracting(User::getUsername).containsExactly("leia");
693706
}
694707

695-
@Test
708+
@Test // GH-5004
696709
void testNearWithRange() {
697710

698711
Range<Distance> range = Distance.between(Distance.of(5, Metrics.KILOMETERS), Distance.of(2000, Metrics.KILOMETERS));
@@ -701,7 +714,7 @@ void testNearWithRange() {
701714
assertThat(users).extracting(GeoResult::getContent).extracting(User::getUsername).containsExactly("vader");
702715
}
703716

704-
@Test
717+
@Test // GH-5004
705718
void testNearReturningGeoPage() {
706719

707720
GeoPage<User> page1 = fragment.findByLocationCoordinatesNear(new Point(-73.99, 40.73),

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/QueryMethodContributionUnitTests.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.data.geo.Point;
3737
import org.springframework.data.geo.Polygon;
3838
import org.springframework.data.mongodb.core.MongoOperations;
39+
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
3940
import org.springframework.data.mongodb.core.geo.Sphere;
4041
import org.springframework.data.mongodb.repository.ReadPreference;
4142
import org.springframework.data.repository.Repository;
@@ -53,7 +54,7 @@
5354
*/
5455
public class QueryMethodContributionUnitTests {
5556

56-
@Test
57+
@Test // GH-5004
5758
void rendersQueryForNearUsingPoint() throws NoSuchMethodException {
5859

5960
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesNear", Point.class);
@@ -64,7 +65,7 @@ void rendersQueryForNearUsingPoint() throws NoSuchMethodException {
6465
.contains("return finder.matching(filterQuery).all()");
6566
}
6667

67-
@Test
68+
@Test // GH-5004
6869
void rendersQueryForWithinUsingCircle() throws NoSuchMethodException {
6970

7071
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Circle.class);
@@ -76,7 +77,7 @@ void rendersQueryForWithinUsingCircle() throws NoSuchMethodException {
7677
.contains("return finder.matching(filterQuery).all()");
7778
}
7879

79-
@Test
80+
@Test // GH-5004
8081
void rendersQueryForWithinUsingSphere() throws NoSuchMethodException {
8182

8283
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Sphere.class);
@@ -88,7 +89,7 @@ void rendersQueryForWithinUsingSphere() throws NoSuchMethodException {
8889
.contains("return finder.matching(filterQuery).all()");
8990
}
9091

91-
@Test
92+
@Test // GH-5004
9293
void rendersQueryForWithinUsingBox() throws NoSuchMethodException {
9394

9495
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Box.class);
@@ -100,7 +101,7 @@ void rendersQueryForWithinUsingBox() throws NoSuchMethodException {
100101
.contains("return finder.matching(filterQuery).all()");
101102
}
102103

103-
@Test
104+
@Test // GH-5004
104105
void rendersQueryForWithinUsingPolygon() throws NoSuchMethodException {
105106

106107
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Polygon.class);
@@ -112,7 +113,18 @@ void rendersQueryForWithinUsingPolygon() throws NoSuchMethodException {
112113
.contains("return finder.matching(filterQuery).all()");
113114
}
114115

115-
@Test
116+
@Test // GH-5004
117+
void rendersQueryForWithinUsingGeoJsonPolygon() throws NoSuchMethodException {
118+
119+
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", GeoJsonPolygon.class);
120+
121+
assertThat(methodSpec.toString()) //
122+
.contains("{'location.coordinates':{'$geoWithin':{'$geometry':?0}}") //
123+
.contains("Object[]{ polygon }") //
124+
.contains("return finder.matching(filterQuery).all()");
125+
}
126+
127+
@Test // GH-5004
116128
void rendersNearQueryForGeoResults() throws NoSuchMethodException {
117129

118130
MethodSpec methodSpec = codeOf(UserRepoWithMeta.class, "findByLocationCoordinatesNear", Point.class,
@@ -127,23 +139,22 @@ void rendersNearQueryForGeoResults() throws NoSuchMethodException {
127139
.contains("return nearFinder.all()");
128140
}
129141

130-
@Test
142+
@Test // GH-5004
131143
void rendersNearQueryWithDistanceRangeForGeoResults() throws NoSuchMethodException {
132144

133-
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesNear", Point.class,
134-
Range.class);
145+
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesNear", Point.class, Range.class);
135146

136147
assertThat(methodSpec.toString()) //
137-
.contains("NearQuery.near(point)") //
138-
.contains("if(distance.getLowerBound().isBounded())") //
139-
.contains("nearQuery.minDistance(min).in(min.getMetric())") //
140-
.contains("if(distance.getUpperBound().isBounded())") //
141-
.contains("nearQuery.maxDistance(max).in(max.getMetric())") //
142-
.contains(".near(nearQuery)") //
143-
.contains("return nearFinder.all()");
148+
.contains("NearQuery.near(point)") //
149+
.contains("if(distance.getLowerBound().isBounded())") //
150+
.contains("nearQuery.minDistance(min).in(min.getMetric())") //
151+
.contains("if(distance.getUpperBound().isBounded())") //
152+
.contains("nearQuery.maxDistance(max).in(max.getMetric())") //
153+
.contains(".near(nearQuery)") //
154+
.contains("return nearFinder.all()");
144155
}
145156

146-
@Test
157+
@Test // GH-5004
147158
void rendersNearQueryReturningGeoPage() throws NoSuchMethodException {
148159

149160
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesNear", Point.class, Distance.class,
@@ -158,7 +169,7 @@ void rendersNearQueryReturningGeoPage() throws NoSuchMethodException {
158169
.contains("GeoPage<>(geoResult, pageable, resultPage.getTotalElements())");
159170
}
160171

161-
@Test
172+
@Test // GH-5004
162173
void rendersNearQueryWithFilterForGeoResults() throws NoSuchMethodException {
163174

164175
MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesNearAndLastname", Point.class,

0 commit comments

Comments
 (0)