/** * 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); }
/** * Send a message to an MQTT topic. * * @param str The message payload to be sent (as a String). * @param topic The topic on which to publish. * @param qos The quality of service requested for this message. */ public void publishString(String str, String topic, AWSIotMqttQos qos) { if (str == null) { throw new IllegalArgumentException("publish string is null"); } if (topic == null || topic.isEmpty()) { throw new IllegalArgumentException("topic is null or empty"); } if (qos == null) { throw new IllegalArgumentException("QoS cannot be null"); } publishData(str.getBytes(StringUtils.UTF8), topic, qos); }
/** * Send a message to an MQTT topic. * * @param str The message payload to be sent (as a String). * @param topic The topic on which to publish. * @param qos The quality of service requested for this message. * @param cb Callback for message status. * @param userData User defined data which will be passed back to the user when the * callback is invoked. */ public void publishString(String str, String topic, AWSIotMqttQos qos, AWSIotMqttMessageDeliveryCallback cb, Object userData) { if (str == null) { throw new IllegalArgumentException("publish string is null"); } if (topic == null || topic.isEmpty()) { throw new IllegalArgumentException("topic is null or empty"); } if (qos == null) { throw new IllegalArgumentException("QoS cannot be null"); } publishData(str.getBytes(StringUtils.UTF8), topic, qos, cb, userData); }
/** * 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(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackNullTopic() 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), null, AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
@Test(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackNullQos() 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), "unit/test/topic", null, 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 = IllegalArgumentException.class) public void testPublishDataNullQos() 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.publishData("test1".getBytes(StringUtils.UTF8), "unit/test/topic", null); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@Test(expected = IllegalArgumentException.class) public void testPublishDataNullTopic() 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.publishData("test1".getBytes(StringUtils.UTF8), null, AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@Test(expected = IllegalArgumentException.class) public void testPublishDataEmptyTopic() 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.publishData("test1".getBytes(StringUtils.UTF8), "", AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }
@Test public void testPublishDataQos0() 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.publishData("test1".getBytes(StringUtils.UTF8), "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(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackNullData() 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(null, "unit/test/topic", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
@Test public void testPublishDataQos1() 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.publishData("test1".getBytes(StringUtils.UTF8), "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)); }
/** * Send a message to an MQTT topic. * * @param str The message payload to be sent (as a String). * @param topic The topic on which to publish. * @param qos The quality of service requested for this message. */ public void publishString(String str, String topic, AWSIotMqttQos qos) { if (str == null) { throw new IllegalArgumentException("publish string is null"); } if (topic == null || topic.isEmpty()) { throw new IllegalArgumentException("topic is null or empty"); } if (qos == null) { throw new IllegalArgumentException("QoS cannot be null"); } publishData(str.getBytes(StringUtils.UTF8), topic, qos); }
/** * Send a message to an MQTT topic. * * @param str The message payload to be sent (as a String). * @param topic The topic on which to publish. * @param qos The quality of service requested for this message. * @param cb Callback for message status. * @param userData User defined data which will be passed back to the user when the * callback is invoked. */ public void publishString(String str, String topic, AWSIotMqttQos qos, AWSIotMqttMessageDeliveryCallback cb, Object userData) { if (str == null) { throw new IllegalArgumentException("publish string is null"); } if (topic == null || topic.isEmpty()) { throw new IllegalArgumentException("topic is null or empty"); } if (qos == null) { throw new IllegalArgumentException("QoS cannot be null"); } publishData(str.getBytes(StringUtils.UTF8), topic, qos, cb, userData); }
@Test(expected = IllegalArgumentException.class) public void testPublishDataNullData() 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.publishData(null, "unit/test/topic", AWSIotMqttQos.QOS0); assertEquals(1, mockClient.connectCalls); assertEquals(0, mockClient.publishCalls); }