@Test public void testUnSubscribeTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); 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); assertEquals(1, mockClient.subscribeCalls); assertTrue(mockClient.mockSubscriptions.containsKey("unit/test/topic")); assertEquals((Integer) 0, mockClient.mockSubscriptions.get("unit/test/topic")); MqttMessage msg = new MqttMessage(); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(1, mcb.receivedMessages.size()); // received first one assertEquals("unit/test/topic" + "test payload", mcb.receivedMessages.get(0)); testClient.unsubscribeTopic("unit/test/topic"); assertEquals(1, mockClient.unsubscribeCalls); assertFalse(mockClient.mockSubscriptions.containsKey("unit/test/topic")); msg.setPayload("test payload".getBytes(StringUtils.UTF8)); mockClient.mockCallback.messageArrived("unit/test/topic", msg); assertEquals(1, mcb.receivedMessages.size()); // didn't receive this one }
@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 = IllegalArgumentException.class) public void testUnSubscribeToTopicEmptyTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); 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(""); assertEquals(0, mockClient.unsubscribeCalls); }
@Test(expected = IllegalArgumentException.class) public void testUnSubscribeToTopicNullTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); 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(null); assertEquals(0, mockClient.unsubscribeCalls); }
@Test public void testUnSubscribeToTopicNullMqttClient() throws Exception { MockMqttClient mockClient = new MockMqttClient(); 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.setMqttClient(null); testClient.unsubscribeTopic("unit/test/topic"); assertEquals(0, mockClient.unsubscribeCalls); }