]> git.basschouten.com Git - openhab-addons.git/blob
e0b5c1e3eba395768fe82af9c392433f4018d0b2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.junit.Assert.*;
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.Before;
25 import org.junit.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 single place and multiple
32  * parameters
33  * and timestamps
34  *
35  * @author Sami Salonen - Initial contribution
36  */
37 @NonNullByDefault
38 public class FMIResponseParsingSinglePlaceTest extends AbstractFMIResponseParsingTest {
39
40     private Path observations1 = getTestResource("observations_single_place.xml");
41
42     @NonNullByDefault({})
43     private FMIResponse observationsResponse1;
44     @NonNullByDefault({})
45     private FMIResponse observationsResponse1NaN;
46     private Location emasalo = new Location("Porvoo Emäsalo", "101023", new BigDecimal("60.20382"),
47             new BigDecimal("25.62546"));
48
49     @Before
50     public void setUp() {
51         try {
52             observationsResponse1 = parseMultiPointCoverageXml(readTestResourceUtf8(observations1));
53             observationsResponse1NaN = parseMultiPointCoverageXml(
54                     readTestResourceUtf8(observations1).replace("276.0", "NaN"));
55         } catch (Throwable e) {
56             throw new RuntimeException("Test data malformed", e);
57         }
58         assertNotNull(observationsResponse1);
59     }
60
61     @Test
62     public void testLocationsSinglePlace() {
63         assertThat(observationsResponse1.getLocations().size(), is(1));
64         assertThat(observationsResponse1.getLocations().stream().findFirst().get(), deeplyEqualTo(emasalo));
65     }
66
67     @Test
68     public void testParameters() {
69         // parameters
70         Optional<Set<String>> parametersOptional = observationsResponse1.getParameters(emasalo);
71         Set<String> parameters = parametersOptional.get();
72         assertThat(parameters.size(), is(6));
73         assertThat(parameters, hasItems("wd_10min", "wg_10min", "rh", "p_sea", "ws_10min", "t2m"));
74     }
75
76     @Test
77     public void testGetDataWithInvalidArguments() {
78         Location loc = observationsResponse1.getLocations().stream().findAny().get();
79         // Invalid parameter or location (fmisid)
80         assertThat(observationsResponse1.getData(loc, "foobar"), is(Optional.empty()));
81         assertThat(observationsResponse1.getData(
82                 new Location("Porvoo Emäsalo", "9999999", new BigDecimal("60.20382"), new BigDecimal("25.62546")),
83                 "rh"), is(Optional.empty()));
84     }
85
86     @Test
87     public void testParseObservations1Data() {
88         Data wd_10min = observationsResponse1.getData(emasalo, "wd_10min").get();
89         assertThat(wd_10min, is(deeplyEqualTo(1552215600L, 60, "312.0", "286.0", "295.0", "282.0", "271.0", "262.0",
90                 "243.0", "252.0", "262.0", "276.0")));
91     }
92
93     @Test
94     public void testParseObservations1NaN() {
95         // last value is null, due to NaN measurement value
96         Data wd_10min = observationsResponse1NaN.getData(emasalo, "wd_10min").get();
97         assertThat(wd_10min, is(deeplyEqualTo(1552215600L, 60, "312.0", "286.0", "295.0", "282.0", "271.0", "262.0",
98                 "243.0", "252.0", "262.0", null)));
99     }
100 }