@Test public void testConnectAlreadyConnected() throws Exception { MockMqttClient mockClient = new MockMqttClient(); TestClientStatusCallback csb = new TestClientStatusCallback(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); assertEquals(1, mockClient.connectCalls); assertEquals(1, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); assertEquals(MqttManagerConnectionState.Connecting, testClient.getConnectionState()); // should still be connecting testClient.connect(testKeystore, csb); assertEquals(1, mockClient.connectCalls); assertEquals(2, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(1)); assertEquals(MqttManagerConnectionState.Connecting, testClient.getConnectionState()); mockClient.mockConnectSuccess(); // now will be connected testClient.connect(testKeystore, csb); assertEquals(1, mockClient.connectCalls); assertEquals(4, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected, csb.statuses.get(2)); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected, csb.statuses.get(3)); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
@Test public void testConnectWebSocket() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); testClient.connect(new TestAwsCredentialsProvider(), csb); Thread.sleep(500); // connect is async, will return before callback is actually set in connect() mockClient.mockConnectSuccess(); assertEquals(1, mockClient.connectCalls); assertTrue(mockClient.mostRecentOptions.isCleanSession()); assertEquals(300, mockClient.mostRecentOptions.getKeepAliveInterval()); assertEquals(2, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected, csb.statuses.get(1)); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
@Test public void testAutoReconnectDisabled() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); testClient.setAutoReconnect(false); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); assertEquals(1, mockClient.connectCalls); }
@Test public void testDisconnect() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); testClient.disconnect(); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); assertEquals(1, mockClient.connectCalls); assertEquals(1, mockClient.disconnectCalls); }
@Test public void testAutoReconnectSuccessWebSocket() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); testClient.setAutoReconnect(true); TestClientStatusCallback csb = new TestClientStatusCallback(); testClient.connect(new TestAwsCredentialsProvider(), csb); Thread.sleep(500); // connect is async, will return before callback is actually set in connect() mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState()); // make sure we wait for 4 seconds, still 1 after 3 seconds Robolectric.getUiThreadScheduler().advanceBy(3000); assertEquals(1, mockClient.connectCalls); // now past 4 Robolectric.getUiThreadScheduler().advanceBy(4100); /* Comment out this temporarily before we upgrade to Robolectric v3 */ //assertEquals(2, mockClient.connectCalls); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
testClient.setUnitTestMillisOverride(1000L); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); assertEquals(1, mockClient.connectCalls); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState()); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState());
testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState());
@Test public void testConnect() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(1, mockClient.connectCalls); assertTrue(mockClient.mostRecentOptions.isCleanSession()); assertEquals(300, mockClient.mostRecentOptions.getKeepAliveInterval()); assertEquals(2, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected, csb.statuses.get(1)); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
@Test public void testConnectDisconnectConnect() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); testClient.setAutoReconnect(false); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); testClient.disconnect(); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(2, mockClient.connectCalls); }
@Test public void testConnectFailureConnectedException() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnConnect = true; mockClient.connectException = new MqttException(MqttException.REASON_CODE_CLIENT_CONNECTED); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
@Test public void testConnectFailureConnectingException() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnConnect = true; mockClient.connectException = new MqttException(MqttException.REASON_CODE_CONNECT_IN_PROGRESS); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); assertEquals(MqttManagerConnectionState.Connecting, testClient.getConnectionState()); }
@Test public void testConnectFailureOtherException() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnConnect = true; mockClient.connectException = new MqttException(MqttException.REASON_CODE_UNEXPECTED_ERROR); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); }
@Test public void testConnectFailureReconnects() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); testClient.setAutoReconnect(true); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectFail(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState()); }
@Test public void testConnectFailureWithAutoreconnectDisabled() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); testClient.setAutoReconnect(false); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectFail(); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); }
@Test(expected = AmazonClientException.class) public void testDisconnectException() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnDisconnect = true; AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); testClient.disconnect(); }
testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState());
testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState());
testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); mockClient.mockDisconnect(); assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState());
@Test public void testConnectWithLwt() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", "bye", AWSIotMqttQos.QOS0); testClient.setMqttLastWillAndTestament(lwt); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(1, mockClient.connectCalls); assertTrue(mockClient.mostRecentOptions.isCleanSession()); assertEquals(300, mockClient.mostRecentOptions.getKeepAliveInterval()); assertEquals("test/lwt", mockClient.mostRecentOptions.getWillDestination()); assertEquals("bye", new String(mockClient.mostRecentOptions.getWillMessage().getPayload())); assertEquals(AWSIotMqttQos.QOS0.asInt(), mockClient.mostRecentOptions.getWillMessage().getQos()); assertFalse(mockClient.mostRecentOptions.getWillMessage().isRetained()); assertEquals(2, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected, csb.statuses.get(1)); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
@Test public void testCreateClientWithEndpoint() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", "ABCDEFG.iot.us-east-1.amazonaws.com"); assertEquals(true, testClient.isAutoReconnect()); assertEquals(4, testClient.getReconnectTimeout()); assertEquals(4, testClient.getMinReconnectRetryTime()); assertEquals(64, testClient.getMaxReconnectRetryTime()); assertEquals(10, testClient.getMaxAutoReconnectAttempts()); assertEquals(300, testClient.getKeepAlive()); assertEquals(10, testClient.getConnectionStabilityTime()); assertTrue(testClient.isOfflinePublishQueueEnabled()); assertEquals(100L, (long)testClient.getOfflinePublishQueueBound()); assertEquals(Region.getRegion(Regions.US_EAST_1), testClient.getRegion()); assertEquals(MqttManagerConnectionState.Disconnected, testClient.getConnectionState()); }