[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.quantity.Temperature;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.nest.internal.wwn.WWNUtils;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits;
@ -32,6 +33,7 @@ import org.openhab.core.library.unit.SIUnits;
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public final class WWNDataUtil {
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.util.Date;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.library.unit.SIUnits;
import org.slf4j.Logger;
@ -30,6 +31,7 @@ import org.slf4j.LoggerFactory;
* @author David Bennett - Initial contribution
* @author Wouter Born - Increase test coverage
*/
@NonNullByDefault
public class WWNGsonParsingTest {
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 org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -41,16 +42,16 @@ import org.osgi.service.jaxrs.client.SseEventSourceFactory;
* @author David Bennett - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@NonNullByDefault
public class WWNAccountHandlerTest {
private ThingHandler handler;
private @NonNullByDefault({}) ThingHandler handler;
private @Mock Bridge bridge;
private @Mock ThingHandlerCallback callback;
private @Mock ClientBuilder clientBuilder;
private @Mock Configuration configuration;
private @Mock SseEventSourceFactory eventSourceFactory;
private @Mock WWNRedirectUrlSupplier redirectUrlSupplier;
private @Mock @NonNullByDefault({}) Bridge bridge;
private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
private @Mock @NonNullByDefault({}) ClientBuilder clientBuilder;
private @Mock @NonNullByDefault({}) Configuration configuration;
private @Mock @NonNullByDefault({}) SseEventSourceFactory eventSourceFactory;
@BeforeEach
public void beforeEach() {
@ -58,7 +59,6 @@ public class WWNAccountHandlerTest {
handler.setCallback(callback);
}
@SuppressWarnings("null")
@Test
public void initializeShouldCallTheCallback() {
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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
import org.openhab.core.config.core.Configuration;
@ -38,6 +38,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID CAMERA_UID = new ThingUID(THING_TYPE_CAMERA, "camera1");
@ -49,8 +50,7 @@ public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
@Override
protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>();
properties.put(WWNDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, CAMERA1_DEVICE_ID);
return ThingBuilder.create(THING_TYPE_CAMERA, CAMERA_UID).withLabel("Test Camera").withBridge(bridge.getUID())
.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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
import org.openhab.core.config.core.Configuration;
@ -38,6 +38,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID SMOKE_DETECTOR_UID = new ThingUID(THING_TYPE_SMOKE_DETECTOR, "smoke1");
@ -49,8 +50,7 @@ public class WWNSmokeDetectorHandlerTest extends WWNThingHandlerOSGiTest {
@Override
protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>();
properties.put(WWNDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, SMOKE1_DEVICE_ID);
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))

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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNStructureConfiguration;
import org.openhab.core.config.core.Configuration;
@ -38,6 +38,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class WWNStructureHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID STRUCTURE_UID = new ThingUID(THING_TYPE_STRUCTURE, "structure1");
@ -49,8 +50,7 @@ public class WWNStructureHandlerTest extends WWNThingHandlerOSGiTest {
@Override
protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>();
properties.put(WWNStructureConfiguration.STRUCTURE_ID, STRUCTURE1_STRUCTURE_ID);
Map<String, Object> properties = Map.of(WWNStructureConfiguration.STRUCTURE_ID, STRUCTURE1_STRUCTURE_ID);
return ThingBuilder.create(THING_TYPE_STRUCTURE, STRUCTURE_UID).withLabel("Test Structure")
.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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nest.internal.wwn.config.WWNDeviceConfiguration;
import org.openhab.core.config.core.Configuration;
@ -42,6 +42,7 @@ import org.openhab.core.thing.binding.builder.ThingBuilder;
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class WWNThermostatHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID THERMOSTAT_UID = new ThingUID(THING_TYPE_THERMOSTAT, "thermostat1");
@ -53,8 +54,7 @@ public class WWNThermostatHandlerTest extends WWNThingHandlerOSGiTest {
@Override
protected Thing buildThing(Bridge bridge) {
Map<String, Object> properties = new HashMap<>();
properties.put(WWNDeviceConfiguration.DEVICE_ID, THERMOSTAT1_DEVICE_ID);
Map<String, Object> properties = Map.of(WWNDeviceConfiguration.DEVICE_ID, THERMOSTAT1_DEVICE_ID);
return ThingBuilder.create(THING_TYPE_THERMOSTAT, THERMOSTAT_UID).withLabel("Test Thermostat")
.withBridge(bridge.getUID()).withChannels(buildChannels(THING_TYPE_THERMOSTAT, THERMOSTAT_UID))

View File

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

View File

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

View File

@ -12,12 +12,12 @@
*/
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.ServerConnector;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Embedded jetty server used in the tests.
@ -27,10 +27,9 @@ import org.slf4j.LoggerFactory;
* @author Velin Yordanov - Initial contribution
* @author Wouter Born - Increase test coverage
*/
@NonNullByDefault
public class WWNTestServer {
private final Logger logger = LoggerFactory.getLogger(WWNTestServer.class);
private Server server;
private @Nullable Server server;
private String host;
private int port;
private int timeout;
@ -43,46 +42,32 @@ public class WWNTestServer {
this.servletHolder = servletHolder;
}
public void startServer() {
Thread thread = new Thread(new Runnable() {
@Override
@SuppressWarnings("resource")
public void run() {
server = new Server();
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(servletHolder, "/*");
server.setHandler(handler);
public void startServer() throws Exception {
Server server = new Server();
// HTTP connector
ServerConnector http = new ServerConnector(server);
http.setHost(host);
http.setPort(port);
http.setIdleTimeout(timeout);
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(servletHolder, "/*");
server.setHandler(handler);
server.addConnector(http);
// HTTP connector
ServerConnector http = new ServerConnector(server);
http.setHost(host);
http.setPort(port);
http.setIdleTimeout(timeout);
server.addConnector(http);
try {
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;
}
}
});
server.start();
thread.start();
this.server = server;
}
public void stopServer() {
try {
server.stop();
} catch (Exception e) {
logger.error("Error in stopping the server", e);
public void stopServer() throws Exception {
Server server = this.server;
if (server == null) {
return;
}
server.stop();
this.server = null;
}
}