From 21be02e829016037e94acf0d67f473cb4209a353 Mon Sep 17 00:00:00 2001 From: Oleksii Ivanov Date: Tue, 18 Apr 2023 20:15:44 +0300 Subject: [PATCH] chore(dynamodb): add unit tests for DynamoDB connector --- .../operation/item/GetItemOperation.java | 6 +- .../dynamodb/BaseDynamoDbOperationTest.java | 53 +++++++++ .../operation/item/AddItemOperationTest.java | 45 ++++++++ .../item/DeleteItemOperationTest.java | 58 ++++++++++ .../operation/item/GetItemOperationTest.java | 79 +++++++++++++ .../item/UpdateItemOperationTest.java | 87 ++++++++++++++ .../table/CreateTableOperationTest.java | 106 ++++++++++++++++++ .../table/DeleteTableOperationTest.java | 38 +++++++ .../table/DescribeTableOperationTest.java | 32 ++++++ .../table/ScanTableOperationTest.java | 97 ++++++++++++++++ 10 files changed, 600 insertions(+), 1 deletion(-) create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/BaseDynamoDbOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/AddItemOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/DeleteItemOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/UpdateItemOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/CreateTableOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DeleteTableOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DescribeTableOperationTest.java create mode 100644 connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/ScanTableOperationTest.java diff --git a/connectors/aws/src/main/java/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperation.java b/connectors/aws/src/main/java/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperation.java index 4ad61d5245..fc8aa28b7d 100644 --- a/connectors/aws/src/main/java/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperation.java +++ b/connectors/aws/src/main/java/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperation.java @@ -7,6 +7,7 @@ package io.camunda.connector.aws.dynamodb.operation.item; import com.amazonaws.services.dynamodbv2.document.DynamoDB; +import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.PrimaryKey; import com.google.gson.Gson; import com.google.gson.JsonObject; @@ -27,7 +28,10 @@ public GetItemOperation(final GetItem getItemModel) { @Override public Object invoke(final DynamoDB dynamoDB) { - return dynamoDB.getTable(getItemModel.getTableName()).getItem(createPrimaryKey()).attributes(); + return Optional.ofNullable( + dynamoDB.getTable(getItemModel.getTableName()).getItem(createPrimaryKey())) + .map(Item::attributes) + .orElse(null); } private PrimaryKey createPrimaryKey() { diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/BaseDynamoDbOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/BaseDynamoDbOperationTest.java new file mode 100644 index 0000000000..d019f77ed4 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/BaseDynamoDbOperationTest.java @@ -0,0 +1,53 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb; + +import com.amazonaws.services.dynamodbv2.document.DynamoDB; +import com.amazonaws.services.dynamodbv2.document.Table; +import com.amazonaws.services.dynamodbv2.model.TableDescription; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.util.Map; + +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public abstract class BaseDynamoDbOperationTest { + @Mock + protected DynamoDB dynamoDB; + @Mock + protected Table table; + + @BeforeEach + public void beforeEach() { + when(dynamoDB.getTable(TestData.Table.NAME)).thenReturn(table); + when(table.describe()).thenReturn(new TableDescription().withTableName(TestData.Table.NAME)); + } + + public interface TestData { + interface Table { + String NAME = "my_table"; + String PARTITION_KEY = "ID"; + String PARTITION_KEY_ROLE_HASH = "HASH"; + String PARTITION_KEY_TYPE_NUMBER = "N"; + String SORT_KEY = "sortKey"; + String SORT_KEY_ROLE_RANGE = "RANGE"; + String SORT_KEY_TYPE_STRING = "S"; + Long READ_CAPACITY = 4L; + Long WRITE_CAPACITY = 5L; + String FILTER_EXPRESSION = "age >= :ageVal"; + Map EXPRESSION_ATTRIBUTE_NAMES = Map.of("#name", "name"); + Map EXPRESSION_ATTRIBUTE_VALUES = Map.of(":ageVal", 30); + } + } +} diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/AddItemOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/AddItemOperationTest.java new file mode 100644 index 0000000000..0b750c5894 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/AddItemOperationTest.java @@ -0,0 +1,45 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.item; + +import com.amazonaws.services.dynamodbv2.document.Item; +import com.amazonaws.services.dynamodbv2.document.PutItemOutcome; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.item.AddItem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +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; + +class AddItemOperationTest extends BaseDynamoDbOperationTest { + @Mock + private AddItem addItemModel; + @Mock + private Item item; + @Mock + private PutItemOutcome putItemOutcome; + + @BeforeEach + public void setUp() { + when(addItemModel.getTableName()).thenReturn(TestData.Table.NAME); + when(addItemModel.getItem()).thenReturn(item); + when(dynamoDB.getTable(addItemModel.getTableName())).thenReturn(table); + when(table.putItem(any(Item.class))).thenReturn(putItemOutcome); + } + + @Test + public void testInvoke() { + AddItemOperation addItemOperation = new AddItemOperation(addItemModel); + PutItemOutcome result = addItemOperation.invoke(dynamoDB); + verify(table).putItem(any(Item.class)); + assertThat(result).isEqualTo(putItemOutcome); + } +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/DeleteItemOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/DeleteItemOperationTest.java new file mode 100644 index 0000000000..61749a9491 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/DeleteItemOperationTest.java @@ -0,0 +1,58 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.item; + +import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome; +import com.amazonaws.services.dynamodbv2.document.KeyAttribute; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.item.DeleteItem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + + +class DeleteItemOperationTest extends BaseDynamoDbOperationTest { + + private DeleteItemOperation deleteItemOperation; + @Mock + private DeleteItemOutcome deleteItemOutcome; + @Captor + private ArgumentCaptor keyAttributeArgumentCaptor; + + @BeforeEach + public void setup() { + Map primaryKeyComponents = new HashMap<>(); + primaryKeyComponents.put("id", "1234"); + DeleteItem deleteItem = new DeleteItem(); + deleteItem.setTableName(TestData.Table.NAME); + deleteItem.setPrimaryKeyComponents(primaryKeyComponents); + this.deleteItemOperation = new DeleteItemOperation(deleteItem); + } + + @Test + public void testInvoke() { + //Given + when(table.deleteItem(keyAttributeArgumentCaptor.capture())).thenReturn(deleteItemOutcome); + //When + Object result = this.deleteItemOperation.invoke(dynamoDB); + //Then + assertThat(result).isEqualTo(deleteItemOutcome); + KeyAttribute keyAttribute = keyAttributeArgumentCaptor.getValue(); + assertThat(keyAttribute.getName()).isEqualTo("id"); + assertThat(keyAttribute.getValue()).isEqualTo("1234"); + + } + +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperationTest.java new file mode 100644 index 0000000000..c417ef7e88 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/GetItemOperationTest.java @@ -0,0 +1,79 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.item; + +import com.amazonaws.services.dynamodbv2.document.Item; +import com.amazonaws.services.dynamodbv2.document.KeyAttribute; +import com.amazonaws.services.dynamodbv2.document.PrimaryKey; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.item.GetItem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class GetItemOperationTest extends BaseDynamoDbOperationTest { + + private GetItemOperation getItemOperation; + @Captor + private ArgumentCaptor keyAttributesCaptor; + + @BeforeEach + public void setup() { + GetItem getItem = new GetItem(); + getItem.setTableName(TestData.Table.NAME); + getItem.setPrimaryKeyComponents(Map.of("id", "1", "type", "user")); + getItemOperation = new GetItemOperation(getItem); + } + + @SuppressWarnings("unchecked") + @Test + void invoke_shouldReturnItemAttributes_whenItemExists() { + // Given + Item mockItem = Item.fromMap(Map.of("id", "1", "type", "user", "name", "Alice")); + when(table.getItem(keyAttributesCaptor.capture())).thenReturn(mockItem); + mockItem.attributes(); + + // When + Iterable> result = (Iterable>) getItemOperation.invoke(dynamoDB); + + // Then + verify(dynamoDB, times(1)).getTable(TestData.Table.NAME); + verify(table, times(1)).getItem(any(PrimaryKey.class)); + List keyAttributeList = List.of(keyAttributesCaptor.getValue()); + assertThat(keyAttributeList) + .asList() + .contains(new KeyAttribute("id", "1"), new KeyAttribute("type", "user")); + assertThat(result).isEqualTo(mockItem.attributes()); + } + + @SuppressWarnings("unchecked") + @Test + void invoke_shouldReturnNull_whenItemDoesNotExist() { + // Given + when(table.getItem(any(KeyAttribute.class), any(KeyAttribute.class))).thenReturn(null); + + // When + Map result = (Map) getItemOperation.invoke(dynamoDB); + + // Then + verify(dynamoDB, times(1)).getTable(TestData.Table.NAME); + verify(table, times(1)).getItem(any(PrimaryKey.class)); + assertThat(result).isNull(); + } + + +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/UpdateItemOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/UpdateItemOperationTest.java new file mode 100644 index 0000000000..6a6e9b8741 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/item/UpdateItemOperationTest.java @@ -0,0 +1,87 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.item; + +import com.amazonaws.services.dynamodbv2.document.AttributeUpdate; +import com.amazonaws.services.dynamodbv2.document.KeyAttribute; +import com.amazonaws.services.dynamodbv2.document.PrimaryKey; +import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.item.UpdateItem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; + +class UpdateItemOperationTest extends BaseDynamoDbOperationTest { + private UpdateItemOperation updateItemOperation; + private UpdateItem updateItem; + @Mock + private UpdateItemOutcome updateItemOutcome; + @Captor + private ArgumentCaptor primaryKeyArgumentCaptor; + @Captor + private ArgumentCaptor attributeUpdateArgumentCaptor; + private KeyAttribute keyAttribute; + private AttributeUpdate attributeUpdate; + + @BeforeEach + public void setUp() { + + keyAttribute = new KeyAttribute("id", "123"); + attributeUpdate = new AttributeUpdate("name").addElements("John Doe"); + + Map primaryKey = Map.of(keyAttribute.getName(), keyAttribute.getValue()); + Map attributeUpdates = Map.of(attributeUpdate.getAttributeName(), "John Doe"); + + updateItem = new UpdateItem(); + updateItem.setTableName(TestData.Table.NAME); + updateItem.setAttributeAction("PUT"); + updateItem.setKeyAttributes(attributeUpdates); + updateItem.setPrimaryKeyComponents(primaryKey); + + updateItemOperation = new UpdateItemOperation(updateItem); + } + + @Test + public void testInvoke() { + // Given + when(table.updateItem( + primaryKeyArgumentCaptor.capture(), + attributeUpdateArgumentCaptor.capture() + )).thenReturn(updateItemOutcome); + // When + Object result = updateItemOperation.invoke(dynamoDB); + // Then + assertThat(result).isInstanceOf(UpdateItemOutcome.class); + assertThat(((UpdateItemOutcome) result).getItem()).isEqualTo(updateItemOutcome.getItem()); + + assertThat(primaryKeyArgumentCaptor.getValue().getComponents()).contains(keyAttribute); + assertThat(attributeUpdateArgumentCaptor.getValue().getAttributeName()).isEqualTo(attributeUpdate.getAttributeName()); + } + + @Test + public void invoke_shouldThrowExceptionWhenUpdateActionIsInvalid() { + //Given + updateItem.setAttributeAction("ADD"); + //When and Then + IllegalArgumentException thrown = + assertThrows( + IllegalArgumentException.class, + () -> updateItemOperation.invoke(dynamoDB), + "IllegalArgumentException was expected"); + assertThat(thrown.getMessage()).contains("Unsupported action [ADD]"); + + } +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/CreateTableOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/CreateTableOperationTest.java new file mode 100644 index 0000000000..094fe4e1bd --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/CreateTableOperationTest.java @@ -0,0 +1,106 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.table; + +import com.amazonaws.services.dynamodbv2.model.BillingMode; +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; +import com.amazonaws.services.dynamodbv2.model.TableDescription; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.table.CreateTable; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class CreateTableOperationTest extends BaseDynamoDbOperationTest { + + private CreateTable createTable; + + @Captor + private ArgumentCaptor requestArgumentCaptor; + + @BeforeEach + public void init() throws InterruptedException { + createTable = new CreateTable(); + createTable.setTableName(TestData.Table.NAME); + //Partition key + createTable.setPartitionKey(TestData.Table.PARTITION_KEY); + createTable.setPartitionKeyRole(TestData.Table.PARTITION_KEY_ROLE_HASH); + createTable.setPartitionKeyType(TestData.Table.PARTITION_KEY_TYPE_NUMBER); + //Sort key + createTable.setSortKey(TestData.Table.SORT_KEY); + createTable.setSortKeyRole(TestData.Table.SORT_KEY_ROLE_RANGE); + createTable.setSortKeyType(TestData.Table.SORT_KEY_TYPE_STRING); + createTable.setDeletionProtection(true); + createTable.setBillingModeStr(BillingMode.PROVISIONED.name()); + createTable.setReadCapacityUnits(TestData.Table.READ_CAPACITY); + createTable.setWriteCapacityUnits(TestData.Table.WRITE_CAPACITY); + + when(dynamoDB.createTable(requestArgumentCaptor.capture())).thenReturn(table); + when(table.waitForActive()).thenReturn(new TableDescription().withTableName(TestData.Table.NAME)); + + + } + + @Test + public void invoke_shouldCreateTableWithPartitionKeyAndAllOptionalKeys() throws InterruptedException { + //Given + createTable.setSortKey(null); + createTable.setSortKeyRole(null); + createTable.setSortKeyType(null); + createTable.setBillingModeStr(BillingMode.PROVISIONED.name()); + createTable.setReadCapacityUnits(null); + createTable.setWriteCapacityUnits(null); + + CreateTableOperation operation = new CreateTableOperation(createTable); + //When + Object invoke = operation.invoke(dynamoDB); + //Then + verify(table, times(1)).waitForActive(); + + assertThat(invoke).isNotNull(); + + CreateTableRequest value = requestArgumentCaptor.getValue(); + + assertThat(value.getTableName()).isEqualTo(TestData.Table.NAME); + assertThat(value.getKeySchema().get(0).getAttributeName()).isEqualTo(TestData.Table.PARTITION_KEY); + assertThat(value.getKeySchema().get(0).getKeyType()).isEqualTo(TestData.Table.PARTITION_KEY_ROLE_HASH); + assertThat(value.getDeletionProtectionEnabled()).isTrue(); + assertThat(value.getBillingMode()).isEqualTo(BillingMode.PROVISIONED.name()); + } + + + @Test + public void invoke_shouldCreateTableWithOutOptionalProperties() throws InterruptedException { + //Given + CreateTableOperation operation = new CreateTableOperation(createTable); + //When + Object invoke = operation.invoke(dynamoDB); + //Then + verify(table, times(1)).waitForActive(); + + assertThat(invoke).isNotNull(); + + CreateTableRequest value = requestArgumentCaptor.getValue(); + + assertThat(value.getTableName()).isEqualTo(TestData.Table.NAME); + assertThat(value.getKeySchema().get(0).getAttributeName()).isEqualTo(TestData.Table.PARTITION_KEY); + assertThat(value.getKeySchema().get(0).getKeyType()).isEqualTo(TestData.Table.PARTITION_KEY_ROLE_HASH); + assertThat(value.getKeySchema().get(1).getAttributeName()).isEqualTo(TestData.Table.SORT_KEY); + assertThat(value.getKeySchema().get(1).getKeyType()).isEqualTo(TestData.Table.SORT_KEY_ROLE_RANGE); + assertThat(value.getDeletionProtectionEnabled()).isTrue(); + assertThat(value.getBillingMode()).isEqualTo(BillingMode.PROVISIONED.name()); + assertThat(value.getProvisionedThroughput().getReadCapacityUnits()).isEqualTo(TestData.Table.READ_CAPACITY); + assertThat(value.getProvisionedThroughput().getWriteCapacityUnits()).isEqualTo(TestData.Table.WRITE_CAPACITY); + } + +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DeleteTableOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DeleteTableOperationTest.java new file mode 100644 index 0000000000..4dfff6c900 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DeleteTableOperationTest.java @@ -0,0 +1,38 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.table; + +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.AwsDynamoDbResult; +import io.camunda.connector.aws.dynamodb.model.table.DeleteTable; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class DeleteTableOperationTest extends BaseDynamoDbOperationTest { + + @Test + public void invoke_shouldDeleteDynamoDbTableAndReturnStatusOk() throws InterruptedException { + //Given + DeleteTable deleteTable = new DeleteTable(); + deleteTable.setTableName(TestData.Table.NAME); + DeleteTableOperation operation = new DeleteTableOperation(deleteTable); + //When + Object invoke = operation.invoke(dynamoDB); + //Then + verify(table, times(1)).delete(); + verify(table, times(1)).waitForDelete(); + + assertThat(invoke).isNotNull(); + AwsDynamoDbResult result = (AwsDynamoDbResult) invoke; + assertThat(result.getAction()).isEqualTo("delete Table [" + TestData.Table.NAME + "]"); + assertThat(result.getStatus()).isEqualTo("OK"); + } + +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DescribeTableOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DescribeTableOperationTest.java new file mode 100644 index 0000000000..8d1d4921e1 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/DescribeTableOperationTest.java @@ -0,0 +1,32 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.table; + +import com.amazonaws.services.dynamodbv2.model.TableDescription; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.table.DescribeTable; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class DescribeTableOperationTest extends BaseDynamoDbOperationTest { + + @Test + public void invoke_shouldReturnDescribeTableResult() { + //Given + DescribeTable describeTable = new DescribeTable(); + describeTable.setTableName(TestData.Table.NAME); + DescribeTableOperation operation = new DescribeTableOperation(describeTable); + //When + Object invoke = operation.invoke(dynamoDB); + //Then + assertThat(invoke).isNotNull(); + TableDescription result = (TableDescription) invoke; + assertThat(result.getTableName()).isEqualTo(TestData.Table.NAME); + } + +} \ No newline at end of file diff --git a/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/ScanTableOperationTest.java b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/ScanTableOperationTest.java new file mode 100644 index 0000000000..7320892297 --- /dev/null +++ b/connectors/aws/src/test/io/camunda/connector/aws/dynamodb/operation/table/ScanTableOperationTest.java @@ -0,0 +1,97 @@ +/* + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. Licensed under a proprietary license. + * See the License.txt file for more information. You may not use this file + * except in compliance with the proprietary license. + */ +package io.camunda.connector.aws.dynamodb.operation.table; + +import com.amazonaws.services.dynamodbv2.document.Item; +import com.amazonaws.services.dynamodbv2.document.ItemCollection; +import com.amazonaws.services.dynamodbv2.document.ScanOutcome; +import com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport; +import io.camunda.connector.aws.dynamodb.BaseDynamoDbOperationTest; +import io.camunda.connector.aws.dynamodb.model.AwsDynamoDbResult; +import io.camunda.connector.aws.dynamodb.model.table.ScanTable; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +class ScanTableOperationTest extends BaseDynamoDbOperationTest { + + @Mock + private ItemCollection itemCollection; + @Mock + private IteratorSupport iterator; + private ScanTableOperation scanTableOperation; + private ScanTable scanTable; + + @SuppressWarnings("unchecked") + private static void assertThatResultIsOk(final AwsDynamoDbResult result) { + assertThat(result.getAction()).isEqualTo("scanTable"); + assertThat(result.getStatus()).isEqualTo("OK"); + final List> items = (List>) result.getResponse(); + assertThat(items.size()).isEqualTo(2); + assertThat(items.get(0).get("id")).isEqualTo("123"); + assertThat(items.get(0).get("name")).isEqualTo("John"); + assertThat(items.get(1).get("id")).isEqualTo("456"); + assertThat(items.get(1).get("name")).isEqualTo("Jane"); + } + + @BeforeEach + public void setUp() { + + List itemList = new ArrayList<>(); + itemList.add(new Item().withPrimaryKey("id", "123").withString("name", "John").withNumber("age", 30)); + itemList.add(new Item().withPrimaryKey("id", "456").withString("name", "Jane").withNumber("age", 35)); + when(iterator.hasNext()).thenReturn(true, true, false); + when(iterator.next()).thenReturn(itemList.get(0), itemList.get(1)); + when(itemCollection.iterator()).thenReturn(iterator); + + scanTable = new ScanTable(); + scanTable.setTableName(TestData.Table.NAME); + scanTable.setFilterExpression(TestData.Table.FILTER_EXPRESSION); + scanTable.setExpressionAttributeNames(TestData.Table.EXPRESSION_ATTRIBUTE_NAMES); + scanTable.setExpressionAttributeValues(TestData.Table.EXPRESSION_ATTRIBUTE_VALUES); + + } + + @Test + public void invoke_shouldScanTableWithoutFilter() { + // Given + scanTable.setFilterExpression(null); + scanTable.setExpressionAttributeValues(null); + scanTable.setProjectionExpression(null); + scanTable.setExpressionAttributeNames(null); + when(dynamoDB.getTable(TestData.Table.NAME).scan(null, null, null, null)).thenReturn(itemCollection); + scanTableOperation = new ScanTableOperation(scanTable); + //When + final AwsDynamoDbResult result = (AwsDynamoDbResult) scanTableOperation.invoke(dynamoDB); + //Then + assertThatResultIsOk(result); + } + + @Test + public void invoke_shouldScanTableWithFilter() { + //Given + when(dynamoDB.getTable(TestData.Table.NAME).scan(TestData.Table.FILTER_EXPRESSION, null, TestData.Table.EXPRESSION_ATTRIBUTE_NAMES, TestData.Table.EXPRESSION_ATTRIBUTE_VALUES)).thenReturn(itemCollection); + scanTableOperation = new ScanTableOperation(scanTable); + //When + final AwsDynamoDbResult result = (AwsDynamoDbResult) scanTableOperation.invoke(dynamoDB); + //Then + assertThatResultIsOk(result); + } +} + + + + + +