Search in sources :

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

/*
    **Tests_SRS_Mqtt_25_026: [**The function shall notify all its concrete classes by calling abstract method onReconnect at the entry of the function**]**
     */
/*
    **Tests_SRS_Mqtt_25_029: [**The function shall notify all its concrete classes by calling abstract method onReconnectComplete at the exit of the function**]**
     */
@Test
public void connectionLostAttemptsToReconnect() 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 = 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)

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

/*
    **Tests_SRS_Mqtt_25_013: [**If the either publishTopic or payload is null or empty, the function shall throw an IOException.**]**
    */
@Test(expected = IOException.class)
public void publishThrowsExceptionWhenPayloadIsNull() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(true);
        final byte[] payload = null;
        new NonStrictExpectations() {

            {
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        mockMqtt = instantiateMqtt(true);
        Deencapsulation.invoke(mockMqtt, "connect");
        //act
        Deencapsulation.invoke(mockMqtt, "publish", mockParseTopic, byte[].class);
        //assert
        new Verifications() {

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

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

/*
    **Tests_SRS_Mqtt_25_001: [**The constructor shall instantiate MQTT lock for using base class.**]**
    */
@Test
public void constructorDoesntInitiliaseConfigWithoutParameters() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(false);
    //act
    Mqtt mockMqtt = instantiateMqtt(false);
    //assert
    Object actualInfoInstance = Deencapsulation.getField(mockMqtt, "info");
    assertNull(actualInfoInstance);
    ConcurrentSkipListMap<String, byte[]> actualMap = Deencapsulation.getField(mockMqtt, "allReceivedMessages");
    assertNull(actualMap);
    Object actualLock = Deencapsulation.getField(mockMqtt, "MQTT_LOCK");
    assertNotNull(actualLock);
    baseConstructorVerifications(false);
    mockMqtt.restartBaseMqtt();
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

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

/*
    ** Tests_SRS_Mqtt_25_004: [**If an instance of the inner class MqttConnectionInfo is already created than it shall return doing nothing.**]**
     */
@Test
public void manyExtendsOfAbstractClassDoesntChangeConfig() throws IOException, MqttException {
    //arrange
    baseConstructorExpectations(true);
    baseConstructorExpectations(false);
    //act
    Mqtt mockMqtt1 = instantiateMqtt(true);
    Object actualInfoInstance1 = Deencapsulation.getField(mockMqtt1, "info");
    ConcurrentSkipListMap<String, byte[]> actualMap1 = Deencapsulation.getField(mockMqtt1, "allReceivedMessages");
    Object actualLock1 = Deencapsulation.getField(mockMqtt1, "MQTT_LOCK");
    Mqtt mockMqtt2 = instantiateMqtt(false);
    Object actualInfoInstance2 = Deencapsulation.getField(mockMqtt2, "info");
    ConcurrentSkipListMap<String, byte[]> actualMap2 = Deencapsulation.getField(mockMqtt2, "allReceivedMessages");
    Object actualLock2 = Deencapsulation.getField(mockMqtt2, "MQTT_LOCK");
    //assert
    assertEquals(actualInfoInstance1, actualInfoInstance2);
    assertEquals(actualMap1, actualMap2);
    assertEquals(actualLock1, actualLock2);
    baseConstructorVerifications(true);
    baseConstructorVerifications(false);
    //cleanup
    testCleanUp(mockMqtt1);
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) Test(org.junit.Test)

Example 20 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 connectFailsIfNoConfigIsProvided.

/*
    **Tests_SRS_Mqtt_25_006: [**If the inner class MqttConnectionInfo has not been instantiated then the function shall throw IOException.**]**
     */
@Test(expected = IOException.class)
public void connectFailsIfNoConfigIsProvided() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    try {
        baseConstructorExpectations(false);
        mockMqtt = instantiateMqtt(false);
        //act
        Deencapsulation.invoke(mockMqtt, "connect");
    } finally {
        mockMqtt.restartBaseMqtt();
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) 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