Search in sources :

Example 36 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method parsePayloadReturnNullIfTopicIsNull.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_010: [**If the topic is null then parsePayload shall stop parsing for payload and return.**]**
     */
@Test
public void parsePayloadReturnNullIfTopicIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    //act
    byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", String.class);
    //assert
    assertNull(parsedPayload);
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 37 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method parsePayloadReturnsBytesForSpecifiedTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_009: [**This parsePayload method look for payload for the corresponding topic from the received messagesqueue.**]**
     */
@Test
public void parsePayloadReturnsBytesForSpecifiedTopic(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    String insertTopic = "$iothub/twin/" + anyString;
    final byte[] insertMessage = { 0x61, 0x62, 0x63 };
    ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
    testMap.put(insertTopic, insertMessage);
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    //act
    byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", insertTopic);
    //assert
    assertNotNull(parsedPayload);
    assertEquals(insertMessage.length, parsedPayload.length);
    for (int i = 0; i < parsedPayload.length; i++) {
        assertEquals(parsedPayload[i], insertMessage[i]);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 38 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method receiveParsesResponseTopicForGetTwinSucceeds.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_038: [**If the topic is of type response topic then this method shall parse further for status and set it for the message by calling setStatus for the message**]**
     */
@Test
public void receiveParsesResponseTopicForGetTwinSucceeds(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId;
    DeviceTwinMessage receivedMessage = null;
    try {
        //arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        String insertTopic = expectedTopic;
        ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
        testMap.put(insertTopic, actualPayload);
        Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        //act
        receivedMessage = (DeviceTwinMessage) testTwin.receive();
    } finally {
        //assert
        assertNotNull(receivedMessage);
        assertTrue(receivedMessage.getMessageType() == MessageType.DeviceTwin);
        assertTrue(receivedMessage.getDeviceOperationType() == DEVICE_OPERATION_TWIN_GET_RESPONSE);
        assertTrue(receivedMessage.getRequestId().equals(mockReqId));
        assertTrue(receivedMessage.getStatus().equals("200"));
        assertTrue(receivedMessage.getVersion() == null);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) HashMap(java.util.HashMap) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 39 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method parsePayloadThrowsExceptionTopicIsNotFound.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_011: [**If the topic is non-null and received messagesqueue could not locate the payload then this method shall throw IOException**]**
     */
@Test(expected = IOException.class)
public void parsePayloadThrowsExceptionTopicIsNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    String insertTopic = "$iothub/twin/res";
    String notTwinTopic = "$iothub/NotTwin/res";
    ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
    testMap.put(insertTopic, "DataData".getBytes());
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    //act
    byte[] parsedPayload = Deencapsulation.invoke(testTwin, "parsePayload", notTwinTopic);
//assert
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 40 with MqttDeviceTwin

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

the class MqttDeviceTwinTest method receiveReturnsNullMessageIfTopicNotFound.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_034: [**If the call parseTopic returns null or empty string then this method shall do nothing and return null**]**
     */
@Test
public void receiveReturnsNullMessageIfTopicNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/NOTtwin/NOTPATCH_NOTRES/properties/" + "?$version=" + mockVersion;
    DeviceTwinMessage receivedMessage = null;
    try {
        //arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        String insertTopic = expectedTopic;
        ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
        testMap.put(insertTopic, actualPayload);
        Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
        //act
        receivedMessage = (DeviceTwinMessage) testTwin.receive();
    } finally {
        //assert
        assertNull(receivedMessage);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Aggregations

MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)42 Test (org.junit.Test)42 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)23 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)17 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 HashMap (java.util.HashMap)8 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)3 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)3 Verifications (mockit.Verifications)3 State (com.microsoft.azure.sdk.iot.device.transport.State)2 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)2 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)1 IOException (java.io.IOException)1 NonStrictExpectations (mockit.NonStrictExpectations)1