Merge pull request #12182 from ulisseslima/master

BAEL-5396 - Configure MongoDB Collection Name for a Class in Spring Data
This commit is contained in:
davidmartinezbarua 2022-05-20 23:53:48 -03:00 committed by GitHub
commit 9c61c1ce39
16 changed files with 497 additions and 0 deletions

View File

@ -16,6 +16,10 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>

View File

@ -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<String> parts = ParsingUtils.splitCamelCaseToLower(name);
List<String> 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());
}
}

View File

@ -0,0 +1,21 @@
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;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@SpringBootApplication
@PropertySource("classpath:boot.collection.name/app.properties")
@EnableMongoRepositories(basePackages = { "com.baeldung.boot.collection.name" })
public class SpringBootCollectionNameApplication {
public static void main(String... args) {
SpringApplication.run(SpringBootCollectionNameApplication.class, args);
}
@Bean
public Naming naming() {
return new Naming();
}
}

View File

@ -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<Compilation, String> {
}

View File

@ -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<MusicAlbum, String> {
}

View File

@ -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<MusicTrack, String> {
}

View File

@ -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<Store, String> {
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<MusicAlbum> getAlbumList() {
return albumRepository.findAll();
}
public Compilation add(Compilation item) {
return compilationRepository.save(item);
}
public List<Compilation> getCompilationList() {
return compilationRepository.findAll();
}
public Store add(Store item) {
return storeRepository.save(item);
}
public List<Store> getStoreList() {
return storeRepository.findAll();
}
public MusicTrack add(MusicTrack item) {
return trackRepository.save(item);
}
public List<MusicTrack> getTrackList() {
return trackRepository.findAll();
}
}

View File

@ -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<DBObject> get(@PathVariable String name) {
return mongo.findAll(DBObject.class, name);
}
}

View File

@ -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<MusicAlbum> getAlbumList() {
return service.getAlbumList();
}
@PostMapping("/compilation")
public Compilation post(@RequestBody Compilation item) {
return service.add(item);
}
@GetMapping("/compilation")
public List<Compilation> getCompilationList() {
return service.getCompilationList();
}
@PostMapping("/store")
public Store post(@RequestBody Store item) {
return service.add(item);
}
@GetMapping("/store")
public List<Store> getStoreList() {
return service.getStoreList();
}
@PostMapping("/track")
public MusicTrack post(@RequestBody MusicTrack item) {
return service.add(item);
}
@GetMapping("/track")
public List<MusicTrack> getTrackList() {
return service.getTrackList();
}
}

View File

@ -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<Compilation> 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<MusicAlbum> 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<Store> 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<MusicTrack> 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);
}
}