Skip to content

Commit ed1b2ca

Browse files
committed
Add functional tests and integration tests
1 parent ebf0fda commit ed1b2ca

File tree

3 files changed

+376
-0
lines changed

3 files changed

+376
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
import static org.junit.Assert.assertTrue;
20+
21+
import java.util.concurrent.CompletableFuture;
22+
import java.util.concurrent.ExecutionException;
23+
import org.junit.AfterClass;
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
import software.amazon.awssdk.core.waiters.WaiterResponse;
27+
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
28+
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
29+
import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;
30+
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
31+
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
32+
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
33+
import software.amazon.awssdk.services.dynamodb.model.KeyType;
34+
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
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
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
69+
public static void cleanUp() {
70+
dynamo.deleteTable(DeleteTableRequest.builder().tableName(TABLE_NAME).build());
71+
72+
dynamo.close();
73+
dynamoAsync.close();
74+
}
75+
76+
@Test
77+
public void checkTableExist_withSyncWaiter() {
78+
DynamoDbWaiter syncWaiter = dynamo.waiter();
79+
WaiterResponse<DescribeTableResponse> response = syncWaiter.waitUntilTableExists(
80+
DescribeTableRequest.builder().tableName(TABLE_NAME).build());
81+
82+
assertThat(response.attemptsExecuted()).isGreaterThanOrEqualTo(1);
83+
assertThat(response.matched().response().get().table().tableName()).isEqualTo(TABLE_NAME);
84+
}
85+
86+
@Test
87+
public void checkTableExist_withAsyncWaiter() throws ExecutionException, InterruptedException {
88+
DynamoDbAsyncWaiter asyncWaiter = dynamoAsync.waiter();
89+
CompletableFuture<WaiterResponse<DescribeTableResponse>> responseFuture = asyncWaiter.waitUntilTableExists(
90+
DescribeTableRequest.builder().tableName(TABLE_NAME).build());
91+
92+
responseFuture.join();
93+
94+
assertThat(responseFuture.get().attemptsExecuted()).isGreaterThanOrEqualTo(1);
95+
assertThat(responseFuture.get().matched().response().get().table().tableName()).isEqualTo(TABLE_NAME);
96+
}
97+
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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.waiters;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.Matchers.any;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.never;
22+
import static org.mockito.Mockito.verify;
23+
import static org.mockito.Mockito.when;
24+
25+
import java.util.concurrent.CompletableFuture;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.ScheduledExecutorService;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import software.amazon.awssdk.core.exception.SdkServiceException;
32+
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy;
33+
import software.amazon.awssdk.core.waiters.WaiterOverrideConfiguration;
34+
import software.amazon.awssdk.core.waiters.WaiterResponse;
35+
import software.amazon.awssdk.http.SdkHttpResponse;
36+
import software.amazon.awssdk.services.restjsonwithwaiters.RestJsonWithWaitersAsyncClient;
37+
import software.amazon.awssdk.services.restjsonwithwaiters.model.AllTypesRequest;
38+
import software.amazon.awssdk.services.restjsonwithwaiters.model.AllTypesResponse;
39+
import software.amazon.awssdk.services.restjsonwithwaiters.waiters.RestJsonWithWaitersAsyncWaiter;
40+
41+
public class WaitersAsyncFunctionalTest {
42+
43+
public RestJsonWithWaitersAsyncClient asyncClient;
44+
public RestJsonWithWaitersAsyncWaiter asyncWaiter;
45+
46+
@Before
47+
public void setup() {
48+
asyncClient = mock(RestJsonWithWaitersAsyncClient.class);
49+
asyncWaiter = RestJsonWithWaitersAsyncWaiter.builder()
50+
.client(asyncClient)
51+
.overrideConfiguration(WaiterOverrideConfiguration.builder()
52+
.maxAttempts(3)
53+
.backoffStrategy(BackoffStrategy.none())
54+
.build())
55+
.build();
56+
}
57+
58+
@After
59+
public void cleanup() {
60+
asyncClient.close();
61+
asyncWaiter.close();
62+
}
63+
64+
@Test
65+
public void allTypeOperation_withAsyncWaiter_shouldReturnResponse() throws ExecutionException, InterruptedException {
66+
AllTypesResponse response = (AllTypesResponse) AllTypesResponse.builder()
67+
.sdkHttpResponse(SdkHttpResponse.builder()
68+
.statusCode(200)
69+
.build())
70+
.build();
71+
72+
CompletableFuture<AllTypesResponse> serviceFuture = new CompletableFuture<>();
73+
74+
75+
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture);
76+
CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder()
77+
.integerMember(1)
78+
.build());
79+
serviceFuture.complete(response);
80+
81+
assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(1);
82+
assertThat(responseFuture.get().matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response));
83+
}
84+
85+
@Test
86+
public void allTypeOperationFailed_withAsyncWaiter_shouldReturnException() throws ExecutionException, InterruptedException {
87+
CompletableFuture<AllTypesResponse> serviceFuture = new CompletableFuture<>();
88+
89+
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture);
90+
CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder().build());
91+
92+
serviceFuture.completeExceptionally(SdkServiceException.builder().statusCode(200).build());
93+
94+
assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(1);
95+
assertThat(responseFuture.get().matched().exception()).hasValueSatisfying(r -> assertThat(r).isInstanceOf(SdkServiceException.class));
96+
}
97+
98+
@Test
99+
public void allTypeOperationRetry_withAsyncWaiter_shouldReturnResponseAfterException() throws ExecutionException, InterruptedException {
100+
AllTypesResponse response1 = (AllTypesResponse) AllTypesResponse.builder()
101+
.sdkHttpResponse(SdkHttpResponse.builder()
102+
.statusCode(404)
103+
.build())
104+
.build();
105+
AllTypesResponse response2 = (AllTypesResponse) AllTypesResponse.builder()
106+
.sdkHttpResponse(SdkHttpResponse.builder()
107+
.statusCode(200)
108+
.build())
109+
.build();
110+
111+
CompletableFuture<AllTypesResponse> serviceFuture1 = new CompletableFuture<>();
112+
CompletableFuture<AllTypesResponse> serviceFuture2 = new CompletableFuture<>();
113+
114+
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture1, serviceFuture2);
115+
116+
CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder().build());
117+
118+
serviceFuture1.complete(response1);
119+
serviceFuture2.complete(response2);
120+
121+
assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(2);
122+
assertThat(responseFuture.get().matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response2));
123+
}
124+
125+
@Test
126+
public void closeWaiterCreatedWithClient_clientDoesNotClose() {
127+
asyncWaiter.close();
128+
verify(asyncClient, never()).close();
129+
}
130+
131+
@Test
132+
public void closeWaiterCreatedWithExecutorService_executorServiceDoesNotClose() {
133+
ScheduledExecutorService executorService = mock(ScheduledExecutorService.class);
134+
RestJsonWithWaitersAsyncWaiter newWaiter = RestJsonWithWaitersAsyncWaiter.builder()
135+
.scheduledExecutorService(executorService)
136+
.overrideConfiguration(WaiterOverrideConfiguration.builder()
137+
.maxAttempts(3)
138+
.backoffStrategy(BackoffStrategy.none())
139+
.build())
140+
.build();
141+
142+
newWaiter.close();
143+
verify(executorService, never()).shutdown();
144+
}
145+
}

0 commit comments

Comments
 (0)