Search in sources :

Example 16 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 openEstablishesConnectionUsingCorrectConfig.

// Tests_SRS_MQTTIOTHUBCONNECTION_15_004: [The function shall establish an MQTT connection with an IoT Hub
// using the provided host name, user name, device ID, and sas token.]
@Test
public void openEstablishesConnectionUsingCorrectConfig() throws IOException {
    baseExpectations();
    openExpectations();
    MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
    connection.open();
    final String actualIotHubUserName = Deencapsulation.getField(connection, "iotHubUserName");
    String clientIdentifier = "DeviceClientType=" + URLEncoder.encode(TransportUtils.JAVA_DEVICE_CLIENT_IDENTIFIER + TransportUtils.CLIENT_VERSION, "UTF-8");
    assertEquals(iotHubHostName + "/" + deviceId + "/" + API_VERSION + "/" + clientIdentifier, actualIotHubUserName);
    String expectedSasToken = mockToken.toString();
    String actualUserPassword = Deencapsulation.getField(connection, "iotHubUserPassword");
    assertEquals(expectedSasToken, actualUserPassword);
    State expectedState = State.OPEN;
    State actualState = Deencapsulation.getField(connection, "state");
    assertEquals(expectedState, actualState);
    new Verifications() {

        {
            new MqttDeviceMethod();
            times = 1;
            new MqttMessaging(sslPrefix + iotHubHostName + sslPortSuffix, deviceId, anyString, anyString, mockIotHubSSLContext);
            mockDeviceMessaging.start();
            times = 1;
            new MqttDeviceTwin();
            times = 1;
        }
    };
}
Also used : MqttIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection) State(com.microsoft.azure.sdk.iot.device.transport.State) MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) MqttDeviceMethod(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceMethod) Verifications(mockit.Verifications) MqttDeviceTwin(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin) Test(org.junit.Test)

Example 17 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 sendShallMessageToLowerLayer.

/*
    **Tests_SRS_MqttMessaging_25_024: [**send method shall publish a message to the IOT Hub on the publish topic by calling method publish().**]**
     */
@Test
public void sendShallMessageToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOException {
    final byte[] messageBody = { 0x61, 0x62, 0x63 };
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = messageBody;
            Deencapsulation.invoke(mockMqtt, "publish", anyString, messageBody);
        }
    };
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    testMqttMessaging.send(mockMessage);
    new Verifications() {

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

Example 18 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 sendShallThrowIOExceptionIfMessageIsNull.

/*
    **Tests_SRS_MqttMessaging_25_025: [**send method shall throw an exception if the message is null.**]**
     */
@Test(expected = IOException.class)
public void sendShallThrowIOExceptionIfMessageIsNull(@Mocked final Mqtt mockMqtt) throws IOException {
    MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
    ;
    testMqttMessaging.send(null);
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 0;
            Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, new byte[1]);
            times = 0;
        }
    };
}
Also used : MqttMessaging(com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging) Test(org.junit.Test)

Example 19 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 sendShallMessageWithPropsAndMessageIdToLowerLayer.

/*
     **Tests_SRS_MqttMessaging_21_027: [**send method shall append the messageid to publishTopic before publishing using the key name `$.mid`.**]**
     */
@Test
public void sendShallMessageWithPropsAndMessageIdToLowerLayer(@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) };
    final String messageidValue = "test-message-id";
    new NonStrictExpectations() {

        {
            mockMessage.getBytes();
            result = messageBody;
            mockMessage.getProperties();
            result = messageProperties;
            mockMessage.getMessageId();
            result = messageidValue;
            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&$.mid=%s", clientId, propertyName, propertyValue, messageidValue);
    new Verifications() {

        {
            mockMessage.getBytes();
            times = 2;
            mockMessage.getProperties();
            Deencapsulation.invoke(mockMqtt, "publish", publishTopicWithProperties, messageBody);
            times = 1;
            mockMessage.getMessageId();
            times = 2;
        }
    };
}
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 20 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 stopCallsRestartBaseEvenIfDisconnectFailsAndThrowsIOException.

@Test(expected = IOException.class)
public void stopCallsRestartBaseEvenIfDisconnectFailsAndThrowsIOException(@Mocked final Mqtt mockMqtt) throws IOException {
    new StrictExpectations() {

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

        {
            Deencapsulation.invoke(mockMqtt, "disconnect");
            times = 1;
            mockMqtt.restartBaseMqtt();
            times = 1;
        }
    };
}
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