From ef94fc92d75f0297269eec2b711501c20f54d623 Mon Sep 17 00:00:00 2001 From: HARDEMAN Sebastien Date: Fri, 22 Apr 2022 14:42:54 +0200 Subject: [PATCH 1/6] BAEL-5559 - Swap two variables using Java initial commit --- .../math/swap/SwappingVariablesUnitTest.java | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java diff --git a/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java new file mode 100644 index 0000000000..b236468b82 --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java @@ -0,0 +1,157 @@ +package com.baeldung.math.swap; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class SwappingVariablesUnitTest { + + @Test + public void swapObjectsUsingMethod() { + + String a = "a"; + String b = "b"; + + swap(a, b); + + Assertions.assertFalse(a.equals("b")); + Assertions.assertFalse(b.equals("a")); + } + + @Test + public void swapObjectsInWrapperUsingMethod() { + + Wrapper a = new Wrapper("a"); + Wrapper b = new Wrapper("b"); + + swap(a, b); + + Assertions.assertTrue(a.string.equals("b")); + Assertions.assertTrue(b.string.equals("a")); + } + + @Test + public void swapIntegersUsingAdditionSubstraction() { + + int a = 5; + int b = 10; + + a = a + b; + b = a - b; + a = a - b; + + Assertions.assertTrue(a == 10); + Assertions.assertTrue(b == 5); + } + + @Test + public void swapIntegersUsingMultiplicationDivision() { + + int a = 5; + int b = 10; + + a = a * b; + b = a / b; + a = a / b; + + Assertions.assertTrue(a == 10); + Assertions.assertTrue(b == 5); + } + + @Test + public void swapIntegersWithOverflow() { + + int a = Integer.MAX_VALUE; + int b = 10; + + a = a * b; + b = a / b; + a = a / b; + + Assertions.assertTrue(a == 10); + Assertions.assertFalse(b == Integer.MAX_VALUE); + } + + @Test + public void swapChars() { + + char a = 'a'; + char b = 'b'; + + a = (char)(a * b); + b = (char)(a / b); + a = (char)(a / b); + + Assertions.assertTrue(a == 'b'); + Assertions.assertTrue(b == 'a'); + } + + @Test + public void swapIntegersUsingXor() { + + int a = 5; + int b = 10; + + a = a ^ b; + b = a ^ b; + a = a ^ b; + + Assertions.assertTrue(a == 10); + Assertions.assertTrue(b == 5); + } + + @Test + public void swapIntegersUsingXorSingleLine() { + + int a = 5; + int b = 10; + + a = a ^ b ^ (b = a); + + Assertions.assertTrue(a == 10); + Assertions.assertTrue(b == 5); + } + + @Test + public void swapIntegersUsingAdditionSubstractionSingleLine() { + + int a = 5; + int b = 10; + + b = (a + b) - (a = b); + + Assertions.assertTrue(a == 10); + Assertions.assertTrue(b == 5); + } + + /** + * Illustrates that swapping in a method doesn't work + */ + private void swap(String a, String b) { + + String temp = b; + b = a; + a = temp; + } + + /** + * Illustrates swapping in a method with Wrapper class + */ + private void swap(Wrapper a, Wrapper b) { + + String temp = b.string; + b.string = a.string; + a.string = temp; + } + + class Wrapper { + + public String string; + + public Wrapper(String string) { + + this.string = string; + } + + + } +} From 9e3777a4e71ca2a6107c8de606c6a90dfcca0e3e Mon Sep 17 00:00:00 2001 From: HARDEMAN Sebastien Date: Mon, 25 Apr 2022 15:00:39 +0200 Subject: [PATCH 2/6] Updating tests names --- .../math/swap/SwappingVariablesUnitTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java index b236468b82..bbfa0ee4f1 100644 --- a/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java +++ b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/swap/SwappingVariablesUnitTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; public class SwappingVariablesUnitTest { @Test - public void swapObjectsUsingMethod() { + public void givenTwoStrings_whenSwappingInMethod_thenFails() { String a = "a"; String b = "b"; @@ -18,7 +18,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapObjectsInWrapperUsingMethod() { + public void givenTwoWrappers_whenSwappingInMethod_thenSuccess() { Wrapper a = new Wrapper("a"); Wrapper b = new Wrapper("b"); @@ -30,7 +30,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapIntegersUsingAdditionSubstraction() { + public void givenTwoIntegers_whenSwappingUsingAdditionSubstraction_thenSuccess() { int a = 5; int b = 10; @@ -44,7 +44,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapIntegersUsingMultiplicationDivision() { + public void givenTwoIntegers_whenSwappingUsingMultiplicationDivision_thenSuccess() { int a = 5; int b = 10; @@ -58,7 +58,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapIntegersWithOverflow() { + public void givenTwoIntegers_whenSwappingWithOverflow_thenFails() { int a = Integer.MAX_VALUE; int b = 10; @@ -72,7 +72,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapChars() { + public void givenTwoChars_whenSwappingWithCast_thenSuccess() { char a = 'a'; char b = 'b'; @@ -86,7 +86,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapIntegersUsingXor() { + public void givenTwoIntegers_whenSwappingUsingXor_thenSuccess() { int a = 5; int b = 10; @@ -100,7 +100,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapIntegersUsingXorSingleLine() { + public void givenTwoIntegers_whenSwappingUsingSingleLineXor_thenSuccess() { int a = 5; int b = 10; @@ -112,7 +112,7 @@ public class SwappingVariablesUnitTest { } @Test - public void swapIntegersUsingAdditionSubstractionSingleLine() { + public void givenTwoIntegers_whenSwappingUsingAdditionSubstractionSingleLine_thenSuccess() { int a = 5; int b = 10; From f77b93b7a4ff494cadc38c00860a2fc47577fa29 Mon Sep 17 00:00:00 2001 From: HARDEMAN Sebastien Date: Tue, 7 Jun 2022 11:57:29 +0200 Subject: [PATCH 3/6] BAEL-5404 - UUID as Entity ID in MongoDB Initial commit --- .../config/CustomRepositoryMongoConfig.java | 32 ++++++++++ .../config/EntityCallbackMongoConfig.java | 48 ++++++++++++++ .../com/baeldung/config/EventMongoConfig.java | 38 +++++++++++ .../UuidIdentifiedEntityEventListener.java | 22 +++++++ .../main/java/com/baeldung/model/Book.java | 26 ++++++++ .../baeldung/model/UuidIdentifiedEntity.java | 25 ++++++++ .../baeldung/repository/BookRepository.java | 11 ++++ .../repository/CustomMongoRepository.java | 13 ++++ .../impl/CustomMongoRepositoryImpl.java | 51 +++++++++++++++ .../repository/CustomRepositoryLiveTest.java | 58 +++++++++++++++++ .../repository/EntityCallbackLiveTest.java | 63 +++++++++++++++++++ .../baeldung/repository/EventLiveTest.java | 63 +++++++++++++++++++ 12 files changed, 450 insertions(+) create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java create mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java create mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java create mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java create mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java new file mode 100644 index 0000000000..585bdba907 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java @@ -0,0 +1,32 @@ +package com.baeldung.config; + +import org.bson.UuidRepresentation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.repository.impl.CustomMongoRepositoryImpl; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = "com.baeldung.repository", repositoryBaseClass = CustomMongoRepositoryImpl.class) +public class CustomRepositoryMongoConfig { + + @Bean + public MongoClient mongo() throws Exception { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .uuidRepresentation(UuidRepresentation.STANDARD) + .applyConnectionString(connectionString).build(); + return MongoClients.create(mongoClientSettings); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java new file mode 100644 index 0000000000..91c64b4801 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java @@ -0,0 +1,48 @@ +package com.baeldung.config; + +import java.util.UUID; + +import org.bson.UuidRepresentation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.model.UuidIdentifiedEntity; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = "com.baeldung.repository") +public class EntityCallbackMongoConfig { + + @Bean + public MongoClient mongo() throws Exception { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .uuidRepresentation(UuidRepresentation.STANDARD) + .applyConnectionString(connectionString).build(); + return MongoClients.create(mongoClientSettings); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } + + @Bean + public BeforeConvertCallback beforeSaveCallback() { + + return (entity, collection) -> { + + if(entity.getId() == null) { + entity.setId(UUID.randomUUID()); + } + return entity; + }; + } + +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java new file mode 100644 index 0000000000..dc862e9513 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.config; + +import org.bson.UuidRepresentation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.event.UuidIdentifiedEntityEventListener; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = "com.baeldung.repository") +public class EventMongoConfig { + + @Bean + public MongoClient mongo() throws Exception { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .uuidRepresentation(UuidRepresentation.STANDARD) + .applyConnectionString(connectionString).build(); + return MongoClients.create(mongoClientSettings); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } + + @Bean + public UuidIdentifiedEntityEventListener uuidIdentifiedEntityEventListener() { + + return new UuidIdentifiedEntityEventListener(); + } +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java new file mode 100644 index 0000000000..002c5994e3 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java @@ -0,0 +1,22 @@ +package com.baeldung.event; + +import java.util.UUID; + +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; +import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; + +import com.baeldung.model.UuidIdentifiedEntity; + +public class UuidIdentifiedEntityEventListener extends AbstractMongoEventListener { + + @Override + public void onBeforeConvert(BeforeConvertEvent event) { + + super.onBeforeConvert(event); + UuidIdentifiedEntity entity = event.getSource(); + + if(entity.getId() == null) { + entity.setId(UUID.randomUUID()); + } + } +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java new file mode 100644 index 0000000000..31ee0c7142 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java @@ -0,0 +1,26 @@ +package com.baeldung.model; + +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Book extends UuidIdentifiedEntity { + + private String title; + private String author; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java new file mode 100644 index 0000000000..9506b4ab9e --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java @@ -0,0 +1,25 @@ +package com.baeldung.model; + +import java.util.UUID; + +import org.springframework.data.annotation.Id; + +public abstract class UuidIdentifiedEntity { + + @Id + private UUID id; + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + + if(this.id != null) { + + throw new UnsupportedOperationException("ID is already defined"); + } + + this.id = id; + } +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java new file mode 100644 index 0000000000..21e068e80d --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.repository; + +import java.util.UUID; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.model.Book; + +public interface BookRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java new file mode 100644 index 0000000000..50f54b7e75 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.repository; + +import java.util.UUID; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.repository.NoRepositoryBean; + +import com.baeldung.model.UuidIdentifiedEntity; + +@NoRepositoryBean +public interface CustomMongoRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java new file mode 100644 index 0000000000..a7fbb7f6d5 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java @@ -0,0 +1,51 @@ +package com.baeldung.repository.impl; + +import java.util.List; +import java.util.UUID; + +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; + +import com.baeldung.model.UuidIdentifiedEntity; +import com.baeldung.repository.CustomMongoRepository; + +public class CustomMongoRepositoryImpl extends SimpleMongoRepository implements CustomMongoRepository { + + public CustomMongoRepositoryImpl(MongoEntityInformation metadata, MongoOperations mongoOperations) { + + super(metadata, mongoOperations); + } + + @Override + public S save(S entity) { + generateId(entity); + return super.save(entity); + } + + @Override + public List saveAll(Iterable entities) { + entities.forEach(entity -> generateId(entity)); + return super.saveAll(entities); + } + + @Override + public S insert(S entity) { + generateId(entity); + return super.insert(entity); + } + + @Override + public List insert(Iterable entities) { + entities.forEach(entity -> generateId(entity)); + return super.insert(entities); + } + + protected void generateId(S entity) { + + if(entity != null && entity.getId() == null) { + entity.setId(UUID.randomUUID()); + } + } + +} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java new file mode 100644 index 0000000000..923ddd88bd --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java @@ -0,0 +1,58 @@ +package com.baeldung.repository; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.config.CustomRepositoryMongoConfig; +import com.baeldung.model.Book; + +/** + * + * This test requires: + * * mongodb instance running on the environment + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = CustomRepositoryMongoConfig.class) +public class CustomRepositoryLiveTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + if (!mongoOps.collectionExists(Book.class)) { + mongoOps.createCollection(Book.class); + } + } + + @After + public void tearDown() { + mongoOps.dropCollection(Book.class); + } + + @Test + public void whenInsertingBook_thenBookIsInserted() { + final Book book = new Book(); + book.setTitle("The Lord of the Rings"); + book.setAuthor("JRR Tolkien"); + Book savedBook = bookRepository.save(book); + + Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedBook.getId())), Book.class); + + assertEquals(result.getTitle(), "The Lord of the Rings"); + } +} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java new file mode 100644 index 0000000000..343472eb36 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java @@ -0,0 +1,63 @@ +package com.baeldung.repository; + +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.config.EntityCallbackMongoConfig; +import com.baeldung.model.Book; + +/** + * + * This test requires: + * * mongodb instance running on the environment + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = EntityCallbackMongoConfig.class) +public class EntityCallbackLiveTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + if (!mongoOps.collectionExists(Book.class)) { + mongoOps.createCollection(Book.class); + } + } + + @After + public void tearDown() { + mongoOps.dropCollection(Book.class); + } + + @Test + public void whenSavingArticle_thenArticleIsInserted() { + final Book book = new Book(); + book.setTitle("The Lord of the Rings"); + book.setAuthor("JRR Tolkien"); + + Book savedArticle = bookRepository.save(book); + + Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); + + assertNotNull(result); + assertEquals(result.getTitle(), "The Lord of the Rings"); + } + + +} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java new file mode 100644 index 0000000000..a6b3406c9a --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java @@ -0,0 +1,63 @@ +package com.baeldung.repository; + +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.config.EventMongoConfig; +import com.baeldung.model.Book; + +/** + * + * This test requires: + * * mongodb instance running on the environment + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = EventMongoConfig.class) +public class EventLiveTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + if (!mongoOps.collectionExists(Book.class)) { + mongoOps.createCollection(Book.class); + } + } + + @After + public void tearDown() { + mongoOps.dropCollection(Book.class); + } + + @Test + public void whenSavingArticle_thenArticleIsInserted() { + final Book book = new Book(); + book.setTitle("The Lord of the Rings"); + book.setAuthor("JRR Tolkien"); + + Book savedArticle = bookRepository.save(book); + + Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); + + assertNotNull(result); + assertEquals(result.getTitle(), "The Lord of the Rings"); + } + + +} From 4b992c47635021a7848a26f2236d824dc02a48fe Mon Sep 17 00:00:00 2001 From: HARDEMAN Sebastien Date: Tue, 7 Jun 2022 13:35:01 +0200 Subject: [PATCH 4/6] Update UuidIdentifiedEntity.java --- .../src/main/java/com/baeldung/model/UuidIdentifiedEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java index 9506b4ab9e..fae951d214 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java @@ -7,7 +7,7 @@ import org.springframework.data.annotation.Id; public abstract class UuidIdentifiedEntity { @Id - private UUID id; + protected UUID id; public UUID getId() { return id; From 073a07f4caaac7993ab9ccfaf24fde4510539d45 Mon Sep 17 00:00:00 2001 From: HARDEMAN Sebastien Date: Mon, 27 Jun 2022 15:32:38 +0200 Subject: [PATCH 5/6] BAEL-5404 - UUID as Entity ID in MongoDB Commit to spring-data-mongodb-2 --- .../UuidIdentifiedEntityEventListener.java | 23 +++++++ .../java/com/baeldung/uuid/model/Book.java | 26 ++++++++ .../uuid/model/UuidIdentifiedEntity.java | 25 ++++++++ .../uuid/repository/BookRepository.java | 11 ++++ .../repository/CustomMongoRepository.java | 14 ++++ .../impl/CustomMongoRepositoryImpl.java | 52 +++++++++++++++ .../uuid/CustomRepositoryLiveTest.java | 59 +++++++++++++++++ .../baeldung/uuid/EntityCallbackLiveTest.java | 64 +++++++++++++++++++ .../java/com/baeldung/uuid/EventLiveTest.java | 64 +++++++++++++++++++ .../config/CustomRepositoryMongoConfig.java | 32 ++++++++++ .../config/EntityCallbackMongoConfig.java | 48 ++++++++++++++ .../uuid/config/EventMongoConfig.java | 38 +++++++++++ 12 files changed, 456 insertions(+) create mode 100644 persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/event/UuidIdentifiedEntityEventListener.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/Book.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/UuidIdentifiedEntity.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/BookRepository.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/CustomMongoRepository.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/impl/CustomMongoRepositoryImpl.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/CustomRepositoryLiveTest.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EntityCallbackLiveTest.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EventLiveTest.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/CustomRepositoryMongoConfig.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EntityCallbackMongoConfig.java create mode 100644 persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EventMongoConfig.java diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/event/UuidIdentifiedEntityEventListener.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/event/UuidIdentifiedEntityEventListener.java new file mode 100644 index 0000000000..ce45d1e7bb --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/event/UuidIdentifiedEntityEventListener.java @@ -0,0 +1,23 @@ +package com.baeldung.uuid.event; + +import java.util.UUID; + +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; +import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; + +import com.baeldung.uuid.model.UuidIdentifiedEntity; + + +public class UuidIdentifiedEntityEventListener extends AbstractMongoEventListener { + + @Override + public void onBeforeConvert(BeforeConvertEvent event) { + + super.onBeforeConvert(event); + UuidIdentifiedEntity entity = event.getSource(); + + if(entity.getId() == null) { + entity.setId(UUID.randomUUID()); + } + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/Book.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/Book.java new file mode 100644 index 0000000000..f6aa1c615e --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/Book.java @@ -0,0 +1,26 @@ +package com.baeldung.uuid.model; + +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Book extends UuidIdentifiedEntity { + + private String title; + private String author; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/UuidIdentifiedEntity.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/UuidIdentifiedEntity.java new file mode 100644 index 0000000000..aee0368b46 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/model/UuidIdentifiedEntity.java @@ -0,0 +1,25 @@ +package com.baeldung.uuid.model; + +import java.util.UUID; + +import org.springframework.data.annotation.Id; + +public abstract class UuidIdentifiedEntity { + + @Id + protected UUID id; + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + + if(this.id != null) { + + throw new UnsupportedOperationException("ID is already defined"); + } + + this.id = id; + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/BookRepository.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/BookRepository.java new file mode 100644 index 0000000000..0d6eb790aa --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/BookRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.uuid.repository; + +import java.util.UUID; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.uuid.model.Book; + +public interface BookRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/CustomMongoRepository.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/CustomMongoRepository.java new file mode 100644 index 0000000000..599bc9ef61 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/CustomMongoRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.uuid.repository; + +import java.util.UUID; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.repository.NoRepositoryBean; + +import com.baeldung.uuid.model.UuidIdentifiedEntity; + + +@NoRepositoryBean +public interface CustomMongoRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/impl/CustomMongoRepositoryImpl.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/impl/CustomMongoRepositoryImpl.java new file mode 100644 index 0000000000..9764d0b06e --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/uuid/repository/impl/CustomMongoRepositoryImpl.java @@ -0,0 +1,52 @@ +package com.baeldung.uuid.repository.impl; + +import java.util.List; +import java.util.UUID; + +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; + +import com.baeldung.uuid.model.UuidIdentifiedEntity; +import com.baeldung.uuid.repository.CustomMongoRepository; + + +public class CustomMongoRepositoryImpl extends SimpleMongoRepository implements CustomMongoRepository { + + public CustomMongoRepositoryImpl(MongoEntityInformation metadata, MongoOperations mongoOperations) { + + super(metadata, mongoOperations); + } + + @Override + public S save(S entity) { + generateId(entity); + return super.save(entity); + } + + @Override + public List saveAll(Iterable entities) { + entities.forEach(entity -> generateId(entity)); + return super.saveAll(entities); + } + + @Override + public S insert(S entity) { + generateId(entity); + return super.insert(entity); + } + + @Override + public List insert(Iterable entities) { + entities.forEach(entity -> generateId(entity)); + return super.insert(entities); + } + + protected void generateId(S entity) { + + if(entity != null && entity.getId() == null) { + entity.setId(UUID.randomUUID()); + } + } + +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/CustomRepositoryLiveTest.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/CustomRepositoryLiveTest.java new file mode 100644 index 0000000000..0fc3fddfd7 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/CustomRepositoryLiveTest.java @@ -0,0 +1,59 @@ +package com.baeldung.uuid; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.uuid.config.CustomRepositoryMongoConfig; +import com.baeldung.uuid.model.Book; +import com.baeldung.uuid.repository.BookRepository; + +/** + * + * This test requires: + * * mongodb instance running on the environment + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = CustomRepositoryMongoConfig.class) +public class CustomRepositoryLiveTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + if (!mongoOps.collectionExists(Book.class)) { + mongoOps.createCollection(Book.class); + } + } + + @After + public void tearDown() { + mongoOps.dropCollection(Book.class); + } + + @Test + public void whenInsertingBook_thenBookIsInserted() { + final Book book = new Book(); + book.setTitle("The Lord of the Rings"); + book.setAuthor("JRR Tolkien"); + Book savedBook = bookRepository.save(book); + + Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedBook.getId())), Book.class); + + assertEquals(result.getTitle(), "The Lord of the Rings"); + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EntityCallbackLiveTest.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EntityCallbackLiveTest.java new file mode 100644 index 0000000000..737c2d8adb --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EntityCallbackLiveTest.java @@ -0,0 +1,64 @@ +package com.baeldung.uuid; + +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.uuid.config.EntityCallbackMongoConfig; +import com.baeldung.uuid.model.Book; +import com.baeldung.uuid.repository.BookRepository; + +/** + * + * This test requires: + * * mongodb instance running on the environment + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = EntityCallbackMongoConfig.class) +public class EntityCallbackLiveTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + if (!mongoOps.collectionExists(Book.class)) { + mongoOps.createCollection(Book.class); + } + } + + @After + public void tearDown() { + mongoOps.dropCollection(Book.class); + } + + @Test + public void whenSavingArticle_thenArticleIsInserted() { + final Book book = new Book(); + book.setTitle("The Lord of the Rings"); + book.setAuthor("JRR Tolkien"); + + Book savedArticle = bookRepository.save(book); + + Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); + + assertNotNull(result); + assertEquals(result.getTitle(), "The Lord of the Rings"); + } + + +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EventLiveTest.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EventLiveTest.java new file mode 100644 index 0000000000..2c6bc72da6 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/EventLiveTest.java @@ -0,0 +1,64 @@ +package com.baeldung.uuid; + +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.uuid.config.EventMongoConfig; +import com.baeldung.uuid.model.Book; +import com.baeldung.uuid.repository.BookRepository; + +/** + * + * This test requires: + * * mongodb instance running on the environment + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = EventMongoConfig.class) +public class EventLiveTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private MongoOperations mongoOps; + + @Before + public void testSetup() { + if (!mongoOps.collectionExists(Book.class)) { + mongoOps.createCollection(Book.class); + } + } + + @After + public void tearDown() { + mongoOps.dropCollection(Book.class); + } + + @Test + public void whenSavingArticle_thenArticleIsInserted() { + final Book book = new Book(); + book.setTitle("The Lord of the Rings"); + book.setAuthor("JRR Tolkien"); + + Book savedArticle = bookRepository.save(book); + + Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); + + assertNotNull(result); + assertEquals(result.getTitle(), "The Lord of the Rings"); + } + + +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/CustomRepositoryMongoConfig.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/CustomRepositoryMongoConfig.java new file mode 100644 index 0000000000..833a407ae2 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/CustomRepositoryMongoConfig.java @@ -0,0 +1,32 @@ +package com.baeldung.uuid.config; + +import org.bson.UuidRepresentation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.uuid.repository.impl.CustomMongoRepositoryImpl; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = "com.baeldung.uuid.repository", repositoryBaseClass = CustomMongoRepositoryImpl.class) +public class CustomRepositoryMongoConfig { + + @Bean + public MongoClient mongo() throws Exception { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .uuidRepresentation(UuidRepresentation.STANDARD) + .applyConnectionString(connectionString).build(); + return MongoClients.create(mongoClientSettings); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EntityCallbackMongoConfig.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EntityCallbackMongoConfig.java new file mode 100644 index 0000000000..baf69dd5e8 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EntityCallbackMongoConfig.java @@ -0,0 +1,48 @@ +package com.baeldung.uuid.config; + +import java.util.UUID; + +import org.bson.UuidRepresentation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.uuid.model.UuidIdentifiedEntity; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = "com.baeldung.uuid.repository") +public class EntityCallbackMongoConfig { + + @Bean + public MongoClient mongo() throws Exception { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .uuidRepresentation(UuidRepresentation.STANDARD) + .applyConnectionString(connectionString).build(); + return MongoClients.create(mongoClientSettings); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } + + @Bean + public BeforeConvertCallback beforeSaveCallback() { + + return (entity, collection) -> { + + if(entity.getId() == null) { + entity.setId(UUID.randomUUID()); + } + return entity; + }; + } + +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EventMongoConfig.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EventMongoConfig.java new file mode 100644 index 0000000000..d26281dd4a --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/uuid/config/EventMongoConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.uuid.config; + +import org.bson.UuidRepresentation; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.uuid.event.UuidIdentifiedEntityEventListener; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = "com.baeldung.uuid.repository") +public class EventMongoConfig { + + @Bean + public MongoClient mongo() throws Exception { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .uuidRepresentation(UuidRepresentation.STANDARD) + .applyConnectionString(connectionString).build(); + return MongoClients.create(mongoClientSettings); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } + + @Bean + public UuidIdentifiedEntityEventListener uuidIdentifiedEntityEventListener() { + + return new UuidIdentifiedEntityEventListener(); + } +} From 1d196d272009c9a6f367d6877de7d2d300876a09 Mon Sep 17 00:00:00 2001 From: HARDEMAN Sebastien Date: Mon, 27 Jun 2022 15:40:42 +0200 Subject: [PATCH 6/6] remove files in order to move them in spring-data-mongodb-2 --- .../config/CustomRepositoryMongoConfig.java | 32 ---------- .../config/EntityCallbackMongoConfig.java | 48 -------------- .../com/baeldung/config/EventMongoConfig.java | 38 ----------- .../UuidIdentifiedEntityEventListener.java | 22 ------- .../main/java/com/baeldung/model/Book.java | 26 -------- .../baeldung/model/UuidIdentifiedEntity.java | 25 -------- .../baeldung/repository/BookRepository.java | 11 ---- .../repository/CustomMongoRepository.java | 13 ---- .../impl/CustomMongoRepositoryImpl.java | 51 --------------- .../repository/CustomRepositoryLiveTest.java | 58 ----------------- .../repository/EntityCallbackLiveTest.java | 63 ------------------- .../baeldung/repository/EventLiveTest.java | 63 ------------------- 12 files changed, 450 deletions(-) delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java delete mode 100644 persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java delete mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java delete mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java delete mode 100644 persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java deleted file mode 100644 index 585bdba907..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/CustomRepositoryMongoConfig.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.config; - -import org.bson.UuidRepresentation; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; - -import com.baeldung.repository.impl.CustomMongoRepositoryImpl; -import com.mongodb.ConnectionString; -import com.mongodb.MongoClientSettings; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; - -@Configuration -@EnableMongoRepositories(basePackages = "com.baeldung.repository", repositoryBaseClass = CustomMongoRepositoryImpl.class) -public class CustomRepositoryMongoConfig { - - @Bean - public MongoClient mongo() throws Exception { - final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); - final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() - .uuidRepresentation(UuidRepresentation.STANDARD) - .applyConnectionString(connectionString).build(); - return MongoClients.create(mongoClientSettings); - } - - @Bean - public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongo(), "test"); - } -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java deleted file mode 100644 index 91c64b4801..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EntityCallbackMongoConfig.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.config; - -import java.util.UUID; - -import org.bson.UuidRepresentation; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; - -import com.baeldung.model.UuidIdentifiedEntity; -import com.mongodb.ConnectionString; -import com.mongodb.MongoClientSettings; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; - -@Configuration -@EnableMongoRepositories(basePackages = "com.baeldung.repository") -public class EntityCallbackMongoConfig { - - @Bean - public MongoClient mongo() throws Exception { - final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); - final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() - .uuidRepresentation(UuidRepresentation.STANDARD) - .applyConnectionString(connectionString).build(); - return MongoClients.create(mongoClientSettings); - } - - @Bean - public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongo(), "test"); - } - - @Bean - public BeforeConvertCallback beforeSaveCallback() { - - return (entity, collection) -> { - - if(entity.getId() == null) { - entity.setId(UUID.randomUUID()); - } - return entity; - }; - } - -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java deleted file mode 100644 index dc862e9513..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/EventMongoConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.config; - -import org.bson.UuidRepresentation; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; - -import com.baeldung.event.UuidIdentifiedEntityEventListener; -import com.mongodb.ConnectionString; -import com.mongodb.MongoClientSettings; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; - -@Configuration -@EnableMongoRepositories(basePackages = "com.baeldung.repository") -public class EventMongoConfig { - - @Bean - public MongoClient mongo() throws Exception { - final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); - final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() - .uuidRepresentation(UuidRepresentation.STANDARD) - .applyConnectionString(connectionString).build(); - return MongoClients.create(mongoClientSettings); - } - - @Bean - public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongo(), "test"); - } - - @Bean - public UuidIdentifiedEntityEventListener uuidIdentifiedEntityEventListener() { - - return new UuidIdentifiedEntityEventListener(); - } -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java deleted file mode 100644 index 002c5994e3..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/event/UuidIdentifiedEntityEventListener.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.event; - -import java.util.UUID; - -import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; -import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; - -import com.baeldung.model.UuidIdentifiedEntity; - -public class UuidIdentifiedEntityEventListener extends AbstractMongoEventListener { - - @Override - public void onBeforeConvert(BeforeConvertEvent event) { - - super.onBeforeConvert(event); - UuidIdentifiedEntity entity = event.getSource(); - - if(entity.getId() == null) { - entity.setId(UUID.randomUUID()); - } - } -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java deleted file mode 100644 index 31ee0c7142..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Book.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.model; - -import org.springframework.data.mongodb.core.mapping.Document; - -@Document -public class Book extends UuidIdentifiedEntity { - - private String title; - private String author; - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getAuthor() { - return author; - } - - public void setAuthor(String author) { - this.author = author; - } -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java deleted file mode 100644 index fae951d214..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/UuidIdentifiedEntity.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.model; - -import java.util.UUID; - -import org.springframework.data.annotation.Id; - -public abstract class UuidIdentifiedEntity { - - @Id - protected UUID id; - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - - if(this.id != null) { - - throw new UnsupportedOperationException("ID is already defined"); - } - - this.id = id; - } -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java deleted file mode 100644 index 21e068e80d..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/BookRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.repository; - -import java.util.UUID; - -import org.springframework.data.mongodb.repository.MongoRepository; - -import com.baeldung.model.Book; - -public interface BookRepository extends MongoRepository { - -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java deleted file mode 100644 index 50f54b7e75..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/CustomMongoRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.repository; - -import java.util.UUID; - -import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.data.repository.NoRepositoryBean; - -import com.baeldung.model.UuidIdentifiedEntity; - -@NoRepositoryBean -public interface CustomMongoRepository extends MongoRepository { - -} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java deleted file mode 100644 index a7fbb7f6d5..0000000000 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/impl/CustomMongoRepositoryImpl.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.repository.impl; - -import java.util.List; -import java.util.UUID; - -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.repository.query.MongoEntityInformation; -import org.springframework.data.mongodb.repository.support.SimpleMongoRepository; - -import com.baeldung.model.UuidIdentifiedEntity; -import com.baeldung.repository.CustomMongoRepository; - -public class CustomMongoRepositoryImpl extends SimpleMongoRepository implements CustomMongoRepository { - - public CustomMongoRepositoryImpl(MongoEntityInformation metadata, MongoOperations mongoOperations) { - - super(metadata, mongoOperations); - } - - @Override - public S save(S entity) { - generateId(entity); - return super.save(entity); - } - - @Override - public List saveAll(Iterable entities) { - entities.forEach(entity -> generateId(entity)); - return super.saveAll(entities); - } - - @Override - public S insert(S entity) { - generateId(entity); - return super.insert(entity); - } - - @Override - public List insert(Iterable entities) { - entities.forEach(entity -> generateId(entity)); - return super.insert(entities); - } - - protected void generateId(S entity) { - - if(entity != null && entity.getId() == null) { - entity.setId(UUID.randomUUID()); - } - } - -} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java deleted file mode 100644 index 923ddd88bd..0000000000 --- a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/CustomRepositoryLiveTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.baeldung.repository; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.config.CustomRepositoryMongoConfig; -import com.baeldung.model.Book; - -/** - * - * This test requires: - * * mongodb instance running on the environment - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = CustomRepositoryMongoConfig.class) -public class CustomRepositoryLiveTest { - - @Autowired - private BookRepository bookRepository; - - @Autowired - private MongoOperations mongoOps; - - @Before - public void testSetup() { - if (!mongoOps.collectionExists(Book.class)) { - mongoOps.createCollection(Book.class); - } - } - - @After - public void tearDown() { - mongoOps.dropCollection(Book.class); - } - - @Test - public void whenInsertingBook_thenBookIsInserted() { - final Book book = new Book(); - book.setTitle("The Lord of the Rings"); - book.setAuthor("JRR Tolkien"); - Book savedBook = bookRepository.save(book); - - Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedBook.getId())), Book.class); - - assertEquals(result.getTitle(), "The Lord of the Rings"); - } -} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java deleted file mode 100644 index 343472eb36..0000000000 --- a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EntityCallbackLiveTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.repository; - -import static org.junit.Assert.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.config.EntityCallbackMongoConfig; -import com.baeldung.model.Book; - -/** - * - * This test requires: - * * mongodb instance running on the environment - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = EntityCallbackMongoConfig.class) -public class EntityCallbackLiveTest { - - @Autowired - private BookRepository bookRepository; - - @Autowired - private MongoOperations mongoOps; - - @Before - public void testSetup() { - if (!mongoOps.collectionExists(Book.class)) { - mongoOps.createCollection(Book.class); - } - } - - @After - public void tearDown() { - mongoOps.dropCollection(Book.class); - } - - @Test - public void whenSavingArticle_thenArticleIsInserted() { - final Book book = new Book(); - book.setTitle("The Lord of the Rings"); - book.setAuthor("JRR Tolkien"); - - Book savedArticle = bookRepository.save(book); - - Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); - - assertNotNull(result); - assertEquals(result.getTitle(), "The Lord of the Rings"); - } - - -} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java deleted file mode 100644 index a6b3406c9a..0000000000 --- a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/EventLiveTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.repository; - -import static org.junit.Assert.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.config.EventMongoConfig; -import com.baeldung.model.Book; - -/** - * - * This test requires: - * * mongodb instance running on the environment - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = EventMongoConfig.class) -public class EventLiveTest { - - @Autowired - private BookRepository bookRepository; - - @Autowired - private MongoOperations mongoOps; - - @Before - public void testSetup() { - if (!mongoOps.collectionExists(Book.class)) { - mongoOps.createCollection(Book.class); - } - } - - @After - public void tearDown() { - mongoOps.dropCollection(Book.class); - } - - @Test - public void whenSavingArticle_thenArticleIsInserted() { - final Book book = new Book(); - book.setTitle("The Lord of the Rings"); - book.setAuthor("JRR Tolkien"); - - Book savedArticle = bookRepository.save(book); - - Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class); - - assertNotNull(result); - assertEquals(result.getTitle(), "The Lord of the Rings"); - } - - -}