[BAEL-5355] code snippets for article - lukaswlodkowski (#12954)
* [BAEL-5355] code snippets for article * [BAEL-5355] fix tests naming convention * [BAEL-5355] try different Mongo configuration, previous one failed on Jenkins. * [BAEL-5355] try different Mongo configuration, previous one failed on Jenkins. * [BAEL-5355] try different Mongo configuration, previous one failed on Jenkins.
This commit is contained in:
parent
1cd1a6cf24
commit
59b58d087d
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<User> users = mongoTemplate.find(query, User.class);
|
||||||
|
|
||||||
|
assertThat(users).usingRecursiveComparison()
|
||||||
|
.isEqualTo(Lists.newArrayList(firstUser, secondUser));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue