[mqtt] Fix most SAT findings (#12492)
Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
@@ -24,8 +24,6 @@ import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChanne
|
||||
import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
|
||||
import org.openhab.binding.mqtt.homeassistant.internal.exception.UnsupportedComponentException;
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
@@ -37,8 +35,6 @@ import com.google.gson.Gson;
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class ComponentFactory {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ComponentFactory.class);
|
||||
|
||||
/**
|
||||
* Create a HA MQTT component. The configuration JSon string is required.
|
||||
*
|
||||
|
||||
@@ -154,7 +154,7 @@ public class ChannelConfigurationTypeAdapterFactory implements TypeAdapterFactor
|
||||
|
||||
field.set(config, newValue);
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,14 @@
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal.exception;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* Exception class for errors in HomeAssistant components configurations
|
||||
*
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class ConfigurationException extends RuntimeException {
|
||||
public ConfigurationException(String message) {
|
||||
super(message);
|
||||
|
||||
@@ -12,11 +12,14 @@
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal.exception;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* Exception class for unsupported components
|
||||
*
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class UnsupportedComponentException extends ConfigurationException {
|
||||
public UnsupportedComponentException(String message) {
|
||||
super(message);
|
||||
|
||||
@@ -19,12 +19,14 @@ import static org.hamcrest.core.IsIterableContaining.hasItem;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.core.config.core.Configuration;
|
||||
|
||||
/**
|
||||
* @author Jochen Klein - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class HaIDTests {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -33,7 +34,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.mockito.Mock;
|
||||
@@ -56,12 +56,13 @@ import org.openhab.core.types.State;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings({ "ConstantConditions" })
|
||||
@NonNullByDefault
|
||||
public abstract class AbstractComponentTests extends AbstractHomeAssistantTests {
|
||||
private final static int SUBSCRIBE_TIMEOUT = 10000;
|
||||
private final static int ATTRIBUTE_RECEIVE_TIMEOUT = 2000;
|
||||
private static final int SUBSCRIBE_TIMEOUT = 10000;
|
||||
private static final int ATTRIBUTE_RECEIVE_TIMEOUT = 2000;
|
||||
|
||||
private @Mock ThingHandlerCallback callback;
|
||||
private LatchThingHandler thingHandler;
|
||||
private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
|
||||
private @NonNullByDefault({}) LatchThingHandler thingHandler;
|
||||
|
||||
@BeforeEach
|
||||
public void setupThingHandler() {
|
||||
@@ -70,12 +71,12 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
|
||||
config.put(HandlerConfiguration.PROPERTY_BASETOPIC, HandlerConfiguration.DEFAULT_BASETOPIC);
|
||||
config.put(HandlerConfiguration.PROPERTY_TOPICS, getConfigTopics());
|
||||
|
||||
when(callback.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
|
||||
when(callbackMock.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
|
||||
|
||||
thingHandler = new LatchThingHandler(haThing, channelTypeProvider, transformationServiceProvider,
|
||||
SUBSCRIBE_TIMEOUT, ATTRIBUTE_RECEIVE_TIMEOUT);
|
||||
thingHandler.setConnection(bridgeConnection);
|
||||
thingHandler.setCallback(callback);
|
||||
thingHandler.setCallback(callbackMock);
|
||||
thingHandler = spy(thingHandler);
|
||||
|
||||
thingHandler.initialize();
|
||||
@@ -124,9 +125,7 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
|
||||
} catch (InterruptedException e) {
|
||||
assertThat(e.getMessage(), false);
|
||||
}
|
||||
var component = thingHandler.getDiscoveredComponent();
|
||||
assertThat(component, CoreMatchers.notNullValue());
|
||||
return component;
|
||||
return Objects.requireNonNull(thingHandler.getDiscoveredComponent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,7 +140,7 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
|
||||
*/
|
||||
protected static void assertChannel(AbstractComponent<@NonNull ? extends AbstractChannelConfiguration> component,
|
||||
String channelId, String stateTopic, String commandTopic, String label, Class<? extends Value> valueClass) {
|
||||
var stateChannel = component.getChannel(channelId);
|
||||
var stateChannel = Objects.requireNonNull(component.getChannel(channelId));
|
||||
assertChannel(stateChannel, stateTopic, commandTopic, label, valueClass);
|
||||
}
|
||||
|
||||
@@ -236,7 +235,6 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
|
||||
return false;
|
||||
}
|
||||
|
||||
@NonNullByDefault
|
||||
protected static class LatchThingHandler extends HomeAssistantThingHandler {
|
||||
private @Nullable CountDownLatch latch;
|
||||
private @Nullable AbstractComponent<@NonNull ? extends AbstractChannelConfiguration> discoveredComponent;
|
||||
@@ -247,6 +245,7 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
|
||||
super(thing, channelTypeProvider, transformationServiceProvider, subscribeTimeout, attributeReceiveTimeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentDiscovered(HaID homeAssistantTopicID, AbstractComponent<@NonNull ?> component) {
|
||||
accept(List.of(component));
|
||||
discoveredComponent = component;
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.TextValue;
|
||||
import org.openhab.core.library.types.StringType;
|
||||
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.StringType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@NonNullByDefault
|
||||
public class AlarmControlPanelTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "alarm_control_panel/0x0000000000000000_alarm_control_panel_zigbee2mqtt";
|
||||
|
||||
@@ -89,6 +91,7 @@ public class AlarmControlPanelTests extends AbstractComponentTests {
|
||||
assertPublished("zigbee2mqtt/alarm/set/state", "ARM_HOME_");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.OnOffValue;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
@@ -27,6 +28,7 @@ import org.openhab.core.types.UnDefType;
|
||||
*
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class BinarySensorTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "binary_sensor/0x0000000000000000_binary_sensor_zigbee2mqtt";
|
||||
|
||||
@@ -148,6 +150,7 @@ public class BinarySensorTests extends AbstractComponentTests {
|
||||
waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.ImageValue;
|
||||
import org.openhab.core.library.types.RawType;
|
||||
@@ -26,6 +27,7 @@ import org.openhab.core.library.types.RawType;
|
||||
*
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class CameraTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "camera/0x0000000000000000_camera_zigbee2mqtt";
|
||||
|
||||
@@ -63,6 +65,7 @@ public class CameraTests extends AbstractComponentTests {
|
||||
assertState(component, Camera.CAMERA_CHANNEL_ID, new RawType(imageBytes, "image/png"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.NumberValue;
|
||||
import org.openhab.binding.mqtt.generic.values.OnOffValue;
|
||||
@@ -34,6 +35,7 @@ import org.openhab.core.library.unit.SIUnits;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@NonNullByDefault
|
||||
public class ClimateTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "climate/0x847127fffe11dd6a_climate_zigbee2mqtt";
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.RollershutterValue;
|
||||
import org.openhab.core.library.types.PercentType;
|
||||
@@ -28,6 +29,7 @@ import org.openhab.core.library.types.StopMoveType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@NonNullByDefault
|
||||
public class CoverTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "cover/0x0000000000000000_cover_zigbee2mqtt";
|
||||
|
||||
@@ -82,6 +84,7 @@ public class CoverTests extends AbstractComponentTests {
|
||||
assertPublished("zigbee2mqtt/cover/set/state", "STOP_", 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.OnOffValue;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.OnOffType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ALL")
|
||||
@NonNullByDefault
|
||||
public class FanTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "fan/0x0000000000000000_fan_zigbee2mqtt";
|
||||
|
||||
@@ -78,6 +80,7 @@ public class FanTests extends AbstractComponentTests {
|
||||
assertPublished("zigbee2mqtt/fan/set/state", "ON_");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -19,11 +19,12 @@ import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
|
||||
import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
|
||||
@@ -36,6 +37,7 @@ import com.google.gson.GsonBuilder;
|
||||
/**
|
||||
* @author Jochen Klein - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class HAConfigurationTests {
|
||||
|
||||
private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
|
||||
@@ -53,7 +55,7 @@ public class HAConfigurationTests {
|
||||
}
|
||||
return result.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +81,7 @@ public class HAConfigurationTests {
|
||||
if (device != null) {
|
||||
assertThat(device.getIdentifiers(), contains("H"));
|
||||
assertThat(device.getConnections(), is(notNullValue()));
|
||||
List<@NonNull Connection> connections = device.getConnections();
|
||||
List<Connection> connections = device.getConnections();
|
||||
if (connections != null) {
|
||||
assertThat(connections.get(0).getType(), is("I1"));
|
||||
assertThat(connections.get(0).getIdentifier(), is("I2"));
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.ColorValue;
|
||||
import org.openhab.core.library.types.HSBType;
|
||||
@@ -28,6 +29,7 @@ import org.openhab.core.library.types.OnOffType;
|
||||
*
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class LightTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "light/0x0000000000000000_light_zigbee2mqtt";
|
||||
|
||||
@@ -85,6 +87,7 @@ public class LightTests extends AbstractComponentTests {
|
||||
assertPublished("zigbee2mqtt/light/set/state", "0,0,0");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.OnOffValue;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.OnOffType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ALL")
|
||||
@NonNullByDefault
|
||||
public class LockTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "lock/0x0000000000000000_lock_zigbee2mqtt";
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.NumberValue;
|
||||
import org.openhab.core.library.types.QuantityType;
|
||||
@@ -29,6 +30,7 @@ import org.openhab.core.types.UnDefType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@NonNullByDefault
|
||||
public class SensorTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.OnOffValue;
|
||||
import org.openhab.core.library.types.OnOffType;
|
||||
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.OnOffType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@NonNullByDefault
|
||||
public class SwitchTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
|
||||
|
||||
@@ -117,6 +119,7 @@ public class SwitchTests extends AbstractComponentTests {
|
||||
assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openhab.binding.mqtt.generic.values.OnOffValue;
|
||||
import org.openhab.binding.mqtt.generic.values.PercentageValue;
|
||||
@@ -32,6 +33,7 @@ import org.openhab.core.types.UnDefType;
|
||||
* @author Anton Kharuzhy - Initial contribution
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@NonNullByDefault
|
||||
public class VacuumTests extends AbstractComponentTests {
|
||||
public static final String CONFIG_TOPIC = "vacuum/rockrobo_vacuum";
|
||||
|
||||
@@ -248,6 +250,7 @@ public class VacuumTests extends AbstractComponentTests {
|
||||
assertPublished("vacuum/send_command", "custom_command");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> getConfigTopics() {
|
||||
return Set.of(CONFIG_TOPIC);
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@
|
||||
*/
|
||||
package org.openhab.binding.mqtt.homeassistant.internal.discovery;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -46,8 +45,9 @@ import org.openhab.core.thing.ThingUID;
|
||||
*/
|
||||
@SuppressWarnings({ "ConstantConditions", "unchecked" })
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@NonNullByDefault
|
||||
public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
|
||||
private HomeAssistantDiscovery discovery;
|
||||
private @NonNullByDefault({}) HomeAssistantDiscovery discovery;
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() {
|
||||
@@ -89,11 +89,11 @@ public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
|
||||
}
|
||||
}
|
||||
|
||||
@NonNullByDefault
|
||||
private static class LatchDiscoveryListener implements DiscoveryListener {
|
||||
private final CopyOnWriteArrayList<DiscoveryResult> discoveryResults = new CopyOnWriteArrayList<>();
|
||||
private @Nullable CountDownLatch latch;
|
||||
|
||||
@Override
|
||||
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
|
||||
discoveryResults.add(result);
|
||||
if (latch != null) {
|
||||
@@ -101,9 +101,11 @@ public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
|
||||
@Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
|
||||
return Collections.emptyList();
|
||||
|
||||
@@ -13,20 +13,15 @@
|
||||
package org.openhab.binding.mqtt.homeassistant.internal.handler;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -47,6 +42,7 @@ import org.openhab.core.thing.binding.ThingHandlerCallback;
|
||||
*/
|
||||
@SuppressWarnings({ "ConstantConditions" })
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@NonNullByDefault
|
||||
public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
|
||||
private static final int SUBSCRIBE_TIMEOUT = 10000;
|
||||
private static final int ATTRIBUTE_RECEIVE_TIMEOUT = 2000;
|
||||
@@ -62,8 +58,8 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
|
||||
private static final List<String> MQTT_TOPICS = CONFIG_TOPICS.stream()
|
||||
.map(AbstractHomeAssistantTests::configTopicToMqtt).collect(Collectors.toList());
|
||||
|
||||
private @Mock ThingHandlerCallback callback;
|
||||
private HomeAssistantThingHandler thingHandler;
|
||||
private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
|
||||
private @NonNullByDefault({}) HomeAssistantThingHandler thingHandler;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
@@ -72,12 +68,12 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
|
||||
config.put(HandlerConfiguration.PROPERTY_BASETOPIC, HandlerConfiguration.DEFAULT_BASETOPIC);
|
||||
config.put(HandlerConfiguration.PROPERTY_TOPICS, CONFIG_TOPICS);
|
||||
|
||||
when(callback.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
|
||||
when(callbackMock.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
|
||||
|
||||
thingHandler = new HomeAssistantThingHandler(haThing, channelTypeProvider, transformationServiceProvider,
|
||||
SUBSCRIBE_TIMEOUT, ATTRIBUTE_RECEIVE_TIMEOUT);
|
||||
thingHandler.setConnection(bridgeConnection);
|
||||
thingHandler.setCallback(callback);
|
||||
thingHandler.setCallback(callbackMock);
|
||||
thingHandler = spy(thingHandler);
|
||||
}
|
||||
|
||||
@@ -86,7 +82,7 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
|
||||
// When initialize
|
||||
thingHandler.initialize();
|
||||
|
||||
verify(callback).statusUpdated(eq(haThing), any());
|
||||
verify(callbackMock).statusUpdated(eq(haThing), any());
|
||||
// Expect a call to the bridge status changed, the start, the propertiesChanged method
|
||||
verify(thingHandler).bridgeStatusChanged(any());
|
||||
verify(thingHandler, timeout(SUBSCRIBE_TIMEOUT)).start(any());
|
||||
|
||||
Reference in New Issue
Block a user