@Test(expected = AmazonClientException.class) public void testUnSubscribeToTopicException() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnUnsubscribe = true; 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, null); TestNewMessageCallback mcb = new TestNewMessageCallback(); testClient.subscribeToTopic("unit/test/topic", AWSIotMqttQos.QOS0, mcb); testClient.unsubscribeTopic("unit/test/topic"); }
@Test(expected = AmazonClientException.class) public void testPublishDisconnected() 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.setOfflinePublishQueueBound(100); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); testClient.disconnect(); testClient.publishString("test payload 0", "test/topic", AWSIotMqttQos.QOS0); // we don't queue when Disconnected (user disconnect or auto-reconnect exhausted) assertEquals(0, testClient.getMqttMessageQueue().size()); }
if (isMetricsEnabled()) { options.setUserName("?SDK=Android&Version=" + SDK_VERSION); (isMetricsEnabled() ? "enabled" : "disabled") + ", username: " + options.getUserName()); resetReconnect(); userDisconnect = false; setupCallbackForMqttClient(); userConnectionCallback(); case MqttException.REASON_CODE_CLIENT_CONNECTED: connectionState = MqttManagerConnectionState.Connected; userConnectionCallback(); break; case MqttException.REASON_CODE_CONNECT_IN_PROGRESS: connectionState = MqttManagerConnectionState.Connecting; userConnectionCallback(); break; default: connectionState = MqttManagerConnectionState.Disconnected; userConnectionCallback(e); break; userConnectionCallback(exception);
public void testLegalReconnectTimeEqual() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), null); assertEquals(4, testClient.getMaxReconnectRetryTime()); testClient.setReconnectRetryLimits(16, 16); assertEquals(16, testClient.getMinReconnectRetryTime()); assertEquals(16, testClient.getMaxReconnectRetryTime()); }
/** * Test Queue Mode Accessors */ @Test public void testGetSetFullQueueMode() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); assertEquals(false, testClient.fullPublishQueueKeepsOldestMessages()); testClient.setFullQueueToKeepOldestMessages(); assertEquals(true, testClient.fullPublishQueueKeepsOldestMessages()); testClient.setFullQueueToKeepNewestMessages(); assertEquals(false, testClient.fullPublishQueueKeepsOldestMessages()); }
final ArrayList<String> messages = new ArrayList<String>(); AWSIotMqttManager mqttManager = new AWSIotMqttManager("int-test-ws-rc", Region.getRegion(Regions.US_EAST_1), endpointPrefix); mqttManager.setAutoReconnect(true); mqttManager.connect(credentialsProvider, new AWSIotMqttClientStatusCallback() { @Override public void onStatusChanged(AWSIotMqttClientStatus status, Throwable throwable) { mqttManager.subscribeToTopic("sdk/test/integration/ws/reconnect", AWSIotMqttQos.QOS0, new AWSIotMqttNewMessageCallback() { @Override public void onMessageArrived(String topic, byte[] data) { mqttManager.publishString("message 0", "sdk/test/integration/ws/reconnect", AWSIotMqttQos.QOS0); mqttManager.publishString("please kill me", "$aws", AWSIotMqttQos.QOS0); mqttManager.disconnect();
@Test(expected = IllegalArgumentException.class) public void testPublishStringWithCallbackNullTopic() 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(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); testClient.publishString("test1", null, AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
@Test(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackEmptyTopic() 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(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); testClient.publishData("test1".getBytes(StringUtils.UTF8), "", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
@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(); }
@Test(expected = IllegalArgumentException.class) public void testConnectWebSocketNullCredentialsProvider() 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((AWSCredentialsProvider)null, csb); }
@Test public void testIsReadyToPublishTrue() 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(); assertTrue(testClient.isReadyToPublish()); }
@Test public void testEnableMetricsCollection() { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); assertTrue(testClient.isMetricsEnabled()); testClient.setMetricsIsEnabled(false); assertFalse(testClient.isMetricsEnabled()); }
@Test public void testDisconnectWhenNotConnected() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); mockClient.isConnected = false; testClient.disconnect(); assertEquals(0, mockClient.disconnectCalls); }
/** * Publish data to an MQTT topic. * * @param data The message payload to be sent as a byte array. * @param topic The topic on which to publish. * @param qos The quality of service requested for this message. */ public void publishData(byte[] data, String topic, AWSIotMqttQos qos) { publishData(data, topic, qos, null, null); }
@Test public void testSetMaxAutoReconnectAttemptsForever() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMaxAutoReconnectAttepts(-1); // -1 = retry forever assertEquals(-1, testClient.getMaxAutoReconnectAttempts()); }
@Test(expected = IllegalArgumentException.class) public void testSetOfflinePublishQueueBoundNegative() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setOfflinePublishQueueBound(-5); assertEquals(100, (int) testClient.getOfflinePublishQueueBound()); }
@Test(expected = IllegalArgumentException.class) public void testSetKeepAliveIllegalValue() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setKeepAlive(-10); }
@Test public void testIsReadyToPublishFalseNull() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(null); assertFalse(testClient.isReadyToPublish()); }
@Test(expected = IllegalArgumentException.class) public void testCreateClientNullClientId() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager(null, Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); }
@Test(expected = IllegalArgumentException.class) public void testReconnectTimeoutException() throws Exception { AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setReconnectRetryLimits(10, 5); }