Search in sources :

Example 1 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(@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 2 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(@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)

Example 3 with DeviceOperations

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

the class DeviceTwinMessageTest method getTwinOpGets.

/*
    **Tests_SRS_DEVICETWINMESSAGE_25_010: [**The function shall return the operation type either set by the setter or the default (DEVICE_OPERATION_UNKNOWN) if unset so far.**]**
     */
@Test
public void getTwinOpGets() {
    // arrange
    final byte[] actualData = {};
    IotHubTransportMessage msg = new IotHubTransportMessage(actualData, MessageType.DEVICE_TWIN);
    msg.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
    // act
    DeviceOperations op = msg.getDeviceOperationType();
    // assert
    assertEquals(op, DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
}
Also used : DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 4 with DeviceOperations

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

the class DeviceTwinMessageTest method constructorSetsRequiredPrivateMembers.

/*
    **Tests_SRS_DEVICETWINMESSAGE_25_001: [**The constructor shall save the message body by calling super with the body as parameter.**]**
     */
@Test
public void constructorSetsRequiredPrivateMembers() {
    // arrange
    final byte[] actualData = {};
    // act
    IotHubTransportMessage msg = new IotHubTransportMessage(actualData, MessageType.DEVICE_TWIN);
    // assert
    String actualVersion = Deencapsulation.getField(msg, "version");
    String actualRequestId = Deencapsulation.getField(msg, "requestId");
    String actualStatus = Deencapsulation.getField(msg, "status");
    DeviceOperations operationType = Deencapsulation.getField(msg, "operationType");
    assertNull(actualVersion);
    assertNull(actualRequestId);
    assertNull(actualStatus);
    assertEquals(operationType, DeviceOperations.DEVICE_OPERATION_UNKNOWN);
}
Also used : DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 5 with DeviceOperations

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

the class DeviceTwinMessageTest method gettersGetDefaultsIfNotSet.

@Test
public void gettersGetDefaultsIfNotSet() {
    // arrange
    final byte[] actualData = {};
    IotHubTransportMessage msg = new IotHubTransportMessage(actualData, MessageType.DEVICE_TWIN);
    // act
    String testVersion = msg.getVersion();
    String testRequestId = msg.getRequestId();
    String testStatus = msg.getStatus();
    DeviceOperations testOpType = msg.getDeviceOperationType();
    // assert
    String actualVersion = Deencapsulation.getField(msg, "version");
    String actualRequestId = Deencapsulation.getField(msg, "requestId");
    String actualStatus = Deencapsulation.getField(msg, "status");
    DeviceOperations operationType = Deencapsulation.getField(msg, "operationType");
    assertEquals(actualRequestId, testRequestId);
    assertEquals(actualStatus, testStatus);
    assertEquals(actualVersion, testVersion);
}
Also used : DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) 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