@Test void testFindByEmailRequestRejected() { 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).find(any(Bson.class)); DatabaseException e = assertThrows(DatabaseException.class, () -> usersDao.findByEmail("test@test.com")); assertEquals(DatabaseError.REQUEST_REJECTED, e.getErrorKind()); verify(collection, times(1)) .find(eq(Filters.eq("_id", "test@test.com"))); }
@Override public User findByEmail(String email) { Objects.requireNonNull(email); Document doc; try { doc = mongoCollection.find(eq("_id", email)).first(); } 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 lookup.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The find request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } if (doc == null) { LOG.warn("The email {} was not found in the database.", email); throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND); } return UsersDao.fromJson(mapper, doc.getString("document")); }
MongoDbUsersDao implements UsersDao { @Override public User findByEmail(String email) { Objects.requireNonNull(email); Document doc; try { doc = mongoCollection.find(eq("_id", email)).first(); } 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 lookup.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The find request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } if (doc == null) { LOG.warn("The email {} was not found in the database.", email); throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND); } return UsersDao.fromJson(mapper, doc.getString("document")); } }
MongoDbUsersDao implements UsersDao { @Override public User findByEmail(String email) { Objects.requireNonNull(email); Document doc; try { doc = mongoCollection.find(eq("_id", email)).first(); } 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 lookup.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The find request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } if (doc == null) { LOG.warn("The email {} was not found in the database.", email); throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND); } return UsersDao.fromJson(mapper, doc.getString("document")); } @Inject  MongoDbUsersDao(MongoCollection<Document> mongoCollection, ObjectMapper mapper); }
MongoDbUsersDao implements UsersDao { @Override public User findByEmail(String email) { Objects.requireNonNull(email); Document doc; try { doc = mongoCollection.find(eq("_id", email)).first(); } 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 lookup.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The find request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } if (doc == null) { LOG.warn("The email {} was not found in the database.", email); throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND); } return UsersDao.fromJson(mapper, doc.getString("document")); } @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 findByEmail(String email) { Objects.requireNonNull(email); Document doc; try { doc = mongoCollection.find(eq("_id", email)).first(); } 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 lookup.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The find request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } if (doc == null) { LOG.warn("The email {} was not found in the database.", email); throw new DatabaseException("The user was not found.", DatabaseError.USER_NOT_FOUND); } return UsersDao.fromJson(mapper, doc.getString("document")); } @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);  }