Search in sources :

Example 6 with DeviceOperations

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

the class MqttDeviceTwinTest method receiveDoesNotSetVersionOnResTopicIfNotFound.

/*
    **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 receiveDoesNotSetVersionOnResTopicIfNotFound() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "201" + "/?$rid=" + mockReqId;
    IotHubTransportMessage receivedMessage = null;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
        testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
        Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
        assertEquals(receivedMessage.getRequestId(), mockReqId);
        assertEquals("201", receivedMessage.getStatus());
        assertNull(receivedMessage.getVersion());
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 7 with DeviceOperations

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations 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() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "201" + "/?$rid=" + mockReqId + "&$version=" + mockVersion;
    IotHubTransportMessage receivedMessage = null;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
        testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
        Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
        assertEquals(receivedMessage.getRequestId(), mockReqId);
        assertEquals("201", receivedMessage.getStatus());
        assertEquals(receivedMessage.getVersion(), mockVersion);
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 8 with DeviceOperations

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

the class MqttDeviceTwinTest method receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds.

@Test
public void receiveParsesResponseTopicForUpdateReportedPropertiesSucceeds() throws TransportException {
    final byte[] actualPayload = "".getBytes(StandardCharsets.UTF_8);
    /*
            The following does not work
            final byte[] actualPayload = null;
         */
    final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId + "&$version=" + mockVersion;
    IotHubTransportMessage receivedMessage = null;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
        testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
        Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
        assertEquals("200", receivedMessage.getStatus());
        assertEquals(receivedMessage.getRequestId(), mockReqId);
        assertEquals(receivedMessage.getVersion(), mockVersion);
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 9 with DeviceOperations

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

the class MqttDeviceTwinTest method receiveDoesNotSetDataForUpdateReportedPropResp.

/*
    ** 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]
     */
@Test
public void receiveDoesNotSetDataForUpdateReportedPropResp() throws TransportException {
    final byte[] actualPayload = "".getBytes(StandardCharsets.UTF_8);
    /*
            The following does not work
            final byte[] actualPayload = null;
         */
    final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId + "&$version=" + mockVersion;
    IotHubTransportMessage receivedMessage = null;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
        testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
        Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        Deencapsulation.setField(testTwin, "stateLock", new Object());
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
        assertEquals(receivedMessage.getRequestId(), mockReqId);
        assertEquals("200", receivedMessage.getStatus());
        assertEquals(receivedMessage.getVersion(), mockVersion);
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertEquals(0, receivedMessageBytes.length);
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 10 with DeviceOperations

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations 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() throws TransportException {
    final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId;
    IotHubTransportMessage receivedMessage = null;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
        testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
        Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
        Map<String, DeviceOperations> requestMap = new HashMap<>();
        requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
        Deencapsulation.setField(testTwin, "requestMap", requestMap);
        // act
        receivedMessage = testTwin.receive();
    } finally {
        // assert
        assertNotNull(receivedMessage);
        assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
        assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
        assertEquals(receivedMessage.getRequestId(), mockReqId);
        assertEquals("200", receivedMessage.getStatus());
        assertNull(receivedMessage.getVersion());
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) HashMap(java.util.HashMap) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Aggregations

DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)29 Test (org.junit.Test)26 HashMap (java.util.HashMap)18 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)16 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)16 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)11 Message (com.microsoft.azure.sdk.iot.device.Message)9 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)8 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)8 MutablePair (org.apache.commons.lang3.tuple.MutablePair)8 Pair (org.apache.commons.lang3.tuple.Pair)8 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)3 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)2 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)2 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)1 Map (java.util.Map)1 Verifications (mockit.Verifications)1 Symbol (org.apache.qpid.proton.amqp.Symbol)1 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)1 Properties (org.apache.qpid.proton.amqp.messaging.Properties)1