Search in sources :

Example 1 with DeviceTwinMessage

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwin method receive.

@Override
public Message receive() throws IOException {
    DeviceTwinMessage messsage;
    /*
        **Codes_SRS_MQTTDEVICETWIN_25_033: [**This method shall call parseTopic to parse the topic from the recevived Messages queue corresponding to the messaging client's operation.**]**
         */
    String topic = parseTopic();
    if (topic != null && topic.length() > 0) {
        /*
            **Codes_SRS_MQTTDEVICETWIN_25_035: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
             */
        byte[] data = parsePayload(topic);
        if (topic.length() > RES.length() && topic.startsWith(RES)) {
            // Tokenize on backslash
            String[] topicTokens = topic.split(Pattern.quote("/"));
            if (data != null && data.length > 0) {
                /*
                    **Codes_SRS_MQTTDEVICETWIN_25_044: [**If the topic is of type response then this method shall set data and operation type as DEVICE_OPERATION_TWIN_GET_RESPONSE if data is not null**]**
                     */
                messsage = new DeviceTwinMessage(data);
                messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_UNKNOWN);
            } else {
                // Case for $iothub/twin/res/{status}/?$rid={request id}
                /*
                    **Tests_SRS_MQTTDEVICETWIN_25_045: [**If the topic is of type response then this method shall set empty data and operation type as DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE if data is null or empty**]**
                     */
                // empty body
                messsage = new DeviceTwinMessage(new byte[0]);
                messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_UNKNOWN);
            }
            if (topicTokens.length > STATUS_TOKEN) {
                /*
                    **Codes_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**]**
                     */
                messsage.setStatus(getStatus(topicTokens[STATUS_TOKEN]));
            } else {
                throw new IOException("Message received without status");
            }
            if (topicTokens.length > REQID_TOKEN) {
                /*
                    **Codes_SRS_MQTTDEVICETWIN_25_040: [**If the topic is of type response topic then this method shall parse further to look for request id which if found is set by calling setRequestId**]**
                     */
                String requestId = getRequestId(topicTokens[REQID_TOKEN]);
                messsage.setRequestId(requestId);
                if (requestMap.containsKey(requestId)) {
                    switch(requestMap.remove(requestId)) {
                        case DEVICE_OPERATION_TWIN_GET_REQUEST:
                            messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_RESPONSE);
                            break;
                        case DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST:
                            messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
                            break;
                        default:
                            messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_UNKNOWN);
                    }
                } else {
                    throw new UnsupportedOperationException();
                }
            }
            if (topicTokens.length > VERSION_TOKEN) {
                /*
                    **Codes_SRS_MQTTDEVICETWIN_25_041: [**If the topic is of type response topic then this method shall parse further to look for version which if found is set by calling setVersion**]**
                     */
                messsage.setVersion(getVersion(topicTokens[VERSION_TOKEN]));
            }
        } else if (topic.length() > PATCH.length() && topic.startsWith(PATCH)) {
            if (topic.startsWith(PATCH + BACKSLASH + PROPERTIES + BACKSLASH + DESIRED)) {
                if (data != null) {
                    /*
                        **Codes_SRS_MQTTDEVICETWIN_25_046: [**If the topic is of type patch for desired properties then this method shall set the data and operation type as DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE if data is not null or empty**]**
                         */
                    messsage = new DeviceTwinMessage(data);
                    messsage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
                } else {
                    /*
                        **Tests_SRS_MQTTDEVICETWIN_25_047: [**If the topic is of type patch for desired properties then this method shall throw unsupportedoperation exception if data is null or empty**]**
                         */
                    throw new UnsupportedOperationException();
                }
                // Case for $iothub/twin/PATCH/properties/desired/?$version={new version}
                // Tokenize on backslash
                String[] topicTokens = topic.split(Pattern.quote("/"));
                if (topicTokens.length > PATCH_VERSION_TOKEN) {
                    /*
                        **Codes_SRS_MQTTDEVICETWIN_25_042: [**If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion**]**
                         */
                    messsage.setVersion(getVersion(topicTokens[PATCH_VERSION_TOKEN]));
                }
            } else {
                /*
                    **Codes_SRS_MQTTDEVICETWIN_25_043: [**If the topic is not of type response for desired properties then this method shall throw unsupportedoperation exception**]**
                     */
                throw new UnsupportedOperationException();
            }
        } else {
            /*
                **Codes_SRS_MQTTDEVICETWIN_25_037: [**This method shall parse topic to look for only either twin response topic or twin patch topic and thorw unsupportedoperation exception other wise.**]**
                 */
            throw new UnsupportedOperationException();
        }
        logger.LogInfo("Message received on DT " + messsage.getDeviceOperationType());
        return messsage;
    } else {
        return null;
    }
}
Also used : DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) IOException(java.io.IOException)

Example 2 with DeviceTwinMessage

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveDoesNotSetVersionForDesiredPropNotifRespIfNotFound.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_042: [**If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion**]**
     */
@Test
public void receiveDoesNotSetVersionForDesiredPropNotifRespIfNotFound(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/PATCH/properties/desired/";
    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
        assertNotNull(receivedMessage);
        assertTrue(receivedMessage.getMessageType() == MessageType.DeviceTwin);
        assertTrue(receivedMessage.getDeviceOperationType() == DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
        assertTrue(receivedMessage.getRequestId() == null);
        assertTrue(receivedMessage.getStatus() == null);
        assertTrue(receivedMessage.getVersion() == null);
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertTrue(receivedMessageBytes.length == actualPayload.length);
        for (int i = 0; i < receivedMessageBytes.length; i++) {
            assertTrue(receivedMessageBytes[i] == actualPayload[i]);
        }
    }
}
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)

Example 3 with DeviceTwinMessage

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveSetVersionForDesiredPropNotifRespIfFound.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_042: [**If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion**]**
     */
@Test
public void receiveSetVersionForDesiredPropNotifRespIfFound(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/PATCH/properties/desired/" + "?$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
        assertNotNull(receivedMessage);
        assertTrue(receivedMessage.getMessageType() == MessageType.DeviceTwin);
        assertTrue(receivedMessage.getDeviceOperationType() == DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
        assertTrue(receivedMessage.getRequestId() == null);
        assertTrue(receivedMessage.getStatus() == null);
        assertTrue(receivedMessage.getVersion().equals(mockVersion));
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertTrue(receivedMessageBytes.length == actualPayload.length);
        for (int i = 0; i < receivedMessageBytes.length; i++) {
            assertTrue(receivedMessageBytes[i] == actualPayload[i]);
        }
    }
}
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)

Example 4 with DeviceTwinMessage

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveSetsVersionOnResTopic.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_041: [**If the topic is of type response topic then this method shall parse further to look for version which if found is set by calling setVersion**]**
     */
@Test
public void receiveSetsVersionOnResTopic(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/res/" + "201" + "/?$rid=" + mockReqId + "&$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);
        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("201"));
        assertTrue(receivedMessage.getVersion().equals(mockVersion));
    }
}
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 5 with DeviceTwinMessage

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds.

@Test
public void receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "".getBytes();
    /*
            The following does not work
            final byte[] actualPayload = null;
         */
    final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId + "&$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);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_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_UPDATE_REPORTED_PROPERTIES_RESPONSE);
        assertTrue(receivedMessage.getStatus().equals("200"));
        assertTrue(receivedMessage.getRequestId().equals(mockReqId));
        assertTrue(receivedMessage.getVersion().equals(mockVersion));
    }
}
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)

Aggregations

DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)30 Test (org.junit.Test)28 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)17 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)11 HashMap (java.util.HashMap)8 IOException (java.io.IOException)2 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)1 IotHubStatusCode (com.microsoft.azure.sdk.iot.device.IotHubStatusCode)1 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)1 NonStrictExpectations (mockit.NonStrictExpectations)1 Verifications (mockit.Verifications)1