Skip to content

Commit 97ec032

Browse files
committed
Add iot unit and integrations tests
1 parent cda489b commit 97ec032

16 files changed

+4982
-0
lines changed

aws-android-sdk-iot/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ dependencies {
55

66
api project(":aws-android-sdk-core")
77
implementation "org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0"
8+
9+
testImplementation "junit:junit:4.12"
10+
testImplementation "org.robolectric:robolectric:2.4"
11+
testImplementation "org.mockito:mockito-all:1.10.5"
812
}
913

1014
sourceCompatibility = "1.7"

aws-android-sdk-iot/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@
2828
<optional>false</optional>
2929
<version>1.1.0</version>
3030
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<version>4.12</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.robolectric</groupId>
39+
<artifactId>robolectric</artifactId>
40+
<version>2.4</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-all</artifactId>
46+
<version>1.10.5</version>
47+
<scope>test</scope>
48+
</dependency>
3149
</dependencies>
3250

3351
<build>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
package com.amazonaws.mobileconnectors.iot;
3+
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
7+
import com.amazonaws.util.StringUtils;
8+
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import java.io.File;
14+
import java.io.FileOutputStream;
15+
16+
public class AWSIotClientIdHelperTest {
17+
18+
private static final String TEST_DIRECTORY = "./";
19+
private static final String PERSISTENCE_FILENAME = "CLIENT_ID";
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
// remove any existing file, clear the static class variable
24+
File persistenceFile = new File(TEST_DIRECTORY, PERSISTENCE_FILENAME);
25+
if (persistenceFile.exists()) {
26+
persistenceFile.delete();
27+
}
28+
AWSIotClientIdHelper.reset();
29+
}
30+
31+
@After
32+
public void tearDown() throws Exception {
33+
// remove any existing file
34+
File persistenceFile = new File(TEST_DIRECTORY, PERSISTENCE_FILENAME);
35+
if (persistenceFile.exists()) {
36+
persistenceFile.delete();
37+
}
38+
}
39+
40+
@Test
41+
public void testGenerateClientId() throws Exception {
42+
String testId1 = AWSIotClientIdHelper.generateClientId();
43+
String testId2 = AWSIotClientIdHelper.generateClientId();
44+
String testId3 = AWSIotClientIdHelper.generateClientId();
45+
// UUIDs are 36 character strings
46+
assertEquals(36, testId1.length());
47+
assertEquals(36, testId2.length());
48+
assertEquals(36, testId3.length());
49+
// generate gets a new ID each call
50+
assertFalse(testId1.equals(testId2));
51+
assertFalse(testId2.equals(testId3));
52+
assertFalse(testId1.equals(testId3));
53+
}
54+
55+
@Test
56+
public void testGetPersistedClientId() throws Exception {
57+
String testId = AWSIotClientIdHelper.getPersistedClientId(TEST_DIRECTORY);
58+
// UUIDs are 36 character strings
59+
assertEquals(36, testId.length());
60+
// ensure same ID is returned 2nd time
61+
assertEquals(testId, AWSIotClientIdHelper.getPersistedClientId(TEST_DIRECTORY));
62+
}
63+
64+
@Test
65+
public void testGetPersistedClientIdFilePresent() throws Exception {
66+
File persistenceFile = new File(TEST_DIRECTORY, PERSISTENCE_FILENAME);
67+
FileOutputStream out = new FileOutputStream(persistenceFile);
68+
out.write("this-is-a-36-character-string-4-test".getBytes(StringUtils.UTF8));
69+
out.close();
70+
String testId = AWSIotClientIdHelper.getPersistedClientId(TEST_DIRECTORY);
71+
// UUIDs are 36 character strings
72+
assertEquals(36, testId.length());
73+
// ensure same ID as create above is returned
74+
assertEquals("this-is-a-36-character-string-4-test", testId);
75+
}
76+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.amazonaws.mobileconnectors.iot;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class AWSIotMqttLastWillAndTestamentTest {
8+
9+
@Test
10+
public void testTopic() throws Exception {
11+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", "bye", AWSIotMqttQos.QOS0);
12+
assertEquals("test/lwt", lwt.getTopic());
13+
}
14+
15+
@Test
16+
public void testMessage() throws Exception {
17+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", "bye", AWSIotMqttQos.QOS0);
18+
assertEquals("bye", lwt.getMessage());
19+
}
20+
21+
@Test
22+
public void testQos() throws Exception {
23+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", "bye", AWSIotMqttQos.QOS0);
24+
assertEquals(AWSIotMqttQos.QOS0, lwt.getQos());
25+
}
26+
27+
@Test(expected=IllegalArgumentException.class)
28+
public void testCreateLwtWithNullTopic() throws Exception {
29+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament(null, "bye", AWSIotMqttQos.QOS0);
30+
}
31+
@Test(expected=IllegalArgumentException.class)
32+
public void testCreateLwtWithEmptyTopic() throws Exception {
33+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("", "bye", AWSIotMqttQos.QOS0);
34+
}
35+
@Test(expected=IllegalArgumentException.class)
36+
public void testCreateLwtWithNullMessage() throws Exception {
37+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", null, AWSIotMqttQos.QOS0);
38+
}
39+
@Test(expected=IllegalArgumentException.class)
40+
public void testCreateLwtWithNullQos() throws Exception {
41+
AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", "bye", null);
42+
}
43+
}

0 commit comments

Comments
 (0)