2 * Copyright (c) 2010-2020 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.data;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.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.Before;
27 import org.junit.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 DwdWarningsData}
35 * Uses the warnings.xml from the resources directory instead of accessing the api endpoint.
37 * The XML has 2 Entries:
39 * <li>Amtliche WARNUNG vor WINDBÖEN, MINOR</li>
40 * <li>Amtliche WARNUNG vor STURMBÖEN, MODERATE</li>
43 * @author Martin Koehler - Initial contribution
45 public class DwdWarningsDataTest {
47 private TestDataProvider testDataProvider;
48 private DwdWarningsData warningsData;
51 public void setUp() throws IOException {
52 this.testDataProvider = new TestDataProvider();
54 warningsData = new DwdWarningsData("");
55 warningsData.setDataAccess(testDataProvider);
56 warningsData.refresh();
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
146 assertThat(warningsData.isNew(0), is(false));
147 assertThat(warningsData.isNew(1), is(false));
148 assertThat(warningsData.isNew(2), is(false));
151 private void loadXmlFromFile() throws IOException {
152 InputStream stream = getClass().getResourceAsStream("warnings.xml");
153 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
156 StringWriter stringWriter = new StringWriter();
157 while ((line = reader.readLine()) != null) {
158 stringWriter.write(line);
161 testDataProvider.rawData = stringWriter.toString();
164 private class TestDataProvider extends DwdWarningDataAccess {
166 private String rawData = "";
169 public String getDataFromEndpoint(String cellId) {