]> git.basschouten.com Git - openhab-addons.git/commitdiff
Added alternative handling for UnmarshalException (#10715)
authorChristoph Weitkamp <github@christophweitkamp.de>
Thu, 20 May 2021 18:26:51 +0000 (20:26 +0200)
committerGitHub <noreply@github.com>
Thu, 20 May 2021 18:26:51 +0000 (20:26 +0200)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateCallback.java
bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/callbacks/FritzAhaUpdateTemplatesCallback.java
bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzDeviceListModelTest.java
bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/dto/AVMFritzTemplateListModelTest.java
itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/discovery/AVMFritzDiscoveryServiceOSGiTest.java
itests/org.openhab.binding.avmfritz.tests/src/main/java/org/openhab/binding/avmfritz/internal/handler/AVMFritzThingHandlerOSGiTest.java

index 31caca094143102ac9a502b60b37ad58a3ff9f49..c98c35802adfed2bbab55a2ed891bca7ff829e40 100644 (file)
@@ -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,
index d976c3b35e014219d6005fa11adc0547344d2e0a..702b2973702cddcc1b8a46a64930b74f51d6216b 100644 (file)
@@ -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);
             }
index 3606d52ef6a0f168b3e5aa4adf77ed9404d00b0b..5c325c9a7ea7f13a1194d1c85037bfebf7d1df11 100644 (file)
@@ -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 =
                 "<devicelist version=\"1\">" +
@@ -62,13 +61,10 @@ public class AVMFritzDeviceListModelTest {
                     "<device identifier=\"13096 0007308\" id=\"30\" functionbitmask=\"1048864\" fwversion=\"05.10\" manufacturer=\"AVM\" productname=\"FRITZ!DECT 440\"><present>1</present><name>FRITZ!DECT 440 #15</name><temperature><celsius>230</celsius><offset>0</offset></temperature><humidity><rel_humidity>43</rel_humidity></humidity><battery>100</battery><batterylow>0</batterylow><button identifier=\"13096 0007308-1\" id=\"5000\"><name>FRITZ!DECT 440 #15: Oben rechts</name><lastpressedtimestamp>1549195586</lastpressedtimestamp></button><button identifier=\"13096 0007308-3\" id=\"5001\"><name>FRITZ!DECT 440 #15: Unten rechts</name><lastpressedtimestamp>1549195595</lastpressedtimestamp></button><button identifier=\"13096 0007308-5\" id=\"5002\"><name>FRITZ!DECT 440 #15: Unten links</name><lastpressedtimestamp>1549195586</lastpressedtimestamp></button><button identifier=\"13096 0007308-7\" id=\"5003\"><name>FRITZ!DECT 440 #15: Oben links</name><lastpressedtimestamp>1549195595</lastpressedtimestamp></button></device>" +
                     "<device identifier=\"14276 0503450-1\" id=\"2000\" functionbitmask=\"335888\" fwversion=\"0.0\" manufacturer=\"0x37c4\" productname=\"Rollotron 1213\"><present>1</present><txbusy>0</txbusy><name>Rollotron 1213 #1</name><blind><endpositionsset>1</endpositionsset><mode>manuell</mode></blind><levelcontrol><level>26</level><levelpercentage>10</levelpercentage></levelcontrol><etsiunitinfo><etsideviceid>406</etsideviceid><unittype>281</unittype><interfaces>256,513,516,517</interfaces></etsiunitinfo><alert><state>0</state><lastalertchgtimestamp></lastalertchgtimestamp></alert></device>" +
                 "</devicelist>";
-        //@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
index 6691c8f91c3aaa017a70760e799481f2088d75e2..3b7c0fc40802998917b710a91a107c15036de96a 100644 (file)
@@ -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 =
                 "<templatelist version=\"1\">" +
                     "<template identifier=\"tmpXXXXX-39DC738C5\" id=\"30103\" functionbitmask=\"6784\" applymask=\"64\"><name>Test template #1</name><devices><device identifier=\"YY:5D:AA-900\" /><device identifier=\"XX:5D:AA-900\" /></devices><applymask><relay_automatic /></applymask></template>" +
                     "<template identifier=\"tmpXXXXX-39722FC0F\" id=\"30003\" functionbitmask=\"6784\" applymask=\"64\"><name>Test template #2</name><devices><device identifier=\"YY:5D:AA-900\" /></devices><applymask><relay_automatic /></applymask></template>" +
                 "</templatelist>";
-        //@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
index c5e5fd5d1f5f76c526ac318854b18c7c9503ac73..4eb5a4020941308107f5f95178710006000eed8b 100644 (file)
@@ -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<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
                 @Nullable Collection<ThingTypeUID> 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
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -149,8 +155,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -212,8 +221,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -262,8 +274,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -315,8 +330,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                  "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -368,8 +386,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -421,8 +442,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
             "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -467,8 +491,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -502,8 +529,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -534,8 +564,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -577,8 +610,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -620,8 +656,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -663,8 +702,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -706,8 +748,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -759,8 +804,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -816,8 +864,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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 =
                 "<devicelist version=\"1\">" +
@@ -868,8 +919,10 @@ public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTe
                 "</devicelist>";
         //@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());
 
index 9a998955003e564fab713884b586aff6f6414543..711936159ad83eea7771730ab29c0f4cd77c2f34 100644 (file)
@@ -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<String, Object> 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();
     }
 }