Search in sources :

Example 11 with Message

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

the class MessageTest method getBodyAsStringReturnsUtf8Body.

// Tests_SRS_MESSAGE_11_022: [The function shall return the message body, encoded using charset UTF-8.]
@Test
public void getBodyAsStringReturnsUtf8Body() {
    final byte[] body = { 0x61, 0x62, 0x63 };
    Message msg = new Message(body);
    String testBody = new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
    String expectedBody = new String(body, UTF8);
    assertThat(testBody, is(expectedBody));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Test(org.junit.Test)

Example 12 with Message

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

the class MessageTest method setPropertyRejectsIllegalName.

// Tests_SRS_MESSAGE_11_030: [If name contains a character not specified in RFC 2047, the function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void setPropertyRejectsIllegalName(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String invalidName = "  ";
    final String value = "test-value";
    new NonStrictExpectations() {

        {
            new MessageProperty(invalidName, value);
            result = new IllegalArgumentException();
        }
    };
    Message msg = new Message(body);
    msg.setProperty(invalidName, value);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 13 with Message

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

the class HttpsSingleMessageTest method toMessageCopiesBody.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
@Test
public void toMessageCopiesBody(@Mocked final HttpsResponse mockResponse, @Mocked final MessageProperty mockProperty, @Mocked final Message mockMsg) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final Map<String, String> headerFields = new HashMap<>();
    final String propertyName = "iothub-app-test-property-name";
    final String propertyValue = "test-property-value";
    headerFields.put(propertyName, propertyValue);
    new NonStrictExpectations() {

        {
            mockResponse.getBody();
            result = body;
            mockResponse.getHeaderFields();
            result = headerFields;
            MessageProperty.isValidAppProperty(propertyName, propertyValue);
            result = true;
            new MessageProperty(propertyName, propertyValue);
            result = mockProperty;
            mockProperty.getName();
            result = propertyName;
            mockProperty.getValue();
            result = propertyValue;
            new Message(body);
            result = mockMsg;
        }
    };
    HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(mockResponse);
    httpsMsg.toMessage();
    final byte[] expectedBody = body;
    new Verifications() {

        {
            new Message(expectedBody);
        }
    };
}
Also used : HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Message(com.microsoft.azure.sdk.iot.device.Message) HashMap(java.util.HashMap) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 14 with Message

use of com.microsoft.azure.sdk.iot.device.Message 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 15 with Message

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

the class MqttDeviceMethodTest method receiveThrowsIfMethodNameCouldNotBeParsed.

/*
    Tests_SRS_MqttDeviceMethod_25_029: [**If method name not found or is null then receive shall throw IOException **]**
     */
@Test(expected = IOException.class)
public void receiveThrowsIfMethodNameCouldNotBeParsed() throws IOException {
    //arrange
    String topic = "$iothub/methods/POST/";
    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();
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Message(com.microsoft.azure.sdk.iot.device.Message) DeviceMethodMessage(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Test(org.junit.Test)

Aggregations

Message (com.microsoft.azure.sdk.iot.device.Message)31 Test (org.junit.Test)29 DeviceMethodMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodMessage)7 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)7 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)6 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)6 NonStrictExpectations (mockit.NonStrictExpectations)6 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)4 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)4 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)3 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)3 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 DeviceConnectionString (tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString)3 EventCallback (tests.integration.com.microsoft.azure.sdk.iot.device.EventCallback)3 Success (tests.integration.com.microsoft.azure.sdk.iot.device.Success)3 Verifications (mockit.Verifications)2 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)1