assertEquals(MqttManagerConnectionState.Reconnecting, testClient.getConnectionState()); testClient.publishString("test payload 0", "test/topic", AWSIotMqttQos.QOS0); testClient.publishString("test payload " + i, "test/topic", AWSIotMqttQos.QOS0); testClient.publishString("test payload 9", "test/topic", AWSIotMqttQos.QOS0); assertEquals(10, testClient.getMqttMessageQueue().size()); testClient.publishString("test payload 10", "test/topic", AWSIotMqttQos.QOS0); assertEquals(11, testClient.getMqttMessageQueue().size());
@Test public void testPublishWithCallbackConnecting() 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(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); // publish, though not yet connected testClient.publishString("test payload 0", "test/topic", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); // we don't queue when Disconnected (user disconnect or auto-reconnect exhausted) assertEquals(0, testClient.getMqttMessageQueue().size()); // callback not called assertEquals(1, mdcb.statuses.size()); assertEquals(1, mdcb.userDatas.size()); }
@Test public void testPublishStringQos1WithCallbackFailure() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnPublish = true; 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); mockClient.mockConnectSuccess(); SimpleUnitTestObject suto = new SimpleUnitTestObject(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS1, mdcb, suto); assertEquals(1, mockClient.connectCalls); assertEquals(1, mdcb.statuses.size()); assertEquals(AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Fail, mdcb.statuses.get(0)); assertEquals(1, mdcb.userDatas.size()); assertEquals(suto, mdcb.userDatas.get(0)); }
@Test public void testPublishWithCallbackReconnecting() 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.setOfflinePublishQueueEnabled(false); TestClientStatusCallback csb = new TestClientStatusCallback(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectFail(); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Reconnecting, csb.statuses.get(1)); // publish, though not yet connected final String userData = "TEST_TOKEN"; testClient.publishString("test payload 0", "test/topic", AWSIotMqttQos.QOS0, mdcb, userData); // we don't queue when Disconnected (user disconnect or auto-reconnect exhausted) assertEquals(0, testClient.getMqttMessageQueue().size()); // callback not called assertEquals(1, mdcb.statuses.size()); assertEquals(AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Fail, mdcb.statuses.get(0)); assertEquals(1, mdcb.userDatas.size()); assertEquals(userData, mdcb.userDatas.get(0)); }
@Test public void testPublishStringQos0WithCallbackFailure() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnPublish = true; 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); mockClient.mockConnectSuccess(); SimpleUnitTestObject suto = new SimpleUnitTestObject(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS0, mdcb, suto); assertEquals(1, mockClient.connectCalls); assertEquals(1, mdcb.statuses.size()); assertEquals(AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Fail, mdcb.statuses.get(0)); assertEquals(1, mdcb.userDatas.size()); assertEquals(suto, mdcb.userDatas.get(0)); }
@Test(expected = AmazonClientException.class) public void testPublishConnecting() 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); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); // publish, though not yet connected 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()); }
@Test public void testPublishWithCallbackDisconnected() 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(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); 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, mdcb, "TEST_TOKEN"); // we don't queue when Disconnected (user disconnect or auto-reconnect exhausted) assertEquals(0, testClient.getMqttMessageQueue().size()); // callback not called assertEquals(1, mdcb.statuses.size()); assertEquals(1, mdcb.userDatas.size()); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringWithCallbackNullQos() 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", "unit/test/topic", null, mdcb, "TEST_TOKEN"); }
@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 public void testPublishStringQos0WithCallback() 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); mockClient.mockConnectSuccess(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); assertEquals(1, mockClient.connectCalls); assertEquals(1, mockClient.publishCalls); assertEquals(0, mockClient.mostRecentPublishQoS); assertEquals(false, mockClient.mostRecentPublishRetained); assertEquals("test1", new String(mockClient.mostRecentPublishPayload, StringUtils.UTF8)); assertEquals(0, mdcb.statuses.size()); // callback hasn't been called yet MockDeliveryToken testDeliveryToken = new MockDeliveryToken(); testDeliveryToken.setUserContext(mockClient.mostRecentPublishUserContext); mockClient.mockCallback.deliveryComplete(testDeliveryToken); assertEquals(1, mdcb.statuses.size()); // now it has assertEquals(AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Success, mdcb.statuses.get(0)); assertEquals(1, mdcb.userDatas.size()); assertEquals("TEST_TOKEN", mdcb.userDatas.get(0)); }
@Test(expected = AmazonClientException.class) public void testPublishStringQos0Failure() throws Exception { MockMqttClient mockClient = new MockMqttClient(); mockClient.throwsExceptionOnPublish = 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(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS0); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringWithCallbackNullString() 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(null, "unit/test/topic", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringWithCallbackEmptyTopic() 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", "", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringNullString() 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); testClient.publishString(null, "unit/test/topic", AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringNullQos() 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); testClient.publishString("test1", "unit/test/topic", null); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@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()); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringEmptyTopic() 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); testClient.publishString("test1", "", AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@Test(expected = IllegalArgumentException.class) public void testPublishStringNullTopic() 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); testClient.publishString("test1", null, AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@Test public void testPublishStringQos0() 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(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(1, mockClient.publishCalls); assertEquals(0, mockClient.mostRecentPublishQoS); assertEquals(false, mockClient.mostRecentPublishRetained); assertEquals("test1", new String(mockClient.mostRecentPublishPayload, StringUtils.UTF8)); }
@Test public void testPublishStringQos1() 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(); testClient.publishString("test1", "unit/test/topic", AWSIotMqttQos.QOS1); assertEquals(1, mockClient.connectCalls); assertEquals(1, mockClient.publishCalls); assertEquals(1, mockClient.mostRecentPublishQoS); assertEquals(false, mockClient.mostRecentPublishRetained); assertEquals("test1", new String(mockClient.mostRecentPublishPayload, StringUtils.UTF8)); }