Search in sources :

Example 1 with MQTTRequest

use of io.irontest.models.teststep.MQTTRequest in project irontest by zheng-wang.

the class MQTTTeststepRunner method run.

@Override
public BasicTeststepRun run() throws Exception {
    Teststep teststep = getTeststep();
    MQTTTeststepProperties otherProperties = (MQTTTeststepProperties) teststep.getOtherProperties();
    // validate arguments
    if ("".equals(StringUtils.trimToEmpty(otherProperties.getTopicString()))) {
        throw new IllegalArgumentException("Topic String not specified.");
    }
    Endpoint endpoint = teststep.getEndpoint();
    MqttClient mqttClient = new MqttClient(endpoint.getUrl(), "irontest-mqtt-teststep");
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setUserName(endpoint.getUsername());
    connOpts.setPassword(getDecryptedEndpointPassword() == null ? null : getDecryptedEndpointPassword().toCharArray());
    mqttClient.connect(connOpts);
    try {
        MQTTRequest request = (MQTTRequest) teststep.getApiRequest();
        MqttMessage message = new MqttMessage(request.getPayload() == null ? null : request.getPayload().getBytes());
        message.setQos(1);
        mqttClient.publish(otherProperties.getTopicString(), message);
    } finally {
        mqttClient.disconnect();
    }
    return new BasicTeststepRun();
}
Also used : Teststep(io.irontest.models.teststep.Teststep) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Endpoint(io.irontest.models.endpoint.Endpoint) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MQTTTeststepProperties(io.irontest.models.teststep.MQTTTeststepProperties) MQTTRequest(io.irontest.models.teststep.MQTTRequest)

Aggregations

Endpoint (io.irontest.models.endpoint.Endpoint)1 MQTTRequest (io.irontest.models.teststep.MQTTRequest)1 MQTTTeststepProperties (io.irontest.models.teststep.MQTTTeststepProperties)1 Teststep (io.irontest.models.teststep.Teststep)1 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)1