Skip to content

Commit

Permalink
[fmiweather] Fixed to work with new API (#10765)
Browse files Browse the repository at this point in the history
Tests updated as well

Signed-off-by: Sami Salonen <[email protected]>
  • Loading branch information
ssalonen authored May 29, 2021
1 parent eab7623 commit 572fd3d
Show file tree
Hide file tree
Showing 11 changed files with 724 additions and 696 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class Client {

private static final Map<String, String> NAMESPACES = new HashMap<>();
static {
NAMESPACES.put("target", "http://xml.fmi.fi/namespace/om/atmosphericfeatures/1.0");
NAMESPACES.put("target", "http://xml.fmi.fi/namespace/om/atmosphericfeatures/1.1");
NAMESPACES.put("gml", "http://www.opengis.net/gml/3.2");
NAMESPACES.put("xlink", "http://www.w3.org/1999/xlink");
NAMESPACES.put("ows", "http://www.opengis.net/ows/1.1");
Expand Down Expand Up @@ -374,7 +374,8 @@ private String[] queryNodeValues(XPathExpression expression, Object source) thro
*/
private String takeFirstOrError(String errorDescription, String[] values) throws FMIUnexpectedResponseException {
if (values.length != 1) {
throw new FMIUnexpectedResponseException(String.format("No unique match found: %s", errorDescription));
throw new FMIUnexpectedResponseException(
String.format("No unique match found: %s (found %d)", errorDescription, values.length));
}
return values[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.binding.fmiweather;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -23,6 +23,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -109,6 +110,36 @@ protected boolean matchesSafely(Data dataValues) {
return timestampMatcher.matches(dataValues.timestampsEpochSecs)
&& valuesMatcher.matches(dataValues.values);
}

@Override
protected void describeMismatchSafely(Data dataValues, @Nullable Description mismatchDescription) {
if (mismatchDescription == null) {
super.describeMismatchSafely(dataValues, mismatchDescription);
return;
}
if (!timestampMatcher.matches(dataValues.timestampsEpochSecs)) {
mismatchDescription.appendText("timestamps mismatch: ");
if (dataValues.timestampsEpochSecs[0] != start) {
mismatchDescription.appendText("start mismatch (was ");
mismatchDescription.appendValue(dataValues.timestampsEpochSecs[0]);
mismatchDescription.appendText(")");
} else if (dataValues.timestampsEpochSecs.length != values.length) {
mismatchDescription.appendText("length mismatch (was ");
mismatchDescription.appendValue(dataValues.timestampsEpochSecs.length);
mismatchDescription.appendText(")");
} else {
mismatchDescription.appendText("interval mismatch (was ");
Set<Long> intervals = new HashSet<>();
for (int i = 1; i < values.length; i++) {
long interval = dataValues.timestampsEpochSecs[i] - dataValues.timestampsEpochSecs[i - 1];
intervals.add(interval);
}
mismatchDescription.appendValue(intervals.toArray());
mismatchDescription.appendText(")");
}
}
mismatchDescription.appendText(", valuesMatch=").appendValue(valuesMatcher.matches(dataValues.values));
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testErrorResponse() {
} catch (FMIResponseException e) {
// OK
assertThat(e.getMessage(), is(
"Exception report (OperationParsingFailed): [Invalid time interval!, The start time is later than the end time., URI:\n\t\t\t/wfs?endtime=1900-03-10T20%3A10%3A00Z&fmisid=101023&parameters=t2m%2Crh%2Cwd_10min%2Cws_10min%2Cwg_10min%2Cp_sea&request=getFeature&service=WFS&starttime=2019-03-10T10%3A10%3A00Z&storedquery_id=fmi%3A%3Aobservations%3A%3Aweather%3A%3Amultipointcoverage&timestep=60&version=2.0.0]"));
"Exception report (OperationParsingFailed): [Invalid time interval!, The start time is later than the end time., URI: /wfs?endtime=1900-03-10T20%3A10%3A00Z&fmisid=101023&parameters=t2m%2Crh%2Cwd_10min%2Cws_10min%2Cwg_10min%2Cp_sea&request=getFeature&service=WFS&starttime=2019-03-10T10%3A10%3A00Z&storedquery_id=fmi%3A%3Aobservations%3A%3Aweather%3A%3Amultipointcoverage&timestep=60&version=2.0.0]"));
return;
} catch (Throwable e) {
fail("Wrong exception, was " + e.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void setUp() {
}
}

@SuppressWarnings("unchecked")
@Test
public void testLocationsMultiplePlacesObservations() {
// locations
Expand All @@ -82,7 +81,6 @@ public void testLocationsMultiplePlacesObservations() {
hasItems(deeplyEqualTo(emasalo), deeplyEqualTo(kilpilahti), deeplyEqualTo(harabacka)));
}

@SuppressWarnings("unchecked")
@Test
public void testLocationsMultiplePlacesForecasts() {
// locations
Expand Down Expand Up @@ -123,19 +121,20 @@ public void testParseObservationsMultipleData() {

@Test
public void testParseForecastsMultipleData() {
long start = 1622116800;
Data temperature = forecastsMultiplePlacesResponse.getData(maarianhamina, "Temperature").get();
assertThat(temperature, is(deeplyEqualTo(1553688000, 360, "3.84", "2.62", "2.26", "1.22", "5.47", "5.52",
"5.42", "4.78", "8.34", "7.15", null, null, null, null)));
assertThat(temperature, is(deeplyEqualTo(start, 360, "7.75", "7.94", "6.72", "8.22", "11.37", "9.69", "6.42",
"9.52", "11.04", "9.69", null, null, null, null)));
Data temperature2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Temperature").get();
assertThat(temperature2, is(deeplyEqualTo(1553688000, 360, "1.54", "2.91", "2.41", "2.36", "4.22", "5.28",
"4.58", "4.0", "4.79", "5.4", null, null, null, null)));
assertThat(temperature2, is(deeplyEqualTo(start, 360, "7.46", "6.56", "6.2", "5.15", "5.05", "5.96", "6.2",
"5.94", "5.69", "5.47", null, null, null, null)));

Data humidity = forecastsMultiplePlacesResponse.getData(maarianhamina, "Humidity").get();
assertThat(humidity, is(deeplyEqualTo(1553688000, 360, "66.57", "87.38", "85.77", "96.3", "75.74", "81.7",
"86.78", "87.96", "70.86", "76.35", null, null, null, null)));
assertThat(humidity, is(deeplyEqualTo(start, 360, "93.76", "93.24", "98.22", "93.93", "75.78", "58.91", "80.42",
"54.11", "40.29", "46.42", null, null, null, null)));
Data humidity2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Humidity").get();
assertThat(humidity2, is(deeplyEqualTo(1553688000, 360, "90.18", "86.22", "89.18", "89.43", "77.26", "78.55",
"83.36", "85.83", "80.82", "76.92", null, null, null, null)));
assertThat(humidity2, is(deeplyEqualTo(start, 360, "93.44", "95.3", "96.15", "93.77", "93.0", "82.1", "81.95",
"81.37", "85.41", "87.8", null, null, null, null)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testParseStations() {
new BigDecimal("25.549164"))),
deeplyEqualTo(new Location("Parainen Utö", "100908", new BigDecimal("59.779094"),
new BigDecimal("21.374788"))),
deeplyEqualTo(new Location("Lemland Nyhamn", "100909", new BigDecimal("59.959108"),
new BigDecimal("19.953736")))));
deeplyEqualTo(new Location("Lemland Nyhamn", "100909", new BigDecimal("59.959194"),
new BigDecimal("19.953667")))));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://opendata.fmi.fi/wfs?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::observations::weather::multipointcoverage&fmisid=101023&starttime=2019-03-10T10:10:00Z&endtime=1900-03-10T20:10:00Z&timestep=60&parameters=t2m,rh,wd_10min,ws_10min,wg_10min,p_sea -->
<ExceptionReport xmlns="http://www.opengis.net/ows/1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd"
version="2.0.0" xml:lang="eng">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/ows/1.1 http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd"
version="2.0.0" xml:lang="eng">


<Exception exceptionCode="OperationParsingFailed">
<ExceptionText>Invalid time interval!</ExceptionText>
<ExceptionText>The start time is later than the end time.</ExceptionText>
<ExceptionText>URI:
/wfs?endtime=1900-03-10T20%3A10%3A00Z&amp;fmisid=101023&amp;parameters=t2m%2Crh%2Cwd_10min%2Cws_10min%2Cwg_10min%2Cp_sea&amp;request=getFeature&amp;service=WFS&amp;starttime=2019-03-10T10%3A10%3A00Z&amp;storedquery_id=fmi%3A%3Aobservations%3A%3Aweather%3A%3Amultipointcoverage&amp;timestep=60&amp;version=2.0.0</ExceptionText>
<Exception exceptionCode="OperationParsingFailed">
<ExceptionText>Invalid time interval!</ExceptionText>
<ExceptionText>The start time is later than the end time.</ExceptionText>
<ExceptionText>URI: /wfs?endtime=1900-03-10T20%3A10%3A00Z&amp;fmisid=101023&amp;parameters=t2m%2Crh%2Cwd_10min%2Cws_10min%2Cwg_10min%2Cp_sea&amp;request=getFeature&amp;service=WFS&amp;starttime=2019-03-10T10%3A10%3A00Z&amp;storedquery_id=fmi%3A%3Aobservations%3A%3Aweather%3A%3Amultipointcoverage&amp;timestep=60&amp;version=2.0.0</ExceptionText>

</Exception>
</Exception>

</ExceptionReport>
Loading

1 comment on commit 572fd3d

@openhab-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/fmiweather-binding-unexpected-api-response/122546/5

Please sign in to comment.