Migrate tests to JUnit 5 (#8519)

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2020-09-21 18:21:26 +02:00
committed by GitHub
parent 6df6783b60
commit bd82ca82df
478 changed files with 3996 additions and 4419 deletions

View File

@@ -12,12 +12,12 @@
*/
package org.openhab.binding.tplinksmarthome.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Test class for {@link CryptUtil} class.
@@ -30,13 +30,13 @@ public class CryptUtilTest {
/**
* Test round trip of encrypt and decrypt that should return the same value.
*
*
* @throws IOException exception in case device not reachable
*/
@Test
public void testCrypt() throws IOException {
assertEquals("Crypting should result in same string", TEST_STRING,
CryptUtil.decrypt(CryptUtil.encrypt(TEST_STRING), TEST_STRING.length()));
assertEquals(TEST_STRING, CryptUtil.decrypt(CryptUtil.encrypt(TEST_STRING), TEST_STRING.length()),
"Crypting should result in same string");
}
/**
@@ -47,7 +47,7 @@ public class CryptUtilTest {
@Test
public void testCryptWithLength() throws IOException {
try (final ByteArrayInputStream is = new ByteArrayInputStream(CryptUtil.encryptWithLength(TEST_STRING))) {
assertEquals("Crypting should result in same string", TEST_STRING, CryptUtil.decryptWithLength(is));
assertEquals(TEST_STRING, CryptUtil.decryptWithLength(is), "Crypting should result in same string");
}
}
}

View File

@@ -12,13 +12,13 @@
*/
package org.openhab.binding.tplinksmarthome.internal;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.tplinksmarthome.internal.model.GetSysinfo;
import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
@@ -65,8 +65,8 @@ public class PropertiesCollectorTest {
final Map<String, Object> props = PropertiesCollector.collectProperties(thingType, "localhost",
ModelTestUtil.jsonFromFile(responseFile, GetSysinfo.class).getSysinfo());
assertEquals("Number of properties not as expected for properties: " + props, expectedSize, props.size());
assertEquals(expectedSize, props.size(), "Number of properties not as expected for properties: " + props);
props.entrySet().stream().forEach(
entry -> assertNotNull("Property '" + entry.getKey() + "' should not be null", entry.getValue()));
entry -> assertNotNull(entry.getValue(), "Property '" + entry.getKey() + "' should not be null"));
}
}

View File

@@ -12,10 +12,9 @@
*/
package org.openhab.binding.tplinksmarthome.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import java.io.IOException;
import java.net.DatagramPacket;
@@ -25,14 +24,13 @@ import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
import org.openhab.core.config.discovery.DiscoveryListener;
@@ -43,36 +41,22 @@ import org.openhab.core.config.discovery.DiscoveryResult;
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@RunWith(value = Parameterized.class)
@ExtendWith(MockitoExtension.class)
public class TPLinkSmartHomeDiscoveryServiceTest {
private static final List<Object[]> TESTS = Arrays.asList(
new Object[][] { { "bulb_get_sysinfo_response_on", 11 }, { "rangeextender_get_sysinfo_response", 11 } });
@Mock
private DatagramSocket discoverSocket;
@Mock
private DiscoveryListener discoveryListener;
private @Mock DatagramSocket discoverSocket;
private @Mock DiscoveryListener discoveryListener;
private TPLinkSmartHomeDiscoveryService discoveryService;
private final String filename;
private final int propertiesSize;
public TPLinkSmartHomeDiscoveryServiceTest(String filename, int propertiesSize) {
this.filename = filename;
this.propertiesSize = propertiesSize;
}
@Parameters(name = "{0}")
public static List<Object[]> data() {
return TESTS;
}
@Before
public void setUp() throws IOException {
initMocks(this);
public void setUp(String filename) throws IOException {
discoveryService = new TPLinkSmartHomeDiscoveryService() {
@Override
protected DatagramSocket sendDiscoveryPacket() throws IOException {
@@ -98,16 +82,20 @@ public class TPLinkSmartHomeDiscoveryServiceTest {
/**
* Test if startScan method finds a device with expected properties.
*
* @throws IOException
*/
@Test
public void testScan() {
@ParameterizedTest
@MethodSource("data")
public void testScan(String filename, int propertiesSize) throws IOException {
setUp(filename);
discoveryService.startScan();
ArgumentCaptor<DiscoveryResult> discoveryResultCaptor = ArgumentCaptor.forClass(DiscoveryResult.class);
verify(discoveryListener).thingDiscovered(any(), discoveryResultCaptor.capture());
DiscoveryResult discoveryResult = discoveryResultCaptor.getValue();
assertEquals("Check if correct binding id found", TPLinkSmartHomeBindingConstants.BINDING_ID,
discoveryResult.getBindingId());
assertEquals("Check if expected number of properties found", propertiesSize,
discoveryResult.getProperties().size());
assertEquals(TPLinkSmartHomeBindingConstants.BINDING_ID, discoveryResult.getBindingId(),
"Check if correct binding id found");
assertEquals(propertiesSize, discoveryResult.getProperties().size(),
"Check if expected number of properties found");
}
}

View File

@@ -12,20 +12,18 @@
*/
package org.openhab.binding.tplinksmarthome.internal;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.binding.tplinksmarthome.internal.device.BulbDevice;
import org.openhab.binding.tplinksmarthome.internal.device.DimmerDevice;
import org.openhab.binding.tplinksmarthome.internal.device.EnergySwitchDevice;
@@ -41,7 +39,7 @@ import org.openhab.core.thing.ThingTypeUID;
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@RunWith(value = Parameterized.class)
@ExtendWith(MockitoExtension.class)
public class TPLinkSmartHomeHandlerFactoryTest {
private static final String SMART_HOME_DEVICE_FIELD = "smartHomeDevice";
@@ -67,42 +65,28 @@ public class TPLinkSmartHomeHandlerFactoryTest {
});
// @formatter:on
@Mock
Thing thing;
private @Mock Thing thing;
private final String name;
private final Class<?> clazz;
public TPLinkSmartHomeHandlerFactoryTest(String name, Class<?> clazz) {
this.name = name;
this.clazz = clazz;
}
@Parameters(name = "{0} - {1}")
public static List<Object[]> data() {
return TESTS;
}
@Before
public void setUp() {
initMocks(this);
}
@Test
public void testCorrectClass()
@ParameterizedTest
@MethodSource("data")
public void testCorrectClass(String name, Class<?> clazz)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
when(thing.getThingTypeUID()).thenReturn(new ThingTypeUID(TPLinkSmartHomeBindingConstants.BINDING_ID, name));
SmartHomeHandler handler = (SmartHomeHandler) factory.createHandler(thing);
if (clazz == null) {
assertNull(name + " should not return any handler but null", handler);
assertNull(handler, name + " should not return any handler but null");
} else {
assertNotNull(name + " should no return null handler", handler);
assertNotNull(handler, name + " should no return null handler");
Field smartHomeDeviceField = SmartHomeHandler.class.getDeclaredField(SMART_HOME_DEVICE_FIELD);
smartHomeDeviceField.setAccessible(true);
assertSame(name + " should return expected device class", clazz,
smartHomeDeviceField.get(handler).getClass());
assertSame(clazz, smartHomeDeviceField.get(handler).getClass(),
name + " should return expected device class");
}
}
}

View File

@@ -12,7 +12,7 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.LB130;
@@ -20,7 +20,8 @@ import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingT
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
@@ -43,6 +44,7 @@ public class BulbDeviceTest extends DeviceTestBase<BulbDevice> {
"bulb_get_sysinfo_response_on");
}
@BeforeEach
@Override
public void setUp() throws IOException {
super.setUp();
@@ -52,120 +54,120 @@ public class BulbDeviceTest extends DeviceTestBase<BulbDevice> {
@Test
public void testHandleCommandBrightness() throws IOException {
assertInput("bulb_transition_light_state_brightness");
assertTrue("Brightness channel should be handled",
device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(33)));
assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(33)),
"Brightness channel should be handled");
}
@Test
public void testHandleCommandBrightnessOnOff() throws IOException {
assertInput("bulb_transition_light_state_on");
assertTrue("Brightness channel with OnOff state should be handled",
device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON),
"Brightness channel with OnOff state should be handled");
}
@Test
public void testHandleCommandColor() throws IOException {
assertInput("bulb_transition_light_state_color");
assertTrue("Color channel should be handled", device.handleCommand(CHANNEL_UID_COLOR, new HSBType("55,44,33")));
assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new HSBType("55,44,33")), "Color channel should be handled");
}
public void testHandleCommandColorBrightness() throws IOException {
assertInput("bulb_transition_light_state_brightness");
assertTrue("Color channel with Percentage state (=brightness) should be handled",
device.handleCommand(CHANNEL_UID_COLOR, new PercentType(33)));
assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new PercentType(33)),
"Color channel with Percentage state (=brightness) should be handled");
}
public void testHandleCommandColorOnOff() throws IOException {
assertInput("bulb_transition_light_state_on");
assertTrue("Color channel with OnOff state should be handled",
device.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON),
"Color channel with OnOff state should be handled");
}
@Test
public void testHandleCommandColorTemperature() throws IOException {
assertInput("bulb_transition_light_state_color_temp");
assertTrue("Color temperature channel should be handled",
device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, new PercentType(40)));
assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, new PercentType(40)),
"Color temperature channel should be handled");
}
@Test
public void testHandleCommandColorTemperatureAbs() throws IOException {
assertInput("bulb_transition_light_state_color_temp");
assertTrue("Color temperature channel should be handled",
device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE_ABS, new DecimalType(5100)));
assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE_ABS, new DecimalType(5100)),
"Color temperature channel should be handled");
}
@Test
public void testHandleCommandColorTemperatureOnOff() throws IOException {
assertInput("bulb_transition_light_state_on");
assertTrue("Color temperature channel with OnOff state should be handled",
device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, OnOffType.ON),
"Color temperature channel with OnOff state should be handled");
}
@Test
public void testHandleCommandSwitch() throws IOException {
assertInput("bulb_transition_light_state_on");
assertTrue("Switch channel should be handled", device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON), "Switch channel should be handled");
}
@Test
public void testUpdateChannelBrightnessOn() {
assertEquals("Brightness should be on", new PercentType(92),
device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState));
assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
"Brightness should be on");
}
@Test
public void testUpdateChannelBrightnessOff() throws IOException {
deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
assertEquals("Brightness should be off", PercentType.ZERO,
device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState));
assertEquals(PercentType.ZERO, device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
"Brightness should be off");
}
@Test
public void testUpdateChannelColorOn() {
assertEquals("Color should be on", new HSBType("7,44,92"),
device.updateChannel(CHANNEL_UID_COLOR, deviceState));
assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
"Color should be on");
}
@Test
public void testUpdateChannelColorOff() throws IOException {
deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
assertEquals("Color should be off", new HSBType("7,44,0"),
device.updateChannel(CHANNEL_UID_COLOR, deviceState));
assertEquals(new HSBType("7,44,0"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
"Color should be off");
}
@Test
public void testUpdateChannelSwitchOn() {
assertSame("Switch should be on", OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState));
assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
}
@Test
public void testUpdateChannelSwitchOff() throws IOException {
deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
assertSame("Switch should be off", OnOffType.OFF, device.updateChannel(CHANNEL_UID_SWITCH, deviceState));
assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be off");
}
@Test
public void testUpdateChannelColorTemperature() {
assertEquals("Color temperature should be set", new PercentType(2),
device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState));
assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
"Color temperature should be set");
}
@Test
public void testUpdateChannelColorTemperatureAbs() {
assertEquals("Color temperature should be set", new DecimalType(2630),
device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState));
assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
"Color temperature should be set");
}
@Test
public void testUpdateChannelOther() {
assertSame("Unknown channel should return UNDEF", UnDefType.UNDEF,
device.updateChannel(CHANNEL_UID_OTHER, deviceState));
assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
"Unknown channel should return UNDEF");
}
@Test
public void testUpdateChannelPower() {
assertEquals("Power values should be set", new DecimalType(10.8),
device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState));
assertEquals(new DecimalType(10.8), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
"Power values should be set");
}
}

View File

@@ -12,10 +12,9 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -25,8 +24,12 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.tplinksmarthome.internal.Connection;
import org.openhab.binding.tplinksmarthome.internal.CryptUtil;
import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeConfiguration;
@@ -37,6 +40,8 @@ import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
@NonNullByDefault
public class DeviceTestBase<T extends SmartHomeDevice> {
@@ -47,10 +52,8 @@ public class DeviceTestBase<T extends SmartHomeDevice> {
private final String deviceStateFilename;
@Mock
private @NonNullByDefault({}) Socket socket;
@Mock
private @NonNullByDefault({}) OutputStream outputStream;
private @Mock @NonNullByDefault({}) Socket socket;
private @Mock @NonNullByDefault({}) OutputStream outputStream;
/**
* Constructor.
@@ -75,9 +78,8 @@ public class DeviceTestBase<T extends SmartHomeDevice> {
device.initialize(connection, configuration);
}
@Before
@BeforeEach
public void setUp() throws IOException {
initMocks(this);
when(socket.getOutputStream()).thenReturn(outputStream);
deviceState = new DeviceState(ModelTestUtil.readJson(deviceStateFilename));
}
@@ -121,7 +123,7 @@ public class DeviceTestBase<T extends SmartHomeDevice> {
byte[] input = (byte[]) arg.getArguments()[0];
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(input)) {
String expectedString = expectedProcessor.apply(CryptUtil.decryptWithLength(inputStream));
assertEquals(filenames[index.get()], json, expectedString);
assertEquals(json, expectedString, filenames[index.get()]);
}
index.incrementAndGet();
return null;

View File

@@ -12,13 +12,13 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
@@ -42,43 +42,44 @@ public class DimmerDeviceTest extends DeviceTestBase<DimmerDevice> {
public void testHandleCommandBrightnessOnOff() throws IOException {
assertInput("dimmer_set_switch_state_on");
setSocketReturnAssert("dimmer_set_switch_state_on");
assertTrue("Brightness channel as OnOffType type should be handled",
device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON),
"Brightness channel as OnOffType type should be handled");
}
@Test
public void testHandleCommandBrightnessZero() throws IOException {
assertInput("dimmer_set_switch_state_off");
setSocketReturnAssert("dimmer_set_switch_state_response");
assertTrue("Brightness channel with percentage 0 should be handled",
device.handleCommand(CHANNEL_UID_BRIGHTNESS, PercentType.ZERO));
assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, PercentType.ZERO),
"Brightness channel with percentage 0 should be handled");
}
@Test
public void testHandleCommandBrightness() throws IOException {
assertInput("dimmer_set_brightness", "dimmer_set_switch_state_on");
setSocketReturnAssert("dimmer_set_brightness_response", "dimmer_set_switch_state_on");
assertTrue("Brightness channel should be handled",
device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(17)));
assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(17)),
"Brightness channel should be handled");
}
@Test
public void testUpdateChannelSwitch() throws IOException {
deviceState = new DeviceState(ModelTestUtil.readJson("hs220_get_sysinfo_response_off"));
assertSame("Dimmer device should be off", OnOffType.OFF,
((PercentType) device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState)).as(OnOffType.class));
assertSame(OnOffType.OFF,
((PercentType) device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState)).as(OnOffType.class),
"Dimmer device should be off");
}
@Test
public void testUpdateChannelBrightness() {
assertEquals("Dimmer brightness should be set", BRIGHTNESS_VALUE,
device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState));
assertEquals(BRIGHTNESS_VALUE, device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
"Dimmer brightness should be set");
}
@Test
public void testUpdateChannelOther() {
assertSame("Unknown channel should return UNDEF", UnDefType.UNDEF,
device.updateChannel(CHANNEL_UID_OTHER, deviceState));
assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
"Unknown channel should return UNDEF");
}
}

View File

@@ -12,13 +12,13 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Test class to test if text read from the device is correctly decoded to handle special characters.

View File

@@ -12,7 +12,7 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
import java.io.IOException;
@@ -20,10 +20,8 @@ import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.types.State;
@@ -34,7 +32,6 @@ import org.openhab.core.types.UnDefType;
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@RunWith(value = Parameterized.class)
@NonNullByDefault
public class EnergySwitchDeviceTest {
@@ -42,45 +39,49 @@ public class EnergySwitchDeviceTest {
.asList(new Object[][] { { "plug_get_realtime_response", }, { "plug_get_realtime_response_v2", } });
private final EnergySwitchDevice device = new EnergySwitchDevice();
private final DeviceState deviceState;
public EnergySwitchDeviceTest(String name) throws IOException {
deviceState = new DeviceState(ModelTestUtil.readJson(name));
}
@Parameters(name = "{0}")
public static List<Object[]> data() {
return TESTS;
}
@Test
public void testUpdateChannelEnergyCurrent() {
assertEquals("Energy current should have valid state value", new QuantityType<>(1 + " A"),
device.updateChannel(CHANNEL_UID_ENERGY_CURRENT, deviceState));
@ParameterizedTest
@MethodSource("data")
public void testUpdateChannelEnergyCurrent(String name) throws IOException {
DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
assertEquals(new QuantityType<>(1 + " A"), device.updateChannel(CHANNEL_UID_ENERGY_CURRENT, deviceState),
"Energy current should have valid state value");
}
@Test
public void testUpdateChannelEnergyTotal() {
assertEquals("Energy total should have valid state value", new QuantityType<>(10 + " kWh"),
device.updateChannel(CHANNEL_UID_ENERGY_TOTAL, deviceState));
@ParameterizedTest
@MethodSource("data")
public void testUpdateChannelEnergyTotal(String name) throws IOException {
DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
assertEquals(new QuantityType<>(10 + " kWh"), device.updateChannel(CHANNEL_UID_ENERGY_TOTAL, deviceState),
"Energy total should have valid state value");
}
@Test
public void testUpdateChannelEnergyVoltage() {
@ParameterizedTest
@MethodSource("data")
public void testUpdateChannelEnergyVoltage(String name) throws IOException {
DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
State state = device.updateChannel(CHANNEL_UID_ENERGY_VOLTAGE, deviceState);
assertEquals("Energy voltage should have valid state value", 230, ((QuantityType<?>) state).intValue());
assertEquals("Channel patten to format voltage correctly", "230 V", state.format("%.0f %unit%"));
assertEquals(230, ((QuantityType<?>) state).intValue(), "Energy voltage should have valid state value");
assertEquals("230 V", state.format("%.0f %unit%"), "Channel patten to format voltage correctly");
}
@Test
public void testUpdateChanneEnergyPower() {
assertEquals("Energy power should have valid state value", new QuantityType<>(20 + " W"),
device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState));
@ParameterizedTest
@MethodSource("data")
public void testUpdateChanneEnergyPower(String name) throws IOException {
DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
assertEquals(new QuantityType<>(20 + " W"), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
"Energy power should have valid state value");
}
@Test
public void testUpdateChannelOther() {
assertSame("Unknown channel should return UNDEF", UnDefType.UNDEF,
device.updateChannel(CHANNEL_UID_OTHER, deviceState));
@ParameterizedTest
@MethodSource("data")
public void testUpdateChannelOther(String name) throws IOException {
DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
"Unknown channel should return UNDEF");
}
}

View File

@@ -12,7 +12,7 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.HS300;
@@ -23,8 +23,8 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants;
import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
import org.openhab.binding.tplinksmarthome.internal.model.SetRelayState;
@@ -56,7 +56,7 @@ public class PowerStripDeviceTest extends DeviceTestBase<PowerStripDevice> {
}
@Override
@Before
@BeforeEach
public void setUp() throws IOException {
super.setUp();
final AtomicInteger inputCounter = new AtomicInteger(0);
@@ -73,22 +73,23 @@ public class PowerStripDeviceTest extends DeviceTestBase<PowerStripDevice> {
.toJson(ModelTestUtil.GSON.fromJson(s, SetRelayState.class));
assertInput(normalize, normalize, "hs300_set_relay_state");
setSocketReturnAssert("hs300_set_relay_state_response");
assertTrue("Outlet channel 2 should be handled", device.handleCommand(CHANNEL_OUTLET_2, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_OUTLET_2, OnOffType.ON), "Outlet channel 2 should be handled");
}
@Test
public void testUpdateChannelOutlet1() {
assertSame("Outlet 1 should be on", OnOffType.ON, device.updateChannel(CHANNEL_OUTLET_1, deviceState));
assertSame(OnOffType.ON, device.updateChannel(CHANNEL_OUTLET_1, deviceState), "Outlet 1 should be on");
}
@Test
public void testUpdateChannelOutlet2() {
assertSame("Outlet 2 should be off", OnOffType.OFF, device.updateChannel(CHANNEL_OUTLET_2, deviceState));
assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_OUTLET_2, deviceState), "Outlet 2 should be off");
}
@Test
public void testUpdateChannelEnergyCurrent() {
assertEquals("Energy current should have valid state value", 1,
((QuantityType<?>) device.updateChannel(CHANNEL_ENERGY_CURRENT_OUTLET_2, deviceState)).intValue());
assertEquals(1,
((QuantityType<?>) device.updateChannel(CHANNEL_ENERGY_CURRENT_OUTLET_2, deviceState)).intValue(),
"Energy current should have valid state value");
}
}

View File

@@ -12,13 +12,13 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.UnDefType;
@@ -36,23 +36,23 @@ public class RangeExtenderDeviceTest extends DeviceTestBase<RangeExtenderDevice>
@Test
public void testHandleCommandSwitch() throws IOException {
assertFalse("Switch channel not yet supported so should not be handled",
device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON));
assertFalse(device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON),
"Switch channel not yet supported so should not be handled");
}
@Test
public void testUpdateChannelSwitch() {
assertSame("Switch should be on", OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState));
assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
}
@Test
public void testUpdateChannelLed() {
assertSame("Led should be on", OnOffType.ON, device.updateChannel(CHANNEL_UID_LED, deviceState));
assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_LED, deviceState), "Led should be on");
}
@Test
public void testUpdateChannelOther() {
assertSame("Unknown channel should return UNDEF", UnDefType.UNDEF,
device.updateChannel(CHANNEL_UID_OTHER, deviceState));
assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
"Unknown channel should return UNDEF");
}
}

View File

@@ -12,13 +12,13 @@
*/
package org.openhab.binding.tplinksmarthome.internal.device;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.UnDefType;
@@ -38,29 +38,29 @@ public class SwitchDeviceTest extends DeviceTestBase<SwitchDevice> {
public void testHandleCommandSwitch() throws IOException {
assertInput("plug_set_relay_state_on");
setSocketReturnAssert("plug_set_relay_state_on");
assertTrue("Switch channel should be handled", device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON), "Switch channel should be handled");
}
@Test
public void testHandleCommandLed() throws IOException {
assertInput("plug_set_led_on");
setSocketReturnAssert("plug_set_led_on");
assertTrue("Led channel should be handled", device.handleCommand(CHANNEL_UID_LED, OnOffType.ON));
assertTrue(device.handleCommand(CHANNEL_UID_LED, OnOffType.ON), "Led channel should be handled");
}
@Test
public void testUpdateChannelSwitch() {
assertSame("Switch should be on", OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState));
assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
}
@Test
public void testUpdateChannelLed() {
assertSame("Led should be on", OnOffType.ON, device.updateChannel(CHANNEL_UID_LED, deviceState));
assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_LED, deviceState), "Led should be on");
}
@Test
public void testUpdateChannelOther() {
assertSame("Unknown channel should return UNDEF", UnDefType.UNDEF,
device.updateChannel(CHANNEL_UID_OTHER, deviceState));
assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
"Unknown channel should return UNDEF");
}
}

View File

@@ -12,22 +12,25 @@
*/
package org.openhab.binding.tplinksmarthome.internal.handler;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_SWITCH;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants;
import org.openhab.binding.tplinksmarthome.internal.Commands;
import org.openhab.binding.tplinksmarthome.internal.Connection;
@@ -52,27 +55,23 @@ import org.openhab.core.types.State;
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
@NonNullByDefault
public class SmartHomeHandlerTest {
private @NonNullByDefault({}) SmartHomeHandler handler;
@Mock
private @NonNullByDefault({}) Connection connection;
@Mock
private @NonNullByDefault({}) ThingHandlerCallback callback;
@Mock
private @NonNullByDefault({}) Thing thing;
@Mock
private @NonNullByDefault({}) SmartHomeDevice smartHomeDevice;
@Mock
private @NonNullByDefault({}) TPLinkSmartHomeDiscoveryService discoveryService;
private @Mock @NonNullByDefault({}) Connection connection;
private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
private @Mock @NonNullByDefault({}) Thing thing;
private @Mock @NonNullByDefault({}) SmartHomeDevice smartHomeDevice;
private @Mock @NonNullByDefault({}) TPLinkSmartHomeDiscoveryService discoveryService;
private final Configuration configuration = new Configuration();
@Before
@BeforeEach
public void setUp() throws IOException {
initMocks(this);
configuration.put(CONFIG_IP, "localhost");
configuration.put(CONFIG_REFRESH, 1);
when(thing.getConfiguration()).thenReturn(configuration);
@@ -90,7 +89,7 @@ public class SmartHomeHandlerTest {
handler.setCallback(callback);
}
@After
@AfterEach
public void after() {
handler.dispose();
}
@@ -102,7 +101,7 @@ public class SmartHomeHandlerTest {
verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
ThingStatusInfo thingStatusInfo = statusInfoCaptor.getValue();
assertEquals("Device should be unknown", ThingStatus.UNKNOWN, thingStatusInfo.getStatus());
assertEquals(ThingStatus.UNKNOWN, thingStatusInfo.getStatus(), "Device should be unknown");
}
@Test
@@ -125,8 +124,8 @@ public class SmartHomeHandlerTest {
handler.handleCommand(channelUID, RefreshType.REFRESH);
ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
verify(callback).stateUpdated(eq(channelUID), stateCaptor.capture());
assertEquals("State of RSSI channel should be set", new QuantityType<>(expectedRssi + " dBm"),
stateCaptor.getValue());
assertEquals(new QuantityType<>(expectedRssi + " dBm"), stateCaptor.getValue(),
"State of RSSI channel should be set");
}
@Test
@@ -137,7 +136,7 @@ public class SmartHomeHandlerTest {
handler.handleCommand(channelUID, RefreshType.REFRESH);
ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
verify(callback).stateUpdated(eq(channelUID), stateCaptor.capture());
assertSame("State of channel switch should be set", OnOffType.ON, stateCaptor.getValue());
assertSame(OnOffType.ON, stateCaptor.getValue(), "State of channel switch should be set");
}
@Test