From 3465e8f30d73409604ec1bc636743b2804574c6d Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Thu, 23 Jun 2022 09:11:20 +0200 Subject: [PATCH] [gardena] Fix null annotation issue (and compiler warning) (#12957) * Fix compiler warning * Add basic test coverage for DataItem deserialization * Add full prefixes to attributes variables * Add missing newlines at end of test payload files * Add full prefix to attributes variable Signed-off-by: Jacob Laursen --- .../gardena/internal/GardenaSmartImpl.java | 16 +++- .../internal/handler/GardenaThingHandler.java | 5 +- .../gardena/internal/model/dto/Device.java | 13 ++- .../model/dto/api/CommonServiceDataItem.java | 4 +- .../dto/api/CreateWebSocketDataItem.java | 4 +- .../model/dto/api/CreateWebSocketRequest.java | 3 + .../internal/model/dto/api/DataItem.java | 6 +- .../model/dto/api/DeviceDataItem.java | 4 +- .../model/dto/api/LocationDataItem.java | 4 +- .../model/dto/api/MowerServiceDataItem.java | 4 +- .../dto/api/PowerSocketServiceDataItem.java | 4 +- .../model/dto/api/SensorServiceDataItem.java | 4 +- .../model/dto/api/ValveServiceDataItem.java | 4 +- .../dto/api/ValveSetServiceDataItem.java | 4 +- .../model/dto/api/WebSocketDataItem.java | 4 +- .../openhab/binding/gardena/DataItemTest.java | 85 +++++++++++++++++++ .../binding/gardena/DeviceDataItem.json | 24 ++++++ .../gardena/SensorServiceDataItem.json | 22 +++++ .../SensorServiceDataItemNoAttributes.json | 12 +++ 19 files changed, 206 insertions(+), 20 deletions(-) create mode 100644 bundles/org.openhab.binding.gardena/src/test/java/org/openhab/binding/gardena/DataItemTest.java create mode 100644 bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/DeviceDataItem.json create mode 100644 bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItem.json create mode 100644 bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItemNoAttributes.json diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/GardenaSmartImpl.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/GardenaSmartImpl.java index fab20abefb9..f06d70fb4f7 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/GardenaSmartImpl.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/GardenaSmartImpl.java @@ -43,10 +43,12 @@ import org.openhab.binding.gardena.internal.model.DataItemDeserializer; import org.openhab.binding.gardena.internal.model.dto.Device; import org.openhab.binding.gardena.internal.model.dto.api.CreateWebSocketRequest; import org.openhab.binding.gardena.internal.model.dto.api.DataItem; +import org.openhab.binding.gardena.internal.model.dto.api.Location; import org.openhab.binding.gardena.internal.model.dto.api.LocationDataItem; import org.openhab.binding.gardena.internal.model.dto.api.LocationResponse; import org.openhab.binding.gardena.internal.model.dto.api.LocationsResponse; import org.openhab.binding.gardena.internal.model.dto.api.PostOAuth2Response; +import org.openhab.binding.gardena.internal.model.dto.api.WebSocket; import org.openhab.binding.gardena.internal.model.dto.api.WebSocketCreatedResponse; import org.openhab.binding.gardena.internal.model.dto.command.GardenaCommand; import org.openhab.binding.gardena.internal.model.dto.command.GardenaCommandRequest; @@ -153,9 +155,14 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList private void startWebsockets() throws Exception { for (LocationDataItem location : locationsResponse.data) { WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(location.id); - String socketId = id + "-" + location.attributes.name; + Location locationAttributes = location.attributes; + WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes; + if (locationAttributes == null || webSocketAttributes == null) { + continue; + } + String socketId = id + "-" + locationAttributes.name; webSockets.put(location.id, new GardenaSmartWebSocket(this, webSocketClient, scheduler, - webSocketCreatedResponse.data.attributes.url, token, socketId, location.id)); + webSocketAttributes.url, token, socketId, location.id)); } } @@ -391,7 +398,10 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList Thread.sleep(3000); WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(socket.getLocationID()); // only restart single socket, do not restart binding - socket.restart(webSocketCreatedResponse.data.attributes.url); + WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes; + if (webSocketAttributes != null) { + socket.restart(webSocketAttributes.url); + } } catch (Exception ex) { // restart binding on error logger.warn("Restarting GardenaSmart Webservice failed ({}): {}, restarting binding", socket.getSocketID(), diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/handler/GardenaThingHandler.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/handler/GardenaThingHandler.java index 67df16068b8..b9661bcd522 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/handler/GardenaThingHandler.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/handler/GardenaThingHandler.java @@ -27,6 +27,7 @@ import org.openhab.binding.gardena.internal.GardenaSmartEventListener; import org.openhab.binding.gardena.internal.exception.GardenaDeviceNotFoundException; import org.openhab.binding.gardena.internal.exception.GardenaException; import org.openhab.binding.gardena.internal.model.dto.Device; +import org.openhab.binding.gardena.internal.model.dto.api.CommonService; import org.openhab.binding.gardena.internal.model.dto.api.DataItem; import org.openhab.binding.gardena.internal.model.dto.command.GardenaCommand; import org.openhab.binding.gardena.internal.model.dto.command.MowerCommand; @@ -284,7 +285,9 @@ public class GardenaThingHandler extends BaseThingHandler { ThingStatus newStatus = ThingStatus.ONLINE; ThingStatusDetail newDetail = ThingStatusDetail.NONE; - if (!CONNECTION_STATUS_ONLINE.equals(device.common.attributes.rfLinkState.value)) { + CommonService commonServiceAttributes = device.common.attributes; + if (commonServiceAttributes == null + || !CONNECTION_STATUS_ONLINE.equals(commonServiceAttributes.rfLinkState.value)) { newStatus = ThingStatus.OFFLINE; newDetail = ThingStatusDetail.COMMUNICATION_ERROR; } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/Device.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/Device.java index 6c6d091deef..b9801ed6daf 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/Device.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/Device.java @@ -19,9 +19,11 @@ import java.util.HashMap; import java.util.Map; import org.openhab.binding.gardena.internal.exception.GardenaException; +import org.openhab.binding.gardena.internal.model.dto.api.CommonService; import org.openhab.binding.gardena.internal.model.dto.api.CommonServiceDataItem; import org.openhab.binding.gardena.internal.model.dto.api.DataItem; import org.openhab.binding.gardena.internal.model.dto.api.DeviceDataItem; +import org.openhab.binding.gardena.internal.model.dto.api.Location; import org.openhab.binding.gardena.internal.model.dto.api.LocationDataItem; import org.openhab.binding.gardena.internal.model.dto.api.MowerServiceDataItem; import org.openhab.binding.gardena.internal.model.dto.api.PowerSocketServiceDataItem; @@ -82,8 +84,10 @@ public class Device { */ public void evaluateDeviceType() { if (deviceType == null) { - if (common.attributes.modelType.value.toLowerCase().startsWith(DEVICE_TYPE_PREFIX)) { - String modelType = common.attributes.modelType.value.toLowerCase(); + CommonService commonServiceAttributes = common.attributes; + if (commonServiceAttributes != null + && commonServiceAttributes.modelType.value.toLowerCase().startsWith(DEVICE_TYPE_PREFIX)) { + String modelType = commonServiceAttributes.modelType.value.toLowerCase(); modelType = modelType.substring(14); deviceType = modelType.replace(" ", "_"); } else { @@ -111,8 +115,9 @@ public class Device { // ignore } else if (dataItem instanceof LocationDataItem) { LocationDataItem locationDataItem = (LocationDataItem) dataItem; - if (locationDataItem.attributes != null) { - location = locationDataItem.attributes.name; + Location locationAttributes = locationDataItem.attributes; + if (locationAttributes != null) { + location = locationAttributes.name; } } else if (dataItem instanceof CommonServiceDataItem) { common = (CommonServiceDataItem) dataItem; diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CommonServiceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CommonServiceDataItem.java index 850a67d75b7..04ffcbf2012 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CommonServiceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CommonServiceDataItem.java @@ -12,11 +12,13 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class CommonServiceDataItem extends DataItem { +public class CommonServiceDataItem extends DataItem<@NonNull CommonService> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketDataItem.java index 47c61abb788..09cf91cd83f 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketDataItem.java @@ -12,11 +12,13 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class CreateWebSocketDataItem extends DataItem { +public class CreateWebSocketDataItem extends DataItem<@NonNull CreateWebSocket> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketRequest.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketRequest.java index 873dfbf617d..5c0e796252a 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketRequest.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/CreateWebSocketRequest.java @@ -12,12 +12,15 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ +@NonNullByDefault public class CreateWebSocketRequest { public CreateWebSocketDataItem data; diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DataItem.java index 5769e782aed..8c6f7b974d5 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DataItem.java @@ -12,6 +12,8 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.gardena.internal.util.StringUtils; /** @@ -20,7 +22,7 @@ import org.openhab.binding.gardena.internal.util.StringUtils; * @author Gerhard Riegler - Initial contribution */ -public class DataItem { +public class DataItem<@NonNull T> { public String id; public String type; @@ -28,5 +30,5 @@ public class DataItem { return StringUtils.substringBeforeLast(id, ":"); } - public T attributes; + public @Nullable T attributes; } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DeviceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DeviceDataItem.java index 1b15da915f5..f81e7032c7b 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DeviceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/DeviceDataItem.java @@ -12,11 +12,13 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class DeviceDataItem extends DataItem { +public class DeviceDataItem extends DataItem<@NonNull Void> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/LocationDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/LocationDataItem.java index 0950fd9432d..2cd15099031 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/LocationDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/LocationDataItem.java @@ -12,11 +12,13 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class LocationDataItem extends DataItem { +public class LocationDataItem extends DataItem<@NonNull Location> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/MowerServiceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/MowerServiceDataItem.java index 705098382f5..21973dd09a5 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/MowerServiceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/MowerServiceDataItem.java @@ -12,11 +12,13 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class MowerServiceDataItem extends DataItem { +public class MowerServiceDataItem extends DataItem<@NonNull MowerService> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/PowerSocketServiceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/PowerSocketServiceDataItem.java index 86ef246b96e..d9890164107 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/PowerSocketServiceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/PowerSocketServiceDataItem.java @@ -12,10 +12,12 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class PowerSocketServiceDataItem extends DataItem { +public class PowerSocketServiceDataItem extends DataItem<@NonNull PowerSocketService> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/SensorServiceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/SensorServiceDataItem.java index f29ce8203ee..dd9b49cbe6a 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/SensorServiceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/SensorServiceDataItem.java @@ -12,10 +12,12 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class SensorServiceDataItem extends DataItem { +public class SensorServiceDataItem extends DataItem<@NonNull SensorService> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveServiceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveServiceDataItem.java index 54f5061adcb..213e56fc20e 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveServiceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveServiceDataItem.java @@ -12,10 +12,12 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class ValveServiceDataItem extends DataItem { +public class ValveServiceDataItem extends DataItem<@NonNull ValveService> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveSetServiceDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveSetServiceDataItem.java index 0c8974f1b0f..1d492e3e058 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveSetServiceDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/ValveSetServiceDataItem.java @@ -12,10 +12,12 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class ValveSetServiceDataItem extends DataItem { +public class ValveSetServiceDataItem extends DataItem<@NonNull ValveSetService> { } diff --git a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/WebSocketDataItem.java b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/WebSocketDataItem.java index 61964ea9dde..fce96e39db9 100644 --- a/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/WebSocketDataItem.java +++ b/bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/model/dto/api/WebSocketDataItem.java @@ -12,10 +12,12 @@ */ package org.openhab.binding.gardena.internal.model.dto.api; +import org.eclipse.jdt.annotation.NonNull; + /** * Represents a Gardena object that is sent via the Gardena API. * * @author Gerhard Riegler - Initial contribution */ -public class WebSocketDataItem extends DataItem { +public class WebSocketDataItem extends DataItem<@NonNull WebSocket> { } diff --git a/bundles/org.openhab.binding.gardena/src/test/java/org/openhab/binding/gardena/DataItemTest.java b/bundles/org.openhab.binding.gardena/src/test/java/org/openhab/binding/gardena/DataItemTest.java new file mode 100644 index 00000000000..ec3bb0099b6 --- /dev/null +++ b/bundles/org.openhab.binding.gardena/src/test/java/org/openhab/binding/gardena/DataItemTest.java @@ -0,0 +1,85 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.gardena; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.jupiter.api.Test; +import org.openhab.binding.gardena.internal.model.DataItemDeserializer; +import org.openhab.binding.gardena.internal.model.dto.api.DataItem; +import org.openhab.binding.gardena.internal.model.dto.api.DeviceDataItem; +import org.openhab.binding.gardena.internal.model.dto.api.SensorService; +import org.openhab.binding.gardena.internal.model.dto.api.SensorServiceDataItem; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * Tests for {@link DataItem} deserialization. + * + * @author Jacob Laursen - Initial contribution + */ +@NonNullByDefault +public class DataItemTest { + + private Gson gson = new GsonBuilder().registerTypeAdapter(DataItem.class, new DataItemDeserializer()).create(); + + public T getObjectFromJson(String filename, Class clazz) throws IOException { + try (InputStream inputStream = DataItemTest.class.getResourceAsStream(filename)) { + if (inputStream == null) { + throw new IOException("inputstream is null"); + } + byte[] bytes = inputStream.readAllBytes(); + if (bytes == null) { + throw new IOException("Resulting byte-array empty"); + } + String json = new String(bytes, StandardCharsets.UTF_8); + return Objects.requireNonNull(gson.fromJson(json, clazz)); + } + } + + @Test + public void sensorServiceWellformed() throws IOException { + DataItem dataItem = getObjectFromJson("SensorServiceDataItem.json", DataItem.class); + assertInstanceOf(SensorServiceDataItem.class, dataItem); + assertEquals("SENSOR", dataItem.type); + SensorService attributes = ((SensorServiceDataItem) dataItem).attributes; + assertNotNull(attributes); + assertEquals(55, attributes.soilHumidity.value); + } + + @Test + public void sensorServiceNoAttributes() throws IOException { + DataItem dataItem = getObjectFromJson("SensorServiceDataItemNoAttributes.json", DataItem.class); + assertInstanceOf(SensorServiceDataItem.class, dataItem); + assertEquals("SENSOR", dataItem.type); + assertNull((SensorServiceDataItem) dataItem.attributes); + } + + @Test + public void device() throws IOException { + DataItem dataItem = getObjectFromJson("DeviceDataItem.json", DataItem.class); + assertInstanceOf(DeviceDataItem.class, dataItem); + assertEquals("DEVICE", dataItem.type); + assertNull((SensorServiceDataItem) dataItem.attributes); + } +} diff --git a/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/DeviceDataItem.json b/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/DeviceDataItem.json new file mode 100644 index 00000000000..9b9ed0eb625 --- /dev/null +++ b/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/DeviceDataItem.json @@ -0,0 +1,24 @@ +{ + "id": "0162efc2-0b62-4983-aa1d-d72442008b7c", + "type": "DEVICE", + "relationships": { + "location": { + "data": { + "id": "d01b4d00-945a-4639-bf5a-f73d2030dfe0", + "type": "LOCATION" + } + }, + "services": { + "data": [ + { + "id": "0162efc2-0b62-4983-aa1d-d72442008b7c", + "type": "SENSOR" + }, + { + "id": "0162efc2-0b62-4983-aa1d-d72442008b7c", + "type": "COMMON" + } + ] + } + } +} diff --git a/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItem.json b/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItem.json new file mode 100644 index 00000000000..316ddb8c760 --- /dev/null +++ b/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItem.json @@ -0,0 +1,22 @@ +{ + "id": "638e82cd-774c-4f34-be10-2eec41d1c0a6", + "type": "SENSOR", + "relationships": { + "device": { + "data": { + "id": "638e82cd-774c-4f34-be10-2eec41d1c0a6", + "type": "DEVICE" + } + } + }, + "attributes": { + "soilHumidity": { + "value": 55, + "timestamp": "2022-06-19T09:30:55.546+00:00" + }, + "soilTemperature": { + "value": 18, + "timestamp": "2022-06-19T09:30:55.747+00:00" + } + } +} diff --git a/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItemNoAttributes.json b/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItemNoAttributes.json new file mode 100644 index 00000000000..9d111c1459e --- /dev/null +++ b/bundles/org.openhab.binding.gardena/src/test/resources/org/openhab/binding/gardena/SensorServiceDataItemNoAttributes.json @@ -0,0 +1,12 @@ +{ + "id": "638e82cd-774c-4f34-be10-2eec41d1c0a6", + "type": "SENSOR", + "relationships": { + "device": { + "data": { + "id": "638e82cd-774c-4f34-be10-2eec41d1c0a6", + "type": "DEVICE" + } + } + } +}