From 3b30d5ab11d8669e5028b3c78e42e141e0be5abd Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sat, 23 Sep 2023 19:28:59 +0200 Subject: [PATCH] [dwdunwetter] Improve server problem handling (#15405) * Fix #14691 * Add country tag --------- Signed-off-by: lsiepel Signed-off-by: Leo Siepel --- .../internal/dto/DwdWarningDataAccess.java | 10 ++- .../internal/dto/DwdWarningsData.java | 2 +- .../src/main/resources/OH-INF/addon/addon.xml | 2 +- .../dto/DwdWarningsDataAccessTest.java | 76 +++++++++++++++++++ .../internal/dto/DwdWarningsDataTest.java | 2 +- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataAccessTest.java diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningDataAccess.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningDataAccess.java index 3339fb3fe0a..749428d281c 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningDataAccess.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningDataAccess.java @@ -51,10 +51,14 @@ public class DwdWarningDataAccess { stringBuilder.append("&CQL_FILTER="); stringBuilder.append(URLEncoder.encode("WARNCELLID LIKE '" + cellId + "'", StandardCharsets.UTF_8)); logger.debug("Refreshing Data for cell {}", cellId); - String rawData = HttpUtil.executeUrl("GET", stringBuilder.toString(), 5000); + String rawData = getByURL(stringBuilder.toString()); logger.trace("Raw request: {}", stringBuilder); logger.trace("Raw response: {}", rawData); + if (rawData == null || !rawData.startsWith("(Duration.ofMinutes(MIN_REFRESH_WAIT_MINUTES), - () -> dataAccess.getDataFromEndpoint("")); + () -> dataAccess.getDataFromEndpoint("TestCity")); } } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/resources/OH-INF/addon/addon.xml b/bundles/org.openhab.binding.dwdunwetter/src/main/resources/OH-INF/addon/addon.xml index 648a16c46d0..39024c6ad4f 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/resources/OH-INF/addon/addon.xml +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/resources/OH-INF/addon/addon.xml @@ -7,5 +7,5 @@ DWD Unwetter Binding This is the binding for DWD Unwetter. cloud - + de diff --git a/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataAccessTest.java b/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataAccessTest.java new file mode 100644 index 00000000000..69092e09428 --- /dev/null +++ b/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataAccessTest.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2010-2023 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.dwdunwetter.internal.dto; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link org.openhab.binding.dwdunwetter.internal.dto.DwdWarningsDataAccess} + * + * @author Leo Siepel - Initial contribution + */ +public class DwdWarningsDataAccessTest { + private TestDataProvider testDataProvider = new TestDataProvider(); + + @BeforeEach + public void setUp() throws IOException { + this.testDataProvider = new TestDataProvider(); + loadXmlFromFile(); + } + + @Test + public void testNullOrBlank() { + assertEquals(testDataProvider.getDataFromEndpoint(null), ""); + assertEquals(testDataProvider.getDataFromEndpoint(""), ""); + } + + @Test + public void testInvalidResponse() { + TestDataProvider testDataProvider = new TestDataProvider(); + testDataProvider.rawData = "Server is not returning xml"; + assertEquals(testDataProvider.getDataFromEndpoint("TestCity"), ""); + } + + private void loadXmlFromFile() throws IOException { + InputStream stream = getClass().getResourceAsStream("warnings.xml"); + BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); + String line = null; + + StringWriter stringWriter = new StringWriter(); + while ((line = reader.readLine()) != null) { + stringWriter.write(line); + } + reader.close(); + testDataProvider.rawData = stringWriter.toString(); + } + + private class TestDataProvider extends DwdWarningDataAccess { + + private String rawData = ""; + + @Override + public String getByURL(String url) { + return rawData; + } + } +} diff --git a/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataTest.java b/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataTest.java index 003201627c6..f103c5117aa 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataTest.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/internal/dto/DwdWarningsDataTest.java @@ -169,7 +169,7 @@ public class DwdWarningsDataTest { private String rawData = ""; @Override - public String getDataFromEndpoint(String cellId) { + public String getByURL(String url) { return rawData; } }