2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.fmiweather;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import java.math.BigDecimal;
19 import java.nio.file.Path;
20 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.fmiweather.internal.client.Data;
27 import org.openhab.binding.fmiweather.internal.client.FMIResponse;
28 import org.openhab.binding.fmiweather.internal.client.Location;
31 * Test cases for Client.parseMultiPointCoverageXml with a xml response having multiple places, parameters
34 * @author Sami Salonen - Initial contribution
37 public class FMIResponseParsingMultiplePlacesTest extends AbstractFMIResponseParsingTest {
39 private Path observationsMultiplePlaces = getTestResource("observations_multiple_places.xml");
40 private Path forecastsMultiplePlaces = getTestResource("forecast_multiple_places.xml");
43 private FMIResponse observationsMultiplePlacesResponse;
45 private FMIResponse observationsMultiplePlacesNaNResponse;
47 private FMIResponse forecastsMultiplePlacesResponse;
49 // observation station points (observations_multiple_places.xml) have fmisid as their id
50 private Location emasalo = new Location("Porvoo Emäsalo", "101023", new BigDecimal("60.20382"),
51 new BigDecimal("25.62546"));
52 private Location kilpilahti = new Location("Porvoo Kilpilahti satama", "100683", new BigDecimal("60.30373"),
53 new BigDecimal("25.54916"));
54 private Location harabacka = new Location("Porvoo Harabacka", "101028", new BigDecimal("60.39172"),
55 new BigDecimal("25.60730"));
57 // forecast points (forecast_multiple_places.xml) have latitude,longitude as their id
58 private Location maarianhamina = new Location("Mariehamn", "60.09726,19.93481", new BigDecimal("60.09726"),
59 new BigDecimal("19.93481"));
60 private Location pointWithNoName = new Location("19.9,61.0973", "61.09726,19.90000", new BigDecimal("61.09726"),
61 new BigDecimal("19.90000"));
66 observationsMultiplePlacesResponse = parseMultiPointCoverageXml(
67 readTestResourceUtf8(observationsMultiplePlaces));
68 observationsMultiplePlacesNaNResponse = parseMultiPointCoverageXml(
69 readTestResourceUtf8(observationsMultiplePlaces).replace("276.0", "NaN"));
70 forecastsMultiplePlacesResponse = parseMultiPointCoverageXml(readTestResourceUtf8(forecastsMultiplePlaces));
71 } catch (Throwable e) {
72 throw new RuntimeException("Test data malformed", e);
76 @SuppressWarnings("unchecked")
78 public void testLocationsMultiplePlacesObservations() {
80 assertThat(observationsMultiplePlacesResponse.getLocations().size(), is(3));
81 assertThat(observationsMultiplePlacesResponse.getLocations(),
82 hasItems(deeplyEqualTo(emasalo), deeplyEqualTo(kilpilahti), deeplyEqualTo(harabacka)));
85 @SuppressWarnings("unchecked")
87 public void testLocationsMultiplePlacesForecasts() {
89 assertThat(forecastsMultiplePlacesResponse.getLocations().size(), is(2));
90 assertThat(forecastsMultiplePlacesResponse.getLocations(),
91 hasItems(deeplyEqualTo(maarianhamina), deeplyEqualTo(pointWithNoName)));
95 public void testParametersMultipleObservations() {
96 for (Location location : new Location[] { emasalo, kilpilahti, harabacka }) {
97 Optional<Set<String>> parametersOptional = observationsMultiplePlacesResponse.getParameters(location);
98 Set<String> parameters = parametersOptional.get();
99 assertThat(parameters.size(), is(6));
100 assertThat(parameters, hasItems("wd_10min", "wg_10min", "rh", "p_sea", "ws_10min", "t2m"));
105 public void testParametersMultipleForecasts() {
106 for (Location location : new Location[] { maarianhamina, pointWithNoName }) {
107 Optional<Set<String>> parametersOptional = forecastsMultiplePlacesResponse.getParameters(location);
108 Set<String> parameters = parametersOptional.get();
109 assertThat(parameters.size(), is(2));
110 assertThat(parameters, hasItems("Temperature", "Humidity"));
115 public void testParseObservationsMultipleData() {
116 Data wd_10min = observationsMultiplePlacesResponse.getData(emasalo, "wd_10min").get();
117 assertThat(wd_10min, is(deeplyEqualTo(1552215600L, 60, "312.0", "286.0", "295.0", "282.0", "271.0", "262.0",
118 "243.0", "252.0", "262.0", "276.0")));
119 Data rh = observationsMultiplePlacesResponse.getData(kilpilahti, "rh").get();
120 assertThat(rh, is(deeplyEqualTo(1552215600L, 60, "73.0", "65.0", "60.0", "59.0", "57.0", "64.0", "66.0", "65.0",
125 public void testParseForecastsMultipleData() {
126 Data temperature = forecastsMultiplePlacesResponse.getData(maarianhamina, "Temperature").get();
127 assertThat(temperature, is(deeplyEqualTo(1553688000, 360, "3.84", "2.62", "2.26", "1.22", "5.47", "5.52",
128 "5.42", "4.78", "8.34", "7.15", null, null, null, null)));
129 Data temperature2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Temperature").get();
130 assertThat(temperature2, is(deeplyEqualTo(1553688000, 360, "1.54", "2.91", "2.41", "2.36", "4.22", "5.28",
131 "4.58", "4.0", "4.79", "5.4", null, null, null, null)));
133 Data humidity = forecastsMultiplePlacesResponse.getData(maarianhamina, "Humidity").get();
134 assertThat(humidity, is(deeplyEqualTo(1553688000, 360, "66.57", "87.38", "85.77", "96.3", "75.74", "81.7",
135 "86.78", "87.96", "70.86", "76.35", null, null, null, null)));
136 Data humidity2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Humidity").get();
137 assertThat(humidity2, is(deeplyEqualTo(1553688000, 360, "90.18", "86.22", "89.18", "89.43", "77.26", "78.55",
138 "83.36", "85.83", "80.82", "76.92", null, null, null, null)));
142 public void testParseObservations1NaN() {
143 // last value is null, due to NaN measurement value
144 Data wd_10min = observationsMultiplePlacesNaNResponse.getData(emasalo, "wd_10min").get();
145 assertThat(wd_10min, is(deeplyEqualTo(1552215600L, 60, "312.0", "286.0", "295.0", "282.0", "271.0", "262.0",
146 "243.0", "252.0", "262.0", null)));