Skip to content

Commit

Permalink
Device Advisor Tests (#224)
Browse files Browse the repository at this point in the history
*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.
  • Loading branch information
xiazhvera authored Mar 2, 2022
1 parent aefbfcf commit 161da65
Show file tree
Hide file tree
Showing 9 changed files with 601 additions and 0 deletions.
59 changes: 59 additions & 0 deletions deviceadvisor/tests/DATestUtils/DATestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package DATestUtils;

import java.util.UUID;

public class DATestUtils {

public enum TestType {
CONNECT, SUB_PUB, SHADOW
}

private final static String ENV_ENDPONT = "DA_ENDPOINT";
private final static String ENV_CERTI = "DA_CERTI";
private final static String ENV_KEY = "DA_KEY";
private final static String ENV_TOPIC = "DA_TOPIC";
private final static String ENV_THING_NAME = "DA_THING_NAME";
private final static String ENV_SHADOW_PROPERTY = "DA_SHADOW_PROPERTY";
private final static String ENV_SHADOW_VALUE_SET = "DA_SHADOW_VALUE_SET";
private final static String ENV_SHADOW_VALUE_DEFAULT = "DA_SHADOW_VALUE_DEFAULT";

public static String endpoint;
public static String certificatePath;
public static String keyPath;
public static String topic;
public static String thing_name;
public static String shadowProperty;
public static String shadowValue;

public static Boolean init(TestType type)
{
endpoint = System.getenv(ENV_ENDPONT);
certificatePath = System.getenv(ENV_CERTI);
keyPath = System.getenv(ENV_KEY);
topic = System.getenv(ENV_TOPIC);
thing_name = System.getenv(ENV_THING_NAME);
shadowProperty = System.getenv(ENV_SHADOW_PROPERTY);
shadowValue = System.getenv(ENV_SHADOW_VALUE_SET);

if (endpoint.isEmpty() || certificatePath.isEmpty() || keyPath.isEmpty())
{
return false;
}

if (topic.isEmpty() && type == TestType.SUB_PUB)
{
return false;
}

if ((thing_name.isEmpty() || shadowProperty.isEmpty() || shadowValue.isEmpty()) && type == TestType.SHADOW)
{
return false;
}

return true;

}

}


54 changes: 54 additions & 0 deletions deviceadvisor/tests/MQTTConnect/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>MQTTConnect</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java bindings for the AWS IoT Core Service</description>
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>aws-iot-device-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainclass>main</mainclass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../DATestUtils</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package MQTTConnect;

import software.amazon.awssdk.crt.CRT;
import software.amazon.awssdk.crt.CrtResource;
import software.amazon.awssdk.crt.CrtRuntimeException;
import software.amazon.awssdk.crt.mqtt.MqttClientConnection;
import software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents;
import software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import DATestUtils.DATestUtils;

public class MQTTConnect {

// When run normally, we want to exit nicely even if something goes wrong
// When run from CI, we want to let an exception escape which in turn causes the
// exec:java task to return a non-zero exit code
static String ciPropValue = System.getProperty("aws.crt.ci");
static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue);

static String clientId = "test-" + UUID.randomUUID().toString();
static int port = 8883;

/*
* When called during a CI run, throw an exception that will escape and fail the exec:java task
* When called otherwise, print what went wrong (if anything) and just continue (return from main)
*/
static void onApplicationFailure(Throwable cause) {
if (isCI) {
throw new RuntimeException("BasicPubSub execution failure", cause);
}
}

public static void main(String[] args) {

if(!DATestUtils.init(DATestUtils.TestType.CONNECT))
{
throw new RuntimeException("Failed to initialize environment variables.");
}

try(AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(DATestUtils.certificatePath, DATestUtils.keyPath)) {

builder.withClientId(clientId)
.withEndpoint(DATestUtils.endpoint)
.withPort((short)port)
.withCleanSession(true)
.withProtocolOperationTimeoutMs(60000);

try(MqttClientConnection connection = builder.build()) {

CompletableFuture<Boolean> connected = connection.connect();
try {
boolean sessionPresent = connected.get();
} catch (Exception ex) {
throw new RuntimeException("Exception occurred during connect", ex);
}

CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();
}
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {

}

CrtResource.waitForNoResources();
}
}
54 changes: 54 additions & 0 deletions deviceadvisor/tests/MQTTPublish/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>MQTTPublish</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java bindings for the AWS IoT Core Service</description>
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>aws-iot-device-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainclass>main</mainclass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../DATestUtils</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package MQTTPublish;

import software.amazon.awssdk.crt.CRT;
import software.amazon.awssdk.crt.CrtResource;
import software.amazon.awssdk.crt.CrtRuntimeException;
import software.amazon.awssdk.crt.mqtt.MqttClientConnection;
import software.amazon.awssdk.crt.mqtt.QualityOfService;
import software.amazon.awssdk.crt.mqtt.MqttMessage;
import software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import DATestUtils.DATestUtils;

public class MQTTPublish {

// When run normally, we want to exit nicely even if something goes wrong
// When run from CI, we want to let an exception escape which in turn causes the
// exec:java task to return a non-zero exit code
static String ciPropValue = System.getProperty("aws.crt.ci");
static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue);

static String clientId = "test-" + UUID.randomUUID().toString();
static String message = "Hello World!";
static int port = 8883;

static String region = "us-east-1";

/*
* When called during a CI run, throw an exception that will escape and fail the exec:java task
* When called otherwise, print what went wrong (if anything) and just continue (return from main)
*/
static void onApplicationFailure(Throwable cause) {
if (isCI) {
throw new RuntimeException("BasicPubSub execution failure", cause);
}
}

public static void main(String[] args) {

if(!DATestUtils.init(DATestUtils.TestType.SUB_PUB))
{
throw new RuntimeException("Failed to initialize environment variables.");
}


try(AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(DATestUtils.certificatePath, DATestUtils.keyPath)) {


builder.withClientId(clientId)
.withEndpoint(DATestUtils.endpoint)
.withPort((short)port)
.withCleanSession(true)
.withProtocolOperationTimeoutMs(60000);

try(MqttClientConnection connection = builder.build()) {

CompletableFuture<Boolean> connected = connection.connect();
try {
boolean sessionPresent = connected.get();
} catch (Exception ex) {
throw new RuntimeException("Exception occurred during connect", ex);
}

CompletableFuture<Integer> published = connection.publish(new MqttMessage(DATestUtils.topic, message.getBytes(), QualityOfService.AT_LEAST_ONCE, false));
published.get();
Thread.sleep(1000);

CompletableFuture<Void> disconnected = connection.disconnect();
disconnected.get();
}
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
onApplicationFailure(ex);
}

CrtResource.waitForNoResources();
}
}
54 changes: 54 additions & 0 deletions deviceadvisor/tests/MQTTSubscribe/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>MQTTSubscribe</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java bindings for the AWS IoT Core Service</description>
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>aws-iot-device-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainclass>main</mainclass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../DATestUtils</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 161da65

Please sign in to comment.