forked from localstack/localstack-java-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicFeaturesSDKV2Test.java
311 lines (263 loc) · 13.4 KB
/
BasicFeaturesSDKV2Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package cloud.localstack.awssdkv2;
import cloud.localstack.Constants;
import cloud.localstack.LocalstackTestRunner;
import cloud.localstack.docker.annotation.LocalstackDockerProperties;
import cloud.localstack.sample.LambdaHandler;
import cloud.localstack.utils.LocalTestUtil;
import lombok.val;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.services.cloudwatch.*;
import software.amazon.awssdk.services.cloudwatch.model.*;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
import software.amazon.awssdk.services.dynamodb.model.*;
import software.amazon.awssdk.services.iam.*;
import software.amazon.awssdk.services.iam.model.*;
import software.amazon.awssdk.services.iam.paginators.ListUsersPublisher;
import software.amazon.awssdk.services.kinesis.*;
import software.amazon.awssdk.services.kinesis.model.*;
import software.amazon.awssdk.services.lambda.model.CreateFunctionRequest;
import software.amazon.awssdk.services.lambda.model.Runtime;
import software.amazon.awssdk.services.s3.*;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerAsyncClient;
import software.amazon.awssdk.services.secretsmanager.model.CreateSecretRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
import software.amazon.awssdk.services.sns.*;
import software.amazon.awssdk.services.sns.model.*;
import software.amazon.awssdk.services.sqs.*;
import software.amazon.awssdk.services.sqs.model.*;
import software.amazon.awssdk.services.ssm.*;
import software.amazon.awssdk.services.ssm.model.*;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.*;
import java.nio.ByteBuffer;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneOffset;
import java.time.Instant;
import software.amazon.awssdk.core.SdkBytes;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@RunWith(LocalstackTestRunner.class)
@LocalstackDockerProperties(ignoreDockerRunErrors=true)
public class BasicFeaturesSDKV2Test {
static {
System.setProperty(SdkSystemSetting.CBOR_ENABLED.property(), "false");
}
@Test
public void testCreateSqsQueueV2() throws Exception {
String queueName = "test-q-"+ UUID.randomUUID().toString();
CreateQueueRequest request = CreateQueueRequest.builder().queueName(queueName).build();
SqsAsyncClient sqsClient = TestUtils.getClientSQSAsyncV2();
CreateQueueResponse queue = sqsClient.createQueue(request).get();
Assert.assertTrue(queue.queueUrl().contains(Constants.DEFAULT_AWS_ACCOUNT_ID + "/" + queueName));
}
@Test
public void testCreateKinesisStreamV2() throws Exception {
String streamName = "test-s-"+ UUID.randomUUID().toString();
KinesisAsyncClient kinesisClient = TestUtils.getClientKinesisAsyncV2();
CreateStreamRequest request = CreateStreamRequest.builder()
.streamName(streamName).shardCount(1).build();
CreateStreamResponse response = kinesisClient.createStream(request).get();
Assert.assertNotNull(response);
}
@Test
public void testCreateKinesisRecordV2() throws Exception {
String streamName = "test-s-"+UUID.randomUUID().toString();
KinesisAsyncClient kinesisClient = TestUtils.getClientKinesisAsyncV2();
CreateStreamRequest request = CreateStreamRequest.builder()
.streamName(streamName).shardCount(1).build();
CreateStreamResponse response = kinesisClient.createStream(request).get();
Assert.assertNotNull(response);
SdkBytes payload = SdkBytes.fromByteBuffer(ByteBuffer.wrap(String.format("testData-%d", 1).getBytes()));
PutRecordRequest.Builder putRecordRequest = PutRecordRequest.builder();
putRecordRequest.streamName(streamName);
putRecordRequest.data(payload);
putRecordRequest.partitionKey(String.format("partitionKey-%d", 1));
Assert.assertNotNull(kinesisClient.putRecord(putRecordRequest.build()));
}
@Test
public void testCreateDynamoDBTable() throws Exception {
DynamoDbAsyncClient dynamoDbAsyncClient = TestUtils.getClientDyanamoAsyncV2();
CreateTableRequest createTableRequest = CreateTableRequest.builder()
.keySchema(
KeySchemaElement.builder()
.keyType(KeyType.HASH)
.attributeName("test")
.build()
)
.attributeDefinitions(AttributeDefinition.builder()
.attributeName("test")
.attributeType(ScalarAttributeType.S)
.build())
.provisionedThroughput(
ProvisionedThroughput.builder()
.readCapacityUnits(5L)
.writeCapacityUnits(5L)
.build())
.tableName("test")
.build();
CreateTableResponse response = dynamoDbAsyncClient.createTable(createTableRequest).get();
Assert.assertNotNull(response);
}
@Test
public void testS3CreateListBuckets() throws Exception {
String bucketName = "test-b-"+UUID.randomUUID().toString();
S3AsyncClient s3Client = TestUtils.getClientS3AsyncV2();
CreateBucketRequest request = CreateBucketRequest.builder().bucket(bucketName).build();
CreateBucketResponse response = s3Client.createBucket(request).get();
Assert.assertNotNull(response);
ListBucketsRequest listRequest = ListBucketsRequest.builder().build();
ListBucketsResponse buckets = s3Client.listBuckets(listRequest).get();
Bucket bucket = buckets.buckets().stream().filter(b -> b.name().equals(bucketName)).findFirst().get();
Assert.assertNotNull(bucket);
}
@Test
public void testSendSNSMessage() throws Exception {
// Test integration of SNS messaging with LocalStack using SDK v2
final String topicName = "test-t-"+UUID.randomUUID().toString();
final SnsAsyncClient clientSNS = TestUtils.getClientSNSAsyncV2();
CreateTopicResponse createTopicResponse = clientSNS.createTopic(
CreateTopicRequest.builder().name(topicName).build()).get();
String topicArn = createTopicResponse.topicArn();
Assert.assertNotNull(topicArn);
PublishRequest publishRequest = PublishRequest.builder().topicArn(topicArn).subject("test subject").message("message test.").build();
PublishResponse publishResponse = clientSNS.publish(publishRequest).get();
Assert.assertNotNull(publishResponse.messageId());
}
@Test
public void testGetSsmParameter() throws Exception {
// Test integration of ssm parameter with LocalStack using SDK v2
final SsmAsyncClient clientSsm = TestUtils.getClientSSMAsyncV2();
clientSsm.putParameter(PutParameterRequest.builder().name("testparameter").value("testvalue").build());
CompletableFuture<GetParameterResponse> getParameterResponse = clientSsm.getParameter(GetParameterRequest.builder().name("testparameter").build());
String parameterValue = getParameterResponse.get().parameter().value();
Assert.assertNotNull(parameterValue);
Assert.assertEquals("testvalue", parameterValue);
}
@Test
public void testGetSecretsManagerSecret() throws Exception {
final SecretsManagerAsyncClient clientSecretsManager = TestUtils.getClientSecretsManagerAsyncV2();
clientSecretsManager.createSecret(CreateSecretRequest.builder().name("testsecret").secretString("secretcontent").build());
CompletableFuture<GetSecretValueResponse> getSecretResponse = clientSecretsManager.getSecretValue(
GetSecretValueRequest.builder().secretId("testsecret").build());
String secretValue = getSecretResponse.get().secretString();
Assert.assertNotNull(secretValue);
Assert.assertEquals("secretcontent", secretValue);
}
@Test
public void testGetSecretAsParam() throws Exception {
final SsmAsyncClient clientSsm = TestUtils.getClientSSMAsyncV2();
final SecretsManagerAsyncClient clientSecretsManager = TestUtils.getClientSecretsManagerAsyncV2();
clientSecretsManager.createSecret(CreateSecretRequest.builder()
.name("testsecret").secretString("secretcontent").build()).join();
CompletableFuture<GetParameterResponse> getParameterResponse = clientSsm.getParameter(
GetParameterRequest.builder().name("/aws/reference/secretsmanager/testsecret").build());
String parameterValue = getParameterResponse.get().parameter().value();
Assert.assertNotNull(parameterValue);
Assert.assertEquals("secretcontent", parameterValue);
}
@Test
public void testCWPutMetrics() throws Exception {
final CloudWatchAsyncClient clientCW = TestUtils.getClientCloudWatchAsyncV2();
Dimension dimension = Dimension.builder()
.name("UNIQUE_PAGES")
.value("URLS")
.build();
// Set an Instant object
String time = ZonedDateTime.now( ZoneOffset.UTC ).format( DateTimeFormatter.ISO_INSTANT );
Instant instant = Instant.parse(time);
double dataPoint = 1.23423;
MetricDatum datum = MetricDatum.builder()
.metricName("PAGES_VISITED")
.unit(StandardUnit.NONE)
.value(dataPoint)
.timestamp(instant)
.dimensions(dimension).build();
PutMetricDataRequest request = PutMetricDataRequest.builder()
.namespace("SITE/TRAFFIC")
.metricData(datum).build();
PutMetricDataResponse response = clientCW.putMetricData(request).get();
Assert.assertNotNull(response);
}
@Test
public void testCWMultipleDimentionsAndMetrics() throws Exception {
final CloudWatchAsyncClient clientCW = TestUtils.getClientCloudWatchAsyncV2();
List<Dimension> awsDimensionList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
awsDimensionList.add(Dimension.builder()
.name("UNIQUE_PAGES"+i)
.value("URLS"+i)
.build());
}
// Set an Instant object
String time = ZonedDateTime.now( ZoneOffset.UTC ).format( DateTimeFormatter.ISO_INSTANT );
Instant instant = Instant.parse(time);
double dataPoint = 1.23423;
List<MetricDatum> metrics = new ArrayList();
for (int i = 0; i < 20; i++) {
metrics.add(MetricDatum.builder()
.metricName("PAGES_VISITED")
.unit(StandardUnit.NONE)
.value(dataPoint)
.timestamp(instant)
.dimensions(awsDimensionList).build());
}
PutMetricDataRequest request = PutMetricDataRequest.builder()
.namespace("SITE/TRAFFIC")
.metricData(metrics).build();
PutMetricDataResponse response = clientCW.putMetricData(request).get();
Assert.assertNotNull(response);
}
@Test
public void testLambdaCreateListFunctions() throws Exception {
val functionName = "test-f-"+UUID.randomUUID().toString();
val lambdaClient = TestUtils.getClientLambdaAsyncV2();
val createFunctionRequest = CreateFunctionRequest.builder().functionName(functionName)
.runtime(Runtime.JAVA8)
.role("r1")
.code(LocalTestUtil.createFunctionCodeSDKV2(LambdaHandler.class))
.handler(LambdaHandler.class.getName()).build();
val response = lambdaClient.createFunction(createFunctionRequest).get();
Assert.assertNotNull(response);
val functions = lambdaClient.listFunctions().get();
val function = functions.functions().stream().filter(f -> f.functionName().equals(functionName)).findFirst().get();
Assert.assertNotNull(function);
}
@Test
public void testIAMUserCreation() throws Exception {
IamAsyncClient iamClient = TestUtils.getClientIamAsyncV2();
String username = UUID.randomUUID().toString();
CreateUserRequest createUserRequest = CreateUserRequest.builder().userName(username).build();
iamClient.createUser(createUserRequest);
boolean userFound = false;
List<User> users = iamClient.listUsers().get().users();
for (int i = 0; i < users.size(); i++) {
System.out.println(users.get(i).userName());
if(users.get(i).userName().equals(username)){
userFound = true;
break;
}
}
Assert.assertTrue(userFound);
}
@Test
public void testIAMListUserPagination() throws Exception {
IamAsyncClient iamClient = TestUtils.getClientIamAsyncV2();
String username = UUID.randomUUID().toString();
CreateUserRequest createUserRequest = CreateUserRequest.builder().userName(username).build();
iamClient.createUser(createUserRequest);
AtomicBoolean userFound = new AtomicBoolean(false);
iamClient.listUsersPaginator().users().subscribe(user -> {
if(user.userName().equals(username)){
userFound.set(true);
}
});
TimeUnit.SECONDS.sleep(2);
Assert.assertTrue(userFound.get());
}
}