@@ -540,4 +540,145 @@ void matchJavaScript() {
540
540
mongoTemplate.save(new Bar(17));
541
541
mongoTemplate.save(new Bar(55));
542
542
}
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
+
0 commit comments