Search in sources :

Example 1 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method receiveSucceeds.

/*
    Tests_SRS_MqttDeviceMethod_25_024: [**This method shall call parseTopic to parse the topic from the received Messages queue looking for presence of $iothub/methods/ in the topics .**]**
    Tests_SRS_MqttDeviceMethod_25_026: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
    Tests_SRS_MqttDeviceMethod_25_028: [**If the topic is of type post topic then this method shall parse further for method name and set it for the message by calling setMethodName for the message**]**
    Tests_SRS_MqttDeviceMethod_25_030: [**If the topic is of type post topic then this method shall parse further to look for request id which if found is set by calling setRequestId**]**
    Tests_SRS_MqttDeviceMethod_25_032: [**If the topic is of type post topic and if method name and request id has been successfully parsed then this method shall set operation type as DEVICE_OPERATION_METHOD_RECEIVE_REQUEST **]**
     */
@Test
public void receiveSucceeds() throws IOException {
    //arrange
    String topic = "$iothub/methods/POST/testMethod/?$rid=10";
    byte[] actualPayload = "TestPayload".getBytes();
    ConcurrentSkipListMap<String, byte[]> testAllReceivedMessages = new ConcurrentSkipListMap<>();
    testAllReceivedMessages.put(topic, actualPayload);
    Deencapsulation.setField(mockedMqtt, "allReceivedMessages", testAllReceivedMessages);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    Message testMessage = testMethod.receive();
    DeviceMethodMessage testDMMessage = (DeviceMethodMessage) testMessage;
    //assert
    assertNotNull(testMessage);
    assertTrue(testMessage.getMessageType().equals(MessageType.DeviceMethods));
    assertTrue(testDMMessage.getRequestId().equals(String.valueOf(10)));
    assertTrue(testDMMessage.getMethodName().equals("testMethod"));
    assertTrue(testDMMessage.getDeviceOperationType().equals(DEVICE_OPERATION_METHOD_RECEIVE_REQUEST));
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Message(com.microsoft.azure.sdk.iot.device.Message) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 2 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method sendThrowsIfNotStarted.

/*
    Tests_SRS_MqttDeviceMethod_25_018: [**send method shall throw an IoException if device method has not been started yet.**]**
     */
@Test(expected = IOException.class)
public void sendThrowsIfNotStarted() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    //act
    testMethod.send(testMessage);
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 3 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method sendDoesNotSendOnDifferentMessageType.

/*
    Tests_SRS_MqttDeviceMethod_25_017: [**send method shall return if the message is not of Type DeviceMethod.**]**
     */
@Test
public void sendDoesNotSendOnDifferentMessageType() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setMessageType(MessageType.DeviceTwin);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    testMethod.send(testMessage);
    //assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockedMqtt, "publish", anyString, actualPayload);
            maxTimes = 0;
            Deencapsulation.invoke(mockedMqtt, "subscribe", anyString);
            maxTimes = 0;
        }
    };
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 4 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method sendThrowsOnNullRequestID.

/*
    Tests_SRS_MqttDeviceMethod_25_021: [**send method shall throw an IoException if message contains a null or empty request id if the operation is of type DEVICE_OPERATION_METHOD_SEND_RESPONSE.**]**
     */
@Test(expected = IOException.class)
public void sendThrowsOnNullRequestID() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setMessageType(MessageType.DeviceMethods);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    testMethod.start();
    //act
    testMethod.send(testMessage);
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 5 with DeviceMethodMessage

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

the class DeviceMethodMessageTest method getMethodNameGets.

/*
    **Tests_SRS_DEVICEMETHODMESSAGE_25_005: [**The method shall return the methodName either set by the setter or the default (null) if unset so far.**]**
     */
@Test
public void getMethodNameGets() {
    //arrange
    DeviceMethodMessage testDMMessage = new DeviceMethodMessage(new byte[0]);
    testDMMessage.setMethodName("TestName");
    //act
    String actualMethodName = testDMMessage.getMethodName();
    //assert
    assertEquals(actualMethodName, "TestName");
}
Also used : DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) Test(org.junit.Test)

Aggregations

DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)15 Test (org.junit.Test)15 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)10 Verifications (mockit.Verifications)4 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)2 Message (com.microsoft.azure.sdk.iot.device.Message)2 HashMap (java.util.HashMap)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)1 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)1 NonStrictExpectations (mockit.NonStrictExpectations)1