@Test void testUnsuccessfulDeleteGetFailure() { DynamoDbClient client = mock(DynamoDbClient.class); UsersDao usersDao = new DynamoDbUsersDao(client, "testTable", MAPPER); when(client.getItem(eq(GET_REQUEST))).thenThrow(SdkException.class); DatabaseException e = assertThrows(DatabaseException.class, () -> usersDao.delete("test@test.com")); assertEquals(DatabaseError.DATABASE_DOWN, e.getErrorKind()); verify(client, times(1)).getItem(eq(GET_REQUEST)); }
@Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder() .value(AttributeValue.builder().s(email).build()) .exists(true) .build())) .build(); try { dynamoDbClient.deleteItem(deleteItemRequest); } catch (ConditionalCheckFailedException e) { LOG.warn("The email {} was not found in the database.", email, e); throw new DatabaseException("The user to delete was not found.", DatabaseError.USER_NOT_FOUND); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } return user; }
DynamoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder() .value(AttributeValue.builder().s(email).build()) .exists(true) .build())) .build(); try { dynamoDbClient.deleteItem(deleteItemRequest); } catch (ConditionalCheckFailedException e) { LOG.warn("The email {} was not found in the database.", email, e); throw new DatabaseException("The user to delete was not found.", DatabaseError.USER_NOT_FOUND); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } return user; } }
DynamoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder() .value(AttributeValue.builder().s(email).build()) .exists(true) .build())) .build(); try { dynamoDbClient.deleteItem(deleteItemRequest); } catch (ConditionalCheckFailedException e) { LOG.warn("The email {} was not found in the database.", email, e); throw new DatabaseException("The user to delete was not found.", DatabaseError.USER_NOT_FOUND); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } return user; } @Inject  DynamoDbUsersDao(DynamoDbClient dynamoDbClient, String tableName, ObjectMapper mapper); }
DynamoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder() .value(AttributeValue.builder().s(email).build()) .exists(true) .build())) .build(); try { dynamoDbClient.deleteItem(deleteItemRequest); } catch (ConditionalCheckFailedException e) { LOG.warn("The email {} was not found in the database.", email, e); throw new DatabaseException("The user to delete was not found.", DatabaseError.USER_NOT_FOUND); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } return user; } @Inject  DynamoDbUsersDao(DynamoDbClient dynamoDbClient, String tableName, 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); }
DynamoDbUsersDao implements UsersDao { @Override public User delete(String email) { Objects.requireNonNull(email); User user = findByEmail(email); DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder() .value(AttributeValue.builder().s(email).build()) .exists(true) .build())) .build(); try { dynamoDbClient.deleteItem(deleteItemRequest); } catch (ConditionalCheckFailedException e) { LOG.warn("The email {} was not found in the database.", email, e); throw new DatabaseException("The user to delete was not found.", DatabaseError.USER_NOT_FOUND); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } return user; } @Inject  DynamoDbUsersDao(DynamoDbClient dynamoDbClient, String tableName, 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);  }