-
Notifications
You must be signed in to change notification settings - Fork 0
FrameWork integration
First we will create a class to connect to cloudmqtt and publish the messages. We will be using Paho MQTT v3.1 Client blocking API.
`package messagingServices;
import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import java.net.URI; import java.net.URISyntaxException;
public class mqttMessagingServices implements MqttCallback {
private final static int qos = 2;
private static String topic = "sensor/temp";
private static final String serverUri = "tcp://m15.cloudmqtt.com:13742";
private static final String username = "########";
private static final String password = "######";
private static final String clientId = "MQTT-Java-Example";
private static MqttClient client;
public static void connect() {
MqttConnectOptions conOpt = new MqttConnectOptions();
conOpt.setCleanSession(true);
conOpt.setUserName(username);
conOpt.setPassword(password.toCharArray());
try {
client = new MqttClient(serverUri, clientId, new MemoryPersistence());
client.connect(conOpt);
client.subscribe(topic, qos);
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void PublishMessage(String payload) {
try {
connect();
MqttMessage message = new MqttMessage(payload.getBytes());
message.setQos(qos);
client.publish(topic, message); // Blocking publish
}
catch(Exception e) {}
}
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub
}
public void messageArrived(String topic, MqttMessage message) throws MqttException {
System.out.println(String.format("[%s] %s", topic, new String(message.getPayload())));
}
public void deliveryComplete(IMqttDeliveryToken token) {
}
} `
If you have used TestNG or implemented Extent/Allure reporting with TestNG then you already are aware of the listener classes. In short TestNG listener always extends org.testng.ITestNGListener and TestNG provides many listener types.
Some of the methods which we have used from ITestListener are as follows:
- onStart is invoked after the test class is instantiated and before any configuration method is called.
- onTestSuccess is invoked on success of a test.
- onTestFailure is invoked on failure of a test.
- onTestSkipped is invoked whenever a test is skipped.
- onTestFailedButWithinSuccessPercentage is invoked each time a method fails but is within the success percentage requested.
- onFinish is invoked after all the tests have run and all their Configuration methods have been called.
We will use these methods to send data to the messaging system as per the suite progression.