From 16bb4772ec608cd91845274dac5eb1671f89de18 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 16 May 2022 23:24:46 -0300 Subject: [PATCH] moved code to spring-boot-persistence-mongodb-2 --- .../spring-boot-persistence-mongodb-2/pom.xml | 4 + .../baeldung/boot/collection/name/Naming.java | 35 ++++++++ .../SpringBootCollectionNameApplication.java | 19 +++++ .../name/dao/CompilationRepository.java | 9 +++ .../name/dao/MusicAlbumRepository.java | 9 +++ .../name/dao/MusicTrackRepository.java | 9 +++ .../collection/name/dao/StoreRepository.java | 9 +++ .../collection/name/data/Compilation.java | 36 +++++++++ .../boot/collection/name/data/MusicAlbum.java | 48 +++++++++++ .../boot/collection/name/data/MusicTrack.java | 50 ++++++++++++ .../boot/collection/name/data/Store.java | 36 +++++++++ .../name/service/MusicStoreService.java | 62 ++++++++++++++ .../name/web/CollectionController.java | 24 ++++++ .../name/web/MusicStoreController.java | 63 +++++++++++++++ .../boot.collection.name/app.properties | 1 + .../MusicStoreServiceIntegrationTest.java | 81 +++++++++++++++++++ 16 files changed, 495 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml b/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml index a6ac4987a1..a172c28a80 100644 --- a/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml +++ b/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml @@ -16,6 +16,10 @@ + + org.springframework.boot + spring-boot-starter-web + org.springframework.boot spring-boot-starter-data-mongodb diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java new file mode 100644 index 0000000000..9808ecccb6 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java @@ -0,0 +1,35 @@ +package com.baeldung.boot.collection.name; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.data.util.ParsingUtils; +import org.springframework.util.StringUtils; + +public class Naming { + public static void main(String[] args) { + String r = new Naming().fix(args[0]); + System.out.println(r); + } + + public String fix(String name) { + List parts = ParsingUtils.splitCamelCaseToLower(name); + List result = new ArrayList<>(); + + for (String part : parts) { + if (StringUtils.hasText(part)) { + result.add(part); + } + } + + return StringUtils.collectionToDelimitedString(result, "_"); + } + + public String convert(Class type) { + return fix(type.getSimpleName()); + } + + public String convert(Object instance) { + return convert(instance.getClass()); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java new file mode 100644 index 0000000000..0a5c36db29 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.boot.collection.name; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; + +@SpringBootApplication +@PropertySource("classpath:boot.collection.name/app.properties") +public class SpringBootCollectionNameApplication { + public static void main(String... args) { + SpringApplication.run(SpringBootCollectionNameApplication.class, args); + } + + @Bean + public Naming naming() { + return new Naming(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java new file mode 100644 index 0000000000..3f83ad4548 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.Compilation; + +public interface CompilationRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java new file mode 100644 index 0000000000..98709361d7 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.MusicAlbum; + +public interface MusicAlbumRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java new file mode 100644 index 0000000000..0964a8de00 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.MusicTrack; + +public interface MusicTrackRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java new file mode 100644 index 0000000000..b446a7d98d --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.Store; + +public interface StoreRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java new file mode 100644 index 0000000000..ce081acf25 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java @@ -0,0 +1,36 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Compilation { + @Id + private String id; + + private String name; + + public Compilation() { + } + + public Compilation(String name) { + super(); + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java new file mode 100644 index 0000000000..ce2e084504 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java @@ -0,0 +1,48 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("albums") +public class MusicAlbum { + @Id + private String id; + + private String name; + + private String artist; + + public MusicAlbum() { + + } + + public MusicAlbum(String name, String artist) { + super(); + this.name = name; + this.artist = artist; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java new file mode 100644 index 0000000000..39ce3994bb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java @@ -0,0 +1,50 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("#{@naming.fix('MusicTrack')}") +public class MusicTrack { + @Id + private String id; + + private String name; + + private String artist; + + public MusicTrack() { + } + + public MusicTrack(String name, String artist) { + this.name = name; + this.artist = artist; + } + + public MusicTrack(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java new file mode 100644 index 0000000000..83f5017a13 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java @@ -0,0 +1,36 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("store-#{@environment.getProperty('collection.suffix')}") +public class Store { + @Id + private String id; + + private String name; + + public Store() { + } + + public Store(String name) { + super(); + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java new file mode 100644 index 0000000000..6083e3d0c3 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java @@ -0,0 +1,62 @@ +package com.baeldung.boot.collection.name.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.boot.collection.name.dao.CompilationRepository; +import com.baeldung.boot.collection.name.dao.MusicAlbumRepository; +import com.baeldung.boot.collection.name.dao.MusicTrackRepository; +import com.baeldung.boot.collection.name.dao.StoreRepository; +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; + +@Service +public class MusicStoreService { + @Autowired + private MusicAlbumRepository albumRepository; + + @Autowired + private CompilationRepository compilationRepository; + + @Autowired + private StoreRepository storeRepository; + + @Autowired + private MusicTrackRepository trackRepository; + + public MusicAlbum add(MusicAlbum item) { + return albumRepository.save(item); + } + + public List getAlbumList() { + return albumRepository.findAll(); + } + + public Compilation add(Compilation item) { + return compilationRepository.save(item); + } + + public List getCompilationList() { + return compilationRepository.findAll(); + } + + public Store add(Store item) { + return storeRepository.save(item); + } + + public List getStoreList() { + return storeRepository.findAll(); + } + + public MusicTrack add(MusicTrack item) { + return trackRepository.save(item); + } + + public List getTrackList() { + return trackRepository.findAll(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java new file mode 100644 index 0000000000..2efca361b9 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java @@ -0,0 +1,24 @@ +package com.baeldung.boot.collection.name.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.mongodb.DBObject; + +@RestController +@RequestMapping("/collection") +public class CollectionController { + @Autowired + private MongoTemplate mongo; + + @GetMapping("/{name}") + public List get(@PathVariable String name) { + return mongo.findAll(DBObject.class, name); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java new file mode 100644 index 0000000000..8c510121c2 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java @@ -0,0 +1,63 @@ +package com.baeldung.boot.collection.name.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; +import com.baeldung.boot.collection.name.service.MusicStoreService; + +@RestController +@RequestMapping("/music") +public class MusicStoreController { + @Autowired + private MusicStoreService service; + + @PostMapping("/album") + public MusicAlbum post(@RequestBody MusicAlbum item) { + return service.add(item); + } + + @GetMapping("/album") + public List getAlbumList() { + return service.getAlbumList(); + } + + @PostMapping("/compilation") + public Compilation post(@RequestBody Compilation item) { + return service.add(item); + } + + @GetMapping("/compilation") + public List getCompilationList() { + return service.getCompilationList(); + } + + @PostMapping("/store") + public Store post(@RequestBody Store item) { + return service.add(item); + } + + @GetMapping("/store") + public List getStoreList() { + return service.getStoreList(); + } + + @PostMapping("/track") + public MusicTrack post(@RequestBody MusicTrack item) { + return service.add(item); + } + + @GetMapping("/track") + public List getTrackList() { + return service.getTrackList(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties new file mode 100644 index 0000000000..98945a76e1 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties @@ -0,0 +1 @@ +collection.suffix=db diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java new file mode 100644 index 0000000000..eda8b8aafb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java @@ -0,0 +1,81 @@ +package com.baeldung.boot.collection.name.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +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.junit4.SpringRunner; + +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; + +@SpringBootTest +@DirtiesContext +@RunWith(SpringRunner.class) +public class MusicStoreServiceIntegrationTest { + @Autowired + private MusicStoreService service; + + @Autowired + private MongoTemplate mongoDb; + + @Test + public void givenAnnotation_whenSearchingByCollectionName_thenFound() { + List list = service.getCompilationList(); + int sizeBefore = list.size(); + + service.add(new Compilation("Spring Hits")); + + list = mongoDb.findAll(Compilation.class, "compilation"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithValue_whenSearchingByCollectionName_thenFound() { + List list = service.getAlbumList(); + int sizeBefore = list.size(); + + service.add(new MusicAlbum("Album 1", "Artist A")); + + list = mongoDb.findAll(MusicAlbum.class, "albums"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithSpELEnvironment_whenSearchingByCollectionName_thenFound() { + List list = service.getStoreList(); + int sizeBefore = list.size(); + + service.add(new Store("Store A")); + + list = mongoDb.findAll(Store.class, "store-db"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithSpELBean_whenSearchingByCollectionName_thenFound() { + List list = service.getTrackList(); + int sizeBefore = list.size(); + + service.add(new MusicTrack("Track 1")); + + list = mongoDb.findAll(MusicTrack.class, "music_track"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } +}