This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MqttService: Remove all deprecated methods, rename close to stop and …
…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
Showing
14 changed files
with
377 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...port.mqtt.test/src/test/java/org/eclipse/smarthome/io/transport/mqtt/MqttServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.