Search in sources :

Example 6 with MqttAsyncClient

use of org.eclipse.paho.client.mqttv3.MqttAsyncClient in project azure-iot-sdk-java by Azure.

the class MqttTest method constructorThrowsExceptionIfMqttAsyncClientFails.

/*
    **Tests_SRS_Mqtt_25_045: [**The constructor throws IOException if MqttException is thrown and doesn't instantiate this instance.**]**
     */
@Test(expected = IOException.class)
public void constructorThrowsExceptionIfMqttAsyncClientFails() throws IOException, MqttException {
    Mqtt mockMqtt = null;
    try {
        //arrange
        new NonStrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttException;
            }
        };
        //act
        mockMqtt = instantiateMqtt(true);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Test(org.junit.Test)

Example 7 with MqttAsyncClient

use of org.eclipse.paho.client.mqttv3.MqttAsyncClient 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 8 with MqttAsyncClient

use of org.eclipse.paho.client.mqttv3.MqttAsyncClient 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)

Example 9 with MqttAsyncClient

use of org.eclipse.paho.client.mqttv3.MqttAsyncClient in project wso2-axis2-transports by wso2.

the class MqttConnectionFactory method createMqttAsyncClient.

private MqttAsyncClient createMqttAsyncClient(String uniqueClientId, int qos) {
    String sslEnable = parameters.get(MqttConstants.MQTT_SSL_ENABLE);
    String tempStore = parameters.get(MqttConstants.MQTT_TEMP_STORE);
    MqttDefaultFilePersistence dataStore = getDataStore(uniqueClientId, qos, tempStore);
    String mqttEndpointURL = "tcp://" + parameters.get(MqttConstants.MQTT_SERVER_HOST_NAME) + ":" + parameters.get(MqttConstants.MQTT_SERVER_PORT);
    // If SSL is enabled in the config, Use SSL tranport
    if (sslEnable != null && sslEnable.equalsIgnoreCase("true")) {
        mqttEndpointURL = "ssl://" + parameters.get(MqttConstants.MQTT_SERVER_HOST_NAME) + ":" + parameters.get(MqttConstants.MQTT_SERVER_PORT);
    }
    MqttAsyncClient mqttClient = null;
    try {
        mqttClient = new MqttAsyncClient(mqttEndpointURL, uniqueClientId, dataStore);
    } catch (MqttException e) {
        log.error("Error while creating the MQTT client...", e);
    }
    return mqttClient;
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttDefaultFilePersistence(org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient)

Example 10 with MqttAsyncClient

use of org.eclipse.paho.client.mqttv3.MqttAsyncClient in project spring-integration by spring-projects.

the class MqttAdapterTests method testDifferentQos.

@Test
public void testDifferentQos() throws Exception {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    factory.setCleanSession(false);
    factory.setConnectionTimeout(23);
    factory.setKeepAliveInterval(45);
    factory.setPassword("pass");
    MemoryPersistence persistence = new MemoryPersistence();
    factory.setPersistence(persistence);
    final SocketFactory socketFactory = mock(SocketFactory.class);
    factory.setSocketFactory(socketFactory);
    final Properties props = new Properties();
    factory.setSslProperties(props);
    factory.setUserName("user");
    Will will = new Will("foo", "bar".getBytes(), 2, true);
    factory.setWill(will);
    factory = spy(factory);
    MqttAsyncClient aClient = mock(MqttAsyncClient.class);
    final MqttClient client = mock(MqttClient.class);
    willAnswer(invocation -> client).given(factory).getClientInstance(anyString(), anyString());
    given(client.isConnected()).willReturn(true);
    new DirectFieldAccessor(client).setPropertyValue("aClient", aClient);
    willAnswer(new CallsRealMethods()).given(client).connect(any(MqttConnectOptions.class));
    willAnswer(new CallsRealMethods()).given(client).subscribe(any(String[].class), any(int[].class));
    willReturn(alwaysComplete).given(aClient).connect(any(MqttConnectOptions.class), any(), any());
    IMqttToken token = mock(IMqttToken.class);
    given(token.getGrantedQos()).willReturn(new int[] { 2, 0 });
    willReturn(token).given(aClient).subscribe(any(String[].class), any(int[].class), any(), any());
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("foo", "bar", factory, "baz", "fix");
    AtomicReference<Method> method = new AtomicReference<>();
    ReflectionUtils.doWithMethods(MqttPahoMessageDrivenChannelAdapter.class, m -> {
        m.setAccessible(true);
        method.set(m);
    }, m -> m.getName().equals("connectAndSubscribe"));
    assertNotNull(method.get());
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    given(logger.isWarnEnabled()).willReturn(true);
    method.get().invoke(adapter);
    verify(logger, atLeastOnce()).warn("Granted QOS different to Requested QOS; topics: [baz, fix] requested: [1, 1] granted: [2, 0]");
    verify(client).setTimeToWait(30_000L);
}
Also used : DefaultMqttPahoClientFactory(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Log(org.apache.commons.logging.Log) SocketFactory(javax.net.SocketFactory) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) Properties(java.util.Properties) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Will(org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory.Will) Test(org.junit.Test)

Aggregations

MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)11 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)8 Test (org.junit.Test)7 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)6 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)4 Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)3 Properties (java.util.Properties)3 SocketFactory (javax.net.SocketFactory)3 IMqttAsyncClient (org.eclipse.paho.client.mqttv3.IMqttAsyncClient)3 MqttException (org.eclipse.paho.client.mqttv3.MqttException)3 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)3 DefaultMqttPahoClientFactory (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory)3 Will (org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory.Will)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 InvalidParameterException (java.security.InvalidParameterException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 IMqttClient (org.eclipse.paho.client.mqttv3.IMqttClient)2 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)2 MqttDefaultFilePersistence (org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence)2