@Test void testUnsuccessfulFindByEmailEmptyItem() { DynamoDbClient client = mock(DynamoDbClient.class); UsersDao usersDao = new DynamoDbUsersDao(client, "testTable", MAPPER); when(client.getItem(eq(GET_REQUEST))) .thenReturn(GetItemResponse.builder().item(Collections.emptyMap()) .build()); DatabaseException e = assertThrows(DatabaseException.class, () -> usersDao.findByEmail("test@test.com")); assertEquals(DatabaseError.USER_NOT_FOUND, e.getErrorKind()); verify(client, times(1)).getItem(eq(GET_REQUEST)); }
@Override public User findByEmail(String email) { Objects.requireNonNull(email); GetItemRequest request = GetItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .build(); GetItemResponse response; try { response = dynamoDbClient.getItem(request); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } if (response.item().size() <= 0) { 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, response.item().get("document").s()); }
DynamoDbUsersDao implements UsersDao { @Override public User findByEmail(String email) { Objects.requireNonNull(email); GetItemRequest request = GetItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .build(); GetItemResponse response; try { response = dynamoDbClient.getItem(request); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } if (response.item().size() <= 0) { 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, response.item().get("document").s()); } }
DynamoDbUsersDao implements UsersDao { @Override public User findByEmail(String email) { Objects.requireNonNull(email); GetItemRequest request = GetItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .build(); GetItemResponse response; try { response = dynamoDbClient.getItem(request); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } if (response.item().size() <= 0) { 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, response.item().get("document").s()); } @Inject  DynamoDbUsersDao(DynamoDbClient dynamoDbClient, String tableName, ObjectMapper mapper); }
DynamoDbUsersDao implements UsersDao { @Override public User findByEmail(String email) { Objects.requireNonNull(email); GetItemRequest request = GetItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .build(); GetItemResponse response; try { response = dynamoDbClient.getItem(request); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } if (response.item().size() <= 0) { 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, response.item().get("document").s()); } @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 findByEmail(String email) { Objects.requireNonNull(email); GetItemRequest request = GetItemRequest.builder() .tableName(tableName) .key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())) .build(); GetItemResponse response; try { response = dynamoDbClient.getItem(request); } catch (SdkException e) { LOG.error("The database is currently unresponsive.", e); throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN); } if (response.item().size() <= 0) { 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, response.item().get("document").s()); } @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);  }