Skip to content

Commit e779477

Browse files
committed
rolled in review comments
1 parent f78dc18 commit e779477

File tree

5 files changed

+50
-87
lines changed

5 files changed

+50
-87
lines changed

javav2/example_code/iotsitewise/src/main/java/com/example/iotsitewise/HelloSitewise.java

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,33 @@
55

66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8-
import software.amazon.awssdk.regions.Region;
98
import software.amazon.awssdk.services.iotsitewise.IoTSiteWiseAsyncClient;
10-
import software.amazon.awssdk.services.iotsitewise.model.AssetSummary;
11-
import software.amazon.awssdk.services.iotsitewise.model.ListAssetsRequest;
12-
import software.amazon.awssdk.services.iotsitewise.paginators.ListAssetsPublisher;
13-
14-
import java.util.List;
9+
import software.amazon.awssdk.services.iotsitewise.model.AssetModelType;
10+
import software.amazon.awssdk.services.iotsitewise.model.ListAssetModelsRequest;
11+
import software.amazon.awssdk.services.iotsitewise.paginators.ListAssetModelsPublisher;
1512
import java.util.concurrent.CompletableFuture;
1613

1714
// snippet-start:[iotsitewise.hello.main]
1815
public class HelloSitewise {
1916
private static final Logger logger = LoggerFactory.getLogger(HelloSitewise.class);
2017
public static void main(String[] args) {
21-
final String usage = """
22-
Usage:
23-
<assetModelId>
24-
25-
Where:
26-
assetModelId - The Id value of the asset model used in the IoT SiteWise program.
27-
""";
28-
29-
if (args.length != 1) {
30-
logger.info(usage);
31-
return;
32-
}
33-
34-
String assetModelId = args[0];
35-
fetchAssets(assetModelId);
18+
fetchAssetModels();
3619
}
3720

3821
/**
39-
* Fetches assets from AWS IoT SiteWise using the provided {@link IoTSiteWiseAsyncClient}.
40-
*
41-
* @param modelId the ID of the asset model to fetch assets for
22+
* Fetches asset models using the provided {@link IoTSiteWiseAsyncClient}.
4223
*/
43-
public static void fetchAssets(String modelId) {
44-
IoTSiteWiseAsyncClient siteWiseAsyncClient = IoTSiteWiseAsyncClient.builder()
45-
.build();
46-
47-
ListAssetsRequest assetsRequest = ListAssetsRequest.builder()
48-
.maxResults(10)
49-
.assetModelId(modelId)
24+
public static void fetchAssetModels() {
25+
IoTSiteWiseAsyncClient siteWiseAsyncClient = IoTSiteWiseAsyncClient.create();
26+
ListAssetModelsRequest assetModelsRequest = ListAssetModelsRequest.builder()
27+
.assetModelTypes(AssetModelType.ASSET_MODEL)
5028
.build();
5129

5230
// Asynchronous paginator - process paginated results.
53-
ListAssetsPublisher listAssetsPaginator = siteWiseAsyncClient.listAssetsPaginator(assetsRequest);
54-
CompletableFuture<Void> future = listAssetsPaginator.subscribe(response -> {
55-
response.assetSummaries().forEach(assetSummary ->
56-
logger.info("Asset Name: {} ", assetSummary.name())
31+
ListAssetModelsPublisher listModelsPaginator = siteWiseAsyncClient.listAssetModelsPaginator(assetModelsRequest);
32+
CompletableFuture<Void> future = listModelsPaginator.subscribe(response -> {
33+
response.assetModelSummaries().forEach(assetSummary ->
34+
logger.info("Asset Model Name: {} ", assetSummary.name())
5735
);
5836
});
5937

javav2/example_code/iotsitewise/src/main/java/com/example/iotsitewise/scenario/CloudFormationHelper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
1010
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
1111
import software.amazon.awssdk.services.cloudformation.CloudFormationAsyncClient;
12-
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
1312
import software.amazon.awssdk.services.cloudformation.model.Capability;
1413
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
1514
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest;
@@ -32,7 +31,6 @@ public class CloudFormationHelper {
3231
private static final Logger logger = LoggerFactory.getLogger(CloudFormationHelper.class);
3332

3433
private static CloudFormationAsyncClient cloudFormationClient;
35-
3634
private static CloudFormationAsyncClient getCloudFormationClient() {
3735
if (cloudFormationClient == null) {
3836
SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder()

javav2/example_code/iotsitewise/src/main/java/com/example/iotsitewise/scenario/SitewiseActions.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import software.amazon.awssdk.services.iotsitewise.model.DescribePortalRequest;
3737
import software.amazon.awssdk.services.iotsitewise.model.GatewayPlatform;
3838
import software.amazon.awssdk.services.iotsitewise.model.GetAssetPropertyValueRequest;
39-
import software.amazon.awssdk.services.iotsitewise.model.GetAssetPropertyValueResponse;
4039
import software.amazon.awssdk.services.iotsitewise.model.GreengrassV2;
4140
import software.amazon.awssdk.services.iotsitewise.model.ListAssetModelPropertiesRequest;
4241
import software.amazon.awssdk.services.iotsitewise.model.ListAssetModelsRequest;
@@ -123,7 +122,7 @@ public CompletableFuture<CreateAssetModelResponse> createAssetModelAsync(String
123122
return getAsyncClient().createAssetModel(createAssetModelRequest)
124123
.whenComplete((response, exception) -> {
125124
if (exception != null) {
126-
throw new RuntimeException("Failed to create asset model: " + exception.getMessage(), exception);
125+
logger.error("Failed to create asset model: {} ", exception.getMessage());
127126
}
128127
});
129128
}
@@ -148,7 +147,7 @@ public CompletableFuture<CreateAssetResponse> createAssetAsync(String assetName,
148147
return getAsyncClient().createAsset(createAssetRequest)
149148
.whenComplete((response, exception) -> {
150149
if (exception != null) {
151-
throw new RuntimeException("Failed to create asset: " + exception.getMessage(), exception);
150+
logger.error("Failed to create asset: {}", exception.getMessage());
152151
}
153152
});
154153
}
@@ -218,22 +217,24 @@ public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseA
218217
/**
219218
* Fetches the value of an asset property.
220219
*
221-
* @param propId the ID of the asset property to fetch.
222-
* @param assetId the ID of the asset to fetch the property value for.
220+
* @param propId the ID of the asset property to fetch.
221+
* @param assetId the ID of the asset to fetch the property value for.
223222
* @throws RuntimeException if an error occurs while fetching the property value.
224223
*/
225-
public CompletableFuture<String> getAssetPropValueAsync(String propId, String assetId) {
224+
public CompletableFuture<Double> getAssetPropValueAsync(String propId, String assetId) {
226225
GetAssetPropertyValueRequest assetPropertyValueRequest = GetAssetPropertyValueRequest.builder()
227226
.propertyId(propId)
228227
.assetId(assetId)
229228
.build();
230229

231-
CompletableFuture<GetAssetPropertyValueResponse> futureResponse = getAsyncClient().getAssetPropertyValue(assetPropertyValueRequest);
232-
return futureResponse.whenComplete((response, exception) -> {
230+
return getAsyncClient().getAssetPropertyValue(assetPropertyValueRequest)
231+
.handle((response, exception) -> {
233232
if (exception != null) {
234-
throw new RuntimeException("Error occurred while fetching property value: " + exception.getMessage(), exception);
233+
logger.error("Error occurred while fetching property value: " + exception.getMessage(), exception);
234+
throw (RuntimeException) exception;
235235
}
236-
}).thenApply(response -> String.valueOf(response.propertyValue().value().doubleValue()));
236+
return response.propertyValue().value().doubleValue();
237+
});
237238
}
238239
// snippet-end:[sitewise.java2_get_property.main]
239240

@@ -300,7 +301,7 @@ public CompletableFuture<DeleteAssetModelResponse> deleteAssetModelAsync(String
300301
return getAsyncClient().deleteAssetModel(deleteAssetModelRequest)
301302
.whenComplete((response, exception) -> {
302303
if (exception != null) {
303-
throw new RuntimeException("Failed to delete asset model with ID: " + assetModelId + ". Error: " + exception.getMessage(), exception);
304+
logger.error("Failed to delete asset model with ID:{}.", exception.getMessage());
304305
}
305306
});
306307
}
@@ -327,7 +328,7 @@ public CompletableFuture<String> createPortalAsync(String portalName, String iam
327328
.handle((response, exception) -> {
328329
if (exception != null) {
329330
logger.error("Failed to create portal: {} ", exception.getCause().getMessage());
330-
throw (CompletionException) exception;
331+
throw (RuntimeException) exception;
331332
}
332333
return response.portalId();
333334
});
@@ -350,7 +351,7 @@ public CompletableFuture<DeletePortalResponse> deletePortalAsync(String portalId
350351
return getAsyncClient().deletePortal(deletePortalRequest)
351352
.whenComplete((response, exception) -> {
352353
if (exception != null) {
353-
throw new RuntimeException("Failed to delete portal with ID: " + portalId + ". Error: " + exception.getMessage(), exception);
354+
logger.error("Failed to delete portal with ID: " + portalId + ". Error: " + exception.getMessage(), exception);
354355
}
355356
});
356357
}
@@ -382,7 +383,7 @@ public CompletableFuture<String> getAssetModelIdAsync(String assetModelName) {
382383

383384
// snippet-start:[sitewise.java2.describe.portal.main]
384385
/**
385-
* Asynchronously describes a portal.
386+
* Describes a portal.
386387
*
387388
* @param portalId the ID of the portal to describe.
388389
* @return a {@link CompletableFuture} that, when completed, will contain the URL of the described portal.
@@ -434,15 +435,15 @@ public CompletableFuture<String> createGatewayAsync(String gatewayName, String m
434435
logger.error("Error creating the gateway.");
435436
throw (RuntimeException) exception;
436437
}
437-
System.out.println("The ARN of the gateway is " + response.gatewayArn());
438+
logger.info("The ARN of the gateway is {}" , response.gatewayArn());
438439
return response.gatewayId();
439440
});
440441
}
441442
// snippet-end:[sitewise.java2.create.gateway.main]
442443

443444
// snippet-start:[sitewise.java2.delete.gateway.main]
444445
/**
445-
* Deletes the specified gateway asynchronously.
446+
* Deletes the specified gateway.
446447
*
447448
* @param gatewayARN the ARN of the gateway to delete.
448449
* @return a CompletableFuture containing the response of the delete operation.
@@ -456,7 +457,7 @@ public CompletableFuture<DeleteGatewayResponse> deleteGatewayAsync(String gatewa
456457
return getAsyncClient().deleteGateway(deleteGatewayRequest)
457458
.whenComplete((response, exception) -> {
458459
if (exception != null) {
459-
throw new RuntimeException("Failed to delete gateway: " + exception.getMessage(), exception);
460+
logger.error("Failed to delete gateway: " + exception.getMessage(), exception);
460461
} else {
461462
logger.info("The Gateway was deleted successfully.");
462463
}
@@ -466,7 +467,7 @@ public CompletableFuture<DeleteGatewayResponse> deleteGatewayAsync(String gatewa
466467

467468
// snippet-start:[sitewise.java2.describe.gateway.main]
468469
/**
469-
* Asynchronously describes the specified gateway.
470+
* Describes the specified gateway.
470471
*
471472
* @param gatewayId the ID of the gateway to describe.
472473
* @return a {@link CompletableFuture} that represents the asynchronous operation
@@ -485,7 +486,6 @@ public CompletableFuture<DescribeGatewayResponse> describeGatewayAsync(String ga
485486
}
486487
});
487488
}
488-
489489
// snippet-end:[sitewise.java2.describe.gateway.main]
490490

491491
private static Map<String, Double> generateSampleData() {

javav2/example_code/iotsitewise/src/main/java/com/example/iotsitewise/scenario/SitewiseScenario.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,8 @@ public class SitewiseScenario {
2727
static SitewiseActions sitewiseActions = new SitewiseActions();
2828

2929
public static void main(String[] args) throws Throwable {
30-
final String usage = """
31-
Usage:
32-
<contactEmail>
33-
34-
Where:
35-
contactEmail - The email address of the contact person associated with the IoT SiteWise program.
36-
37-
""";
38-
39-
// if (args.length != 1) {
40-
// logger.info(usage);
41-
// return;
42-
// }
43-
4430
Scanner scanner = new Scanner(System.in);
45-
String contactEmail = "scmacdon@amazon.com" ; //args[0];
31+
String contactEmail = "user@mydomain.com"; // Change email address.
4632
String assetModelName = "MyAssetModel1";
4733
String assetName = "MyAsset1" ;
4834
String portalName = "MyPortal1" ;
@@ -420,17 +406,16 @@ public static void runScenario(String assetModelName, String assetName, String
420406

421407
private static void waitForInputToContinue(Scanner scanner) {
422408
while (true) {
423-
System.out.println("");
424-
System.out.println("Enter 'c' followed by <ENTER> to continue:");
409+
logger.info("");
410+
logger.info("Enter 'c' followed by <ENTER> to continue:");
425411
String input = scanner.nextLine();
426412

427413
if (input.trim().equalsIgnoreCase("c")) {
428-
System.out.println("Continuing with the program...");
429-
System.out.println("");
414+
logger.info("Continuing with the program...");
415+
logger.info("");
430416
break;
431417
} else {
432-
// Handle invalid input.
433-
System.out.println("Invalid input. Please try again.");
418+
logger.info("Invalid input. Please try again.");
434419
}
435420
}
436421
}
@@ -440,11 +425,10 @@ public static void countdown(int minutes) throws InterruptedException {
440425
for (int i = minutes * 60 + seconds; i >= 0; i--) {
441426
int displayMinutes = i / 60;
442427
int displaySeconds = i % 60;
443-
System.out.print(String.format("\r%02d:%02d", displayMinutes, displaySeconds));
428+
System.out.printf("\r%02d:%02d", displayMinutes, displaySeconds);
444429
Thread.sleep(1000); // Wait for 1 second
445430
}
446-
447-
System.out.println("Countdown complete!");
431+
logger.info("Countdown complete!");
448432
}
449433
}
450434
// snippet-end:[iotsitewise.java2.scenario.main]

javav2/example_code/iotsitewise/src/test/java/SitewiseTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.CompletableFuture;
2929
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3030
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3132

3233
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
3334
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -42,8 +43,6 @@ public class SitewiseTests {
4243
private static final String gatewayName = "myGateway"+ UUID.randomUUID();
4344
private static final String myThing = "myThing"+ UUID.randomUUID();
4445

45-
private static String assetModelHello = "";
46-
4746
private static String assetModelId = "";
4847

4948
private static String iamRole = "";
@@ -67,18 +66,20 @@ public static void setUp() {
6766
Map<String, String> stackOutputs = CloudFormationHelper.getStackOutputsAsync(ROLES_STACK).join();
6867
iamRole = stackOutputs.get("SitewiseRoleArn");
6968

69+
/*
70+
The following values used in these integration tests are retrieved from AWS Secrets Manager.
71+
*/
7072
Gson gson = new Gson();
7173
String json = getSecretValues();
7274
SecretValues values = gson.fromJson(json, SecretValues.class);
7375
contactEmail = values.getContactEmail();
74-
assetModelHello = values.getAssetModelHello();
7576
}
7677

7778
@Test
7879
@Tag("IntegrationTest")
7980
@Order(1)
8081
public void testHelloService() {
81-
assertDoesNotThrow(() -> HelloSitewise.fetchAssets(assetModelHello));
82+
assertDoesNotThrow(HelloSitewise::fetchAssetModels);
8283
System.out.println(" Test 1 passed");
8384
}
8485

@@ -93,8 +94,8 @@ public void testCreateAssetModel() {
9394
if (response == null || response.assetModelId() == null) {
9495
throw new RuntimeException("Simulating failure: response or assetModelId is null");
9596
}
96-
9797
assetModelId = response.assetModelId();
98+
assertNotNull(assetModelId);
9899
});
99100

100101
System.out.println("Test 2 passed");
@@ -109,6 +110,7 @@ public void testCreateAsset() throws InterruptedException {
109110
CompletableFuture<CreateAssetResponse> future = sitewiseActions.createAssetAsync(assetName, assetModelId);
110111
CreateAssetResponse response = future.join();
111112
assetId = response.assetId();
113+
assertNotNull(assetId);
112114
});
113115
System.out.println("Test 3 passed");
114116
}
@@ -153,6 +155,7 @@ public void testGETHumValue() {
153155
public void testCreatePortal() {
154156
assertDoesNotThrow(() -> {
155157
portalId = sitewiseActions.createPortalAsync(portalName, iamRole, contactEmail).join();
158+
assertNotNull(portalId);
156159
});
157160
System.out.println("Test 7 passed");
158161
}
@@ -163,7 +166,7 @@ public void testCreatePortal() {
163166
public void testDescribePortal() {
164167
assertDoesNotThrow(() -> {
165168
String portalUrl = sitewiseActions.describePortalAsync(portalId).join();
166-
assertFalse(portalUrl.isEmpty());
169+
assertNotNull(portalUrl);
167170
});
168171
System.out.println("Test 8 passed");
169172
}
@@ -174,7 +177,7 @@ public void testDescribePortal() {
174177
public void testCreateGateway() {
175178
assertDoesNotThrow(() -> {
176179
gatewayId = sitewiseActions.createGatewayAsync(gatewayName, myThing).join();
177-
assertFalse(gatewayId.isEmpty());
180+
assertNotNull(gatewayId);
178181
});
179182
System.out.println("Test 9 passed");
180183
}

0 commit comments

Comments
 (0)