1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.services.dynamodb; 17 18 import static org.assertj.core.api.Assertions.assertThat; 19 20 import java.util.concurrent.CompletableFuture; 21 import org.assertj.core.api.Condition; 22 import org.junit.AfterClass; 23 import org.junit.BeforeClass; 24 import org.junit.Test; 25 import software.amazon.awssdk.core.waiters.WaiterResponse; 26 import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; 27 import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; 28 import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; 29 import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; 30 import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; 31 import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; 32 import software.amazon.awssdk.services.dynamodb.model.KeyType; 33 import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; 34 import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; 35 import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; 36 import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbAsyncWaiter; 37 import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter; 38 import utils.resources.tables.BasicTempTable; 39 import utils.test.util.DynamoDBTestBase; 40 41 public class WaitersIntegrationTest extends DynamoDBTestBase { 42 43 private static final String TABLE_NAME = "java-sdk-waiter-test" + System.currentTimeMillis(); 44 private static final String HASH_KEY_NAME = BasicTempTable.HASH_KEY_NAME; 45 private static DynamoDbAsyncClient dynamoAsync; 46 47 @BeforeClass setUp()48 public static void setUp() { 49 DynamoDBTestBase.setUpTestBase(); 50 51 dynamoAsync = DynamoDbAsyncClient.builder().region(REGION).credentialsProvider(CREDENTIALS_PROVIDER_CHAIN).build(); 52 53 dynamo.createTable(CreateTableRequest.builder().tableName(TABLE_NAME) 54 .keySchema(KeySchemaElement.builder().keyType(KeyType.HASH) 55 .attributeName(HASH_KEY_NAME) 56 .build()) 57 .attributeDefinitions(AttributeDefinition.builder() 58 .attributeType(ScalarAttributeType.N) 59 .attributeName(HASH_KEY_NAME) 60 .build()) 61 .provisionedThroughput(ProvisionedThroughput.builder() 62 .readCapacityUnits(5L) 63 .writeCapacityUnits(5L) 64 .build()) 65 .build()); 66 } 67 68 @AfterClass cleanUp()69 public static void cleanUp() { 70 dynamoAsync.deleteTable(DeleteTableRequest.builder().tableName(TABLE_NAME).build()); 71 72 WaiterResponse<DescribeTableResponse> waiterResponse = 73 dynamoAsync.waiter().waitUntilTableNotExists(b -> b.tableName(TABLE_NAME)).join(); 74 75 assertThat(waiterResponse.matched().response()).isEmpty(); 76 assertThat(waiterResponse.matched().exception()).hasValueSatisfying( 77 new Condition<>(t -> t instanceof ResourceNotFoundException, "ResourceNotFoundException")); 78 79 dynamo.close(); 80 dynamoAsync.close(); 81 } 82 83 @Test checkTableExist_withSyncWaiter()84 public void checkTableExist_withSyncWaiter() { 85 DynamoDbWaiter syncWaiter = dynamo.waiter(); 86 WaiterResponse<DescribeTableResponse> response = syncWaiter.waitUntilTableExists( 87 DescribeTableRequest.builder().tableName(TABLE_NAME).build()); 88 89 assertThat(response.attemptsExecuted()).isGreaterThanOrEqualTo(1); 90 assertThat(response.matched().response()).hasValueSatisfying(b -> assertThat(b.table().tableName()).isEqualTo(TABLE_NAME)); 91 assertThat(response.matched().exception()).isEmpty(); 92 } 93 94 @Test checkTableExist_withAsyncWaiter()95 public void checkTableExist_withAsyncWaiter() { 96 DynamoDbAsyncWaiter asyncWaiter = dynamoAsync.waiter(); 97 CompletableFuture<WaiterResponse<DescribeTableResponse>> responseFuture = asyncWaiter.waitUntilTableExists( 98 DescribeTableRequest.builder().tableName(TABLE_NAME).build()); 99 100 WaiterResponse<DescribeTableResponse> response = responseFuture.join(); 101 102 assertThat(response.attemptsExecuted()).isGreaterThanOrEqualTo(1); 103 assertThat(response.matched().response()).hasValueSatisfying(b -> assertThat(b.table().tableName()).isEqualTo(TABLE_NAME)); 104 assertThat(response.matched().exception()).isEmpty(); 105 } 106 }