Search in sources :

Example 11 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method sendThrowsOnInvalidOperation.

@Test(expected = IOException.class)
public void sendThrowsOnInvalidOperation() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_UNKNOWN);
    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 12 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method sendThrowsOnSendingResponseWithoutReceivingMethodInvoke.

/*
    Tests_SRS_MqttDeviceMethod_25_023: [**send method shall throw an exception if a response is sent without having a method invoke on the request id if the operation is of type DEVICE_OPERATION_METHOD_SEND_RESPONSE.**]**
     */
@Test(expected = IOException.class)
public void sendThrowsOnSendingResponseWithoutReceivingMethodInvoke() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
    testMessage.setRequestId("ReqId");
    testMessage.setStatus("testStatus");
    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 13 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method receiveReturnsEmptyPayLoadIfNullPayloadParsed.

@Test
public void receiveReturnsEmptyPayLoadIfNullPayloadParsed() throws IOException {
    //arrange
    String topic = "$iothub/methods/POST/testMethod/?$rid=10";
    byte[] actualPayload = "".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.getBytes().length == 0);
    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 14 with DeviceMethodMessage

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

the class MqttDeviceMethodTest method sendThrowsOnMismatchedRequestType.

/*
    Tests_SRS_MqttDeviceMethod_25_019: [**send method shall throw an IoException if the getDeviceOperationType() is not of type DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST or DEVICE_OPERATION_METHOD_SEND_RESPONSE .**]**
     */
@Test(expected = IOException.class)
public void sendThrowsOnMismatchedRequestType() throws IOException {
    final byte[] actualPayload = "TestMessage".getBytes();
    final DeviceMethodMessage testMessage = new DeviceMethodMessage(actualPayload);
    testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
    testMessage.setRequestId("ReqId");
    testMessage.setStatus("testStatus");
    MqttDeviceMethod testMethod = new MqttDeviceMethod();
    Map<String, DeviceOperations> testRequestMap = new HashMap<>();
    testRequestMap.put("ReqId", DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
    Deencapsulation.setField(testMethod, "requestMap", testRequestMap);
    testMethod.start();
    //act
    testMethod.send(testMessage);
}
Also used : HashMap(java.util.HashMap) DeviceOperations(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Example 15 with DeviceMethodMessage

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

the class MqttIotHubConnectionTest method receiveDeviceMethodMessageSucceeds.

@Test
public void receiveDeviceMethodMessageSucceeds() throws IOException {
    baseExpectations();
    openExpectations();
    final byte[] expectedMessageBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockDeviceMethods.receive();
            result = new DeviceMethodMessage(expectedMessageBody);
        }
    };
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    Message message = connection.receiveMessage();
    byte[] actualMessageBody = message.getBytes();
    assertNotNull(message);
    for (int i = 0; i < expectedMessageBody.length; i++) {
        assertEquals(expectedMessageBody[i], actualMessageBody[i]);
    }
    new Verifications() {

        {
            mockDeviceMethods.receive();
            times = 1;
            mockDeviceTwin.receive();
            times = 0;
            mockDeviceMessaging.receive();
            times = 0;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) DeviceTwinMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) 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