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.dwdunwetter.internal.dto;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.StringWriter;
23 import java.nio.charset.StandardCharsets;
24 import java.time.ZoneId;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.core.library.types.DateTimeType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.types.UnDefType;
33 * Tests for {@link org.openhab.binding.dwdunwetter.internal.dto.DwdWarningsData}
36 * Uses the warnings.xml from the resources directory instead of accessing the api endpoint.
39 * The XML has 2 Entries:
42 * <li>Amtliche WARNUNG vor WINDBÖEN, MINOR
43 * <li>Amtliche WARNUNG vor STURMBÖEN, MODERATE
46 * @author Martin Koehler - Initial contribution
48 public class DwdWarningsDataTest {
50 private TestDataProvider testDataProvider;
51 private DwdWarningsData warningsData;
54 public void setUp() throws IOException {
55 this.testDataProvider = new TestDataProvider();
57 warningsData = new DwdWarningsData("");
58 warningsData.setDataAccess(testDataProvider);
59 warningsData.refresh();
63 public void testGetHeadline() {
64 assertThat(warningsData.getHeadline(0), is("Amtliche WARNUNG vor STURMBÖEN"));
65 assertThat(warningsData.getHeadline(1), is("Amtliche WARNUNG vor WINDBÖEN"));
66 assertThat(warningsData.getHeadline(2), is(UnDefType.UNDEF));
70 public void testGetSeverity() {
71 assertThat(warningsData.getSeverity(0), is("Moderate"));
72 assertThat(warningsData.getSeverity(1), is("Minor"));
73 assertThat(warningsData.getSeverity(2), is(UnDefType.UNDEF));
77 public void testGetEvent() {
78 assertThat(warningsData.getEvent(0), is("STURMBÖEN"));
79 assertThat(warningsData.getEvent(1), is("WINDBÖEN"));
80 assertThat(warningsData.getEvent(2), is(UnDefType.UNDEF));
84 public void testGetDescription() {
85 assertThat(warningsData.getDescription(0), is(
86 "Es treten Sturmböen mit Geschwindigkeiten zwischen 60 km/h (17m/s, 33kn, Bft 7) und 80 km/h (22m/s, 44kn, Bft 9) anfangs aus südwestlicher, später aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit schweren Sturmböen um 90 km/h (25m/s, 48kn, Bft 10) gerechnet werden."));
87 assertThat(warningsData.getDescription(1), is(
88 "Es treten Windböen mit Geschwindigkeiten bis 60 km/h (17m/s, 33kn, Bft 7) aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit Sturmböen bis 80 km/h (22m/s, 44kn, Bft 9) gerechnet werden."));
89 assertThat(warningsData.getDescription(2), is(UnDefType.UNDEF));
93 public void testGetAltitude() {
94 assertThat(warningsData.getAltitude(0).format("%.0f ft"), is("0 ft"));
95 assertThat(warningsData.getAltitude(1).format("%.0f ft"), is("0 ft"));
96 assertThat(warningsData.getAltitude(2), is(UnDefType.UNDEF));
100 public void testGetCeiling() {
101 assertThat(warningsData.getCeiling(0).format("%.0f ft"), is("9843 ft"));
102 assertThat(warningsData.getCeiling(1).format("%.0f ft"), is("9843 ft"));
103 assertThat(warningsData.getCeiling(2), is(UnDefType.UNDEF));
107 public void testGetExpires() {
108 // Conversion is needed as getExpires returns the Date with the System Default Zone
109 assertThat(((DateTimeType) warningsData.getExpires(0)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
110 .toString(), is("2018-12-22T18:00Z[UTC]"));
111 assertThat(((DateTimeType) warningsData.getExpires(1)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
112 .toString(), is("2018-12-23T01:00Z[UTC]"));
113 assertThat(warningsData.getExpires(2), is(UnDefType.UNDEF));
117 public void testGetOnset() {
118 // Conversion is needed as getOnset returns the Date with the System Default Zone
119 assertThat(((DateTimeType) warningsData.getOnset(0)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
120 .toString(), is("2018-12-21T10:00Z[UTC]"));
121 assertThat(((DateTimeType) warningsData.getOnset(1)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
122 .toString(), is("2018-12-22T18:00Z[UTC]"));
123 assertThat(warningsData.getOnset(2), is(UnDefType.UNDEF));
127 public void testGetEffective() {
128 // Conversion is needed as getEffective returns the Date with the System Default Zone
129 assertThat(((DateTimeType) warningsData.getEffective(0)).getZonedDateTime()
130 .withZoneSameInstant(ZoneId.of("UTC")).toString(), is("2018-12-22T03:02Z[UTC]"));
131 assertThat(((DateTimeType) warningsData.getEffective(1)).getZonedDateTime()
132 .withZoneSameInstant(ZoneId.of("UTC")).toString(), is("2018-12-22T10:15Z[UTC]"));
133 assertThat(warningsData.getEffective(2), is(UnDefType.UNDEF));
137 public void testGetWarning() {
138 assertThat(warningsData.getWarning(0), is(OnOffType.ON));
139 assertThat(warningsData.getWarning(1), is(OnOffType.ON));
140 assertThat(warningsData.getWarning(2), is(OnOffType.OFF));
144 public void testisNew() {
145 assertThat(warningsData.isNew(0), is(true));
146 assertThat(warningsData.isNew(1), is(true));
147 assertThat(warningsData.isNew(2), is(false));
149 assertThat(warningsData.isNew(0), is(false));
150 assertThat(warningsData.isNew(1), is(false));
151 assertThat(warningsData.isNew(2), is(false));
154 private void loadXmlFromFile() throws IOException {
155 InputStream stream = getClass().getResourceAsStream("warnings.xml");
156 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
159 StringWriter stringWriter = new StringWriter();
160 while ((line = reader.readLine()) != null) {
161 stringWriter.write(line);
164 testDataProvider.rawData = stringWriter.toString();
167 private class TestDataProvider extends DwdWarningDataAccess {
169 private String rawData = "";
172 public String getDataFromEndpoint(String cellId) {