Search in sources :

Example 51 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method sendThrowsIllegalArgumentExceptionIfMessageIsNull.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_021: [send method shall throw an IllegalArgumentException if the message is null.]
     */
@Test(expected = IllegalArgumentException.class)
public void sendThrowsIllegalArgumentExceptionIfMessageIsNull(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockMessage) throws TransportException {
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId + "&$version=" + mockVersion;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        // act
        testTwin.send(null);
    } finally {
        // assert
        new Verifications() {

            {
                mockMessage.getBytes();
                times = 0;
                Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload, mockMessage);
                times = 0;
            }
        };
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 52 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method sendThrowsIllegalArgumentExceptionForGetTwinOnCorrectTopicIfReqIdIsNullOrEmpty.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_025: [send method shall throw an IllegalArgumentException if message contains a null or empty request id if the operation is of type DEVICE_OPERATION_TWIN_GET_REQUEST.]
     */
@Test(expected = IllegalArgumentException.class)
public void sendThrowsIllegalArgumentExceptionForGetTwinOnCorrectTopicIfReqIdIsNullOrEmpty(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockMessage) throws TransportException {
    final byte[] actualPayload = { 0x61, 0x62, 0x63 };
    final String expectedTopic = "$iothub/twin/GET/?$rid=" + mockReqId;
    try {
        // arrange
        MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
        testTwin.start();
        new NonStrictExpectations() {

            {
                mockMessage.getBytes();
                result = actualPayload;
                mockMessage.getMessageType();
                result = MessageType.DEVICE_TWIN;
                mockMessage.getDeviceOperationType();
                result = DEVICE_OPERATION_TWIN_GET_REQUEST;
                mockMessage.getRequestId();
                result = null;
            }
        };
        // act
        testTwin.send(mockMessage);
    } finally {
        // assert
        new Verifications() {

            {
                mockMessage.getBytes();
                times = 1;
                Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, actualPayload, mockMessage);
                times = 0;
            }
        };
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 53 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class MqttDeviceTwinTest method receiveSetVersionForDesiredPropNotifRespIfFound.

/*
    **Tests_SRS_MQTTDEVICETWIN_25_042: [If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion]
     */
@Test
public void receiveSetVersionForDesiredPropNotifRespIfFound() throws TransportException {
    final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes(StandardCharsets.UTF_8);
    final String expectedTopic = "$iothub/twin/PATCH/properties/desired/" + "?$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);
        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_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
        assertNull(receivedMessage.getRequestId());
        assertNull(receivedMessage.getStatus());
        assertEquals(receivedMessage.getVersion(), mockVersion);
        byte[] receivedMessageBytes = receivedMessage.getBytes();
        assertEquals(receivedMessageBytes.length, actualPayload.length);
        for (int i = 0; i < receivedMessageBytes.length; i++) {
            assertEquals(receivedMessageBytes[i], actualPayload[i]);
        }
    }
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) 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 54 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class IotHubTransportMessageTest method constructorWithPropertiesSavesProperties.

// Tests_SRS_IOTHUBTRANSPORTMESSAGE_34_017: [This constructor shall return an instance of IotHubTransportMessage with provided bytes, messagetype, correlationid, messageid, and application properties.]
@Test
public void constructorWithPropertiesSavesProperties() {
    // arrange
    byte[] expectedData = new byte[] { 12, 34, 56 };
    MessageType expectedMessageType = MessageType.DEVICE_TELEMETRY;
    String expectedCorrelationId = "1234";
    String expectedMessageId = "5678";
    MessageProperty[] expectedProperties = new MessageProperty[2];
    expectedProperties[0] = new MessageProperty("bob", "job");
    expectedProperties[1] = new MessageProperty("john", "bill");
    // act
    IotHubTransportMessage transportMessage = new IotHubTransportMessage(expectedData, expectedMessageType, expectedMessageId, expectedCorrelationId, expectedProperties);
    // assert
    assertArrayEquals(expectedData, transportMessage.getBytes());
    assertEquals(expectedMessageType, transportMessage.getMessageType());
    assertEquals(expectedMessageId, transportMessage.getMessageId());
    assertEquals(expectedCorrelationId, transportMessage.getCorrelationId());
    assertEquals(expectedProperties.length, transportMessage.getProperties().length);
}
Also used : MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MessageType(com.microsoft.azure.sdk.iot.device.MessageType) Test(org.junit.Test)

Example 55 with IotHubTransportMessage

use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.

the class HttpsTransportManagerTest method invokeMethodOnDeviceSuccess.

// Tests_SRS_HTTPSTRANSPORTMANAGER_34_018: [If a moduleId is provided, this function shall call invokeMethod with the provided request and
// a uri in the format twins/<device id>/modules/<module id>/methods?api-version=<api_version>.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_021: [This function shall set the methodrequest json as the body of the http message.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_022: [This function shall set the http method to POST.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_023: [This function shall set the http message's uri path to the provided uri path.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_024 [This function shall set a custom property of 'x-ms-edge-moduleId' to the value of <device id>/<module id> of the sending module/device.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_025 [This function shall send the built message.]
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_027 [If the http response doesn't contain an error code, this function return a method result with the response message body as the method result body.]
@Test
public void invokeMethodOnDeviceSuccess() throws TransportException, IOException, URISyntaxException {
    // arrange
    final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
    final String expectedDeviceId = "myDevice";
    final String expectedModuleId = "myModule";
    final String expectedSenderDeviceId = "mySenderDevice";
    final String expectedSenderModuleId = "mySenderModule";
    final String expectedMethodRequestJson = "someJson";
    final String expectedResponseBody = "some body";
    // assert
    new Expectations(HttpsTransportManager.class) {

        {
            mockedMethodRequest.toJson();
            result = expectedMethodRequestJson;
            new IotHubTransportMessage(expectedMethodRequestJson);
            result = mockedTransportMessage;
            mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
            mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/methods");
            mockConfig.getDeviceId();
            result = expectedSenderDeviceId;
            mockConfig.getModuleId();
            result = expectedSenderModuleId;
            transportManager.send(mockedTransportMessage, (Map) any);
            result = mockResponseMessage;
            mockResponseMessage.getStatus();
            result = IotHubStatusCode.OK_EMPTY;
            mockResponseMessage.getBytes();
            result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
            new MethodResult(expectedResponseBody);
        }
    };
    // act
    transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, "");
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult) Test(org.junit.Test)

Aggregations

IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)129 Test (org.junit.Test)108 Message (com.microsoft.azure.sdk.iot.device.Message)37 MutablePair (org.apache.commons.lang3.tuple.MutablePair)33 Pair (org.apache.commons.lang3.tuple.Pair)33 DeviceTwin (com.microsoft.azure.sdk.iot.device.DeviceTwin)30 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)25 HashMap (java.util.HashMap)24 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)16 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)16 MessageType (com.microsoft.azure.sdk.iot.device.MessageType)14 Verifications (mockit.Verifications)10 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)8 NonStrictExpectations (mockit.NonStrictExpectations)8 IOException (java.io.IOException)5 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)4 MessageCallback (com.microsoft.azure.sdk.iot.device.MessageCallback)3 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)3 HttpsTransportManager (com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager)3 HashSet (java.util.HashSet)3