Skip to content

DDB Enhanced: Added functional tests to assert that empty strings and… #1780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.enhanced.dynamodb.functionaltests;

import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.Expression;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse;

@RunWith(MockitoJUnitRunner.class)
public class EmptyBinaryTest {
private static final String TABLE_NAME = "TEST_TABLE";
private static final SdkBytes EMPTY_BYTES = SdkBytes.fromUtf8String("");
private static final AttributeValue EMPTY_BINARY = AttributeValue.builder().b(EMPTY_BYTES).build();

@Mock
private DynamoDbClient mockDynamoDbClient;

private DynamoDbTable<TestBean> dynamoDbTable;

@DynamoDbBean
public static class TestBean {
private String id;
private SdkBytes b;

@DynamoDbPartitionKey
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public SdkBytes getB() {
return b;
}

public void setB(SdkBytes b) {
this.b = b;
}
}

private static final TableSchema<TestBean> TABLE_SCHEMA = TableSchema.fromBean(TestBean.class);

@Before
public void initializeTable() {
DynamoDbEnhancedClient dynamoDbEnhancedClient = DynamoDbEnhancedClient.builder()
.dynamoDbClient(mockDynamoDbClient)
.build();

this.dynamoDbTable = dynamoDbEnhancedClient.table(TABLE_NAME, TABLE_SCHEMA);
}

@Test
public void putEmptyBytes() {
TestBean testBean = new TestBean();
testBean.setId("id123");
testBean.setB(EMPTY_BYTES);

dynamoDbTable.putItem(testBean);

Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s("id123").build());
expectedItemMap.put("b", EMPTY_BINARY);

PutItemRequest expectedRequest = PutItemRequest.builder()
.tableName(TABLE_NAME)
.item(expectedItemMap)
.build();

verify(mockDynamoDbClient).putItem(expectedRequest);
}

@Test
public void getEmptyBytes() {
Map<String, AttributeValue> itemMap = new HashMap<>();
itemMap.put("id", AttributeValue.builder().s("id123").build());
itemMap.put("b", EMPTY_BINARY);

GetItemResponse response = GetItemResponse.builder()
.item(itemMap)
.build();

when(mockDynamoDbClient.getItem(any(GetItemRequest.class))).thenReturn(response);

TestBean result = dynamoDbTable.getItem(r -> r.key(k -> k.partitionValue("id123")));

assertThat(result.getId()).isEqualTo("id123");
assertThat(result.getB()).isEqualTo(EMPTY_BYTES);
}

@Test
public void updateEmptyBytesWithCondition() {
Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s("id123").build());
expectedItemMap.put("b", EMPTY_BINARY);
TestBean testBean = new TestBean();
testBean.setId("id123");
testBean.setB(EMPTY_BYTES);

UpdateItemResponse response = UpdateItemResponse.builder()
.attributes(expectedItemMap)
.build();
when(mockDynamoDbClient.updateItem(any(UpdateItemRequest.class))).thenReturn(response);

Expression conditionExpression = Expression.builder()
.expression("#attr = :val")
.expressionNames(singletonMap("#attr", "b"))
.expressionValues(singletonMap(":val", EMPTY_BINARY))
.build();

TestBean result = dynamoDbTable.updateItem(r -> r.item(testBean).conditionExpression(conditionExpression));

Map<String, String> expectedExpressionAttributeNames = new HashMap<>();
expectedExpressionAttributeNames.put("#AMZN_MAPPED_b", "b");
expectedExpressionAttributeNames.put("#attr", "b");
Map<String, AttributeValue> expectedExpressionAttributeValues = new HashMap<>();
expectedExpressionAttributeValues.put(":AMZN_MAPPED_b", EMPTY_BINARY);
expectedExpressionAttributeValues.put(":val", EMPTY_BINARY);
Map<String, AttributeValue> expectedKeyMap = new HashMap<>();
expectedKeyMap.put("id", AttributeValue.builder().s("id123").build());

UpdateItemRequest expectedRequest =
UpdateItemRequest.builder()
.tableName(TABLE_NAME)
.key(expectedKeyMap)
.returnValues(ReturnValue.ALL_NEW)
.updateExpression("SET #AMZN_MAPPED_b = :AMZN_MAPPED_b")
.conditionExpression("#attr = :val")
.expressionAttributeNames(expectedExpressionAttributeNames)
.expressionAttributeValues(expectedExpressionAttributeValues)
.build();

verify(mockDynamoDbClient).updateItem(expectedRequest);
assertThat(result.getId()).isEqualTo("id123");
assertThat(result.getB()).isEqualTo(EMPTY_BYTES);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.enhanced.dynamodb.functionaltests;

import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.Expression;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse;

@RunWith(MockitoJUnitRunner.class)
public class EmptyStringTest {
private static final String TABLE_NAME = "TEST_TABLE";
private static final AttributeValue EMPTY_STRING = AttributeValue.builder().s("").build();

@Mock
private DynamoDbClient mockDynamoDbClient;

private DynamoDbTable<TestBean> dynamoDbTable;

@DynamoDbBean
public static class TestBean {
private String id;
private String s;

@DynamoDbPartitionKey
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getS() {
return s;
}

public void setS(String s) {
this.s = s;
}
}

private static final TableSchema<TestBean> TABLE_SCHEMA = TableSchema.fromBean(TestBean.class);

@Before
public void initializeTable() {
DynamoDbEnhancedClient dynamoDbEnhancedClient = DynamoDbEnhancedClient.builder()
.dynamoDbClient(mockDynamoDbClient)
.build();

this.dynamoDbTable = dynamoDbEnhancedClient.table(TABLE_NAME, TABLE_SCHEMA);
}

@Test
public void putEmptyString() {
TestBean testBean = new TestBean();
testBean.setId("id123");
testBean.setS("");

dynamoDbTable.putItem(testBean);

Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s("id123").build());
expectedItemMap.put("s", EMPTY_STRING);

PutItemRequest expectedRequest = PutItemRequest.builder()
.tableName(TABLE_NAME)
.item(expectedItemMap)
.build();

verify(mockDynamoDbClient).putItem(expectedRequest);
}

@Test
public void getEmptyString() {
Map<String, AttributeValue> itemMap = new HashMap<>();
itemMap.put("id", AttributeValue.builder().s("id123").build());
itemMap.put("s", EMPTY_STRING);

GetItemResponse response = GetItemResponse.builder()
.item(itemMap)
.build();

when(mockDynamoDbClient.getItem(any(GetItemRequest.class))).thenReturn(response);

TestBean result = dynamoDbTable.getItem(r -> r.key(k -> k.partitionValue("id123")));

assertThat(result.getId()).isEqualTo("id123");
assertThat(result.getS()).isEmpty();
}

@Test
public void updateEmptyStringWithCondition() {
Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s("id123").build());
expectedItemMap.put("s", EMPTY_STRING);
TestBean testBean = new TestBean();
testBean.setId("id123");
testBean.setS("");

UpdateItemResponse response = UpdateItemResponse.builder()
.attributes(expectedItemMap)
.build();
when(mockDynamoDbClient.updateItem(any(UpdateItemRequest.class))).thenReturn(response);

Expression conditionExpression = Expression.builder()
.expression("#attr = :val")
.expressionNames(singletonMap("#attr", "s"))
.expressionValues(singletonMap(":val", EMPTY_STRING))
.build();

TestBean result = dynamoDbTable.updateItem(r -> r.item(testBean).conditionExpression(conditionExpression));

Map<String, String> expectedExpressionAttributeNames = new HashMap<>();
expectedExpressionAttributeNames.put("#AMZN_MAPPED_s", "s");
expectedExpressionAttributeNames.put("#attr", "s");
Map<String, AttributeValue> expectedExpressionAttributeValues = new HashMap<>();
expectedExpressionAttributeValues.put(":AMZN_MAPPED_s", EMPTY_STRING);
expectedExpressionAttributeValues.put(":val", EMPTY_STRING);
Map<String, AttributeValue> expectedKeyMap = new HashMap<>();
expectedKeyMap.put("id", AttributeValue.builder().s("id123").build());

UpdateItemRequest expectedRequest =
UpdateItemRequest.builder()
.tableName(TABLE_NAME)
.key(expectedKeyMap)
.returnValues(ReturnValue.ALL_NEW)
.updateExpression("SET #AMZN_MAPPED_s = :AMZN_MAPPED_s")
.conditionExpression("#attr = :val")
.expressionAttributeNames(expectedExpressionAttributeNames)
.expressionAttributeValues(expectedExpressionAttributeValues)
.build();

verify(mockDynamoDbClient).updateItem(expectedRequest);
assertThat(result.getId()).isEqualTo("id123");
assertThat(result.getS()).isEmpty();
}
}