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