Skip to content

Commit 14f311b

Browse files
authored
Merge pull request #46 from jupiter-tools/add-geojson-tests
Improve support of the GeoJson
2 parents b39be9c + 86085db commit 14f311b

File tree

13 files changed

+879
-1
lines changed

13 files changed

+879
-1
lines changed

README.adoc

+142-1
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,145 @@ void matchJavaScript() {
540540
mongoTemplate.save(new Bar(17));
541541
mongoTemplate.save(new Bar(55));
542542
}
543-
----
543+
----
544+
545+
## Use GeoJson in data sets
546+
547+
With this library you can use different GeoJson data types in
548+
your tests. Let's consider the next document:
549+
550+
[source, java]
551+
----
552+
@Document
553+
public class StarShip {
554+
@Id
555+
private String id;
556+
private String name;
557+
private int armor;
558+
private int damage;
559+
private GeoJsonPoint location;
560+
private GeoJsonPolygon shape;
561+
}
562+
----
563+
564+
### Export GeoJson data after tests execution in the dataset
565+
566+
When you use `@ExportMongoDataSet` annotation in tests
567+
then documents from the database will be serialized
568+
to the target file in a next way:
569+
570+
[souce, json]
571+
----
572+
{
573+
"com.jupiter.tools.spring.test.mongo.documents.StarShip": [
574+
{
575+
"id": "5cbbff29921376648d6f4e81",
576+
"name": "Dreadnought",
577+
"armor": 100,
578+
"damage": 50,
579+
"location": {
580+
"type": "Point",
581+
"coordinates": [20.0, 40.0]
582+
},
583+
"shape": {
584+
"type": "Polygon",
585+
"coordinates": [
586+
[
587+
[20.0, 40.0],
588+
[22.0, 42.0],
589+
[22.0, 40.0],
590+
[20.0, 42.0]
591+
]
592+
]
593+
}
594+
}
595+
]
596+
}
597+
----
598+
599+
example of the test with the exporting dataset:
600+
601+
[source, java]
602+
----
603+
@Test
604+
@ExportMongoDataSet(outputFile = "target/dataset/export.json")
605+
void exportPolygon() {
606+
607+
Point p1 = new Point(20, 40);
608+
Point p2 = new Point(22, 42);
609+
Point p3 = new Point(22, 40);
610+
Point p4 = new Point(20, 42);
611+
612+
StarShip dreadnought = StarShip.builder()
613+
.name("Dreadnought")
614+
.armor(100)
615+
.damage(50)
616+
.location(new GeoJsonPoint(20, 40))
617+
.shape(new GeoJsonPolygon(p1, p2, p3, p4))
618+
.build();
619+
620+
mongoTemplate.save(dreadnought);
621+
}
622+
----
623+
624+
### Expected GeoJson in the data set
625+
626+
Let's look at the sample of the searching an object in MongoDB within the (GeoJson) rectangle boundary:
627+
628+
[source, java]
629+
----
630+
@Test
631+
@MongoDataSet(value = "dataset/geo/geo_within.json")
632+
void findWithinRectangle() {
633+
// Act
634+
GeoJsonPolygon boundary = new GeoJsonPolygon(new Point(0, 0),
635+
new Point(10, 0),
636+
new Point(10, 10),
637+
new Point(0, 10),
638+
new Point(0, 0));
639+
640+
Query query = new Query();
641+
query.addCriteria(Criteria.where("location")
642+
.within(boundary));
643+
// Act
644+
List<StarShip> ships = mongoTemplate.find(query, StarShip.class);
645+
646+
// Assert
647+
assertThat(ships).hasSize(1);
648+
assertThat(ships.get(0).getName()).isEqualTo("x-wing");
649+
}
650+
----
651+
652+
we used a next data set with two objects in the file `dataset/geo/geo_within.json` :
653+
654+
[source, json]
655+
----
656+
{
657+
"com.jupiter.tools.spring.test.mongo.documents.StarShip": [
658+
{
659+
"id": "5cbaa745921376602705886f",
660+
"name": "x-wing",
661+
"location": {
662+
"type": "Point",
663+
"coordinates": [
664+
1.0,
665+
5.0
666+
]
667+
}
668+
},
669+
{
670+
"id": "5cbaa74592137660270588ff",
671+
"name": "falcon",
672+
"location": {
673+
"type": "Point",
674+
"coordinates": [
675+
15.0,
676+
10.0
677+
]
678+
}
679+
}
680+
]
681+
}
682+
----
683+
684+

src/main/java/com/jupiter/tools/spring/test/mongo/internal/expect/dynamic/value/DynamicDataSet.java

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ private void applyReplacerToMap(Map<String, Object> mapValues) {
5050

5151
for (String key : maps.keySet()) {
5252
Object value = maps.get(key);
53+
if(value == null){
54+
continue;
55+
}
5356
if (!this.complexityDataTypes.isComplexType(value)) {
5457
for (DynamicValue replacer : this.dynamicValueEvaluators) {
5558
if (replacer.isNecessary(value)) {

src/main/java/com/jupiter/tools/spring/test/mongo/internal/exportdata/MongoDataExport.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.jupiter.tools.spring.test.mongo.internal.DataSet;
66
import com.jupiter.tools.spring.test.mongo.internal.exportdata.scanner.DocumentClasses;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.jupiter.tools.spring.test.mongo.internal.geo.GeoJsonSerializationModule;
89
import org.springframework.data.mongodb.core.MongoTemplate;
910

1011
import java.util.HashMap;
@@ -30,6 +31,7 @@ public MongoDataExport(MongoTemplate mongoTemplate, DocumentClasses documentClas
3031
this.mongoTemplate = mongoTemplate;
3132
this.documentClasses = documentClasses;
3233
this.objectMapper = new ObjectMapper();
34+
this.objectMapper.registerModule(new GeoJsonSerializationModule());
3335
//this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
3436
}
3537

0 commit comments

Comments
 (0)