2 * Copyright (c) 2010-2021 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.GregorianCalendar;
22 import java.util.List;
23 import java.util.function.Consumer;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
29 import org.openhab.binding.deutschebahn.internal.timetable.dto.EventStatus;
30 import org.openhab.binding.deutschebahn.internal.timetable.dto.Message;
31 import org.openhab.core.library.types.DateTimeType;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.StringType;
35 import org.openhab.core.types.State;
38 * Tests Mapping from {@link Event} attribute values to openhab state values.
40 * @author Sönke Küper - initial contribution.
43 @SuppressWarnings("unchecked")
44 public class EventAttributeTest {
46 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";
48 private <VALUE_TYPE, STATE_TYPE extends State> void doTestEventAttribute( //
49 String channelName, //
50 @Nullable String expectedChannelName, //
51 Consumer<Event> setValue, //
52 VALUE_TYPE expectedValue, //
53 @Nullable STATE_TYPE expectedState, //
54 EventType eventType, //
55 boolean performSetterTest) { //
56 final EventAttribute<VALUE_TYPE, STATE_TYPE> attribute = (EventAttribute<VALUE_TYPE, STATE_TYPE>) EventAttribute
57 .getByChannelName(channelName, eventType);
58 assertThat(attribute, is(not(nullValue())));
59 assertThat(attribute.getChannelTypeName(), is(expectedChannelName == null ? channelName : expectedChannelName));
60 assertThat(attribute.getValue(new Event()), is(nullValue()));
61 assertThat(attribute.getState(new Event()), is(nullValue()));
63 // Create an event and set the attribute value.
64 final Event eventWithValueSet = new Event();
65 setValue.accept(eventWithValueSet);
67 // then try get value and state.
68 assertThat(attribute.getValue(eventWithValueSet), is(expectedValue));
69 assertThat(attribute.getState(eventWithValueSet), is(expectedState));
71 // Try set Value in new Event
72 final Event copyTarget = new Event();
73 attribute.setValue(copyTarget, expectedValue);
74 if (performSetterTest) {
75 assertThat(attribute.getValue(copyTarget), is(expectedValue));
80 public void testGetNonExistingChannel() {
81 assertThat(EventAttribute.getByChannelName("unkownChannel", EventType.ARRIVAL), is(nullValue()));
85 public void testPlannedPath() {
86 doTestEventAttribute("planned-path", null, (Event e) -> e.setPpth(SAMPLE_PATH), SAMPLE_PATH,
87 new StringType(SAMPLE_PATH), EventType.DEPARTURE, true);
91 public void testChangedPath() {
92 doTestEventAttribute("changed-path", null, (Event e) -> e.setCpth(SAMPLE_PATH), SAMPLE_PATH,
93 new StringType(SAMPLE_PATH), EventType.DEPARTURE, true);
97 public void testPlannedPlatform() {
98 String platform = "2";
99 doTestEventAttribute("planned-platform", null, (Event e) -> e.setPp(platform), platform,
100 new StringType(platform), EventType.DEPARTURE, true);
104 public void testChangedPlatform() {
105 String platform = "2";
106 doTestEventAttribute("changed-platform", null, (Event e) -> e.setCp(platform), platform,
107 new StringType(platform), EventType.DEPARTURE, true);
111 public void testWings() {
112 String wings = "-906407760000782942-1403311431";
113 doTestEventAttribute("wings", null, (Event e) -> e.setWings(wings), wings, new StringType(wings),
114 EventType.DEPARTURE, true);
118 public void testTransition() {
119 String transition = "2016448009055686515-1403311438-1";
120 doTestEventAttribute("transition", null, (Event e) -> e.setTra(transition), transition,
121 new StringType(transition), EventType.DEPARTURE, true);
125 public void testPlannedDistantEndpoint() {
126 String endpoint = "Hannover Hbf";
127 doTestEventAttribute("planned-distant-endpoint", null, (Event e) -> e.setPde(endpoint), endpoint,
128 new StringType(endpoint), EventType.DEPARTURE, true);
132 public void testChangedDistantEndpoint() {
133 String endpoint = "Hannover Hbf";
134 doTestEventAttribute("changed-distant-endpoint", null, (Event e) -> e.setCde(endpoint), endpoint,
135 new StringType(endpoint), EventType.DEPARTURE, true);
139 public void testLine() {
140 String line = "RE60";
141 doTestEventAttribute("line", null, (Event e) -> e.setL(line), line, new StringType(line), EventType.DEPARTURE,
146 public void testPlannedTime() {
147 String time = "2109111825";
148 GregorianCalendar expectedValue = new GregorianCalendar(2021, 8, 11, 18, 25, 0);
149 DateTimeType expectedState = new DateTimeType(
150 ZonedDateTime.ofInstant(expectedValue.toInstant(), ZoneId.systemDefault()));
151 doTestEventAttribute("planned-time", null, (Event e) -> e.setPt(time), expectedValue.getTime(), expectedState,
152 EventType.DEPARTURE, true);
156 public void testChangedTime() {
157 String time = "2109111825";
158 GregorianCalendar expectedValue = new GregorianCalendar(2021, 8, 11, 18, 25, 0);
159 DateTimeType expectedState = new DateTimeType(
160 ZonedDateTime.ofInstant(expectedValue.toInstant(), ZoneId.systemDefault()));
161 doTestEventAttribute("changed-time", null, (Event e) -> e.setCt(time), expectedValue.getTime(), expectedState,
162 EventType.DEPARTURE, true);
166 public void testCancellationTime() {
167 String time = "2109111825";
168 GregorianCalendar expectedValue = new GregorianCalendar(2021, 8, 11, 18, 25, 0);
169 DateTimeType expectedState = new DateTimeType(
170 ZonedDateTime.ofInstant(expectedValue.toInstant(), ZoneId.systemDefault()));
171 doTestEventAttribute("cancellation-time", null, (Event e) -> e.setClt(time), expectedValue.getTime(),
172 expectedState, EventType.DEPARTURE, true);
176 public void testPlannedStatus() {
177 EventStatus expectedValue = EventStatus.A;
178 doTestEventAttribute("planned-status", null, (Event e) -> e.setPs(expectedValue), expectedValue,
179 new StringType(expectedValue.name().toLowerCase()), EventType.DEPARTURE, true);
183 public void testChangedStatus() {
184 EventStatus expectedValue = EventStatus.C;
185 doTestEventAttribute("changed-status", null, (Event e) -> e.setCs(expectedValue), expectedValue,
186 new StringType(expectedValue.name().toLowerCase()), EventType.DEPARTURE, true);
190 public void testHidden() {
191 doTestEventAttribute("hidden", null, (Event e) -> e.setHi(0), 0, OnOffType.OFF, EventType.DEPARTURE, true);
192 doTestEventAttribute("hidden", null, (Event e) -> e.setHi(1), 1, OnOffType.ON, EventType.DEPARTURE, true);
196 public void testDistantChange() {
197 doTestEventAttribute("distant-change", null, (Event e) -> e.setDc(42), 42, new DecimalType(42),
198 EventType.DEPARTURE, true);
202 public void testPlannedFinalStation() {
203 doTestEventAttribute("planned-final-station", "planned-target-station", (Event e) -> e.setPpth(SAMPLE_PATH),
204 "Lehrte", new StringType("Lehrte"), EventType.DEPARTURE, false);
205 doTestEventAttribute("planned-final-station", "planned-start-station", (Event e) -> e.setPpth(SAMPLE_PATH),
206 "Bielefeld Hbf", new StringType("Bielefeld Hbf"), EventType.ARRIVAL, false);
210 public void testChangedFinalStation() {
211 doTestEventAttribute("changed-final-station", "changed-target-station", (Event e) -> e.setCpth(SAMPLE_PATH),
212 "Lehrte", new StringType("Lehrte"), EventType.DEPARTURE, false);
213 doTestEventAttribute("changed-final-station", "changed-start-station", (Event e) -> e.setCpth(SAMPLE_PATH),
214 "Bielefeld Hbf", new StringType("Bielefeld Hbf"), EventType.ARRIVAL, false);
218 public void testPlannedIntermediateStations() {
219 String expectedFollowing = "Bielefeld Hbf - Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf";
220 doTestEventAttribute("planned-intermediate-stations", "planned-following-stations",
221 (Event e) -> e.setPpth(SAMPLE_PATH), expectedFollowing, new StringType(expectedFollowing),
222 EventType.DEPARTURE, false);
223 String expectedPrevious = "Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf - Lehrte";
224 doTestEventAttribute("planned-intermediate-stations", "planned-previous-stations",
225 (Event e) -> e.setPpth(SAMPLE_PATH), expectedPrevious, new StringType(expectedPrevious),
226 EventType.ARRIVAL, false);
230 public void testChangedIntermediateStations() {
231 String expectedFollowing = "Bielefeld Hbf - Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf";
232 doTestEventAttribute("changed-intermediate-stations", "changed-following-stations",
233 (Event e) -> e.setCpth(SAMPLE_PATH), expectedFollowing, new StringType(expectedFollowing),
234 EventType.DEPARTURE, false);
235 String expectedPrevious = "Herford - Löhne(Westf) - Bad Oeynhausen - Porta Westfalica - Minden(Westf) - Bückeburg - Stadthagen - Haste - Wunstorf - Hannover Hbf - Lehrte";
236 doTestEventAttribute("changed-intermediate-stations", "changed-previous-stations",
237 (Event e) -> e.setCpth(SAMPLE_PATH), expectedPrevious, new StringType(expectedPrevious),
238 EventType.ARRIVAL, false);
242 public void testMessages() {
243 String expectedOneMessage = "Verzögerungen im Betriebsablauf";
244 List<Message> messages = new ArrayList<>();
245 Message m1 = new Message();
248 doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
249 new StringType(expectedOneMessage), EventType.DEPARTURE, true);
251 String expectedTwoMessages = "Verzögerungen im Betriebsablauf - keine Qualitätsmängel";
252 Message m2 = new Message();
255 doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
256 new StringType(expectedTwoMessages), EventType.DEPARTURE, true);
260 public void testFilterDuplicateMessages() {
261 String expectedOneMessage = "andere Reihenfolge der Wagen - technische Störung am Zug - Zug verkehrt richtig gereiht";
262 List<Message> messages = new ArrayList<>();
263 Message m1 = new Message();
266 Message m2 = new Message();
269 Message m3 = new Message();
272 Message m4 = new Message();
275 Message m5 = new Message();
279 doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
280 new StringType(expectedOneMessage), EventType.DEPARTURE, true);