Search in sources :

Example 21 with Message

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

the class HttpsSingleMessageTest method toMessageRemovesPrefixFromProperties.

// Tests_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
@Test
public void toMessageRemovesPrefixFromProperties(@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 String expectedPropertyName = "test-property-name";
    final String expectedPropertyValue = propertyValue;
    new Verifications() {

        {
            mockMsg.setProperty(expectedPropertyName, expectedPropertyValue);
        }
    };
}
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 22 with Message

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

the class MessageTest method getPropertyRejectsNonexistentProperty.

// Tests_SRS_MESSAGE_11_034: [If no value associated with the property name is found, the function shall return null.]
@Test
public void getPropertyRejectsNonexistentProperty(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    Message msg = new Message(body);
    String testValue = msg.getProperty(name);
    // expected is null since test-name property doesn't exist
    String expectedValue = null;
    assertThat(testValue, is(expectedValue));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Test(org.junit.Test)

Example 23 with Message

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

the class MessageTest method isExpiredReturnsFalseIfCurrentTimeIsSmallerThanExpiryTime.

// Tests_SRS_MESSAGE_15_036: [The function shall return true if the current time is greater than the expiry time and false otherwise.]
@Test
public void isExpiredReturnsFalseIfCurrentTimeIsSmallerThanExpiryTime() throws InterruptedException {
    final byte[] body = { 0x61, 0x62, 0x63 };
    Message msg = new Message(body);
    msg.setExpiryTime(1000);
    boolean actualResult = msg.isExpired();
    boolean expectedResult = false;
    assertThat(expectedResult, is(actualResult));
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Test(org.junit.Test)

Example 24 with Message

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

the class MqttDeviceMethodTest method receiveReturnsNullMessageIfTopicNotFound.

/*
    Tests_SRS_MqttDeviceMethod_25_025: [**If the call parseTopic returns null or empty string then this method shall do nothing and return null**]**
     */
@Test
public void receiveReturnsNullMessageIfTopicNotFound() throws IOException {
    //arrange
    String topic = "$iothub/not_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();
    //assert
    assertNull(testMessage);
}
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)

Example 25 with Message

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

the class MqttDeviceMethodTest method receiveThrowsIfRIDCouldNotBeParsed.

/*
    Tests_SRS_MqttDeviceMethod_25_031: [**If request id is not found or is null then receive shall throw IOException **]**
     */
@Test(expected = IOException.class)
public void receiveThrowsIfRIDCouldNotBeParsed() throws IOException {
    //arrange
    String topic = "$iothub/methods/POST/testMethod/";
    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