@Test void testConflictingInsert() { DynamoDbClient client = mock(DynamoDbClient.class); UsersDao usersDao = new DynamoDbUsersDao(client, "testTable", MAPPER); when(client.putItem(any(PutItemRequest.class))) .thenThrow(ConditionalCheckFailedException.class); DatabaseException e = assertThrows(DatabaseException.class, () -> usersDao.insert(USER)); assertEquals(DatabaseError.CONFLICT, e.getErrorKind()); verify(client, times(1)).putItem(any(PutItemRequest.class)); }
@Override public User insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Map<String, AttributeValue> item = new HashMap<>(); item.put("email", AttributeValue.builder().s(user.getEmail().getAddress()).build()); item.put("id", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("version", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("creation_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("update_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("document", AttributeValue.builder().s(UsersDao.toJson(mapper, user)).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(item) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder().exists(false).build())) .build(); try { dynamoDbClient.putItem(putItemRequest); } catch (ConditionalCheckFailedException e) { LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); } catch (AwsServiceException e) { LOG.error("The database rejected the create request.", e); throw new DatabaseException("The database rejected the create request.", DatabaseError.REQUEST_REJECTED); } 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 insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Map<String, AttributeValue> item = new HashMap<>(); item.put("email", AttributeValue.builder().s(user.getEmail().getAddress()).build()); item.put("id", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("version", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("creation_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("update_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("document", AttributeValue.builder().s(UsersDao.toJson(mapper, user)).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(item) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder().exists(false).build())) .build(); try { dynamoDbClient.putItem(putItemRequest); } catch (ConditionalCheckFailedException e) { LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); } catch (AwsServiceException e) { LOG.error("The database rejected the create request.", e); throw new DatabaseException("The database rejected the create request.", DatabaseError.REQUEST_REJECTED); } 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 insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Map<String, AttributeValue> item = new HashMap<>(); item.put("email", AttributeValue.builder().s(user.getEmail().getAddress()).build()); item.put("id", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("version", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("creation_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("update_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("document", AttributeValue.builder().s(UsersDao.toJson(mapper, user)).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(item) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder().exists(false).build())) .build(); try { dynamoDbClient.putItem(putItemRequest); } catch (ConditionalCheckFailedException e) { LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); } catch (AwsServiceException e) { LOG.error("The database rejected the create request.", e); throw new DatabaseException("The database rejected the create request.", DatabaseError.REQUEST_REJECTED); } 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 insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Map<String, AttributeValue> item = new HashMap<>(); item.put("email", AttributeValue.builder().s(user.getEmail().getAddress()).build()); item.put("id", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("version", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("creation_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("update_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("document", AttributeValue.builder().s(UsersDao.toJson(mapper, user)).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(item) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder().exists(false).build())) .build(); try { dynamoDbClient.putItem(putItemRequest); } catch (ConditionalCheckFailedException e) { LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); } catch (AwsServiceException e) { LOG.error("The database rejected the create request.", e); throw new DatabaseException("The database rejected the create request.", DatabaseError.REQUEST_REJECTED); } 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 insert(User user) { Objects.requireNonNull(user); long now = Instant.now().toEpochMilli(); Map<String, AttributeValue> item = new HashMap<>(); item.put("email", AttributeValue.builder().s(user.getEmail().getAddress()).build()); item.put("id", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("version", AttributeValue.builder().s(UUID.randomUUID().toString()).build()); item.put("creation_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("update_time", AttributeValue.builder().n(String.valueOf(now)).build()); item.put("document", AttributeValue.builder().s(UsersDao.toJson(mapper, user)).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(item) .expected(Collections.singletonMap("email", ExpectedAttributeValue.builder().exists(false).build())) .build(); try { dynamoDbClient.putItem(putItemRequest); } catch (ConditionalCheckFailedException e) { LOG.error("The user {} already exists in the database.", user.getEmail(), e); throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT); } catch (AwsServiceException e) { LOG.error("The database rejected the create request.", e); throw new DatabaseException("The database rejected the create request.", DatabaseError.REQUEST_REJECTED); } 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);  }