11/*
2- * Copyright 2014-2021 the original author or authors.
2+ * Copyright 2014-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package example .springdata .mongodb .customer ;
1717
18- import static org .assertj .core .api .Assertions .*;
19- import static org .assertj .core .data .Offset .offset ;
20-
2118import example .springdata .mongodb .util .MongoContainers ;
22-
23- import java .util .stream .Stream ;
24-
2519import org .junit .jupiter .api .BeforeEach ;
2620import org .junit .jupiter .api .Test ;
27-
2821import org .springframework .beans .factory .annotation .Autowired ;
2922import org .springframework .boot .test .autoconfigure .data .mongo .DataMongoTest ;
3023import org .springframework .data .domain .Limit ;
3124import org .springframework .data .geo .Distance ;
3225import org .springframework .data .geo .Metrics ;
3326import org .springframework .data .geo .Point ;
27+ import org .springframework .data .geo .Polygon ;
3428import org .springframework .data .mongodb .core .MongoOperations ;
29+ import org .springframework .data .mongodb .core .geo .GeoJsonPoint ;
30+ import org .springframework .data .mongodb .core .geo .GeoJsonPolygon ;
3531import org .springframework .data .mongodb .core .index .GeospatialIndex ;
32+ import org .springframework .data .mongodb .core .query .Criteria ;
33+ import org .springframework .data .mongodb .core .query .Query ;
3634import org .springframework .data .querydsl .QSort ;
3735import org .springframework .test .context .DynamicPropertyRegistry ;
3836import org .springframework .test .context .DynamicPropertySource ;
39-
4037import org .testcontainers .containers .MongoDBContainer ;
4138import org .testcontainers .junit .jupiter .Container ;
4239import org .testcontainers .junit .jupiter .Testcontainers ;
4340
41+ import java .util .List ;
42+ import java .util .stream .Stream ;
43+
44+ import static org .assertj .core .api .Assertions .assertThat ;
45+ import static org .assertj .core .data .Offset .offset ;
46+
4447/**
4548 * Integration test for {@link CustomerRepository}.
4649 *
4750 * @author Oliver Gierke
51+ * @author Rishabh Saraswat
4852 */
4953@ Testcontainers
5054@ DataMongoTest
5155class CustomerRepositoryIntegrationTest {
5256
5357 @ Container //
54- private static MongoDBContainer mongoDBContainer = MongoContainers .getDefaultContainer ();
58+ private static final MongoDBContainer mongoDBContainer = MongoContainers .getDefaultContainer ();
5559
5660 @ DynamicPropertySource
5761 static void setProperties (DynamicPropertyRegistry registry ) {
@@ -60,17 +64,23 @@ static void setProperties(DynamicPropertyRegistry registry) {
6064
6165 @ Autowired CustomerRepository repository ;
6266 @ Autowired MongoOperations operations ;
67+ @ Autowired StoreRepository storeRepository ;
6368
6469 private Customer dave , oliver , carter ;
70+ private Store store ;
6571
6672 @ BeforeEach
6773 void setUp () {
6874
6975 repository .deleteAll ();
7076
77+ GeoJsonPolygon polygon = new GeoJsonPolygon (new Point (0.0 , 0.0 ), new Point (0.0 , 1.0 ), new Point (1.0 , 1.0 ), new Point (1.0 , 0.0 ), new Point (0.0 , 0.0 ));
78+
7179 dave = repository .save (new Customer ("Dave" , "Matthews" ));
7280 oliver = repository .save (new Customer ("Oliver August" , "Matthews" ));
7381 carter = repository .save (new Customer ("Carter" , "Beauford" ));
82+
83+ store = storeRepository .save (new Store ("store-1" , polygon ));
7484 }
7585
7686 /**
@@ -146,4 +156,28 @@ void exposesGeoSpatialFunctionality() {
146156 assertThat (distanceToFirstStore .getMetric ()).isEqualTo (Metrics .KILOMETERS );
147157 assertThat (distanceToFirstStore .getValue ()).isCloseTo (0.862 , offset (0.001 ));
148158 }
159+
160+ /**
161+ * Test case to show the usage of the geospatial operator {@code $geoIntersects}.
162+ */
163+ @ Test
164+ void supportsGeoIntersectsPointInside () {
165+ operations .indexOps (Store .class ).createIndex (new GeospatialIndex ("serviceArea" ));
166+ GeoJsonPoint pointInside = new GeoJsonPoint (0.5 , 0.5 );
167+ Query pointInsideQuery = Query .query (Criteria .where ("serviceArea" ).intersects (pointInside ));
168+
169+ List <Store > stores = operations .find (pointInsideQuery , Store .class );
170+ assertThat (stores ).hasSize (1 );
171+ assertThat (stores .get (0 ).getName ()).isEqualTo ("store-1" );
172+ }
173+
174+ @ Test
175+ void supportsGeoIntersectsPointOutside () {
176+ operations .indexOps (Store .class ).createIndex (new GeospatialIndex ("serviceArea" ));
177+ GeoJsonPoint pointOutside = new GeoJsonPoint (0.5 , 0.5 );
178+ Query pointOutsideQuery = Query .query (Criteria .where ("serviceArea" ).intersects (pointOutside ));
179+
180+ List <Store > stores = operations .find (pointOutsideQuery , Store .class );
181+ assertThat (stores ).isEmpty ();
182+ }
149183}
0 commit comments