use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testConflictingInsert.
@Test
void testConflictingInsert() {
MongoCollection<Document> collection = mock(MongoCollection.class);
var exception = mock(MongoWriteException.class);
var error = mock(WriteError.class);
MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
when(exception.getError()).thenReturn(error);
when(error.getCategory()).thenReturn(ErrorCategory.DUPLICATE_KEY);
doThrow(exception).when(collection).insertOne(any(Document.class));
CompletionException e = assertThrows(CompletionException.class, () -> usersDao.insert(USER).join());
assertTrue(e.getCause() instanceof DatabaseException);
var exp = (DatabaseException) e.getCause();
assertEquals(DatabaseException.Error.CONFLICT, exp.getError());
verify(collection, times(1)).insertOne(argThat((Document doc) -> doc.containsKey("_id") && doc.containsKey("id") && doc.containsKey("version") && doc.containsKey("creation_time") && doc.containsKey("update_time") && doc.containsKey("document")));
}
use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testUpdatePutRequestUnknownException.
@Test
void testUpdatePutRequestUnknownException() {
MongoCollection<Document> collection = mock(MongoCollection.class);
FindIterable<Document> findIterable = mock(FindIterable.class);
when(findIterable.first()).thenReturn(DOCUMENT);
doReturn(findIterable).when(collection).find(any(Bson.class));
doThrow(new IllegalStateException()).when(collection).updateOne(any(Bson.class), any(Bson.class));
MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
CompletionException e = assertThrows(CompletionException.class, () -> usersDao.update(null, USER).join());
assertTrue(e.getCause() instanceof DatabaseException);
var exp = (DatabaseException) e.getCause();
assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
verify(collection, times(1)).find(any(Bson.class));
verify(collection, times(1)).updateOne(argThat((Bson bson) -> {
BsonDocument doc = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());
return doc.containsKey("version");
}), any(Bson.class));
}
use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testUpdateGetDatabaseDown.
@Test
void testUpdateGetDatabaseDown() {
MongoCollection<Document> collection = mock(MongoCollection.class);
doThrow(MongoTimeoutException.class).when(collection).find(any(Bson.class));
MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
CompletionException e = assertThrows(CompletionException.class, () -> usersDao.update(null, USER).join());
assertTrue(e.getCause() instanceof DatabaseException);
var exp = (DatabaseException) e.getCause();
assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
}
use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testUnsuccessfulDeleteUnknownException.
@Test
void testUnsuccessfulDeleteUnknownException() {
MongoCollection<Document> collection = mock(MongoCollection.class);
FindIterable<Document> findIterable = mock(FindIterable.class);
when(findIterable.first()).thenReturn(DOCUMENT);
doReturn(findIterable).when(collection).find(any(Bson.class));
doThrow(new IllegalStateException()).when(collection).deleteOne(any(Bson.class));
MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
CompletionException e = assertThrows(CompletionException.class, () -> usersDao.delete("test@test.com").join());
assertTrue(e.getCause() instanceof DatabaseException);
var exp = (DatabaseException) e.getCause();
assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
verify(collection, times(1)).deleteOne(eq(Filters.eq("_id", "test@test.com")));
}
use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testInsertWithUnknownException.
@Test
void testInsertWithUnknownException() {
MongoCollection<Document> collection = mock(MongoCollection.class);
MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
doThrow(new IllegalStateException()).when(collection).insertOne(any(Document.class));
CompletionException e = assertThrows(CompletionException.class, () -> usersDao.insert(USER).join());
assertTrue(e.getCause() instanceof DatabaseException);
var exp = (DatabaseException) e.getCause();
assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
verify(collection, times(1)).insertOne(argThat((Document doc) -> doc.containsKey("_id") && doc.containsKey("id") && doc.containsKey("version") && doc.containsKey("creation_time") && doc.containsKey("update_time") && doc.containsKey("document")));
}
Aggregations