Search in sources :

Example 21 with Mqtt

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

the class MqttTest method connectDoesNothingIfAlreadyConnected.

/*
    **Tests_SRS_Mqtt_25_008: [**If the MQTT connection is already open, the function shall do nothing.**]**
     */
@Test
public void connectDoesNothingIfAlreadyConnected() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    new NonStrictExpectations() {

        {
            mockMqttAsyncClient.isConnected();
            result = true;
        }
    };
    Mqtt mockMqtt = instantiateMqtt(true);
    //act
    Deencapsulation.invoke(mockMqtt, "connect");
    //assert
    new Verifications() {

        {
            mockMqttAsyncClient.isConnected();
            minTimes = 1;
            mockMqttAsyncClient.connect(mockMqttConnectionOptions);
            times = 0;
            mockMqttToken.waitForCompletion();
            times = 0;
        }
    };
    mockMqtt.restartBaseMqtt();
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 22 with Mqtt

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

the class MqttTest method publishSucceedsWhenConnected.

/*
    **Tests_SRS_Mqtt_25_014: [**The function shall publish message payload on the publishTopic specified to the IoT Hub given in the configuration.**]**
     */
@Test
public void publishSucceedsWhenConnected() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    baseConnectExpectation();
    basePublishExpectations();
    final byte[] payload = { 0x61, 0x62, 0x63 };
    Mqtt mockMqtt = instantiateMqtt(true);
    Deencapsulation.invoke(mockMqtt, "connect");
    //act
    Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, payload);
    //assert
    new Verifications() {

        {
            mockMqttAsyncClient.isConnected();
            minTimes = 2;
            mockMqttAsyncClient.publish(mockParseTopic, mockMqttMessage);
            times = 1;
        }
    };
    testCleanUp(mockMqtt);
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 23 with Mqtt

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

the class MqttTest method subscribeSucceeds.

/*
    **Tests_SRS_Mqtt_25_017: [**The function shall subscribe to subscribeTopic specified to the IoT Hub given in the configuration.**]**
     */
@Test
public void subscribeSucceeds() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    baseConnectExpectation();
    new NonStrictExpectations() {

        {
            mockMqttAsyncClient.isConnected();
            result = true;
            mockMqttAsyncClient.subscribe(mockParseTopic, anyInt);
            result = mockMqttToken;
        }
    };
    Mqtt mockMqtt = instantiateMqtt(true);
    Deencapsulation.invoke(mockMqtt, "connect");
    //act
    Deencapsulation.invoke(mockMqtt, "subscribe", mockParseTopic);
    //assert
    new Verifications() {

        {
            mockMqttAsyncClient.isConnected();
            minTimes = 1;
            mockMqttAsyncClient.subscribe(mockParseTopic, anyInt);
            times = 1;
        }
    };
    testCleanUp(mockMqtt);
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 24 with Mqtt

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

the class MqttTest method subscribeFailsWhenConfigIsNotSet.

@Test(expected = IOException.class)
public void subscribeFailsWhenConfigIsNotSet() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(false);
        mockMqtt = instantiateMqtt(false);
        //act
        Deencapsulation.invoke(mockMqtt, "subscribe", mockParseTopic);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 25 with Mqtt

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

the class MqttTest method connectionLostAttemptsToReconnectAgainIfConnectFails.

/*
    **Tests_SRS_Mqtt_25_027: [**The function shall attempt to reconnect to the IoTHub in a loop with exponential backoff until it succeeds**]**
     */
@Test
public void connectionLostAttemptsToReconnectAgainIfConnectFails() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    Throwable t = new Throwable();
    try {
        new StrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttAsyncClient;
                mockMqttAsyncClient.setCallback((Mqtt) any);
                new MqttConnectOptions();
                result = mockMqttConnectionOptions;
                mockMqttConnectionOptions.setKeepAliveInterval(anyInt);
                mockMqttConnectionOptions.setCleanSession(anyBoolean);
                mockMqttConnectionOptions.setMqttVersion(anyInt);
                mockMqttConnectionOptions.setUserName(anyString);
                mockMqttConnectionOptions.setPassword(password.toCharArray());
                mockMqttConnectionOptions.setSocketFactory(mockIotHubSSLContext.getIotHubSSlContext().getSocketFactory());
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttException;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttToken;
                mockMqttToken.waitForCompletion();
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        //act
        try {
            mockMqtt = instantiateMqtt(true);
            mockMqtt.connectionLost(t);
        } catch (Exception e) {
            System.out.print("Completed throwing exception - " + e.getCause() + e.getMessage());
        }
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) Test(org.junit.Test)

Aggregations

Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)29 Test (org.junit.Test)29 Message (com.microsoft.azure.sdk.iot.device.Message)4 MqttMessaging (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttMessaging)4 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)3 IOException (java.io.IOException)2 InvalidParameterException (java.security.InvalidParameterException)2