@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)); DatabaseException e = assertThrows(DatabaseException.class, () -> usersDao.insert(USER)); assertEquals(DatabaseError.DATABASE_DOWN, e.getErrorKind()); 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"))); }
@Override public User insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Document doc = new Document("_id", user.getEmail().getAddress()) .append("id", UUID.randomUUID().toString()) .append("version", UUID.randomUUID().toString()) .append("creation_time", String.valueOf(now)) .append("update_time", String.valueOf(now)) .append("document", UsersDao.toJson(mapper, user)); try { mongoCollection.insertOne(doc); } catch (MongoWriteException e) { switch (e.getError().getCategory()) { case DUPLICATE_KEY: LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); case EXECUTION_TIMEOUT: LOG.error("The insert operation for user {} timed out.", user.getEmail(), e); throw new DatabaseException("The insert operation timed out.", DatabaseError.DATABASE_DOWN); case UNCATEGORIZED: default: LOG.error("The insert for {} was rejected for an unknown reason.", user.getEmail(), e); throw new DatabaseException("The insert request was rejected for an unknown reason.", DatabaseError.REQUEST_REJECTED); } } 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 insert.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The insert request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; }
MongoDbUsersDao implements UsersDao { @Override public User insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Document doc = new Document("_id", user.getEmail().getAddress()) .append("id", UUID.randomUUID().toString()) .append("version", UUID.randomUUID().toString()) .append("creation_time", String.valueOf(now)) .append("update_time", String.valueOf(now)) .append("document", UsersDao.toJson(mapper, user)); try { mongoCollection.insertOne(doc); } catch (MongoWriteException e) { switch (e.getError().getCategory()) { case DUPLICATE_KEY: LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); case EXECUTION_TIMEOUT: LOG.error("The insert operation for user {} timed out.", user.getEmail(), e); throw new DatabaseException("The insert operation timed out.", DatabaseError.DATABASE_DOWN); case UNCATEGORIZED: default: LOG.error("The insert for {} was rejected for an unknown reason.", user.getEmail(), e); throw new DatabaseException("The insert request was rejected for an unknown reason.", DatabaseError.REQUEST_REJECTED); } } 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 insert.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The insert request was rejected for the following reason: " + e.getErrorMessage(), DatabaseError.REQUEST_REJECTED); } return user; } }
MongoDbUsersDao implements UsersDao { @Override public User insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Document doc = new Document("_id", user.getEmail().getAddress()) .append("id", UUID.randomUUID().toString()) .append("version", UUID.randomUUID().toString()) .append("creation_time", String.valueOf(now)) .append("update_time", String.valueOf(now)) .append("document", UsersDao.toJson(mapper, user)); try { mongoCollection.insertOne(doc); } catch (MongoWriteException e) { switch (e.getError().getCategory()) { case DUPLICATE_KEY: LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); case EXECUTION_TIMEOUT: LOG.error("The insert operation for user {} timed out.", user.getEmail(), e); throw new DatabaseException("The insert operation timed out.", DatabaseError.DATABASE_DOWN); case UNCATEGORIZED: default: LOG.error("The insert for {} was rejected for an unknown reason.", user.getEmail(), e); throw new DatabaseException("The insert request was rejected for an unknown reason.", DatabaseError.REQUEST_REJECTED); } } 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 insert.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The insert 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 insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Document doc = new Document("_id", user.getEmail().getAddress()) .append("id", UUID.randomUUID().toString()) .append("version", UUID.randomUUID().toString()) .append("creation_time", String.valueOf(now)) .append("update_time", String.valueOf(now)) .append("document", UsersDao.toJson(mapper, user)); try { mongoCollection.insertOne(doc); } catch (MongoWriteException e) { switch (e.getError().getCategory()) { case DUPLICATE_KEY: LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); case EXECUTION_TIMEOUT: LOG.error("The insert operation for user {} timed out.", user.getEmail(), e); throw new DatabaseException("The insert operation timed out.", DatabaseError.DATABASE_DOWN); case UNCATEGORIZED: default: LOG.error("The insert for {} was rejected for an unknown reason.", user.getEmail(), e); throw new DatabaseException("The insert request was rejected for an unknown reason.", DatabaseError.REQUEST_REJECTED); } } 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 insert.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The insert 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 insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Document doc = new Document("_id", user.getEmail().getAddress()) .append("id", UUID.randomUUID().toString()) .append("version", UUID.randomUUID().toString()) .append("creation_time", String.valueOf(now)) .append("update_time", String.valueOf(now)) .append("document", UsersDao.toJson(mapper, user)); try { mongoCollection.insertOne(doc); } catch (MongoWriteException e) { switch (e.getError().getCategory()) { case DUPLICATE_KEY: LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); case EXECUTION_TIMEOUT: LOG.error("The insert operation for user {} timed out.", user.getEmail(), e); throw new DatabaseException("The insert operation timed out.", DatabaseError.DATABASE_DOWN); case UNCATEGORIZED: default: LOG.error("The insert for {} was rejected for an unknown reason.", user.getEmail(), e); throw new DatabaseException("The insert request was rejected for an unknown reason.", DatabaseError.REQUEST_REJECTED); } } 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 insert.", e.getErrorCode(), e.getErrorMessage(), e); throw new DatabaseException("The insert 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);  }