Commit 5c51753b authored by René Schöne's avatar René Schöne
Browse files

Add MqttHandlerTest

parent 39dcfeef
Pipeline #9764 passed with stages
in 13 minutes and 55 seconds
......@@ -32,8 +32,11 @@ test:
image: openjdk:11
stage: test
services:
- name: "eclipse-mosquitto:1.6.9"
- name: "eclipse-mosquitto:1.6"
alias: "mqtt"
- name: "eclipse-mosquitto:1.6"
alias: "mqtt_different_port"
command: ["mosquitto", "-p", "55883"]
needs:
- build
script:
......
......@@ -120,11 +120,17 @@ public class MqttHandler {
}
/**
* Sets the host (with default port) to receive messages from, and connects to it.
* Sets the host to receive messages from, and connects to it.
* @param host name of the host to connect to, format is either <code>"$name"</code> or <code>"$name:$port"</code>
* @throws IOException if could not connect, or could not subscribe to a topic
* @return self
*/
public MqttHandler setHost(String host) throws java.io.IOException {
if (host.contains(":")) {
int colon_index = host.indexOf(":");
return setHost(host.substring(0, colon_index),
Integer.parseInt(host.substring(colon_index + 1)));
}
return setHost(host, DEFAULT_PORT);
}
......
package org.jastadd.ragconnect.tests;
import example.ast.MqttHandler;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.Assumptions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Testing the {@link MqttHandler} used in the "example" test case.
*
* @author rschoene - Initial contribution
*/
@Tag("New")
public class MqttHandlerTest {
@Test
public void defaultBehaviour() {
MqttHandler handler = new MqttHandler();
try {
handler.setHost(TestUtils.getMqttHost());
} catch (IOException e) {
fail("Fail during setHost", e);
}
boolean ready = handler.waitUntilReady(2, TimeUnit.SECONDS);
assertTrue(ready);
handler.close();
}
@Test
public void testWelcomeMessage() throws Exception {
MqttHandler welcomeMessageSubscriber = new MqttHandler();
List<String> receivedMessages = new ArrayList<>();
welcomeMessageSubscriber.setHost(TestUtils.getMqttHost());
assertTrue(welcomeMessageSubscriber.waitUntilReady(2, TimeUnit.SECONDS));
welcomeMessageSubscriber.newConnection("components", bytes -> receivedMessages.add(new String(bytes)));
assertThat(receivedMessages).isEmpty();
MqttHandler handler = new MqttHandler();
handler.setHost(TestUtils.getMqttHost());
assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS));
TestUtils.waitForMqtt();
assertEquals(1, receivedMessages.size());
}
@Test
public void testDontSendWelcomeMessage() throws Exception {
MqttHandler welcomeMessageSubscriber = new MqttHandler();
List<String> receivedMessages = new ArrayList<>();
welcomeMessageSubscriber.setHost(TestUtils.getMqttHost());
assertTrue(welcomeMessageSubscriber.waitUntilReady(2, TimeUnit.SECONDS));
welcomeMessageSubscriber.newConnection("components", bytes -> receivedMessages.add(new String(bytes)));
assertThat(receivedMessages).isEmpty();
MqttHandler handler = new MqttHandler().dontSendWelcomeMessage();
handler.setHost(TestUtils.getMqttHost());
assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS));
TestUtils.waitForMqtt();
assertThat(receivedMessages).isEmpty();
}
@Test
public void testSecondHost() throws Exception {
// only run this if we have a second mqtt broker running
Assumptions.assumeThat(TestUtils.getSecondMqttHost()).isNotNull();
MqttHandler welcomeMessageSubscriber = new MqttHandler();
List<String> receivedMessages = new ArrayList<>();
welcomeMessageSubscriber.setHost(TestUtils.getSecondMqttHost());
assertTrue(welcomeMessageSubscriber.waitUntilReady(2, TimeUnit.SECONDS));
welcomeMessageSubscriber.newConnection("components", bytes -> receivedMessages.add(new String(bytes)));
assertThat(receivedMessages).isEmpty();
MqttHandler handler = new MqttHandler();
handler.setHost(TestUtils.getSecondMqttHost());
assertTrue(handler.waitUntilReady(2, TimeUnit.SECONDS));
TestUtils.waitForMqtt();
assertEquals(1, receivedMessages.size());
}
}
......@@ -29,6 +29,16 @@ public class TestUtils {
}
}
public static String getSecondMqttHost() {
if (System.getenv("GITLAB_CI") != null) {
// we are in the CI, so use "mqtt_different_port" as host
return "mqtt_different_port";
} {
// else no second local mqtt broker
return null;
}
}
public static String mqttUri(String path) {
return "mqtt://" + getMqttHost() + "/" + path;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment