diff --git a/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/objectId/User.java b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/objectId/User.java new file mode 100644 index 0000000000..6401759265 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/objectId/User.java @@ -0,0 +1,24 @@ +package com.baeldung.objectId; + +import org.bson.types.ObjectId; + +public class User { + public static final String NAME_FIELD = "name"; + + private final ObjectId id; + private final String name; + + public User(ObjectId id, String name) { + this.id = id; + this.name = name; + } + + public ObjectId getId() { + return id; + } + + public String getName() { + return name; + } +} + diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/SameObjectIdUnitTest.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/SameObjectIdUnitTest.java new file mode 100644 index 0000000000..4e456fc2d0 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/SameObjectIdUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.objectId; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.ByteBuffer; +import java.util.Date; + +import org.bson.types.ObjectId; +import org.junit.jupiter.api.Test; + +public class SameObjectIdUnitTest { + + @Test + public void givenSameDateAndCounter_whenComparingObjectIds_thenTheyAreNotEqual() { + final Date date = new Date(); + final ObjectId objectIdDate = new ObjectId(date); + final ObjectId objectIdDateCounter1 = new ObjectId(date, 100); + final ObjectId objectIdDateCounter2 = new ObjectId(date, 100); + + assertThat(objectIdDate).isNotEqualTo(objectIdDateCounter1); + assertThat(objectIdDate).isNotEqualTo(objectIdDateCounter2); + + assertThat(objectIdDateCounter1).isEqualTo(objectIdDateCounter2); + } + + @Test + public void givenSameArrayOfBytes_whenComparingObjectIdsCreatedViaDifferentMethods_thenTheObjectIdsAreEqual() { + final byte[] bytes = "123456789012".getBytes(); + final ObjectId objectIdBytes = new ObjectId(bytes); + + final ByteBuffer buffer = ByteBuffer.wrap(bytes); + final ObjectId objectIdByteBuffer = new ObjectId(buffer); + + assertThat(objectIdBytes).isEqualTo(objectIdByteBuffer); + } +} diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/SameObjectIdUsedToInsertSameObjectIdUnitTest.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/SameObjectIdUsedToInsertSameObjectIdUnitTest.java new file mode 100644 index 0000000000..7041315459 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/SameObjectIdUsedToInsertSameObjectIdUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.objectId; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.assertj.core.util.Lists; +import org.bson.types.ObjectId; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.data.mongodb.core.MongoTemplate; +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.junit.jupiter.SpringExtension; + +import com.baeldung.objectId.config.MongoConfig; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = MongoConfig.class) +public class SameObjectIdUsedToInsertSameObjectIdUnitTest { + @Autowired + private MongoTemplate mongoTemplate; + + @BeforeEach + public void setUp() { + mongoTemplate.dropCollection(User.class); + } + + @Test + public void givenUserInDatabase_whenInsertingAnotherUserWithTheSameObjectId_DKEThrownAndInsertRetried() { + //given + final String userName = "Kevin"; + final User firstUser = new User(ObjectId.get(), userName); + final User secondUser = new User(ObjectId.get(), userName); + + mongoTemplate.insert(firstUser); + + //when + try { + mongoTemplate.insert(firstUser); + } catch (DuplicateKeyException dke) { + mongoTemplate.insert(secondUser); + } + + //then + final Query query = new Query(); + query.addCriteria(Criteria.where(User.NAME_FIELD) + .is(userName)); + final List users = mongoTemplate.find(query, User.class); + + assertThat(users).usingRecursiveComparison() + .isEqualTo(Lists.newArrayList(firstUser, secondUser)); + } +} + + diff --git a/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/config/MongoConfig.java b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/config/MongoConfig.java new file mode 100644 index 0000000000..61df4abb42 --- /dev/null +++ b/persistence-modules/spring-data-mongodb-2/src/test/java/com/baeldung/objectId/config/MongoConfig.java @@ -0,0 +1,42 @@ +package com.baeldung.objectId.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.util.SocketUtils; + +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.ImmutableMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfig; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.runtime.Network; + +@Configuration +public class MongoConfig { + private static final String CONNECTION_STRING = "mongodb://%s:%d"; + private static final String HOST = "localhost"; + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + int randomPort = SocketUtils.findAvailableTcpPort(); + + ImmutableMongodConfig mongoDbConfig = MongodConfig.builder() + .version(Version.Main.PRODUCTION) + .net(new Net(HOST, randomPort, Network.localhostIsIPv6())) + .build(); + + MongodStarter starter = MongodStarter.getDefaultInstance(); + MongodExecutable mongodExecutable = starter.prepare(mongoDbConfig); + mongodExecutable.start(); + + return new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, HOST, randomPort)), "mongo_auth"); + } +}