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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public final class WWNDataUtil {
public static final String COMPLETE_DATA_FILE_NAME = "top-level-streaming-data.json";
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;
* @author David Bennett - Initial contribution
* @author Wouter Born - Increase test coverage
*/
+@NonNullByDefault
public class WWNGsonParsingTest {
private final Logger logger = LoggerFactory.getLogger(WWNGsonParsingTest.class);
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;
* @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() {
handler.setCallback(callback);
}
- @SuppressWarnings("null")
@Test
public void initializeShouldCallTheCallback() {
when(bridge.getConfiguration()).thenReturn(configuration);
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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public class WWNCameraHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID CAMERA_UID = new ThingUID(THING_TYPE_CAMERA, "camera1");
@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))
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;
*
* @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");
@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))
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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public class WWNStructureHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID STRUCTURE_UID = new ThingUID(THING_TYPE_STRUCTURE, "structure1");
@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))
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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public class WWNThermostatHandlerTest extends WWNThingHandlerOSGiTest {
private static final ThingUID THERMOSTAT_UID = new ThingUID(THING_TYPE_THERMOSTAT, "thermostat1");
@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))
*/
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;
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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public abstract class WWNThingHandlerOSGiTest extends JavaOSGiTest {
private static final String SERVER_HOST = "127.0.0.1";
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();
-
- protected Bridge bridge;
- protected WWNTestAccountHandler bridgeHandler;
- protected Thing thing;
- protected WWNBaseHandler<?> thingHandler;
+ 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 @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;
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);
.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);
}
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();
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));
}
}
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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public class WWNTestAccountHandler extends WWNAccountHandler {
class NestTestRedirectUrlSupplier extends WWNRedirectUrlSupplier {
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;
*
* @author Wouter Born - Initial contribution
*/
+@NonNullByDefault
public class WWNTestApiServlet extends HttpServlet {
private static final long serialVersionUID = -5414910055159062745L;
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;
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<>();
public void closeConnections() {
Set<Thread> threads = listenerQueues.keySet();
listenerQueues.clear();
- threads.forEach(thread -> thread.interrupt());
+ threads.forEach(Thread::interrupt);
}
public void reset() {
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);
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, "");
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);
}
*/
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.
* @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;
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;
}
}