use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testInsertTimeout.
@Test
void testInsertTimeout() {
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.EXECUTION_TIMEOUT);
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.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")));
}
use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.
the class MongoDbUsersDaoTest method testInsertRejected.
@Test
void testInsertRejected() {
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.UNCATEGORIZED);
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.REQUEST_REJECTED, 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 testInsertWithRequestRejected.
@Test
void testInsertWithRequestRejected() {
MongoCollection<Document> collection = mock(MongoCollection.class);
MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
MongoCommandException exception = mock(MongoCommandException.class);
when(exception.getErrorMessage()).thenReturn("Test error");
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.REQUEST_REJECTED, 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 testUpdateGetRequestRejected.
@Test
void testUpdateGetRequestRejected() {
MongoCollection<Document> collection = mock(MongoCollection.class);
MongoCommandException exception = mock(MongoCommandException.class);
when(exception.getErrorMessage()).thenReturn("Test error");
doThrow(exception).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.REQUEST_REJECTED, 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 testExistingUserWithNewEmailDatabaseDown.
@Test
void testExistingUserWithNewEmailDatabaseDown() {
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("originalemail@test.com", 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")));
}
Aggregations