@Test public void connectWithValidCredentials() throws Exception { LOG.info("*** connectWithCredentials ***"); m_client = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("dbuser"); options.setPassword("password".toCharArray()); m_client.connect(options); assertTrue(true); }
private void initializeClient(ProcessContext context) { // NOTE: This method is called when isConnected returns false which can happen when the client is null, or when it is // non-null but not connected, so we need to handle each case and only create a new client when it is null try { if (mqttClient == null) { logger.debug("Creating client"); mqttClient = createMqttClient(broker, clientID, persistence); mqttClient.setCallback(this); } if (!mqttClient.isConnected()) { logger.debug("Connecting client"); mqttClient.connect(connOpts); } } catch (MqttException e) { logger.error("Connection to {} lost (or was never connected) and connection failed. Yielding processor", new Object[]{broker}, e); context.yield(); } }
@Test(expected = MqttException.class) public void checkClientAuthenticationFail() throws Exception { LOG.info("*** checkClientAuthenticationFail ***"); SSLSocketFactory ssf = configureSSLSocketFactory("unsignedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); // actual a "Broken pipe" is thrown, this is not very specific. try { m_client.connect(options); } catch (MqttException e) { e.printStackTrace(); throw e; } }
@Test public void checkSupportSSL() throws Exception { LOG.info("*** checkSupportSSL ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
@Test public void checkClientAuthentication() throws Exception { LOG.info("*** checkClientAuthentication ***"); SSLSocketFactory ssf = configureSSLSocketFactory("signedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
private void initializeClient(ProcessContext context) { // NOTE: This method is called when isConnected returns false which can happen when the client is null, or when it is // non-null but not connected, so we need to handle each case and only create a new client when it is null try { if (mqttClient == null) { logger.debug("Creating client"); mqttClient = createMqttClient(broker, clientID, persistence); mqttClient.setCallback(this); } if (!mqttClient.isConnected()) { logger.debug("Connecting client"); mqttClient.connect(connOpts); mqttClient.subscribe(topicFilter, qos); } } catch (MqttException e) { logger.error("Connection to {} lost (or was never connected) and connection failed. Yielding processor", new Object[]{broker}, e); context.yield(); } }
@Test public void checkRestartCleanSubscriptionTree() throws Exception { // subscribe to /topic m_subscriber.connect(CLEAN_SESSION_OPT); m_subscriber.subscribe("/topic", 1); m_subscriber.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); // reconnect the Subscriber subscribing to the same /topic but different QoS m_subscriber.connect(CLEAN_SESSION_OPT); m_subscriber.subscribe("/topic", 2); // should be just one registration so a publisher receive one notification m_publisher.connect(CLEAN_SESSION_OPT); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 1, false); // read the messages MqttMessage msg = m_messageCollector.waitMessage(1); assertEquals("Hello world MQTT!!", new String(msg.getPayload(), UTF_8)); // no more messages on the same topic will be received assertNull(m_messageCollector.waitMessage(1)); }
@Before public void setUp() throws Exception { startServer(); m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", new MemoryPersistence()); m_callback = new MessageCollector(); m_subscriber.setCallback(m_callback); m_subscriber.connect(); m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", new MemoryPersistence()); m_publisher.connect(); }
@Test public void checkDontPublishInactiveClientsAfterServerRestart() throws Exception { IMqttClient conn = subscribeAndPublish("/topic"); conn.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); m_publisher.connect(); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 0, false); }
/** * Connect subscribe to topic and publish on the same topic */ private IMqttClient subscribeAndPublish(String topic) throws Exception { IMqttClient client = new MqttClient("tcp://localhost:1883", "SubPub"); MessageCollector collector = new MessageCollector(); client.setCallback(collector); client.connect(); client.subscribe(topic, 1); client.publish(topic, "Hello world MQTT!!".getBytes(UTF_8), 0, false); MqttMessage msg = collector.waitMessage(1); assertEquals("Hello world MQTT!!", new String(msg.getPayload(), UTF_8)); return client; } }
@Ignore("This test hasn't any meaning using in memory storage service") @Test public void testCleanSession_maintainClientSubscriptions_withServerRestart() throws Exception { LOG.info("*** testCleanSession_maintainClientSubscriptions_withServerRestart ***"); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); m_server.stopServer(); m_server.startServer(IntegrationUtils.prepareTestProperties()); // reconnect and publish m_client.connect(options); m_client.publish("/topic", "Test my payload".getBytes(UTF_8), 0, false); assertEquals("/topic", m_messagesCollector.getTopic()); }
@Test public void testClientDoesntRemainSubscribedAfterASubscriptionAndServerRestart() throws Exception { // subscribe to /topic m_subscriber.connect(); // subscribe /topic m_subscriber.subscribe("/topic", 0); // unsubscribe from /topic m_subscriber.unsubscribe("/topic"); m_subscriber.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); // subscriber reconnects m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_dataStore); m_subscriber.setCallback(m_messageCollector); m_subscriber.connect(); // publisher publishes on /topic m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); m_publisher.connect(); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 1, false); // Expected // the subscriber doesn't get notified (it's fully unsubscribed) assertNull(m_messageCollector.waitMessage(1)); }
@Test public void testWillMessageIsPublishedOnClientBadDisconnection() throws InterruptedException, MqttException { LOG.info("*** testWillMessageIsPublishedOnClientBadDisconnection ***"); String willTestamentTopic = "/will/test"; String willTestamentMsg = "Bye bye"; m_willSubscriber.connect(); m_willSubscriber.subscribe(willTestamentTopic, 0); m_client.clientId("FAKECLNT").connect(willTestamentTopic, willTestamentMsg); // kill will publisher m_client.close(); // Verify will testament is published org.eclipse.paho.client.mqttv3.MqttMessage receivedTestament = m_messageCollector.waitMessage(1); assertEquals(willTestamentMsg, new String(receivedTestament.getPayload(), UTF_8)); m_willSubscriber.disconnect(); }
@Test public void connectWithWrongCredentials() { LOG.info("*** connectWithWrongCredentials ***"); try { m_client = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("dbuser"); options.setPassword("wrongPassword".toCharArray()); m_client.connect(options); } catch (MqttException e) { if (e instanceof MqttSecurityException) { assertTrue(true); return; } else { assertTrue(e.getMessage(), false); return; } } assertTrue("must not be connected. cause : wrong password given to client", false); }
@Test public void testWillMessageIsWiredOnClientKeepAliveExpiry() throws Exception { LOG.info("*** testWillMessageIsWiredOnClientKeepAliveExpiry ***"); String willTestamentTopic = "/will/test"; String willTestamentMsg = "Bye bye"; m_willSubscriber.connect(); m_willSubscriber.subscribe(willTestamentTopic, 0); m_client.clientId("FAKECLNT").connect(willTestamentTopic, willTestamentMsg); long connectTime = System.currentTimeMillis(); Awaitility.await() .atMost(7, TimeUnit.SECONDS) .untilAsserted(() -> { // but after the 2 KEEP ALIVE timeout expires it gets fired, // NB it's 1,5 * KEEP_ALIVE so 3 secs and some millis to propagate the message org.eclipse.paho.client.mqttv3.MqttMessage msg = m_messageCollector.getMessageImmediate(); assertNotNull("the will message should be fired after keep alive!", msg); // the will message hasn't to be received before the elapsing of Keep Alive timeout assertTrue(System.currentTimeMillis() - connectTime > 3000); assertEquals(willTestamentMsg, new String(msg.getPayload(), UTF_8)); }); m_willSubscriber.disconnect(); }
@Test public void checkSupportSSLForMultipleClient() throws Exception { LOG.info("*** checkSupportSSLForMultipleClient ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence()); MqttConnectOptions secondClientOptions = new MqttConnectOptions(); secondClientOptions.setSocketFactory(ssf); secondClient.connect(secondClientOptions); secondClient.publish("/topic", new MqttMessage("message".getBytes(UTF_8))); secondClient.disconnect(); m_client.disconnect(); }
subscriberB.subscribe("a/+", 2); m_client.connect(); m_client.publish("a/b", "Hello world MQTT!!".getBytes(UTF_8), 2, false);
private void verifyNotUnsubscribe(IMqttClient client) throws Exception { verify(client).connect(any(MqttConnectOptions.class)); verify(client).subscribe(any(String[].class), any(int[].class)); verify(client, never()).unsubscribe(any(String[].class)); verify(client).disconnectForcibly(anyLong()); }
private void verifyUnsubscribe(IMqttClient client) throws Exception { verify(client).connect(any(MqttConnectOptions.class)); verify(client).subscribe(any(String[].class), any(int[].class)); verify(client).unsubscribe(any(String[].class)); verify(client).disconnectForcibly(anyLong()); }