Added alternative handling for UnmarshalException (#10715)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp
2021-05-20 20:26:51 +02:00
committed by GitHub
parent 7aab765b5a
commit 5a836b82cb
6 changed files with 127 additions and 75 deletions

View 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());

View 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();
}
}