From 5a836b82cb91ae50cb4b737ac0bf042af7fb3f28 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Thu, 20 May 2021 20:26:51 +0200 Subject: [PATCH] Added alternative handling for UnmarshalException (#10715) Signed-off-by: Christoph Weitkamp --- .../callbacks/FritzAhaUpdateCallback.java | 7 +- .../FritzAhaUpdateTemplatesCallback.java | 6 +- .../dto/AVMFritzDeviceListModelTest.java | 20 ++- .../dto/AVMFritzTemplateListModelTest.java | 21 ++- .../AVMFritzDiscoveryServiceOSGiTest.java | 127 +++++++++++++----- .../handler/AVMFritzThingHandlerOSGiTest.java | 21 ++- 6 files changed, 127 insertions(+), 75 deletions(-) diff --git a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateCallback.java b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateCallback.java index 31caca094..c98c35802 100644 --- a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateCallback.java +++ b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateCallback.java @@ -17,6 +17,7 @@ import static org.eclipse.jetty.http.HttpMethod.GET; import java.io.StringReader; import javax.xml.bind.JAXBException; +import javax.xml.bind.UnmarshalException; import javax.xml.bind.Unmarshaller; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -58,6 +59,7 @@ public class FritzAhaUpdateCallback extends FritzAhaReauthCallback { this.handler = handler; } + @SuppressWarnings({ "null", "unused" }) @Override public void execute(int status, String response) { super.execute(status, response); @@ -66,13 +68,16 @@ public class FritzAhaUpdateCallback extends FritzAhaReauthCallback { try { XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response)); Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel model = (DeviceListModel) unmarshaller.unmarshal(xsr); + DeviceListModel model = unmarshaller.unmarshal(xsr, DeviceListModel.class).getValue(); if (model != null) { handler.onDeviceListAdded(model.getDevicelist()); } else { logger.debug("no model in response"); } handler.setStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null); + } catch (UnmarshalException e) { + logger.debug("Failed to unmarshal XML document: {}", e.getMessage()); + handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); } catch (JAXBException | XMLStreamException e) { logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e); handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, diff --git a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateTemplatesCallback.java b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateTemplatesCallback.java index d976c3b35..702b29737 100644 --- a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateTemplatesCallback.java +++ b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateTemplatesCallback.java @@ -17,6 +17,7 @@ import static org.eclipse.jetty.http.HttpMethod.GET; import java.io.StringReader; import javax.xml.bind.JAXBException; +import javax.xml.bind.UnmarshalException; import javax.xml.bind.Unmarshaller; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -54,6 +55,7 @@ public class FritzAhaUpdateTemplatesCallback extends FritzAhaReauthCallback { this.handler = handler; } + @SuppressWarnings({ "null", "unused" }) @Override public void execute(int status, String response) { super.execute(status, response); @@ -62,12 +64,14 @@ public class FritzAhaUpdateTemplatesCallback extends FritzAhaReauthCallback { try { XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response)); Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller(); - TemplateListModel model = (TemplateListModel) unmarshaller.unmarshal(xsr); + TemplateListModel model = unmarshaller.unmarshal(xsr, TemplateListModel.class).getValue(); if (model != null) { handler.addTemplateList(model.getTemplates()); } else { logger.debug("no template in response"); } + } catch (UnmarshalException e) { + logger.debug("Failed to unmarshal XML document: {}", e.getMessage()); } catch (JAXBException | XMLStreamException e) { logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e); } diff --git a/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzDeviceListModelTest.java b/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzDeviceListModelTest.java index 3606d52ef..5c325c9a7 100644 --- a/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzDeviceListModelTest.java +++ b/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzDeviceListModelTest.java @@ -21,13 +21,13 @@ import java.util.Optional; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openhab.binding.avmfritz.internal.util.JAXBUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Tests for {@link DeviceListModel}. @@ -38,12 +38,11 @@ import org.slf4j.LoggerFactory; @NonNullByDefault public class AVMFritzDeviceListModelTest { - private final Logger logger = LoggerFactory.getLogger(AVMFritzDeviceListModelTest.class); - private @NonNullByDefault({}) DeviceListModel devices; + @SuppressWarnings("null") @BeforeEach - public void setUp() { + public void setUp() throws JAXBException, XMLStreamException { //@formatter:off final String xml = "" + @@ -62,13 +61,10 @@ public class AVMFritzDeviceListModelTest { "1FRITZ!DECT 440 #152300431000" + "10Rollotron 1213 #11manuell2610406281256,513,516,5170" + ""; - //@formatter:off - try { - Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); - } catch (JAXBException e) { - logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e); - } + //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); + Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); + devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); } @Test diff --git a/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzTemplateListModelTest.java b/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzTemplateListModelTest.java index 6691c8f91..3b7c0fc40 100644 --- a/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzTemplateListModelTest.java +++ b/bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzTemplateListModelTest.java @@ -19,6 +19,8 @@ import java.util.Optional; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; @@ -26,8 +28,6 @@ import org.junit.jupiter.api.Test; import org.openhab.binding.avmfritz.internal.dto.templates.TemplateListModel; import org.openhab.binding.avmfritz.internal.dto.templates.TemplateModel; import org.openhab.binding.avmfritz.internal.util.JAXBUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Tests for {@link TemplateListModel}. @@ -37,26 +37,21 @@ import org.slf4j.LoggerFactory; @NonNullByDefault public class AVMFritzTemplateListModelTest { - private final Logger logger = LoggerFactory.getLogger(AVMFritzTemplateListModelTest.class); - private @NonNullByDefault({}) TemplateListModel templates; + @SuppressWarnings("null") @BeforeEach - public void setUp() { + public void setUp() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + "" + "" + ""; - //@formatter:off - - try { - Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller(); - templates = (TemplateListModel) u.unmarshal(new StringReader(xml)); - } catch (JAXBException e) { - logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e); - } + //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); + Unmarshaller u = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller(); + templates = u.unmarshal(xsr, TemplateListModel.class).getValue(); } @Test diff --git a/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/discovery/AVMFritzDiscoveryServiceOSGiTest.java b/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/discovery/AVMFritzDiscoveryServiceOSGiTest.java index c5e5fd5d1..4eb5a4020 100644 --- a/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/discovery/AVMFritzDiscoveryServiceOSGiTest.java +++ b/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/discovery/AVMFritzDiscoveryServiceOSGiTest.java @@ -18,10 +18,12 @@ import static org.openhab.core.thing.Thing.*; import java.io.StringReader; import java.util.Collection; -import java.util.Collections; +import java.util.List; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -67,7 +69,7 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe @Override public @Nullable Collection removeOlderResults(DiscoveryService source, long timestamp, @Nullable Collection thingTypeUIDs, @Nullable ThingUID bridgeUID) { - return Collections.emptyList(); + return List.of(); } }; @@ -104,8 +106,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertTrue(discovery.getSupportedThingTypes().contains(GROUP_SWITCH_THING_TYPE)); } + @SuppressWarnings("null") @Test - public void invalidDiscoveryResult() throws JAXBException { + public void invalidDiscoveryResult() throws JAXBException, XMLStreamException { // attribute productname is important for a valid discovery result //@formatter:off String xml = @@ -121,8 +124,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -133,8 +138,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertNull(discoveryResult); } + @SuppressWarnings("null") @Test - public void validDECTRepeater100Result() throws JAXBException { + public void validDECTRepeater100Result() throws JAXBException, XMLStreamException { //@formatter:off final String xml = "" + @@ -149,8 +155,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on - final Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); + Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -185,8 +193,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertNull(discoveryResult); } + @SuppressWarnings("null") @Test - public void validDECT200DiscoveryResult() throws JAXBException { + public void validDECT200DiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -212,8 +221,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -235,8 +246,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validDECT210DiscoveryResult() throws JAXBException { + public void validDECT210DiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -262,8 +274,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -285,8 +299,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validCometDECTDiscoveryResult() throws JAXBException { + public void validCometDECTDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -315,8 +330,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -338,8 +355,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validDECT300DiscoveryResult() throws JAXBException { + public void validDECT300DiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -368,8 +386,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -391,8 +411,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validDECT301DiscoveryResult() throws JAXBException { + public void validDECT301DiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -421,8 +442,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -444,8 +467,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validPowerline546EDiscoveryResult() throws JAXBException { + public void validPowerline546EDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -467,8 +491,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -490,8 +516,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void invalidHANFUNContactDiscoveryResult() throws JAXBException { + public void invalidHANFUNContactDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -502,8 +529,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -514,8 +543,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertNull(discoveryResult); } + @SuppressWarnings("null") @Test - public void validHANFUNMagneticContactDiscoveryResult() throws JAXBException { + public void validHANFUNMagneticContactDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -534,8 +564,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -557,8 +589,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validHANFUNOpticalContactDiscoveryResult() throws JAXBException { + public void validHANFUNOpticalContactDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -577,8 +610,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -600,8 +635,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validHANFUNMotionSensorDiscoveryResult() throws JAXBException { + public void validHANFUNMotionSensorDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -620,8 +656,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -643,8 +681,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validHANFUNMSmokeDetectorDiscoveryResult() throws JAXBException { + public void validHANFUNMSmokeDetectorDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -663,8 +702,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -686,8 +727,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validHANFUNSwitchtDiscoveryResult() throws JAXBException { + public void validHANFUNSwitchtDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -706,8 +748,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -729,8 +773,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validHANFUNBlindDiscoveryResult() throws JAXBException { + public void validHANFUNBlindDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -759,8 +804,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -782,8 +829,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validHeatingGroupDiscoveryResult() throws JAXBException { + public void validHeatingGroupDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -816,8 +864,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); @@ -841,8 +891,9 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe assertEquals(CONFIG_AIN, discoveryResult.getRepresentationProperty()); } + @SuppressWarnings("null") @Test - public void validSwitchGroupDiscoveryResult() throws JAXBException { + public void validSwitchGroupDiscoveryResult() throws JAXBException, XMLStreamException { //@formatter:off String xml = "" + @@ -868,8 +919,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe ""; //@formatter:on + XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(xml)); Unmarshaller u = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller(); - DeviceListModel devices = (DeviceListModel) u.unmarshal(new StringReader(xml)); + DeviceListModel devices = u.unmarshal(xsr, DeviceListModel.class).getValue(); + assertNotNull(devices); assertEquals(1, devices.getDevicelist().size()); diff --git a/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzThingHandlerOSGiTest.java b/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzThingHandlerOSGiTest.java index 9a9989550..711936159 100644 --- a/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzThingHandlerOSGiTest.java +++ b/itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzThingHandlerOSGiTest.java @@ -16,7 +16,6 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*; -import java.util.HashMap; import java.util.Map; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -97,15 +96,15 @@ public abstract class AVMFritzThingHandlerOSGiTest extends JavaOSGiTest { } private Bridge buildBridge() { - Map properties = new HashMap<>(); - properties.put(CONFIG_IP_ADDRESS, "fritz.box"); - properties.put(CONFIG_PROTOCOL, "http"); - properties.put(CONFIG_USER, "user"); - properties.put(CONFIG_PASSWORD, "password"); - properties.put(CONFIG_POLLING_INTERVAL, 15); - properties.put(CONFIG_SYNC_TIMEOUT, 2000); - - return BridgeBuilder.create(BRIDGE_THING_TYPE, "1").withLabel(BOX_MODEL_NAME) - .withConfiguration(new Configuration(properties)).build(); + return BridgeBuilder.create(BRIDGE_THING_TYPE, "1") // + .withLabel(BOX_MODEL_NAME) // + .withConfiguration(new Configuration(Map.of( // + CONFIG_IP_ADDRESS, "fritz.box", // + CONFIG_PROTOCOL, "http", // + CONFIG_USER, "user", // + CONFIG_PASSWORD, "password", // + CONFIG_POLLING_INTERVAL, 15, // + CONFIG_SYNC_TIMEOUT, 2000))) // + .build(); } }