Skip to content

Commit 161da65

Browse files
authored
Device Advisor Tests (#224)
*Description of changes:* Added test samples for device advisor tests. Sample Name | Description and sample workflow | Test Cases -- | -- | -- MQTT Connection | The sample will be used to test MQTT connection feature. It will 1. open a MQTT connection 2.wait for callback 3.Then close the connection. | MQTT- Connection MQTT Subscribe | The sample will be used to test MQTT connection feature. It will 1. open a MQTT connection 2.subscribe for a topic 3. wait for callback 4. close the connection. | MQTT- Subscribe MQTT Publish | The sample will be used to test MQTT connection feature. It will 1. open a MQTT connection 2.publish to a topic 3. wait for callback 4. close the connection. | MQTT- Publish Shadow Update | The sample will be used to test MQTT connection feature. It will 1. open a MQTT connection 2. update shadow field 3.close the connection. | Shadow-Shadow Publish, Shadow Update By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent aefbfcf commit 161da65

File tree

9 files changed

+601
-0
lines changed

9 files changed

+601
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package DATestUtils;
2+
3+
import java.util.UUID;
4+
5+
public class DATestUtils {
6+
7+
public enum TestType {
8+
CONNECT, SUB_PUB, SHADOW
9+
}
10+
11+
private final static String ENV_ENDPONT = "DA_ENDPOINT";
12+
private final static String ENV_CERTI = "DA_CERTI";
13+
private final static String ENV_KEY = "DA_KEY";
14+
private final static String ENV_TOPIC = "DA_TOPIC";
15+
private final static String ENV_THING_NAME = "DA_THING_NAME";
16+
private final static String ENV_SHADOW_PROPERTY = "DA_SHADOW_PROPERTY";
17+
private final static String ENV_SHADOW_VALUE_SET = "DA_SHADOW_VALUE_SET";
18+
private final static String ENV_SHADOW_VALUE_DEFAULT = "DA_SHADOW_VALUE_DEFAULT";
19+
20+
public static String endpoint;
21+
public static String certificatePath;
22+
public static String keyPath;
23+
public static String topic;
24+
public static String thing_name;
25+
public static String shadowProperty;
26+
public static String shadowValue;
27+
28+
public static Boolean init(TestType type)
29+
{
30+
endpoint = System.getenv(ENV_ENDPONT);
31+
certificatePath = System.getenv(ENV_CERTI);
32+
keyPath = System.getenv(ENV_KEY);
33+
topic = System.getenv(ENV_TOPIC);
34+
thing_name = System.getenv(ENV_THING_NAME);
35+
shadowProperty = System.getenv(ENV_SHADOW_PROPERTY);
36+
shadowValue = System.getenv(ENV_SHADOW_VALUE_SET);
37+
38+
if (endpoint.isEmpty() || certificatePath.isEmpty() || keyPath.isEmpty())
39+
{
40+
return false;
41+
}
42+
43+
if (topic.isEmpty() && type == TestType.SUB_PUB)
44+
{
45+
return false;
46+
}
47+
48+
if ((thing_name.isEmpty() || shadowProperty.isEmpty() || shadowValue.isEmpty()) && type == TestType.SHADOW)
49+
{
50+
return false;
51+
}
52+
53+
return true;
54+
55+
}
56+
57+
}
58+
59+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
5+
<artifactId>MQTTConnect</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>${project.groupId}:${project.artifactId}</name>
9+
<description>Java bindings for the AWS IoT Core Service</description>
10+
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
19+
<artifactId>aws-iot-device-sdk</artifactId>
20+
<version>1.0.0-SNAPSHOT</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.codehaus.mojo</groupId>
27+
<artifactId>exec-maven-plugin</artifactId>
28+
<version>1.4.0</version>
29+
<configuration>
30+
<mainclass>main</mainclass>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.codehaus.mojo</groupId>
35+
<artifactId>build-helper-maven-plugin</artifactId>
36+
<version>3.2.0</version>
37+
<executions>
38+
<execution>
39+
<id>add-source</id>
40+
<phase>generate-sources</phase>
41+
<goals>
42+
<goal>add-source</goal>
43+
</goals>
44+
<configuration>
45+
<sources>
46+
<source>../DATestUtils</source>
47+
</sources>
48+
</configuration>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
package MQTTConnect;
7+
8+
import software.amazon.awssdk.crt.CRT;
9+
import software.amazon.awssdk.crt.CrtResource;
10+
import software.amazon.awssdk.crt.CrtRuntimeException;
11+
import software.amazon.awssdk.crt.mqtt.MqttClientConnection;
12+
import software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents;
13+
import software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder;
14+
15+
import java.util.UUID;
16+
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.ExecutionException;
18+
19+
import DATestUtils.DATestUtils;
20+
21+
public class MQTTConnect {
22+
23+
// When run normally, we want to exit nicely even if something goes wrong
24+
// When run from CI, we want to let an exception escape which in turn causes the
25+
// exec:java task to return a non-zero exit code
26+
static String ciPropValue = System.getProperty("aws.crt.ci");
27+
static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue);
28+
29+
static String clientId = "test-" + UUID.randomUUID().toString();
30+
static int port = 8883;
31+
32+
/*
33+
* When called during a CI run, throw an exception that will escape and fail the exec:java task
34+
* When called otherwise, print what went wrong (if anything) and just continue (return from main)
35+
*/
36+
static void onApplicationFailure(Throwable cause) {
37+
if (isCI) {
38+
throw new RuntimeException("BasicPubSub execution failure", cause);
39+
}
40+
}
41+
42+
public static void main(String[] args) {
43+
44+
if(!DATestUtils.init(DATestUtils.TestType.CONNECT))
45+
{
46+
throw new RuntimeException("Failed to initialize environment variables.");
47+
}
48+
49+
try(AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(DATestUtils.certificatePath, DATestUtils.keyPath)) {
50+
51+
builder.withClientId(clientId)
52+
.withEndpoint(DATestUtils.endpoint)
53+
.withPort((short)port)
54+
.withCleanSession(true)
55+
.withProtocolOperationTimeoutMs(60000);
56+
57+
try(MqttClientConnection connection = builder.build()) {
58+
59+
CompletableFuture<Boolean> connected = connection.connect();
60+
try {
61+
boolean sessionPresent = connected.get();
62+
} catch (Exception ex) {
63+
throw new RuntimeException("Exception occurred during connect", ex);
64+
}
65+
66+
CompletableFuture<Void> disconnected = connection.disconnect();
67+
disconnected.get();
68+
}
69+
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
70+
71+
}
72+
73+
CrtResource.waitForNoResources();
74+
}
75+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
5+
<artifactId>MQTTPublish</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>${project.groupId}:${project.artifactId}</name>
9+
<description>Java bindings for the AWS IoT Core Service</description>
10+
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
19+
<artifactId>aws-iot-device-sdk</artifactId>
20+
<version>1.0.0-SNAPSHOT</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.codehaus.mojo</groupId>
27+
<artifactId>exec-maven-plugin</artifactId>
28+
<version>1.4.0</version>
29+
<configuration>
30+
<mainclass>main</mainclass>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.codehaus.mojo</groupId>
35+
<artifactId>build-helper-maven-plugin</artifactId>
36+
<version>3.2.0</version>
37+
<executions>
38+
<execution>
39+
<id>add-source</id>
40+
<phase>generate-sources</phase>
41+
<goals>
42+
<goal>add-source</goal>
43+
</goals>
44+
<configuration>
45+
<sources>
46+
<source>../DATestUtils</source>
47+
</sources>
48+
</configuration>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
package MQTTPublish;
7+
8+
import software.amazon.awssdk.crt.CRT;
9+
import software.amazon.awssdk.crt.CrtResource;
10+
import software.amazon.awssdk.crt.CrtRuntimeException;
11+
import software.amazon.awssdk.crt.mqtt.MqttClientConnection;
12+
import software.amazon.awssdk.crt.mqtt.QualityOfService;
13+
import software.amazon.awssdk.crt.mqtt.MqttMessage;
14+
import software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder;
15+
16+
import java.util.UUID;
17+
import java.util.concurrent.CompletableFuture;
18+
import java.util.concurrent.ExecutionException;
19+
20+
import DATestUtils.DATestUtils;
21+
22+
public class MQTTPublish {
23+
24+
// When run normally, we want to exit nicely even if something goes wrong
25+
// When run from CI, we want to let an exception escape which in turn causes the
26+
// exec:java task to return a non-zero exit code
27+
static String ciPropValue = System.getProperty("aws.crt.ci");
28+
static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue);
29+
30+
static String clientId = "test-" + UUID.randomUUID().toString();
31+
static String message = "Hello World!";
32+
static int port = 8883;
33+
34+
static String region = "us-east-1";
35+
36+
/*
37+
* When called during a CI run, throw an exception that will escape and fail the exec:java task
38+
* When called otherwise, print what went wrong (if anything) and just continue (return from main)
39+
*/
40+
static void onApplicationFailure(Throwable cause) {
41+
if (isCI) {
42+
throw new RuntimeException("BasicPubSub execution failure", cause);
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
48+
if(!DATestUtils.init(DATestUtils.TestType.SUB_PUB))
49+
{
50+
throw new RuntimeException("Failed to initialize environment variables.");
51+
}
52+
53+
54+
try(AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(DATestUtils.certificatePath, DATestUtils.keyPath)) {
55+
56+
57+
builder.withClientId(clientId)
58+
.withEndpoint(DATestUtils.endpoint)
59+
.withPort((short)port)
60+
.withCleanSession(true)
61+
.withProtocolOperationTimeoutMs(60000);
62+
63+
try(MqttClientConnection connection = builder.build()) {
64+
65+
CompletableFuture<Boolean> connected = connection.connect();
66+
try {
67+
boolean sessionPresent = connected.get();
68+
} catch (Exception ex) {
69+
throw new RuntimeException("Exception occurred during connect", ex);
70+
}
71+
72+
CompletableFuture<Integer> published = connection.publish(new MqttMessage(DATestUtils.topic, message.getBytes(), QualityOfService.AT_LEAST_ONCE, false));
73+
published.get();
74+
Thread.sleep(1000);
75+
76+
CompletableFuture<Void> disconnected = connection.disconnect();
77+
disconnected.get();
78+
}
79+
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
80+
onApplicationFailure(ex);
81+
}
82+
83+
CrtResource.waitForNoResources();
84+
}
85+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
5+
<artifactId>MQTTSubscribe</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>${project.groupId}:${project.artifactId}</name>
9+
<description>Java bindings for the AWS IoT Core Service</description>
10+
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
19+
<artifactId>aws-iot-device-sdk</artifactId>
20+
<version>1.0.0-SNAPSHOT</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.codehaus.mojo</groupId>
27+
<artifactId>exec-maven-plugin</artifactId>
28+
<version>1.4.0</version>
29+
<configuration>
30+
<mainclass>main</mainclass>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.codehaus.mojo</groupId>
35+
<artifactId>build-helper-maven-plugin</artifactId>
36+
<version>3.2.0</version>
37+
<executions>
38+
<execution>
39+
<id>add-source</id>
40+
<phase>generate-sources</phase>
41+
<goals>
42+
<goal>add-source</goal>
43+
</goals>
44+
<configuration>
45+
<sources>
46+
<source>../DATestUtils</source>
47+
</sources>
48+
</configuration>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>

0 commit comments

Comments
 (0)