Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dwdunwetter] Improve server problem handling #15405

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ public String getDataFromEndpoint(String cellId) {
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("<?xml") || !rawData.contains("FeatureCollection")) {
logger.warn("Communication error occurred while getting data, response is not in expected XML-format");
return "";
}
return rawData;
} catch (IOException e) {
logger.warn("Communication error occurred while getting data: {}", e.getMessage());
Expand All @@ -63,4 +67,8 @@ public String getDataFromEndpoint(String cellId) {

return "";
}

public String getByURL(String url) throws IOException {
return HttpUtil.executeUrl("GET", url, 5000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,6 @@ public void updateCache() {
*/
protected void setDataAccess(DwdWarningDataAccess dataAccess) {
dataAccessCached = new ExpiringCache<>(Duration.ofMinutes(MIN_REFRESH_WAIT_MINUTES),
() -> dataAccess.getDataFromEndpoint(""));
() -> dataAccess.getDataFromEndpoint("TestCity"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<name>DWD Unwetter Binding</name>
<description>This is the binding for DWD Unwetter.</description>
<connection>cloud</connection>

<countries>de</countries>
</addon:addon>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private class TestDataProvider extends DwdWarningDataAccess {
private String rawData = "";

@Override
public String getDataFromEndpoint(String cellId) {
public String getByURL(String url) {
return rawData;
}
}
Expand Down