2 * Copyright (c) 2010-2023 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);
77 public void testLocationsMultiplePlacesObservations() {
79 assertThat(observationsMultiplePlacesResponse.getLocations().size(), is(3));
80 assertThat(observationsMultiplePlacesResponse.getLocations(),
81 hasItems(deeplyEqualTo(emasalo), deeplyEqualTo(kilpilahti), deeplyEqualTo(harabacka)));
85 public void testLocationsMultiplePlacesForecasts() {
87 assertThat(forecastsMultiplePlacesResponse.getLocations().size(), is(2));
88 assertThat(forecastsMultiplePlacesResponse.getLocations(),
89 hasItems(deeplyEqualTo(maarianhamina), deeplyEqualTo(pointWithNoName)));
93 public void testParametersMultipleObservations() {
94 for (Location location : new Location[] { emasalo, kilpilahti, harabacka }) {
95 Optional<Set<String>> parametersOptional = observationsMultiplePlacesResponse.getParameters(location);
96 Set<String> parameters = parametersOptional.get();
97 assertThat(parameters.size(), is(6));
98 assertThat(parameters, hasItems("wd_10min", "wg_10min", "rh", "p_sea", "ws_10min", "t2m"));
103 public void testParametersMultipleForecasts() {
104 for (Location location : new Location[] { maarianhamina, pointWithNoName }) {
105 Optional<Set<String>> parametersOptional = forecastsMultiplePlacesResponse.getParameters(location);
106 Set<String> parameters = parametersOptional.get();
107 assertThat(parameters.size(), is(2));
108 assertThat(parameters, hasItems("Temperature", "Humidity"));
113 public void testParseObservationsMultipleData() {
114 Data wd_10min = observationsMultiplePlacesResponse.getData(emasalo, "wd_10min").get();
115 assertThat(wd_10min, is(deeplyEqualTo(1552215600L, 60, "312.0", "286.0", "295.0", "282.0", "271.0", "262.0",
116 "243.0", "252.0", "262.0", "276.0")));
117 Data rh = observationsMultiplePlacesResponse.getData(kilpilahti, "rh").get();
118 assertThat(rh, is(deeplyEqualTo(1552215600L, 60, "73.0", "65.0", "60.0", "59.0", "57.0", "64.0", "66.0", "65.0",
123 public void testParseForecastsMultipleData() {
124 long start = 1668340800;
125 Data temperature = forecastsMultiplePlacesResponse.getData(maarianhamina, "Temperature").get();
126 assertThat(temperature, is(deeplyEqualTo(start, 360, "9.2", "4.8", "7.4", "5.6", "7.7", "7.9", "7.6", null,
127 null, null, null, null, null, null)));
128 Data temperature2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Temperature").get();
129 assertThat(temperature2, is(deeplyEqualTo(start, 360, "7.6", "7.6", "8.0", "6.2", "7.6", "7.3", "6.1", null,
130 null, null, null, null, null, null)));
132 Data humidity = forecastsMultiplePlacesResponse.getData(maarianhamina, "Humidity").get();
133 assertThat(humidity, is(deeplyEqualTo(start, 360, "73.9", "98.0", "92.7", "94.9", "91.4", "92.1", "95.0", null,
134 null, null, null, null, null, null)));
135 Data humidity2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Humidity").get();
136 assertThat(humidity2, is(deeplyEqualTo(start, 360, "84.8", "89.9", "92.4", "99.3", "88.3", "88.7", "93.9", null,
137 null, null, null, null, null, null)));
141 public void testParseObservations1NaN() {
142 // last value is null, due to NaN measurement value
143 Data wd_10min = observationsMultiplePlacesNaNResponse.getData(emasalo, "wd_10min").get();
144 assertThat(wd_10min, is(deeplyEqualTo(1552215600L, 60, "312.0", "286.0", "295.0", "282.0", "271.0", "262.0",
145 "243.0", "252.0", "262.0", null)));