[nest] Cleanup test code (#14642)

* Fix SAT issues
* Start Jetty Server in same thread to improve stability
* Cleanup some code

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2023-03-20 22:14:38 +01:00 committed by GitHub
parent 99087f08c3
commit 6f7366de7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 120 additions and 127 deletions

View File

@ -23,6 +23,7 @@ import java.util.stream.Collectors;
import javax.measure.Unit; import javax.measure.Unit;
import javax.measure.quantity.Temperature; import javax.measure.quantity.Temperature;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.nest.internal.wwn.WWNUtils; import org.openhab.binding.nest.internal.wwn.WWNUtils;
import org.openhab.core.library.unit.ImperialUnits; import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.SIUnits;
@ -32,6 +33,7 @@ import org.openhab.core.library.unit.SIUnits;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public final class WWNDataUtil { public final class WWNDataUtil {
public static final String COMPLETE_DATA_FILE_NAME = "top-level-streaming-data.json"; public static final String COMPLETE_DATA_FILE_NAME = "top-level-streaming-data.json";

View File

@ -19,6 +19,7 @@ import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.SIUnits;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -30,6 +31,7 @@ import org.slf4j.LoggerFactory;
* @author David Bennett - Initial contribution * @author David Bennett - Initial contribution
* @author Wouter Born - Increase test coverage * @author Wouter Born - Increase test coverage
*/ */
@NonNullByDefault
public class WWNGsonParsingTest { public class WWNGsonParsingTest {
private final Logger logger = LoggerFactory.getLogger(WWNGsonParsingTest.class); private final Logger logger = LoggerFactory.getLogger(WWNGsonParsingTest.class);

View File

@ -19,6 +19,7 @@ import static org.mockito.Mockito.*;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -41,16 +42,16 @@ import org.osgi.service.jaxrs.client.SseEventSourceFactory;
* @author David Bennett - Initial contribution * @author David Bennett - Initial contribution
*/ */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
@NonNullByDefault
public class WWNAccountHandlerTest { public class WWNAccountHandlerTest {
private ThingHandler handler; private @NonNullByDefault({}) ThingHandler handler;
private @Mock Bridge bridge; private @Mock @NonNullByDefault({}) Bridge bridge;
private @Mock ThingHandlerCallback callback; private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
private @Mock ClientBuilder clientBuilder; private @Mock @NonNullByDefault({}) ClientBuilder clientBuilder;
private @Mock Configuration configuration; private @Mock @NonNullByDefault({}) Configuration configuration;
private @Mock SseEventSourceFactory eventSourceFactory; private @Mock @NonNullByDefault({}) SseEventSourceFactory eventSourceFactory;
private @Mock WWNRedirectUrlSupplier redirectUrlSupplier;
@BeforeEach @BeforeEach
public void beforeEach() { public void beforeEach() {
@ -58,7 +59,6 @@ public class WWNAccountHandlerTest {
handler.setCallback(callback); handler.setCallback(callback);
} }
@SuppressWarnings("null")
@Test @Test
public void initializeShouldCallTheCallback() { public void initializeShouldCallTheCallback() {
when(bridge.getConfiguration()).thenReturn(configuration); when(bridge.getConfiguration()).thenReturn(configuration);

View File

@ -19,9 +19,9 @@ import static org.openhab.binding.nest.internal.wwn.dto.WWNDataUtil.*;
import static org.openhab.core.library.types.OnOffType.*; import static org.openhab.core.library.types.OnOffType.*;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration; import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
@ -38,6 +38,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest { public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID CAMERA_UID = new ThingUID(THING_TYPE_CAMERA, "camera1"); private static final ThingUID CAMERA_UID = new ThingUID(THING_TYPE_CAMERA, "camera1");
@ -49,8 +50,7 @@ public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
@Override @Override
protected Thing buildThing(Bridge bridge) { protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>(); Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
properties.put(WWNDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
return ThingBuilder.create(THING_TYPE_CAMERA, CAMERA_UID).withLabel("Test Camera").withBridge(bridge.getUID()) return ThingBuilder.create(THING_TYPE_CAMERA, CAMERA_UID).withLabel("Test Camera").withBridge(bridge.getUID())
.withChannels(buildChannels(THING_TYPE_CAMERA, CAMERA_UID)) .withChannels(buildChannels(THING_TYPE_CAMERA, CAMERA_UID))

View File

@ -19,9 +19,9 @@ import static org.openhab.binding.nest.internal.wwn.dto.WWNDataUtil.*;
import static org.openhab.core.library.types.OnOffType.OFF; import static org.openhab.core.library.types.OnOffType.OFF;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration; import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
@ -38,6 +38,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest { public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID SMOKE_DETECTOR_UID = new ThingUID(THING_TYPE_SMOKE_DETECTOR, "smoke1"); private static final ThingUID SMOKE_DETECTOR_UID = new ThingUID(THING_TYPE_SMOKE_DETECTOR, "smoke1");
@ -49,8 +50,7 @@ public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest {
@Override @Override
protected Thing buildThing(Bridge bridge) { protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>(); Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
properties.put(WWNDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
return ThingBuilder.create(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID).withLabel("Test Smoke Detector") return ThingBuilder.create(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID).withLabel("Test Smoke Detector")
.withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID)) .withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_SMOKE_DETECTOR, SMOKE_DETECTOR_UID))

View File

@ -19,9 +19,9 @@ import static org.openhab.binding.nest.internal.wwn.dto.WWNDataUtil.*;
import static org.openhab.core.library.types.OnOffType.OFF; import static org.openhab.core.library.types.OnOffType.OFF;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNStructureConfiguration; import org.openhab.binding.nest.internal.wwn.config.WWNStructureConfiguration;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
@ -38,6 +38,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public class WWNStructureHandlerTest extends WWNThingHandlerOSGiTest { public class WWNStructureHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID STRUCTURE_UID = new ThingUID(THING_TYPE_STRUCTURE, "structure1"); private static final ThingUID STRUCTURE_UID = new ThingUID(THING_TYPE_STRUCTURE, "structure1");
@ -49,8 +50,7 @@ public class WWNStructureHandlerTest extends WWNThingHandlerOSGiTest {
@Override @Override
protected Thing buildThing(Bridge bridge) { protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>(); Map<String, Object> properties = Map.of(WWNStructureConfiguration.STRUCTURE_ID, STRUCTURE1_STRUCTURE_ID);
properties.put(WWNStructureConfiguration.STRUCTURE_ID, STRUCTURE1_STRUCTURE_ID);
return ThingBuilder.create(THING_TYPE_STRUCTURE, STRUCTURE_UID).withLabel("Test Structure") return ThingBuilder.create(THING_TYPE_STRUCTURE, STRUCTURE_UID).withLabel("Test Structure")
.withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_STRUCTURE, STRUCTURE_UID)) .withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_STRUCTURE, STRUCTURE_UID))

View File

@ -21,9 +21,9 @@ import static org.openhab.core.library.unit.ImperialUnits.FAHRENHEIT;
import static org.openhab.core.library.unit.SIUnits.CELSIUS; import static org.openhab.core.library.unit.SIUnits.CELSIUS;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration; import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
@ -42,6 +42,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public class WWNThermostatHandlerTest extends WWNThingHandlerOSGiTest { public class WWNThermostatHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID THERMOSTAT_UID = new ThingUID(THING_TYPE_THERMOSTAT, "thermostat1"); private static final ThingUID THERMOSTAT_UID = new ThingUID(THING_TYPE_THERMOSTAT, "thermostat1");
@ -53,8 +54,7 @@ public class WWNThermostatHandlerTest extends WWNThingHandlerOSGiTest {
@Override @Override
protected Thing buildThing(Bridge bridge) { protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>(); Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, THERMOSTAT1_DEVICE_ID);
properties.put(WWNDeviceConfiguration.DEVICE_ID, THERMOSTAT1_DEVICE_ID);
return ThingBuilder.create(THING_TYPE_THERMOSTAT, THERMOSTAT_UID).withLabel("Test Thermostat") return ThingBuilder.create(THING_TYPE_THERMOSTAT, THERMOSTAT_UID).withLabel("Test Thermostat")
.withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_THERMOSTAT, THERMOSTAT_UID)) .withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_THERMOSTAT, THERMOSTAT_UID))

View File

@ -12,10 +12,12 @@
*/ */
package org.openhab.binding.nest.internal.wwn.handler; package org.openhab.binding.nest.internal.wwn.handler;
import static java.util.Map.entry;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNot.not;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.openhab.binding.nest.internal.wwn.rest.WWNStreamingRestClient.PUT; import static org.openhab.binding.nest.internal.wwn.rest.WWNStreamingRestClient.PUT;
@ -23,17 +25,19 @@ import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.function.Function; import java.util.function.Function;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -90,6 +94,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest { public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
private static final String SERVER_HOST = "127.0.0.1"; private static final String SERVER_HOST = "127.0.0.1";
@ -99,26 +104,26 @@ public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
private final Logger logger = LoggerFactory.getLogger(WWNThingHandlerOSGiTest.class); private final Logger logger = LoggerFactory.getLogger(WWNThingHandlerOSGiTest.class);
private static WWNTestServer server; private static @Nullable WWNTestServer server;
private static WWNTestApiServlet servlet = new WWNTestApiServlet(); private static WWNTestApiServlet servlet = new WWNTestApiServlet();
private ChannelTypeRegistry channelTypeRegistry; private @NonNullByDefault({}) ChannelTypeRegistry channelTypeRegistry;
private ChannelGroupTypeRegistry channelGroupTypeRegistry; private @NonNullByDefault({}) ChannelGroupTypeRegistry channelGroupTypeRegistry;
private ItemFactory itemFactory; private @NonNullByDefault({}) ItemFactory itemFactory;
private ItemRegistry itemRegistry; private @NonNullByDefault({}) ItemRegistry itemRegistry;
private EventPublisher eventPublisher; private @NonNullByDefault({}) EventPublisher eventPublisher;
private ManagedThingProvider managedThingProvider; private @NonNullByDefault({}) ManagedThingProvider managedThingProvider;
private ThingTypeRegistry thingTypeRegistry; private @NonNullByDefault({}) ThingTypeRegistry thingTypeRegistry;
private ManagedItemChannelLinkProvider managedItemChannelLinkProvider; private @NonNullByDefault({}) ManagedItemChannelLinkProvider managedItemChannelLinkProvider;
private VolatileStorageService volatileStorageService = new VolatileStorageService(); private @NonNullByDefault({}) VolatileStorageService volatileStorageService = new VolatileStorageService();
protected Bridge bridge; protected @NonNullByDefault({}) Bridge bridge;
protected WWNTestAccountHandler bridgeHandler; protected @NonNullByDefault({}) WWNTestAccountHandler bridgeHandler;
protected Thing thing; protected @NonNullByDefault({}) Thing thing;
protected WWNBaseHandler<?> thingHandler; protected @NonNullByDefault({}) WWNBaseHandler<?> thingHandler;
private Class<? extends WWNBaseHandler<?>> thingClass; private Class<? extends WWNBaseHandler<?>> thingClass;
private WWNTestHandlerFactory nestTestHandlerFactory; private @NonNullByDefault({}) WWNTestHandlerFactory nestTestHandlerFactory;
private @NonNullByDefault({}) ClientBuilder clientBuilder; private @NonNullByDefault({}) ClientBuilder clientBuilder;
private @NonNullByDefault({}) SseEventSourceFactory eventSourceFactory; private @NonNullByDefault({}) SseEventSourceFactory eventSourceFactory;
@ -133,39 +138,34 @@ public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
server.startServer(); server.startServer();
} }
@AfterAll
public static void tearDownClass() throws Exception {
WWNTestServer testServer = server;
if (testServer != null) {
testServer.stopServer();
}
}
@BeforeEach @BeforeEach
public void setUp() throws ItemNotFoundException { public void setUp() throws ItemNotFoundException {
registerService(volatileStorageService); registerService(volatileStorageService);
managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class); managedThingProvider = Objects.requireNonNull(getService(ThingProvider.class, ManagedThingProvider.class),
assertThat("Could not get ManagedThingProvider", managedThingProvider, is(notNullValue())); "Could not get ManagedThingProvider");
thingTypeRegistry = Objects.requireNonNull(getService(ThingTypeRegistry.class),
thingTypeRegistry = getService(ThingTypeRegistry.class); "Could not get ThingTypeRegistry");
assertThat("Could not get ThingTypeRegistry", thingTypeRegistry, is(notNullValue())); channelTypeRegistry = Objects.requireNonNull(getService(ChannelTypeRegistry.class),
"Could not get ChannelTypeRegistry");
channelTypeRegistry = getService(ChannelTypeRegistry.class); channelGroupTypeRegistry = Objects.requireNonNull(getService(ChannelGroupTypeRegistry.class),
assertThat("Could not get ChannelTypeRegistry", channelTypeRegistry, is(notNullValue())); "Could not get ChannelGroupTypeRegistry");
eventPublisher = Objects.requireNonNull(getService(EventPublisher.class), "Could not get EventPublisher");
channelGroupTypeRegistry = getService(ChannelGroupTypeRegistry.class); itemFactory = Objects.requireNonNull(getService(ItemFactory.class), "Could not get ItemFactory");
assertThat("Could not get ChannelGroupTypeRegistry", channelGroupTypeRegistry, is(notNullValue())); itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class), "Could not get ItemRegistry");
managedItemChannelLinkProvider = Objects.requireNonNull(getService(ManagedItemChannelLinkProvider.class),
eventPublisher = getService(EventPublisher.class); "Could not get ManagedItemChannelLinkProvider");
assertThat("Could not get EventPublisher", eventPublisher, is(notNullValue())); clientBuilder = Objects.requireNonNull(getService(ClientBuilder.class), "Could not get ClientBuilder");
eventSourceFactory = Objects.requireNonNull(getService(SseEventSourceFactory.class),
itemFactory = getService(ItemFactory.class); "Could not get SseEventSourceFactory");
assertThat("Could not get ItemFactory", itemFactory, is(notNullValue()));
itemRegistry = getService(ItemRegistry.class);
assertThat("Could not get ItemRegistry", itemRegistry, is(notNullValue()));
managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
assertThat("Could not get ManagedItemChannelLinkProvider", managedItemChannelLinkProvider, is(notNullValue()));
clientBuilder = getService(ClientBuilder.class);
assertThat("Could not get ClientBuilder", clientBuilder, is(notNullValue()));
eventSourceFactory = getService(SseEventSourceFactory.class);
assertThat("Could not get SseEventSourceFactory", eventSourceFactory, is(notNullValue()));
ComponentContext componentContext = mock(ComponentContext.class); ComponentContext componentContext = mock(ComponentContext.class);
when(componentContext.getBundleContext()).thenReturn(bundleContext); when(componentContext.getBundleContext()).thenReturn(bundleContext);
@ -180,8 +180,9 @@ public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
.thenReturn(mock(ThingType.class)); .thenReturn(mock(ThingType.class));
registerService(thingTypeProvider); registerService(thingTypeProvider);
nestTestHandlerFactory = getService(ThingHandlerFactory.class, WWNTestHandlerFactory.class); nestTestHandlerFactory = Objects.requireNonNull(
assertThat("Could not get NestTestHandlerFactory", nestTestHandlerFactory, is(notNullValue())); getService(ThingHandlerFactory.class, WWNTestHandlerFactory.class),
"Could not get NestTestHandlerFactory");
bridge = buildBridge(); bridge = buildBridge();
thing = buildThing(bridge); thing = buildThing(bridge);
@ -209,12 +210,12 @@ public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
} }
protected Bridge buildBridge() { protected Bridge buildBridge() {
Map<String, Object> properties = new HashMap<>(); Map<String, Object> properties = Map.ofEntries( //
properties.put(WWNAccountConfiguration.ACCESS_TOKEN, entry(WWNAccountConfiguration.ACCESS_TOKEN,
"c.eQ5QBBPiFOTNzPHbmZPcE9yPZ7GayzLusifgQR2DQRFNyUS9ESvlhJF0D7vG8Y0TFV39zX1vIOsWrv8RKCMrFepNUb9FqHEboa4MtWLUsGb4tD9oBh0jrV4HooJUmz5sVA5KZR0dkxyLYyPc"); "c.eQ5QBBPiFOTNzPHbmZPcE9yPZ7GayzLusifgQR2DQRFNyUS9ESvlhJF0D7vG8Y0TFV39zX1vIOsWrv8RKCMrFepNUb9FqHEboa4MtWLUsGb4tD9oBh0jrV4HooJUmz5sVA5KZR0dkxyLYyPc"),
properties.put(WWNAccountConfiguration.PINCODE, "64P2XRYT"); entry(WWNAccountConfiguration.PINCODE, "64P2XRYT"),
properties.put(WWNAccountConfiguration.PRODUCT_ID, "8fdf9885-ca07-4252-1aa3-f3d5ca9589e0"); entry(WWNAccountConfiguration.PRODUCT_ID, "8fdf9885-ca07-4252-1aa3-f3d5ca9589e0"),
properties.put(WWNAccountConfiguration.PRODUCT_SECRET, "QITLR3iyUlWaj9dbvCxsCKp4f"); entry(WWNAccountConfiguration.PRODUCT_SECRET, "QITLR3iyUlWaj9dbvCxsCKp4f"));
return BridgeBuilder.create(WWNTestAccountHandler.THING_TYPE_TEST_BRIDGE, "test_account") return BridgeBuilder.create(WWNTestAccountHandler.THING_TYPE_TEST_BRIDGE, "test_account")
.withLabel("Test Account").withConfiguration(new Configuration(properties)).build(); .withLabel("Test Account").withConfiguration(new Configuration(properties)).build();
@ -227,16 +228,14 @@ public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID); ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID);
List<Channel> channels = new ArrayList<>(); List<Channel> channels = new ArrayList<>(buildChannels(thingUID, thingType.getChannelDefinitions(), id -> id));
channels.addAll(buildChannels(thingUID, thingType.getChannelDefinitions(), (id) -> id));
for (ChannelGroupDefinition channelGroupDefinition : thingType.getChannelGroupDefinitions()) { for (ChannelGroupDefinition channelGroupDefinition : thingType.getChannelGroupDefinitions()) {
ChannelGroupType channelGroupType = channelGroupTypeRegistry ChannelGroupType channelGroupType = channelGroupTypeRegistry
.getChannelGroupType(channelGroupDefinition.getTypeUID()); .getChannelGroupType(channelGroupDefinition.getTypeUID());
String groupId = channelGroupDefinition.getId(); String groupId = channelGroupDefinition.getId();
if (channelGroupType != null) { if (channelGroupType != null) {
channels.addAll( channels.addAll(
buildChannels(thingUID, channelGroupType.getChannelDefinitions(), (id) -> groupId + "#" + id)); buildChannels(thingUID, channelGroupType.getChannelDefinitions(), id -> groupId + "#" + id));
} }
} }

View File

@ -19,6 +19,7 @@ import java.util.Set;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.nest.internal.wwn.exceptions.InvalidWWNAccessTokenException; import org.openhab.binding.nest.internal.wwn.exceptions.InvalidWWNAccessTokenException;
import org.openhab.binding.nest.internal.wwn.handler.WWNAccountHandler; import org.openhab.binding.nest.internal.wwn.handler.WWNAccountHandler;
import org.openhab.binding.nest.internal.wwn.handler.WWNRedirectUrlSupplier; import org.openhab.binding.nest.internal.wwn.handler.WWNRedirectUrlSupplier;
@ -32,6 +33,7 @@ import org.osgi.service.jaxrs.client.SseEventSourceFactory;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public class WWNTestAccountHandler extends WWNAccountHandler { public class WWNTestAccountHandler extends WWNAccountHandler {
class NestTestRedirectUrlSupplier extends WWNRedirectUrlSupplier { class NestTestRedirectUrlSupplier extends WWNRedirectUrlSupplier {

View File

@ -31,6 +31,8 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -43,6 +45,7 @@ import com.google.gson.reflect.TypeToken;
* *
* @author Wouter Born - Initial contribution * @author Wouter Born - Initial contribution
*/ */
@NonNullByDefault
public class WWNTestApiServlet extends HttpServlet { public class WWNTestApiServlet extends HttpServlet {
private static final long serialVersionUID = -5414910055159062745L; private static final long serialVersionUID = -5414910055159062745L;
@ -54,9 +57,9 @@ public class WWNTestApiServlet extends HttpServlet {
private final Logger logger = LoggerFactory.getLogger(WWNTestApiServlet.class); private final Logger logger = LoggerFactory.getLogger(WWNTestApiServlet.class);
private class SseEvent { private static class SseEvent {
private String name; private String name;
private String data; private @Nullable String data;
public SseEvent(String name) { public SseEvent(String name) {
this.name = name; this.name = name;
@ -67,17 +70,13 @@ public class WWNTestApiServlet extends HttpServlet {
this.data = data; this.data = data;
} }
public String getData() { public @Nullable String getData() {
return data; return data;
} }
public String getName() { public String getName() {
return name; return name;
} }
public boolean hasData() {
return data != null && !data.isEmpty();
}
} }
private final Map<String, Map<String, String>> nestIdPropertiesMap = new ConcurrentHashMap<>(); private final Map<String, Map<String, String>> nestIdPropertiesMap = new ConcurrentHashMap<>();
@ -91,7 +90,7 @@ public class WWNTestApiServlet extends HttpServlet {
public void closeConnections() { public void closeConnections() {
Set<Thread> threads = listenerQueues.keySet(); Set<Thread> threads = listenerQueues.keySet();
listenerQueues.clear(); listenerQueues.clear();
threads.forEach(thread -> thread.interrupt()); threads.forEach(Thread::interrupt);
} }
public void reset() { public void reset() {
@ -118,8 +117,9 @@ public class WWNTestApiServlet extends HttpServlet {
writer.write(event.getName()); writer.write(event.getName());
writer.write(NEW_LINE); writer.write(NEW_LINE);
if (event.hasData()) { String eventData = event.getData();
for (String dataLine : event.getData().split(NEW_LINE)) { if (eventData != null) {
for (String dataLine : eventData.split(NEW_LINE)) {
writer.write("data: "); writer.write("data: ");
writer.write(dataLine); writer.write(dataLine);
writer.write(NEW_LINE); writer.write(NEW_LINE);
@ -195,7 +195,10 @@ public class WWNTestApiServlet extends HttpServlet {
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
} }
private String getNestIdFromURI(String uri) { private @Nullable String getNestIdFromURI(@Nullable String uri) {
if (uri == null) {
return null;
}
for (String updatePath : UPDATE_PATHS) { for (String updatePath : UPDATE_PATHS) {
if (uri.startsWith(updatePath)) { if (uri.startsWith(updatePath)) {
return uri.replaceAll(updatePath, ""); return uri.replaceAll(updatePath, "");
@ -213,7 +216,7 @@ public class WWNTestApiServlet extends HttpServlet {
return properties; return properties;
} }
public String getNestIdPropertyState(String nestId, String propertyName) { public @Nullable String getNestIdPropertyState(String nestId, String propertyName) {
Map<String, String> properties = nestIdPropertiesMap.get(nestId); Map<String, String> properties = nestIdPropertiesMap.get(nestId);
return properties == null ? null : properties.get(propertyName); return properties == null ? null : properties.get(propertyName);
} }

View File

@ -12,12 +12,12 @@
*/ */
package org.openhab.binding.nest.internal.wwn.test; package org.openhab.binding.nest.internal.wwn.test;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Embedded jetty server used in the tests. * Embedded jetty server used in the tests.
@ -27,10 +27,9 @@ import org.slf4j.LoggerFactory;
* @author Velin Yordanov - Initial contribution * @author Velin Yordanov - Initial contribution
* @author Wouter Born - Increase test coverage * @author Wouter Born - Increase test coverage
*/ */
@NonNullByDefault
public class WWNTestServer { public class WWNTestServer {
private final Logger logger = LoggerFactory.getLogger(WWNTestServer.class); private @Nullable Server server;
private Server server;
private String host; private String host;
private int port; private int port;
private int timeout; private int timeout;
@ -43,12 +42,9 @@ public class WWNTestServer {
this.servletHolder = servletHolder; this.servletHolder = servletHolder;
} }
public void startServer() { public void startServer() throws Exception {
Thread thread = new Thread(new Runnable() { Server server = new Server();
@Override
@SuppressWarnings("resource")
public void run() {
server = new Server();
ServletHandler handler = new ServletHandler(); ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(servletHolder, "/*"); handler.addServletWithMapping(servletHolder, "/*");
server.setHandler(handler); server.setHandler(handler);
@ -58,31 +54,20 @@ public class WWNTestServer {
http.setHost(host); http.setHost(host);
http.setPort(port); http.setPort(port);
http.setIdleTimeout(timeout); http.setIdleTimeout(timeout);
server.addConnector(http); server.addConnector(http);
try {
server.start(); server.start();
server.join();
} catch (InterruptedException ex) {
logger.error("Server got interrupted", ex);
return;
} catch (Exception e) {
logger.error("Error in starting the server", e);
return;
}
}
});
thread.start(); this.server = server;
}
public void stopServer() throws Exception {
Server server = this.server;
if (server == null) {
return;
} }
public void stopServer() {
try {
server.stop(); server.stop();
} catch (Exception e) { this.server = null;
logger.error("Error in stopping the server", e);
return;
}
} }
} }