Search in sources :

Example 6 with MqttMessaging

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

the class MqttIotHubConnectionTest method openThrowsIOExceptionIfConnectionFails.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_005: [If an MQTT connection is unable to be established for any reason,
// the function shall throw an IOException.]
@Test(expected = IOException.class)
public void openThrowsIOExceptionIfConnectionFails() throws IOException {
    baseExpectations();
    new NonStrictExpectations() {

        {
            new IotHubSasToken(mockConfig, anyLong);
            result = mockToken;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            result = new IOException(anyString);
        }
    };
    try {
        MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
        connection.open();
    } catch (Exception e) {
        new Verifications() {

            {
            }
        };
        throw e;
    }
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) IotHubSasToken(com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with MqttMessaging

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

the class MqttMessagingTest method startCallsConnectAndSubscribe.

/*
    **Tests_SRS_MqttMessaging_25_020: [**start method shall be call connect to establish a connection to IOT Hub with the given configuration.**]**

    **Tests_SRS_MqttMessaging_25_021: [**start method shall subscribe to messaging subscribe topic once connected.**]**
     */
@Test
public void startCallsConnectAndSubscribe(@Mocked final Mqtt mockMqtt) throws IOException {
    new NonStrictExpectations() {

        {
            Deencapsulation.invoke(mockMqtt, "connect");
            Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    testMqttMessaging.start();
    new Verifications() {

        {
            Deencapsulation.invoke(mockMqtt, "connect");
            times = 1;
            Deencapsulation.invoke(mockMqtt, "subscribe", anyString);
            times = 1;
        }
    };
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 8 with MqttMessaging

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

the class MqttMessagingTest method sendShallMessageWithPropertiesToLowerLayer.

/*
     **Tests_SRS_MqttMessaging_25_026: [**send method shall append the message properties to publishTopic before publishing.**]**
     */
@Test
public void sendShallMessageWithPropertiesToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] messageBody = { 0x61, 0x62, 0x63 };
    final String propertyName = "key";
    final String propertyValue = "value";
    final MessageProperty[] messageProperties = new MessageProperty[] { new MessageProperty(propertyName, propertyValue) };
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = messageBody;
            mockMessage.getProperties();
            result = messageProperties;
            Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    testMqttMessaging.send(mockMessage);
    final String publishTopicWithProperties = String.format("devices/%s/messages/events/%s=%s", clientId, propertyName, propertyValue);
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 2;
            mockMessage.getProperties();
            Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithProperties, messageBody);
            times = 1;
        }
    };
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Test(org.junit.Test)

Example 9 with MqttMessaging

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

the class MqttMessagingTest method parsePayloadShallThrowIOExceptionIfQueueIsNull.

/*
    **Tests_SRS_MqttMessaging_25_013: [**If receiveMessage queue is null then this method shall throw IOException.**]**
     */
@Test(expected = IOException.class)
public void parsePayloadShallThrowIOExceptionIfQueueIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    final String insertTopic = "$iothub/twin/PATCH/properties/desired/#";
    ConcurrentSkipListMap<String, byte[]> testMap = null;
    Deencapsulation.setField(mockMqtt, "allReceivedMessages", testMap);
    byte[] retrieveMessage = Deencapsulation.invoke(testMqttMessaging, "parsePayload", insertTopic);
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 10 with MqttMessaging

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

the class MqttMessagingTest method constructorCallsBaseConstructorWithArguments.

/*
    **Tests_SRS_MqttMessaging_25_002: [**The constructor shall use the configuration to instantiate super class and passing the parameters.**]**
    */
/*
    **Tests_SRS_MqttMessaging_25_003: [**The constructor construct publishTopic and subscribeTopic from deviceId.**]**
    */
@Test
public void constructorCallsBaseConstructorWithArguments(@Mocked final Mqtt mockMqtt) throws IOException {
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    String actualPublishTopic = Deencapsulation.getField(testMqttMessaging, "publishTopic");
    assertNotNull(actualPublishTopic);
    String actualSubscribeTopic = Deencapsulation.getField(testMqttMessaging, "subscribeTopic");
    assertNotNull(actualSubscribeTopic);
    String actualParseTopic = Deencapsulation.getField(testMqttMessaging, "parseTopic");
    assertNotNull(actualParseTopic);
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Aggregations

MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)29 Test (org.junit.Test)29 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)8 MqttIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection)5 Verifications (mockit.Verifications)5 Message (com.microsoft.azure.sdk.iot.device.Message)4 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)4 IotHubSasToken (com.microsoft.azure.sdk.iot.device.auth.IotHubSasToken)3 MqttDeviceMethod (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod)3 IOException (java.io.IOException)3 NonStrictExpectations (mockit.NonStrictExpectations)3 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)2 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)2 State (com.microsoft.azure.sdk.iot.device.transport.State)1