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);
}
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations