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;
17 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import java.math.BigDecimal;
20 import java.nio.file.Path;
21 import java.util.Optional;
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;
32 * Test cases for Client.parseMultiPointCoverageXml with a xml response having single place and multiple
36 * @author Sami Salonen - Initial contribution
39 public class FMIResponseParsingSinglePlaceTest extends AbstractFMIResponseParsingTest {
41 private Path observations1 = getTestResource("observations_single_place.xml");
44 private FMIResponse observationsResponse1;
46 private FMIResponse observationsResponse1NaN;
47 private Location emasalo = new Location("Porvoo Emäsalo", "101023", new BigDecimal("60.20382"),
48 new BigDecimal("25.62546"));
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);
59 assertNotNull(observationsResponse1);
63 public void testLocationsSinglePlace() {
64 assertThat(observationsResponse1.getLocations().size(), is(1));
65 assertThat(observationsResponse1.getLocations().stream().findFirst().get(), deeplyEqualTo(emasalo));
69 public void testParameters() {
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"));
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()));
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")));
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)));