2 * Copyright (c) 2010-2022 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.deutschebahn.internal;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.GregorianCalendar;
23 import java.util.List;
24 import java.util.function.Consumer;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
30 import org.openhab.binding.deutschebahn.internal.timetable.dto.EventStatus;
31 import org.openhab.binding.deutschebahn.internal.timetable.dto.Message;
32 import org.openhab.core.library.types.DateTimeType;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.types.State;
39 * Tests Mapping from {@link Event} attribute values to openhab state values.
41 * @author Sönke Küper - initial contribution.
44 @SuppressWarnings("unchecked")
45 public class EventAttributeTest {
47 private static final String SAMPLE_PATH = "Bielefeld Hbf|Herford|Löhne(Westf)|Bad Oeynhausen|Porta Westfalica|Minden(Westf)|Bückeburg|Stadthagen|Haste|Wunstorf|Hannover Hbf|Lehrte";
49 private <VALUE_TYPE, STATE_TYPE extends State> void doTestEventAttribute( //
50 String channelName, //
51 @Nullable String expectedChannelName, //
52 Consumer<Event> setValue, //
53 VALUE_TYPE expectedValue, //
54 @Nullable STATE_TYPE expectedState, //
55 EventType eventType, //
56 boolean performSetterTest) { //
57 final EventAttribute<VALUE_TYPE, STATE_TYPE> attribute = (EventAttribute<VALUE_TYPE, STATE_TYPE>) EventAttribute
58 .getByChannelName(channelName, eventType);
59 assertThat(attribute, is(not(nullValue())));
60 assertThat(attribute.getChannelTypeName(), is(expectedChannelName == null ? channelName : expectedChannelName));
61 assertThat(attribute.getValue(new Event()), is(nullValue()));
62 assertThat(attribute.getState(new Event()), is(nullValue()));
64 // Create an event and set the attribute value.
65 final Event eventWithValueSet = new Event();
66 setValue.accept(eventWithValueSet);
68 // then try get value and state.
69 assertThat(attribute.getValue(eventWithValueSet), is(expectedValue));
70 assertThat(attribute.getState(eventWithValueSet), is(expectedState));
72 // Try set Value in new Event
73 final Event copyTarget = new Event();
74 attribute.setValue(copyTarget, expectedValue);
75 if (performSetterTest) {
76 assertThat(attribute.getValue(copyTarget), is(expectedValue));
81 public void testGetNonExistingChannel() {
82 assertThat(EventAttribute.getByChannelName("unkownChannel", EventType.ARRIVAL), is(nullValue()));
86 public void testPlannedPath() {
87 doTestEventAttribute("planned-path", null, (Event e) -> e.setPpth(SAMPLE_PATH), SAMPLE_PATH,
88 new StringType(SAMPLE_PATH), EventType.DEPARTURE, true);
92 public void testChangedPath() {
93 doTestEventAttribute("changed-path", null, (Event e) -> e.setCpth(SAMPLE_PATH), SAMPLE_PATH,
94 new StringType(SAMPLE_PATH), EventType.DEPARTURE, true);
98 public void testPlannedPlatform() {
99 String platform = "2";
100 doTestEventAttribute("planned-platform", null, (Event e) -> e.setPp(platform), platform,
101 new StringType(platform), EventType.DEPARTURE, true);
105 public void testChangedPlatform() {
106 String platform = "2";
107 doTestEventAttribute("changed-platform", null, (Event e) -> e.setCp(platform), platform,
108 new StringType(platform), EventType.DEPARTURE, true);
112 public void testWings() {
113 String wings = "-906407760000782942-1403311431";
114 doTestEventAttribute("wings", null, (Event e) -> e.setWings(wings), wings, new StringType(wings),
115 EventType.DEPARTURE, true);
119 public void testTransition() {
120 String transition = "2016448009055686515-1403311438-1";
121 doTestEventAttribute("transition", null, (Event e) -> e.setTra(transition), transition,
122 new StringType(transition), EventType.DEPARTURE, true);
126 public void testPlannedDistantEndpoint() {
127 String endpoint = "Hannover Hbf";
128 doTestEventAttribute("planned-distant-endpoint", null, (Event e) -> e.setPde(endpoint), endpoint,
129 new StringType(endpoint), EventType.DEPARTURE, true);
133 public void testChangedDistantEndpoint() {
134 String endpoint = "Hannover Hbf";
135 doTestEventAttribute("changed-distant-endpoint", null, (Event e) -> e.setCde(endpoint), endpoint,
136 new StringType(endpoint), EventType.DEPARTURE, true);
140 public void testLine() {
141 String line = "RE60";
142 doTestEventAttribute("line", null, (Event e) -> e.setL(line), line, new StringType(line), EventType.DEPARTURE,
147 public void testPlannedTime() {
148 String time = "2109111825";
149 GregorianCalendar expectedValue = new GregorianCalendar(2021, 8, 11, 18, 25, 0);
150 DateTimeType expectedState = new DateTimeType(
151 ZonedDateTime.ofInstant(expectedValue.toInstant(), ZoneId.systemDefault()));
152 doTestEventAttribute("planned-time", null, (Event e) -> e.setPt(time), expectedValue.getTime(), expectedState,
153 EventType.DEPARTURE, true);
157 public void testChangedTime() {
158 String time = "2109111825";
159 GregorianCalendar expectedValue = new GregorianCalendar(2021, 8, 11, 18, 25, 0);
160 DateTimeType expectedState = new DateTimeType(
161 ZonedDateTime.ofInstant(expectedValue.toInstant(), ZoneId.systemDefault()));
162 doTestEventAttribute("changed-time", null, (Event e) -> e.setCt(time), expectedValue.getTime(), expectedState,
163 EventType.DEPARTURE, true);
167 public void testCancellationTime() {
168 String time = "2109111825";
169 GregorianCalendar expectedValue = new GregorianCalendar(2021, 8, 11, 18, 25, 0);
170 DateTimeType expectedState = new DateTimeType(
171 ZonedDateTime.ofInstant(expectedValue.toInstant(), ZoneId.systemDefault()));
172 doTestEventAttribute("cancellation-time", null, (Event e) -> e.setClt(time), expectedValue.getTime(),
173 expectedState, EventType.DEPARTURE, true);
177 public void testPlannedStatus() {
178 EventStatus expectedValue = EventStatus.A;
179 doTestEventAttribute("planned-status", null, (Event e) -> e.setPs(expectedValue), expectedValue,
180 new StringType(expectedValue.name().toLowerCase()), EventType.DEPARTURE, true);
184 public void testChangedStatus() {
185 EventStatus expectedValue = EventStatus.C;
186 doTestEventAttribute("changed-status", null, (Event e) -> e.setCs(expectedValue), expectedValue,
187 new StringType(expectedValue.name().toLowerCase()), EventType.DEPARTURE, true);
191 public void testHidden() {
192 doTestEventAttribute("hidden", null, (Event e) -> e.setHi(0), 0, OnOffType.OFF, EventType.DEPARTURE, true);
193 doTestEventAttribute("hidden", null, (Event e) -> e.setHi(1), 1, OnOffType.ON, EventType.DEPARTURE, true);
197 public void testDistantChange() {
198 doTestEventAttribute("distant-change", null, (Event e) -> e.setDc(42), 42, new DecimalType(42),
199 EventType.DEPARTURE, true);
203 public void testPlannedFinalStation() {
204 doTestEventAttribute("planned-final-station", "planned-target-station", (Event e) -> e.setPpth(SAMPLE_PATH),
205 "Lehrte", new StringType("Lehrte"), EventType.DEPARTURE, false);
206 doTestEventAttribute("planned-final-station", "planned-start-station", (Event e) -> e.setPpth(SAMPLE_PATH),
207 "Bielefeld Hbf", new StringType("Bielefeld Hbf"), EventType.ARRIVAL, false);
211 public void testChangedFinalStation() {
212 doTestEventAttribute("changed-final-station", "changed-target-station", (Event e) -> e.setCpth(SAMPLE_PATH),
213 "Lehrte", new StringType("Lehrte"), EventType.DEPARTURE, false);
214 doTestEventAttribute("changed-final-station", "changed-start-station", (Event e) -> e.setCpth(SAMPLE_PATH),
215 "Bielefeld Hbf", new StringType("Bielefeld Hbf"), EventType.ARRIVAL, false);
219 public void testPlannedIntermediateStations() {
220 String expectedFollowing = "Bielefeld Hbf - Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf";
221 doTestEventAttribute("planned-intermediate-stations", "planned-following-stations",
222 (Event e) -> e.setPpth(SAMPLE_PATH),
223 Arrays.asList("Bielefeld Hbf", "Herford", "Löhne(Westf)", "Bad Oeynhausen", "Porta Westfalica",
224 "Minden(Westf)", "Bückeburg", "Stadthagen", "Haste", "Wunstorf", "Hannover Hbf"),
225 new StringType(expectedFollowing), EventType.DEPARTURE, false);
226 String expectedPrevious = "Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf - Lehrte";
227 doTestEventAttribute("planned-intermediate-stations", "planned-previous-stations",
228 (Event e) -> e.setPpth(SAMPLE_PATH),
229 Arrays.asList("Herford", "Löhne(Westf)", "Bad Oeynhausen", "Porta Westfalica", "Minden(Westf)",
230 "Bückeburg", "Stadthagen", "Haste", "Wunstorf", "Hannover Hbf", "Lehrte"),
231 new StringType(expectedPrevious), EventType.ARRIVAL, false);
235 public void testChangedIntermediateStations() {
236 String expectedFollowing = "Bielefeld Hbf - Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf";
237 doTestEventAttribute("changed-intermediate-stations", "changed-following-stations",
238 (Event e) -> e.setCpth(SAMPLE_PATH),
239 Arrays.asList("Bielefeld Hbf", "Herford", "Löhne(Westf)", "Bad Oeynhausen", "Porta Westfalica",
240 "Minden(Westf)", "Bückeburg", "Stadthagen", "Haste", "Wunstorf", "Hannover Hbf"),
241 new StringType(expectedFollowing), EventType.DEPARTURE, false);
242 String expectedPrevious = "Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf - Lehrte";
243 doTestEventAttribute("changed-intermediate-stations", "changed-previous-stations",
244 (Event e) -> e.setCpth(SAMPLE_PATH),
245 Arrays.asList("Herford", "Löhne(Westf)", "Bad Oeynhausen", "Porta Westfalica", "Minden(Westf)",
246 "Bückeburg", "Stadthagen", "Haste", "Wunstorf", "Hannover Hbf", "Lehrte"),
247 new StringType(expectedPrevious), EventType.ARRIVAL, false);
251 public void testMessages() {
252 String expectedOneMessage = "Verzögerungen im Betriebsablauf";
253 List<Message> messages = new ArrayList<>();
254 Message m1 = new Message();
257 doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
258 new StringType(expectedOneMessage), EventType.DEPARTURE, true);
260 String expectedTwoMessages = "Verzögerungen im Betriebsablauf - keine Qualitätsmängel";
261 Message m2 = new Message();
264 doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
265 new StringType(expectedTwoMessages), EventType.DEPARTURE, true);
269 public void testFilterDuplicateMessages() {
270 String expectedOneMessage = "andere Reihenfolge der Wagen - technische Störung am Zug - Zug verkehrt richtig gereiht";
271 List<Message> messages = new ArrayList<>();
272 Message m1 = new Message();
275 Message m2 = new Message();
278 Message m3 = new Message();
281 Message m4 = new Message();
284 Message m5 = new Message();
288 doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
289 new StringType(expectedOneMessage), EventType.DEPARTURE, true);