diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 0b39b9ef16..bff23cffc1 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -64,6 +64,7 @@ spring-boot-persistence-h2 spring-boot-persistence-mongodb spring-boot-persistence-mongodb-2 + spring-boot-persistence-mongodb-3 spring-data-arangodb spring-data-cassandra spring-data-cassandra-test @@ -115,4 +116,4 @@ 1.16.3 - \ No newline at end of file + diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/README.md b/persistence-modules/spring-boot-persistence-mongodb-2/README.md index a050de8f47..f62fef1a3e 100644 --- a/persistence-modules/spring-boot-persistence-mongodb-2/README.md +++ b/persistence-modules/spring-boot-persistence-mongodb-2/README.md @@ -7,4 +7,4 @@ - [Count Documents Using Spring Data MongoDB Repository](https://www.baeldung.com/spring-data-mongodb-count) - [Spring Data MongoDB – Configure Connection](https://www.baeldung.com/spring-data-mongodb-connection) - [Connect to Multiple Databases Using Spring Data MongoDB](https://www.baeldung.com/mongodb-multiple-databases-spring-data) -- More articles: [[<--prev]](../spring-boot-persistence-mongodb) +- More articles: [[<--prev]](../spring-boot-persistence-mongodb) [[next-->]](../spring-boot-persistence-mongodb-3) diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/README.md b/persistence-modules/spring-boot-persistence-mongodb-3/README.md new file mode 100644 index 0000000000..853ce16054 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/README.md @@ -0,0 +1,3 @@ +# Relevant Articles + +- More articles: [[<--prev]](../spring-boot-persistence-mongodb-2) diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/pom.xml b/persistence-modules/spring-boot-persistence-mongodb-3/pom.xml new file mode 100644 index 0000000000..efb988d0a0 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + spring-boot-persistence-mongodb-3 + spring-boot-persistence-mongodb-3 + war + This is simple boot application for Spring boot persistence mongodb + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + test + + + + diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/hashmap/SpringBootHashMapApplication.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/hashmap/SpringBootHashMapApplication.java new file mode 100644 index 0000000000..ef1ad8a7b5 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/hashmap/SpringBootHashMapApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.boot.hashmap; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootHashMapApplication { + + public static void main(String... args) { + SpringApplication.run(SpringBootHashMapApplication.class, args); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/resources/application.properties b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/resources/application.properties new file mode 100644 index 0000000000..8309c4461f --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=spring-boot-persistence-mongodb-3 diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/hashmap/MongoDbHashMapIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/hashmap/MongoDbHashMapIntegrationTest.java new file mode 100644 index 0000000000..bbf425f277 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/hashmap/MongoDbHashMapIntegrationTest.java @@ -0,0 +1,120 @@ +package com.baeldung.boot.hashmap; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.bson.Document; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.TestPropertySource; + +import com.mongodb.BasicDBObject; + +@SpringBootTest +@DirtiesContext +@TestPropertySource("/embedded.properties") +class MongoDbHashMapIntegrationTest { + + private static final Map MAP = new HashMap<>(); + private static final Set> MAP_SET = new HashSet<>(); + + @Autowired + private MongoTemplate mongo; + + private void assertHasMongoId(Map saved) { + assertNotNull(saved); + assertNotNull(saved.get("_id")); + } + + @BeforeAll + static void init() { + MAP.put("name", "Document A"); + MAP.put("number", 2); + MAP.put("dynamic", true); + + Map otherMap = new HashMap<>(); + otherMap.put("name", "Other Document"); + otherMap.put("number", 22); + + MAP_SET.add(MAP); + MAP_SET.add(otherMap); + } + + @Test + void whenUsingMap_thenInsertSucceeds() { + Map saved = mongo.insert(MAP, "map-collection"); + + assertHasMongoId(saved); + } + + @Test + void whenMapSet_thenInsertSucceeds() { + Collection> saved = mongo.insert(MAP_SET, "map-set"); + + saved.forEach(this::assertHasMongoId); + assertEquals(2, saved.size()); + } + + @Test + void givenMap_whenDocumentConstructed_thenInsertSucceeds() { + Document document = new Document(MAP); + + Document saved = mongo.insert(document, "doc-collection"); + + assertHasMongoId(saved); + } + + @Test + void givenMap_whenBasicDbObjectConstructed_thenInsertSucceeds() { + BasicDBObject dbObject = new BasicDBObject(MAP); + + BasicDBObject saved = mongo.insert(dbObject, "db-collection"); + + assertHasMongoId(saved); + } + + @Test + void givenObjectList_whenDocumentSetConstructed_thenInsertSucceeds() { + Map> input = new HashMap<>(); + List listOne = new ArrayList<>(); + listOne.add("Doc A"); + listOne.add(1); + + List listTwo = new ArrayList<>(); + listTwo.add("Doc B"); + listTwo.add(2); + + input.put("a", listOne); + input.put("b", listTwo); + + Set docs = input.entrySet() + .stream() + .collect(HashSet::new, (set, entry) -> { + Document document = new Document(); + + document.put("_id", entry.getKey()); + Iterator iterator = entry.getValue() + .iterator(); + document.put("name", iterator.next()); + document.put("number", iterator.next()); + + set.add(document); + }, Set::addAll); + + Collection saved = mongo.insert(docs, "custom-set"); + saved.forEach(this::assertHasMongoId); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/test/resources/embedded.properties b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/resources/embedded.properties new file mode 100644 index 0000000000..a5b5fb9804 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/resources/embedded.properties @@ -0,0 +1 @@ +spring.mongodb.embedded.version=4.4.9 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/test/resources/logback-test.xml b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..8d4771e308 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/resources/logback-test.xml @@ -0,0 +1,12 @@ + + + + + [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n + + + + + + + \ No newline at end of file