Search in sources :

Example 16 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 stopThrowsExceptionIfUnSubscribesFails.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_020: [**stop method shall unsubscribe from twin response topic ($iothub/twin/res/#) and throw IoException otherwise.**]**
     */
@Test(expected = IOException.class)
public void stopThrowsExceptionIfUnSubscribesFails(@Mocked final Mqtt mockMqtt) throws IOException {
    try {
        //arrange
        new StrictExpectations() {

            {
                Deencapsulation.invoke(mockMqtt, "unsubscribe", resTopic);
                result = mockIOException;
            }
        };
        MqttDeviceTwin testTwin = new MqttDeviceTwin();
        Deencapsulation.setField(testTwin, "isStarted", true);
        //act
        testTwin.stop();
    } finally {
    //assert
    }
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 17 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 receiveSetsReqIdOnResTopic.

/*
    **Tests_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**]**
     */
@Test
public void receiveSetsReqIdOnResTopic(@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 18 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 startSubscribesToDeviceTwinResponse.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_019: [**start method shall subscribe to twin response topic ($iothub/twin/res/#) if connected.**]**
     */
@Test
public void startSubscribesToDeviceTwinResponse(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    //act
    testTwin.start();
    //assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "subscribe", resTopic);
            times = 1;
        }
    };
}
Also used : MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 19 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 receiveThrowsUnsupportedExceptionOnAnythingOtherThenPatchDesiredProp.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_043: [**If the topic is not of type response for desired properties then this method shall throw unsupportedoperation exception**]**
     */
@Test(expected = UnsupportedOperationException.class)
public void receiveThrowsUnsupportedExceptionOnAnythingOtherThenPatchDesiredProp(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes();
    final String expectedTopic = "$iothub/twin/PATCH/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)

Example 20 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 parseTopicReturnsNullIfRecevedQueueIsEmpty.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_006: [**If received messages queue is empty then parseTopic shall return null string.**]**
     */
@Test
public void parseTopicReturnsNullIfRecevedQueueIsEmpty(@Mocked final Mqtt mockMqtt) throws IOException {
    //arrange
    MqttDeviceTwin testTwin = new MqttDeviceTwin();
    ConcurrentSkipListMap<String, byte[]> testMap = new ConcurrentSkipListMap<String, byte[]>();
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    //act
    String parsedTopic = Deencapsulation.invoke(testTwin, "parseTopic");
    //assert
    assertNull(parsedTopic);
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) 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