Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
MqttService: Remove all deprecated methods, rename close to stop and …
Browse files Browse the repository at this point in the history
…more.

* Remove name from MqttBrokerConnection. This is only necessary for the MqttService.
  Consumers of MqttBrokerConnection should not be bothered with that detail.
* Remove textConfigured flag. Again this is not interesting for anything outside MqttService.
* Don't parse configuration parameters ourself, but use a configuration values class instead.
* Use Nullable were it makes sense.

Signed-off-by: David Gräff <david.graeff@web.de>
  • Loading branch information
David Gräff committed Sep 8, 2017
1 parent 002c468 commit 5ca6321
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 728 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package org.eclipse.smarthome.io.transport.mqtt;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
Expand All @@ -27,31 +27,10 @@
*
* @author David Graeff - Initial contribution
*/
public class MqttBrokerConnectionTests {
@Test
public void testConstructor() throws ConfigurationException {
// Test tcp and ssl URLs
MqttBrokerConnection a = new MqttBrokerConnection("name", "tcp://123.123.123.123", false);
MqttBrokerConnection b = new MqttBrokerConnection("name", "ssl://123.123.123.123", true);
assertFalse(a.isTextualConfiguredBroker());
assertTrue(b.isTextualConfiguredBroker());
}

@Test(expected = ConfigurationException.class)
public void testConstructorInvalidProtocol() throws ConfigurationException {
new MqttBrokerConnection("name", "unsupported://123.123.123.123", false);
}

@Test(expected = ConfigurationException.class)
public void testConstructorInvalidName() throws ConfigurationException, MqttException {
new MqttBrokerConnection(" ", "tcp://123.123.123.123", false);
}

public class MqttBrokerConnectionTest {
@Test
public void messageConsumerTests() throws ConfigurationException, MqttException {
final String url = "tcp://123.123.123.123";
final String name = "TestName12@!";
MqttBrokerConnection a = new MqttBrokerConnection(name, url, false);
MqttBrokerConnection a = new MqttBrokerConnection("123.123.123.123", null, false, null);
// Expect no consumers
assertFalse(a.hasConsumers());

Expand All @@ -69,38 +48,33 @@ public void messageConsumerTests() throws ConfigurationException, MqttException

@Test
public void reconnectPolicyDefaultTest() throws ConfigurationException, MqttException, InterruptedException {
final String url = "tcp://123.123.123.123";
final String name = "TestName12@!";
MqttBrokerConnection a = new MqttBrokerConnection(name, url, false);
MqttBrokerConnection a = new MqttBrokerConnection("123.123.123.123", null, false, null);

// Check if the default policy is set and that the broker within the policy is set.
assertTrue(a.getReconnectStrategy() instanceof PeriodicReconnectStrategy);
AbstractReconnectStrategy p = a.getReconnectStrategy();
assertThat(p.getBrokerConnection(), is(a));
assertThat(p.getBrokerConnection(), equalTo(a));
}

@Test
public void reconnectPolicyTests() throws ConfigurationException, MqttException, InterruptedException {
final String url = "tcp://123.123.123.123";
final String name = "TestName12@!";
MqttBrokerConnection a = spy(new MqttBrokerConnection(name, url, false));
MqttBrokerConnection a = spy(new MqttBrokerConnection("123.123.123.123", null, false, null));

// Check setter
a.setReconnectStrategy(new PeriodicReconnectStrategy());
assertThat(a.getReconnectStrategy().getBrokerConnection(), is(a));
assertThat(a.getReconnectStrategy().getBrokerConnection(), equalTo(a));

// Prepare a Mock to test if lostConnect is called and
// if the PeriodicReconnectPolicy indeed calls start()
PeriodicReconnectStrategy mockPolicy = spy(new PeriodicReconnectStrategy());
doReturn(a).when(mockPolicy).getBrokerConnection();
doReturn(0).when(mockPolicy).getFirstReconnectAfter();
doReturn(10000).when(mockPolicy).getReconnectFrequency();
PeriodicReconnectStrategy mockPolicy = spy(new PeriodicReconnectStrategy(10000, 0));
doNothing().when(a).start();
mockPolicy.start();
a.isConnecting = true;

// Fake a disconnect
a.setReconnectStrategy(mockPolicy);
IMqttActionListener l = a.createConnectionListener();
doReturn(false).when(a).isConnected();
doReturn(MqttConnectionState.DISCONNECTED).when(a).connectionState();
IMqttToken token = mock(IMqttToken.class);
when(token.getException()).thenReturn(new org.eclipse.paho.client.mqttv3.MqttException(1));
l.onFailure(token, null);
Expand All @@ -118,9 +92,7 @@ public void reconnectPolicyTests() throws ConfigurationException, MqttException,

@Test
public void connectionObserverTests() throws ConfigurationException, MqttException {
final String url = "tcp://123.123.123.123";
final String name = "TestName12@!";
MqttBrokerConnection a = spy(new MqttBrokerConnection(name, url, false));
MqttBrokerConnection a = spy(new MqttBrokerConnection("123.123.123.123", null, false, null));

// Add an observer
assertFalse(a.hasConnectionObservers());
Expand All @@ -133,7 +105,7 @@ public void connectionObserverTests() throws ConfigurationException, MqttExcepti

// Cause a success callback
IMqttActionListener l = a.createConnectionListener();
doReturn(true).when(a).isConnected();
doReturn(MqttConnectionState.CONNECTED).when(a).connectionState();
l.onSuccess(null);
verify(connectionObserver, times(1)).connectionStateChanged(eq(MqttConnectionState.CONNECTED), anyObject());

Expand All @@ -143,7 +115,7 @@ public void connectionObserverTests() throws ConfigurationException, MqttExcepti
1);
when(token.getException()).thenReturn(testException);

doReturn(false).when(a).isConnected();
doReturn(MqttConnectionState.DISCONNECTED).when(a).connectionState();
l.onFailure(token, null);
verify(connectionObserver, times(1)).connectionStateChanged(eq(MqttConnectionState.DISCONNECTED),
eq(testException));
Expand All @@ -155,9 +127,7 @@ public void connectionObserverTests() throws ConfigurationException, MqttExcepti

@Test
public void lastWillAndTestamentTests() throws ConfigurationException {
final String url = "tcp://123.123.123.123";
final String name = "TestName12@!";
MqttBrokerConnection a = new MqttBrokerConnection(name, url, false);
MqttBrokerConnection a = new MqttBrokerConnection("123.123.123.123", null, false, null);

assertNull(a.getLastWill());
assertNull(MqttWillAndTestament.fromString(""));
Expand All @@ -174,19 +144,27 @@ public void lastWillAndTestamentConstructorTests() {
new MqttWillAndTestament("", new byte[0], 0, false);
}

@Test(expected = IllegalArgumentException.class)
public void tooLongClientID() throws ConfigurationException {
// client ids longer than 23 characters should throw
MqttBrokerConnection a = new MqttBrokerConnection("123.123.123.123", null, false,
"clientidclientidclientidclientid");
}

@Test(expected = IllegalArgumentException.class)
public void qosInvalid() throws ConfigurationException {
MqttBrokerConnection a = new MqttBrokerConnection("123.123.123.123", null, false, null);
a.setQos(10);
}

@Test
public void setterGetterTests() throws ConfigurationException {
final String url = "tcp://123.123.123.123";
final String name = "TestName12@!";
MqttBrokerConnection a = new MqttBrokerConnection(name, url, false);
assertEquals("URL getter", a.getUrl(), url);
assertEquals("Name getter", a.getName(), name);

a.setClientId("clientid");
public void setterGetterTests() {
MqttBrokerConnection a = new MqttBrokerConnection("123.123.123.123", null, false, "clientid");
assertEquals("URL getter", a.getHost(), "123.123.123.123");
assertEquals("Name getter", a.getPort(), null);
assertEquals("Secure getter", a.isSecure(), false);

assertEquals("ClientID getter/setter", "clientid", a.getClientId());
// client ids longer than 23 characters should be ignored
a.setClientId("clientidclientidclientidclientid");
assertEquals("ClientID too long check", "clientid", a.getClientId());

a.setCredentials("user@!", "password123@^");
assertEquals("User getter/setter", "user@!", a.getUser());
Expand All @@ -200,10 +178,6 @@ public void setterGetterTests() throws ConfigurationException {
a.setRetain(true);
assertTrue(a.isRetain());

assertEquals(MqttBrokerConnection.DEFAULT_QOS, a.getQos());
a.setQos(10);
assertEquals(MqttBrokerConnection.DEFAULT_QOS, a.getQos());
a.setQos(-10);
assertEquals(MqttBrokerConnection.DEFAULT_QOS, a.getQos());
a.setQos(2);
assertEquals(2, a.getQos());
Expand All @@ -214,6 +188,6 @@ public void setterGetterTests() throws ConfigurationException {
assertNotNull(a.getSSLContextProvider());
assertNotNull(a.getReconnectStrategy());

assertFalse(a.isConnected());
assertThat(a.connectionState(), equalTo(MqttConnectionState.DISCONNECTED));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.io.transport.mqtt;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;

import javax.naming.ConfigurationException;

import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.io.transport.mqtt.internal.ConnectionConfiguration;
import org.junit.Test;

/**
* Tests the MqttService class
*
* @author David Graeff - Initial contribution
*/
public class MqttServiceTest {
// Tests addBrokersListener/removeBrokersListener
@Test
public void brokerConnectionListenerTests() throws ConfigurationException {
MqttService service = new MqttService();
assertFalse(service.hasBrokerObservers());
MqttServiceObserver observer = mock(MqttServiceObserver.class);

service.addBrokersListener(observer);
assertTrue(service.hasBrokerObservers());

MqttBrokerConnection connection = new MqttBrokerConnection("123.123.123.123", 0, false, null);
assertTrue(service.addBrokerConnection("name", connection));
verify(observer).brokerAdded(eq("name"), eq(connection));

service.removeBrokerConnection("name");
verify(observer).brokerRemoved(eq("name"), eq(connection));

service.removeBrokersListener(observer);
assertFalse(service.hasBrokerObservers());
}

// Tests extractBrokerConfigurations() and addBrokerConnection(map)
@Test
public void extractBrokerConfigurationsTests() throws ConfigurationException, MqttException {
MqttService service = new MqttService();

Map<String, Object> properties = new Hashtable<>();
properties.put("bam.host", "123.123.123.123");
properties.put("bam.port", BigDecimal.valueOf(1234));
properties.put("bam.secure", false);
Map<String, Map<String, Object>> map = service.extractBrokerConfigurations(properties);
assertEquals(map.size(), 1);
Map<String, Object> data = map.get("bam");
@SuppressWarnings("null")
ConnectionConfiguration c = new Configuration(data).as(ConnectionConfiguration.class);
assertEquals("123.123.123.123", c.host);
assertEquals(false, c.secure);
assertEquals(BigDecimal.valueOf(1234), c.port);
}

// Tests if updates to the system broker connections via ConfigAdmin are processed correctly
@Test
public void updateViaConfigAdminTests() throws ConfigurationException, MqttException {
MqttService service = new MqttService();

Map<String, Object> properties = new Hashtable<>();
properties.put("bam.host", "123.123.123.123");

// Test activate
service.activate(properties);
assertThat(service.getAllBrokerConnections().size(), equalTo(1));
assertNotNull(service.getBrokerConnection("bam"));

Map<String, Object> properties2 = new Hashtable<>();
properties2.put("bam2.url", "123.123.123.123");

// Test configuration change
service.modified(properties2);
assertThat(service.getAllBrokerConnections().size(), equalTo(1));
assertNull(service.getBrokerConnection("bam"));
assertNotNull(service.getBrokerConnection("bam2"));

// Test if old broker connections are freed correctly
service.modified(Collections.emptyMap());
assertThat(service.getAllBrokerConnections().size(), equalTo(0));
}

@Test
public void brokerConnectionAddRemoveEnumerateTests() {
MqttService service = new MqttService();
MqttBrokerConnection connection = new MqttBrokerConnection("123.123.123.123", 0, false, null);
// Add
assertThat(service.getAllBrokerConnections().size(), equalTo(0));
assertTrue(service.addBrokerConnection("name", connection));
assertFalse(service.addBrokerConnection("name", connection));

// Get/Enumerate
assertNotNull(service.getBrokerConnection("name"));
assertThat(service.getAllBrokerConnections().size(), equalTo(1));

// Remove
service.removeBrokerConnection("name");
assertThat(service.getAllBrokerConnections().size(), equalTo(0));
}
}
Loading

0 comments on commit 5ca6321

Please sign in to comment.