]> git.basschouten.com Git - openhab-addons.git/blob
1a7a09587a2581b8101f35d03ca746e3dcc9c0ae
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.data;
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 DwdWarningsData}
34  *
35  * Uses the warnings.xml from the resources directory instead of accessing the api endpoint.
36  *
37  * The XML has 2 Entries:
38  * <ol>
39  * <li>Amtliche WARNUNG vor WINDBÖEN, MINOR</li>
40  * <li>Amtliche WARNUNG vor STURMBÖEN, MODERATE</li>
41  * </ol>
42  *
43  * @author Martin Koehler - Initial contribution
44  */
45 public class DwdWarningsDataTest {
46
47     private TestDataProvider testDataProvider;
48     private DwdWarningsData warningsData;
49
50     @BeforeEach
51     public void setUp() throws IOException {
52         this.testDataProvider = new TestDataProvider();
53         loadXmlFromFile();
54         warningsData = new DwdWarningsData("");
55         warningsData.setDataAccess(testDataProvider);
56         warningsData.refresh();
57     }
58
59     @Test
60     public void testGetHeadline() {
61         assertThat(warningsData.getHeadline(0), is("Amtliche WARNUNG vor STURMBÖEN"));
62         assertThat(warningsData.getHeadline(1), is("Amtliche WARNUNG vor WINDBÖEN"));
63         assertThat(warningsData.getHeadline(2), is(UnDefType.NULL));
64     }
65
66     @Test
67     public void testGetSeverity() {
68         assertThat(warningsData.getSeverity(0), is("Moderate"));
69         assertThat(warningsData.getSeverity(1), is("Minor"));
70         assertThat(warningsData.getSeverity(2), is(UnDefType.NULL));
71     }
72
73     @Test
74     public void testGetEvent() {
75         assertThat(warningsData.getEvent(0), is("STURMBÖEN"));
76         assertThat(warningsData.getEvent(1), is("WINDBÖEN"));
77         assertThat(warningsData.getEvent(2), is(UnDefType.NULL));
78     }
79
80     @Test
81     public void testGetDescription() {
82         assertThat(warningsData.getDescription(0), is(
83                 "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."));
84         assertThat(warningsData.getDescription(1), is(
85                 "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."));
86         assertThat(warningsData.getDescription(2), is(UnDefType.NULL));
87     }
88
89     @Test
90     public void testGetAltitude() {
91         assertThat(warningsData.getAltitude(0).format("%.0f ft"), is("0 ft"));
92         assertThat(warningsData.getAltitude(1).format("%.0f ft"), is("0 ft"));
93         assertThat(warningsData.getAltitude(2), is(UnDefType.NULL));
94     }
95
96     @Test
97     public void testGetCeiling() {
98         assertThat(warningsData.getCeiling(0).format("%.0f ft"), is("9843 ft"));
99         assertThat(warningsData.getCeiling(1).format("%.0f ft"), is("9843 ft"));
100         assertThat(warningsData.getCeiling(2), is(UnDefType.NULL));
101     }
102
103     @Test
104     public void testGetExpires() {
105         // Conversion is needed as getExpires returns the Date with the System Default Zone
106         assertThat(((DateTimeType) warningsData.getExpires(0)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
107                 .toString(), is("2018-12-22T18:00Z[UTC]"));
108         assertThat(((DateTimeType) warningsData.getExpires(1)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
109                 .toString(), is("2018-12-23T01:00Z[UTC]"));
110         assertThat(warningsData.getExpires(2), is(UnDefType.NULL));
111     }
112
113     @Test
114     public void testGetOnset() {
115         // Conversion is needed as getOnset returns the Date with the System Default Zone
116         assertThat(((DateTimeType) warningsData.getOnset(0)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
117                 .toString(), is("2018-12-21T10:00Z[UTC]"));
118         assertThat(((DateTimeType) warningsData.getOnset(1)).getZonedDateTime().withZoneSameInstant(ZoneId.of("UTC"))
119                 .toString(), is("2018-12-22T18:00Z[UTC]"));
120         assertThat(warningsData.getOnset(2), is(UnDefType.NULL));
121     }
122
123     @Test
124     public void testGetEffective() {
125         // Conversion is needed as getEffective returns the Date with the System Default Zone
126         assertThat(((DateTimeType) warningsData.getEffective(0)).getZonedDateTime()
127                 .withZoneSameInstant(ZoneId.of("UTC")).toString(), is("2018-12-22T03:02Z[UTC]"));
128         assertThat(((DateTimeType) warningsData.getEffective(1)).getZonedDateTime()
129                 .withZoneSameInstant(ZoneId.of("UTC")).toString(), is("2018-12-22T10:15Z[UTC]"));
130         assertThat(warningsData.getEffective(2), is(UnDefType.NULL));
131     }
132
133     @Test
134     public void testGetWarning() {
135         assertThat(warningsData.getWarning(0), is(OnOffType.ON));
136         assertThat(warningsData.getWarning(1), is(OnOffType.ON));
137         assertThat(warningsData.getWarning(2), is(OnOffType.OFF));
138     }
139
140     @Test
141     public void testisNew() {
142         assertThat(warningsData.isNew(0), is(true));
143         assertThat(warningsData.isNew(1), is(true));
144         assertThat(warningsData.isNew(2), is(false));
145         // No longer new
146         assertThat(warningsData.isNew(0), is(false));
147         assertThat(warningsData.isNew(1), is(false));
148         assertThat(warningsData.isNew(2), is(false));
149     }
150
151     private void loadXmlFromFile() throws IOException {
152         InputStream stream = getClass().getResourceAsStream("warnings.xml");
153         BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
154         String line = null;
155
156         StringWriter stringWriter = new StringWriter();
157         while ((line = reader.readLine()) != null) {
158             stringWriter.write(line);
159         }
160         reader.close();
161         testDataProvider.rawData = stringWriter.toString();
162     }
163
164     private class TestDataProvider extends DwdWarningDataAccess {
165
166         private String rawData = "";
167
168         @Override
169         public String getDataFromEndpoint(String cellId) {
170             return rawData;
171         }
172     }
173 }