]> git.basschouten.com Git - openhab-addons.git/blob
8d9537bab658aa2b665b7a58243f65b9b4c7cc13
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.fmiweather;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.math.BigDecimal;
19 import java.nio.file.Path;
20 import java.util.Optional;
21 import java.util.Set;
22
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;
29
30 /**
31  * Test cases for Client.parseMultiPointCoverageXml with a xml response having multiple places, parameters
32  * and timestamps
33  *
34  * @author Sami Salonen - Initial contribution
35  */
36 @NonNullByDefault
37 public class FMIResponseParsingMultiplePlacesTest extends AbstractFMIResponseParsingTest {
38
39     private Path observationsMultiplePlaces = getTestResource("observations_multiple_places.xml");
40     private Path forecastsMultiplePlaces = getTestResource("forecast_multiple_places.xml");
41
42     @NonNullByDefault({})
43     private FMIResponse observationsMultiplePlacesResponse;
44     @NonNullByDefault({})
45     private FMIResponse observationsMultiplePlacesNaNResponse;
46     @NonNullByDefault({})
47     private FMIResponse forecastsMultiplePlacesResponse;
48
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"));
56
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"));
62
63     @BeforeEach
64     public void setUp() {
65         try {
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);
73         }
74     }
75
76     @Test
77     public void testLocationsMultiplePlacesObservations() {
78         // locations
79         assertThat(observationsMultiplePlacesResponse.getLocations().size(), is(3));
80         assertThat(observationsMultiplePlacesResponse.getLocations(),
81                 hasItems(deeplyEqualTo(emasalo), deeplyEqualTo(kilpilahti), deeplyEqualTo(harabacka)));
82     }
83
84     @Test
85     public void testLocationsMultiplePlacesForecasts() {
86         // locations
87         assertThat(forecastsMultiplePlacesResponse.getLocations().size(), is(2));
88         assertThat(forecastsMultiplePlacesResponse.getLocations(),
89                 hasItems(deeplyEqualTo(maarianhamina), deeplyEqualTo(pointWithNoName)));
90     }
91
92     @Test
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"));
99         }
100     }
101
102     @Test
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"));
109         }
110     }
111
112     @Test
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",
119                 "71.0", "77.0")));
120     }
121
122     @Test
123     public void testParseForecastsMultipleData() {
124         long start = 1622116800;
125         Data temperature = forecastsMultiplePlacesResponse.getData(maarianhamina, "Temperature").get();
126         assertThat(temperature, is(deeplyEqualTo(start, 360, "7.75", "7.94", "6.72", "8.22", "11.37", "9.69", "6.42",
127                 "9.52", "11.04", "9.69", null, null, null, null)));
128         Data temperature2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Temperature").get();
129         assertThat(temperature2, is(deeplyEqualTo(start, 360, "7.46", "6.56", "6.2", "5.15", "5.05", "5.96", "6.2",
130                 "5.94", "5.69", "5.47", null, null, null, null)));
131
132         Data humidity = forecastsMultiplePlacesResponse.getData(maarianhamina, "Humidity").get();
133         assertThat(humidity, is(deeplyEqualTo(start, 360, "93.76", "93.24", "98.22", "93.93", "75.78", "58.91", "80.42",
134                 "54.11", "40.29", "46.42", null, null, null, null)));
135         Data humidity2 = forecastsMultiplePlacesResponse.getData(pointWithNoName, "Humidity").get();
136         assertThat(humidity2, is(deeplyEqualTo(start, 360, "93.44", "95.3", "96.15", "93.77", "93.0", "82.1", "81.95",
137                 "81.37", "85.41", "87.8", null, null, null, null)));
138     }
139
140     @Test
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)));
146     }
147 }