@Test void testUnsuccessfulDeleteRequestRejected() { MongoCollection<Document> collection = mock(MongoCollection.class); FindIterable<Document> findIterable = mock(FindIterable.class); MongoCommandException exception = mock(MongoCommandException.class); when(exception.getErrorMessage()).thenReturn("Test error"); when(findIterable.first()).thenReturn(DOCUMENT); doReturn(findIterable).when(collection).find(any(Bson.class)); doThrow(exception).when(collection).deleteOne(any(Bson.class)); MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER); DatabaseException e = assertThrows(DatabaseException.class, () -> usersDao.delete("test@test.com")); assertEquals(DatabaseError.REQUEST_REJECTED, e.getErrorKind()); verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com"))); verify(collection, times(1)).deleteOne(eq(Filters.eq("_id", "test@test.com"))); }
@Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); try { mongoCollection.deleteOne(eq("_id", email)); } catch (MongoTimeoutException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } catch (MongoCommandException e) { LOG.error("There was a MongoDB exception (code: {}, reason: {}) when attempting to delete.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The delete request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; }
MongoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); try { mongoCollection.deleteOne(eq("_id", email)); } catch (MongoTimeoutException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } catch (MongoCommandException e) { LOG.error("There was a MongoDB exception (code: {}, reason: {}) when attempting to delete.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The delete request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; } }
MongoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); try { mongoCollection.deleteOne(eq("_id", email)); } catch (MongoTimeoutException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } catch (MongoCommandException e) { LOG.error("There was a MongoDB exception (code: {}, reason: {}) when attempting to delete.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The delete request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; } @Inject  MongoDbUsersDao(MongoCollection<Document> mongoCollection, ObjectMapper mapper); }
MongoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); try { mongoCollection.deleteOne(eq("_id", email)); } catch (MongoTimeoutException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } catch (MongoCommandException e) { LOG.error("There was a MongoDB exception (code: {}, reason: {}) when attempting to delete.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The delete request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; } @Inject  MongoDbUsersDao(MongoCollection<Document> mongoCollection, ObjectMapper mapper); @Override User insert(User user); @Override User findByEmail(String email); @Override User update(@Nullable String existingEmail, User user); @Override User delete(String email); }
MongoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); try { mongoCollection.deleteOne(eq("_id", email)); } catch (MongoTimeoutException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } catch (MongoCommandException e) { LOG.error("There was a MongoDB exception (code: {}, reason: {}) when attempting to delete.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The delete request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; } @Inject  MongoDbUsersDao(MongoCollection<Document> mongoCollection, ObjectMapper mapper); @Override User insert(User user); @Override User findByEmail(String email); @Override User update(@Nullable String existingEmail, User user); @Override User delete(String email);  }