use of software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription in project scalardb by scalar-labs.
the class DynamoAdminTest method createTable_WhenMetadataTableExists_ShouldCreateOnlyTable.
private void createTable_WhenMetadataTableExists_ShouldCreateOnlyTable(Optional<String> tableMetadataNamespace) throws ExecutionException {
// Arrange
String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2", Order.DESC).addClusteringKey("c3", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.INT).addColumn("c5", DataType.BLOB).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
when(client.describeTable(any(DescribeTableRequest.class))).thenReturn(describeTableResponse);
TableDescription tableDescription = mock(TableDescription.class);
when(describeTableResponse.table()).thenReturn(tableDescription);
when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
DescribeContinuousBackupsResponse describeContinuousBackupsResponse = mock(DescribeContinuousBackupsResponse.class);
when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class))).thenReturn(describeContinuousBackupsResponse);
ContinuousBackupsDescription continuousBackupsDescription = mock(ContinuousBackupsDescription.class);
when(describeContinuousBackupsResponse.continuousBackupsDescription()).thenReturn(continuousBackupsDescription);
when(continuousBackupsDescription.continuousBackupsStatus()).thenReturn(ContinuousBackupsStatus.ENABLED);
if (tableMetadataNamespace.isPresent()) {
when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
}
Map<String, String> options = new HashMap<>();
options.put(DynamoAdmin.REQUEST_UNIT, "100");
options.put(DynamoAdmin.NO_SCALING, "true");
options.put(DynamoAdmin.NO_BACKUP, "true");
// Act
admin.createTable(NAMESPACE, TABLE, metadata, options);
// Assert
ArgumentCaptor<CreateTableRequest> createTableRequestCaptor = ArgumentCaptor.forClass(CreateTableRequest.class);
verify(client).createTable(createTableRequestCaptor.capture());
CreateTableRequest actualCreateTableRequest = createTableRequestCaptor.getValue();
List<AttributeDefinition> attributeDefinitions = actualCreateTableRequest.attributeDefinitions();
assertThat(attributeDefinitions.size()).isEqualTo(3);
assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.B);
assertThat(attributeDefinitions.get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
assertThat(attributeDefinitions.get(1).attributeType()).isEqualTo(ScalarAttributeType.B);
assertThat(attributeDefinitions.get(2).attributeName()).isEqualTo("c4");
assertThat(attributeDefinitions.get(2).attributeType()).isEqualTo(ScalarAttributeType.N);
assertThat(actualCreateTableRequest.keySchema().size()).isEqualTo(2);
assertThat(actualCreateTableRequest.keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
assertThat(actualCreateTableRequest.keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequest.keySchema().get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
assertThat(actualCreateTableRequest.keySchema().get(1).keyType()).isEqualTo(KeyType.RANGE);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().size()).isEqualTo(1);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).indexName()).isEqualTo(FULL_TABLE_NAME + ".global_index.c4");
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().size()).isEqualTo(1);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().get(0).attributeName()).isEqualTo("c4");
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).projection().projectionType()).isEqualTo(ProjectionType.ALL);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.provisionedThroughput().writeCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.provisionedThroughput().readCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.tableName()).isEqualTo(FULL_TABLE_NAME);
// for the table metadata table
ArgumentCaptor<PutItemRequest> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemRequest.class);
verify(client).putItem(putItemRequestCaptor.capture());
PutItemRequest actualPutItemRequest = putItemRequestCaptor.getValue();
assertThat(actualPutItemRequest.tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
Map<String, AttributeValue> itemValues = new HashMap<>();
itemValues.put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build());
Map<String, AttributeValue> columns = new HashMap<>();
columns.put("c1", AttributeValue.builder().s("text").build());
columns.put("c2", AttributeValue.builder().s("bigint").build());
columns.put("c3", AttributeValue.builder().s("boolean").build());
columns.put("c4", AttributeValue.builder().s("int").build());
columns.put("c5", AttributeValue.builder().s("blob").build());
columns.put("c6", AttributeValue.builder().s("double").build());
columns.put("c7", AttributeValue.builder().s("float").build());
itemValues.put(DynamoAdmin.METADATA_ATTR_COLUMNS, AttributeValue.builder().m(columns).build());
itemValues.put(DynamoAdmin.METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c1").build()).build());
itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c2").build(), AttributeValue.builder().s("c3").build()).build());
Map<String, AttributeValue> clusteringOrders = new HashMap<>();
clusteringOrders.put("c2", AttributeValue.builder().s("DESC").build());
clusteringOrders.put("c3", AttributeValue.builder().s("ASC").build());
itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(clusteringOrders).build());
itemValues.put(DynamoAdmin.METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss("c4").build());
assertThat(actualPutItemRequest.item()).isEqualTo(itemValues);
verify(applicationAutoScalingClient, never()).registerScalableTarget(any(RegisterScalableTargetRequest.class));
verify(applicationAutoScalingClient, never()).putScalingPolicy(any(PutScalingPolicyRequest.class));
verify(client, never()).updateContinuousBackups(any(UpdateContinuousBackupsRequest.class));
}
use of software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription in project scalardb by scalar-labs.
the class DynamoAdminTest method createTable_WhenMetadataTableNotExist_ShouldCreateTableAndMetadataTable.
private void createTable_WhenMetadataTableNotExist_ShouldCreateTableAndMetadataTable(Optional<String> tableMetadataNamespace) throws ExecutionException {
// Arrange
String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2", Order.ASC).addClusteringKey("c3", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
when(client.describeTable(DescribeTableRequest.builder().tableName(FULL_TABLE_NAME).build())).thenReturn(describeTableResponse);
TableDescription tableDescription = mock(TableDescription.class);
when(describeTableResponse.table()).thenReturn(tableDescription);
when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
DescribeContinuousBackupsResponse describeContinuousBackupsResponse = mock(DescribeContinuousBackupsResponse.class);
when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class))).thenReturn(describeContinuousBackupsResponse);
ContinuousBackupsDescription continuousBackupsDescription = mock(ContinuousBackupsDescription.class);
when(describeContinuousBackupsResponse.continuousBackupsDescription()).thenReturn(continuousBackupsDescription);
when(continuousBackupsDescription.continuousBackupsStatus()).thenReturn(ContinuousBackupsStatus.ENABLED);
// for the table metadata table
describeTableResponse = mock(DescribeTableResponse.class);
tableDescription = mock(TableDescription.class);
when(describeTableResponse.table()).thenReturn(tableDescription);
when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
when(client.describeTable(DescribeTableRequest.builder().tableName(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE).build())).thenThrow(ResourceNotFoundException.class).thenReturn(describeTableResponse);
if (tableMetadataNamespace.isPresent()) {
when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
}
// Act
admin.createTable(NAMESPACE, TABLE, metadata);
// Assert
ArgumentCaptor<CreateTableRequest> createTableRequestCaptor = ArgumentCaptor.forClass(CreateTableRequest.class);
verify(client, times(2)).createTable(createTableRequestCaptor.capture());
List<CreateTableRequest> actualCreateTableRequests = createTableRequestCaptor.getAllValues();
List<AttributeDefinition> attributeDefinitions = actualCreateTableRequests.get(0).attributeDefinitions();
assertThat(attributeDefinitions.size()).isEqualTo(3);
assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.B);
assertThat(attributeDefinitions.get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
assertThat(attributeDefinitions.get(1).attributeType()).isEqualTo(ScalarAttributeType.B);
assertThat(attributeDefinitions.get(2).attributeName()).isEqualTo("c4");
assertThat(attributeDefinitions.get(2).attributeType()).isEqualTo(ScalarAttributeType.B);
assertThat(actualCreateTableRequests.get(0).keySchema().size()).isEqualTo(2);
assertThat(actualCreateTableRequests.get(0).keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
assertThat(actualCreateTableRequests.get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequests.get(0).keySchema().get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
assertThat(actualCreateTableRequests.get(0).keySchema().get(1).keyType()).isEqualTo(KeyType.RANGE);
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().size()).isEqualTo(1);
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).indexName()).isEqualTo(FULL_TABLE_NAME + ".global_index.c4");
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().size()).isEqualTo(1);
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().get(0).attributeName()).isEqualTo("c4");
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).projection().projectionType()).isEqualTo(ProjectionType.ALL);
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(10);
assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(10);
assertThat(actualCreateTableRequests.get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(10);
assertThat(actualCreateTableRequests.get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(10);
assertThat(actualCreateTableRequests.get(0).tableName()).isEqualTo(FULL_TABLE_NAME);
// for the table metadata table
attributeDefinitions = actualCreateTableRequests.get(1).attributeDefinitions();
assertThat(attributeDefinitions.size()).isEqualTo(1);
assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.METADATA_ATTR_TABLE);
assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.S);
assertThat(actualCreateTableRequests.get(1).keySchema().size()).isEqualTo(1);
assertThat(actualCreateTableRequests.get(1).keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.METADATA_ATTR_TABLE);
assertThat(actualCreateTableRequests.get(1).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequests.get(1).provisionedThroughput().writeCapacityUnits()).isEqualTo(1);
assertThat(actualCreateTableRequests.get(1).provisionedThroughput().readCapacityUnits()).isEqualTo(1);
assertThat(actualCreateTableRequests.get(1).tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
ArgumentCaptor<PutItemRequest> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemRequest.class);
verify(client).putItem(putItemRequestCaptor.capture());
PutItemRequest actualPutItemRequest = putItemRequestCaptor.getValue();
assertThat(actualPutItemRequest.tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
Map<String, AttributeValue> itemValues = new HashMap<>();
itemValues.put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build());
Map<String, AttributeValue> columns = new HashMap<>();
columns.put("c1", AttributeValue.builder().s("text").build());
columns.put("c2", AttributeValue.builder().s("bigint").build());
columns.put("c3", AttributeValue.builder().s("boolean").build());
columns.put("c4", AttributeValue.builder().s("blob").build());
columns.put("c5", AttributeValue.builder().s("int").build());
columns.put("c6", AttributeValue.builder().s("double").build());
columns.put("c7", AttributeValue.builder().s("float").build());
itemValues.put(DynamoAdmin.METADATA_ATTR_COLUMNS, AttributeValue.builder().m(columns).build());
itemValues.put(DynamoAdmin.METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c1").build()).build());
itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c2").build(), AttributeValue.builder().s("c3").build()).build());
Map<String, AttributeValue> clusteringOrders = new HashMap<>();
clusteringOrders.put("c2", AttributeValue.builder().s("ASC").build());
clusteringOrders.put("c3", AttributeValue.builder().s("ASC").build());
itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(clusteringOrders).build());
itemValues.put(DynamoAdmin.METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss("c4").build());
assertThat(actualPutItemRequest.item()).isEqualTo(itemValues);
verify(applicationAutoScalingClient, times(4)).registerScalableTarget(any(RegisterScalableTargetRequest.class));
verify(applicationAutoScalingClient, times(4)).putScalingPolicy(any(PutScalingPolicyRequest.class));
ArgumentCaptor<UpdateContinuousBackupsRequest> updateContinuousBackupsRequestCaptor = ArgumentCaptor.forClass(UpdateContinuousBackupsRequest.class);
verify(client, times(2)).updateContinuousBackups(updateContinuousBackupsRequestCaptor.capture());
List<UpdateContinuousBackupsRequest> updateContinuousBackupsRequests = updateContinuousBackupsRequestCaptor.getAllValues();
assertThat(updateContinuousBackupsRequests.size()).isEqualTo(2);
assertThat(updateContinuousBackupsRequests.get(0).tableName()).isEqualTo(FULL_TABLE_NAME);
assertThat(updateContinuousBackupsRequests.get(1).tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
}
Aggregations