]> git.basschouten.com Git - openhab-addons.git/blob
003201627c67c1d2cba7b4a187cc0b69905d17e5
[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.dwdunwetter.internal.dto;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
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;
25
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;
31
32 /**
33  * Tests for {@link org.openhab.binding.dwdunwetter.internal.dto.DwdWarningsData}
34  *
35  * <p>
36  * Uses the warnings.xml from the resources directory instead of accessing the api endpoint.
37  *
38  * <p>
39  * The XML has 2 Entries:
40  *
41  * <ol>
42  * <li>Amtliche WARNUNG vor WINDBÖEN, MINOR
43  * <li>Amtliche WARNUNG vor STURMBÖEN, MODERATE
44  * </ol>
45  *
46  * @author Martin Koehler - Initial contribution
47  */
48 public class DwdWarningsDataTest {
49
50     private TestDataProvider testDataProvider;
51     private DwdWarningsData warningsData;
52
53     @BeforeEach
54     public void setUp() throws IOException {
55         this.testDataProvider = new TestDataProvider();
56         loadXmlFromFile();
57         warningsData = new DwdWarningsData("");
58         warningsData.setDataAccess(testDataProvider);
59         warningsData.refresh();
60     }
61
62     @Test
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));
67     }
68
69     @Test
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));
74     }
75
76     @Test
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));
81     }
82
83     @Test
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));
90     }
91
92     @Test
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));
97     }
98
99     @Test
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));
104     }
105
106     @Test
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));
114     }
115
116     @Test
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));
124     }
125
126     @Test
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));
134     }
135
136     @Test
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));
141     }
142
143     @Test
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));
148         // No longer new
149         assertThat(warningsData.isNew(0), is(false));
150         assertThat(warningsData.isNew(1), is(false));
151         assertThat(warningsData.isNew(2), is(false));
152     }
153
154     private void loadXmlFromFile() throws IOException {
155         InputStream stream = getClass().getResourceAsStream("warnings.xml");
156         BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
157         String line = null;
158
159         StringWriter stringWriter = new StringWriter();
160         while ((line = reader.readLine()) != null) {
161             stringWriter.write(line);
162         }
163         reader.close();
164         testDataProvider.rawData = stringWriter.toString();
165     }
166
167     private class TestDataProvider extends DwdWarningDataAccess {
168
169         private String rawData = "";
170
171         @Override
172         public String getDataFromEndpoint(String cellId) {
173             return rawData;
174         }
175     }
176 }