Search in sources :

Example 21 with MqttMessaging

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method parsePayloadShallThrowIOExceptionIfQueueIsEmpty.

@Test(expected = IOException.class)
public void parsePayloadShallThrowIOExceptionIfQueueIsEmpty(@Mocked final Mqtt mockMqtt) throws IOException {
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    final String insertTopic = "devices/" + clientId + "/messages/devicebound/abc";
    final byte[] insertMessage = { 0x61, 0x62, 0x63 };
    ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    byte[] retrieveMessage = Deencapsulation.invoke(testMqttMessaging, "parsePayload", insertTopic);
    assertNull(retrieveMessage);
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 22 with MqttMessaging

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method parseTopicReturnsNullIfQueueIsEmpty.

/*
    **Tests_SRS_MqttMessaging_25_007: [**If received messages queue is empty then parseTopic shall return null string.**]**
     */
@Test
public void parseTopicReturnsNullIfQueueIsEmpty(@Mocked final Mqtt mockMqtt) throws IOException {
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    String retrieveTopic = Deencapsulation.invoke(testMqttMessaging, "parseTopic");
    assertNull(retrieveTopic);
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 23 with MqttMessaging

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method sendShallThrowIOExceptionIfMessageIsEmpty.

@Test(expected = IOException.class)
public void sendShallThrowIOExceptionIfMessageIsEmpty(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] messageBody = {};
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = messageBody;
            Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
            result = mockIOException;
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    testMqttMessaging.send(null);
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 1;
            Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, new byte[1]);
            times = 1;
        }
    };
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 24 with MqttMessaging

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.

the class MqttTest method receiveReturnsNullMessageWhenParseTopicReturnsNull.

/*
    **Tests_SRS_Mqtt_25_022: [**If the call parseTopic returns null or empty string then this method shall do nothing and return null**]**
     */
@Test
public void receiveReturnsNullMessageWhenParseTopicReturnsNull() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(true);
    baseConnectExpectation();
    new MockUp<MqttMessaging>() {

        @Mock
        String parseTopic() throws IOException {
            return null;
        }

        @Mock
        byte[] parsePayload(String topic) throws IOException {
            return payload;
        }
    };
    final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    try {
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Message receivedMessage = mockMqtt.receive();
        //assert
        assertNull(receivedMessage);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 25 with MqttMessaging

use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging in project azure-iot-sdk-java by Azure.

the class MqttMessagingTest method parsePayloadRemovesTheKeyValuePairFromQueueIfFound.

/*
    **Tests_SRS_MqttMessaging_25_014: [**If the topic is found in the message queue then parsePayload shall delete it from the queue.**]**
     */
@Test
public void parsePayloadRemovesTheKeyValuePairFromQueueIfFound(@Mocked final Mqtt mockMqtt) throws IOException {
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    final String insertTopic = "devices/" + clientId + "/messages/devicebound/abc";
    final byte[] insertMessage = { 0x61, 0x62, 0x63 };
    ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
    testMap.put(insertTopic, insertMessage);
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    byte[] retrieveMessage = Deencapsulation.invoke(testMqttMessaging, "parsePayload", insertTopic);
    assertTrue(testMap.isEmpty());
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Aggregations

MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)29 Test (org.junit.Test)29 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)8 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)5 Verifications (mockit.Verifications)5 Message (com.microsoft.azure.sdk.iot.device.Message)4 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)4 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)3 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)3 IOException (java.io.IOException)3 NonStrictExpectations (mockit.NonStrictExpectations)3 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)2 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)2 State (com.microsoft.azure.sdk.iot.device.transport.State)1