I am getting the following exception while executing my integration test cases for transactional feature of MongoDB.
org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 10107 (NotMaster): 'not master' on server localhost:60876. The full response is {"operationTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$gleStats": {"lastOpTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "electionId": {"$oid": "7fffffff0000000000000001"}}, "lastCommittedOpTime": {"$timestamp": {"t": 0, "i": 0}}, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615104939, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}; nested exception is com.mongodb.MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server localhost:60876. The full response is {"operationTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$gleStats": {"lastOpTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "electionId": {"$oid": "7fffffff0000000000000001"}}, "lastCommittedOpTime": {"$timestamp": {"t": 0, "i": 0}}, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615104939, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2874)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:568)
at org.springframework.data.mongodb.core.MongoTemplate.saveDocument(MongoTemplate.java:1485)
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1421)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1363)
....................
Caused by: com.mongodb.MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server localhost:60876. The full response is {"operationTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$gleStats": {"lastOpTime": {"$timestamp": {"t": 1615104939, "i": 1}}, "electionId": {"$oid": "7fffffff0000000000000001"}}, "lastCommittedOpTime": {"$timestamp": {"t": 0, "i": 0}}, "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1615104939, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}
at com.mongodb.internal.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:244)
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:171)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:302)
My test class has the following method.
@Test
@Order(1)
void testUpdateValidationsById() {
MyEntity entity = new MyEntity();
entity.setId("6030a6e1b32d1f70b4ce8039");
entity.setAttributeType("IP Address, FQDN");
entity.setCategory("NTP");
entity.setCategoryLabel("NTP");
entity.setTrueMsgDetails("It is true");
entity.setFalseMsgDetails("It is false");
entity.setCorevalidation("Core Validation");
ResponseEntity<?> re = controller.updateValidationsById(entity);
boolean flag = re.getStatusCode().is2xxSuccessful();
assertEquals(true, flag);
}
My test configuration class looks like this.
@Profile("test")
@ActiveProfiles("test")
@TestConfiguration
public class TestMongoDBConfig implements InitializingBean, DisposableBean {
private MongodExecutable executable;
private String replicaSetName = "rs0";
private int mongoDBPort = 27021;
@Override
public void afterPropertiesSet() throws Exception {
mongoDBPort = Network.getFreeServerPort();
IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder().useNoPrealloc(false).useSmallFiles(false)
.master(false).verbose(false).useNoJournal(false).syncDelay(0).build();
// IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.V4_0)
// .net(new Net(27023, Network.localhostIsIPv6()))
.net(new Net(mongoDBPort, Network.localhostIsIPv6()))
.replication(new Storage(null, replicaSetName, 5000)).configServer(true).cmdOptions(cmdOptions)
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
executable = starter.prepare(mongodConfig);
executable.start();
}
@Bean(name = "test1")
public MongoClient mongoClient() {
ConnectionString cs = new ConnectionString("mongodb://localhost:" + mongoDBPort + "/");
MongoClient mongoClient = MongoClients.create(cs);
System.out.println("--------------------------------------");
System.out.println("Mongo Selected ort : " + mongoDBPort);
System.out.println("MongoClient : " + mongoClient);
System.out.println("--------------------------------------");
mongoClient.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
return mongoClient;
}
/**
* Destroy.
*
* @throws Exception the exception
*/
@Override
public void destroy() throws Exception {
executable.stop();
}
}