Search in sources :

Example 1 with Message

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

the class MqttTest method receiveThrowsExceptionWhenConfigurationIsNotSet.

@Test(expected = InvalidParameterException.class)
public void receiveThrowsExceptionWhenConfigurationIsNotSet() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(false);
    new MockUp<MqttMessaging>() {

        @Mock
        String parseTopic() throws IOException {
            return mockParseTopic;
        }

        @Mock
        byte[] parsePayload(String topic) throws IOException {
            return null;
        }
    };
    final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    Deencapsulation.setField(mockMqtt, "info", null);
    //act
    try {
        Message receivedMessage = mockMqtt.receive();
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 2 with Message

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

the class MqttTest method receiveSuccess.

/*
    **Tests_SRS_Mqtt_25_021: [**This method shall call parseTopic to parse the topic from the recevived Messages queue corresponding to the messaging client's operation.**]**
     */
/*
    **Tests_SRS_Mqtt_25_023: [**This method shall call parsePayload to get the message payload from the recevived Messages queue corresponding to the messaging client's operation.**]**
     */
/*
    Tests_SRS_Mqtt_25_024: [**This method shall construct new Message with the bytes obtained from parsePayload and return the message.**]**
     */
@Test
public void receiveSuccess() throws IOException, MqttException {
    //arrange
    final byte[] payload = { 0x61, 0x62, 0x63 };
    baseConstructorExpectations(true);
    baseConnectExpectation();
    new MockUp<MqttMessaging>() {

        @Mock
        String parseTopic() throws IOException {
            return mockParseTopic;
        }

        @Mock
        byte[] parsePayload(String topic) throws IOException {
            return payload;
        }
    };
    final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    try {
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Message receivedMessage = mockMqtt.receive();
        //assert
        byte[] actualPayload = receivedMessage.getBytes();
        assertTrue(actualPayload.length == payload.length);
        for (int i = 0; i < payload.length; i++) {
            assertEquals(actualPayload[i], payload[i]);
        }
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Message(com.microsoft.azure.sdk.iot.device.Message) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 3 with Message

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

the class MessageTest method setPropertyRejectsIllegalValue.

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

        {
            new MessageProperty(name, invalidValue);
            result = new IllegalArgumentException();
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, invalidValue);
}
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 4 with Message

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

the class MessageTest method setPropertyRejectsNullValue.

// Tests_SRS_MESSAGE_11_029: [If value is null, the function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void setPropertyRejectsNullValue() {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    Message msg = new Message(body);
    msg.setProperty(name, null);
}
Also used : Message(com.microsoft.azure.sdk.iot.device.Message) Test(org.junit.Test)

Example 5 with Message

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

the class MessageTest method getPropertiesReturnsCopyOfProperties.

// Tests_SRS_MESSAGE_11_033: [The function shall return a copy of the message properties.]
@Test
public void getPropertiesReturnsCopyOfProperties(@Mocked final MessageProperty mockProperty) {
    final byte[] body = { 0x61, 0x62, 0x63 };
    final String name = "test-name";
    final String value = "test-value";
    final String httpsName = "test-https-name";
    new NonStrictExpectations() {

        {
            new MessageProperty(name, value);
            result = mockProperty;
            mockProperty.hasSameName(name);
            result = true;
            mockProperty.getValue();
            result = value;
            mockProperty.getName();
            result = httpsName;
        }
    };
    Message msg = new Message(body);
    msg.setProperty(name, value);
    MessageProperty[] testProperties = msg.getProperties();
    int expectedNumProperties = 1;
    assertThat(testProperties.length, is(expectedNumProperties));
    assertThat(testProperties[0], is(not(mockProperty)));
}
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)

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