]> git.basschouten.com Git - openhab-addons.git/blob
7cf2e70e9010d9ced105c2b97676951daf6f2fed
[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.deutschebahn.internal;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17
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;
25
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;
37
38 /**
39  * Tests Mapping from {@link Event} attribute values to openhab state values.
40  * 
41  * @author Sönke Küper - initial contribution.
42  */
43 @NonNullByDefault
44 @SuppressWarnings("unchecked")
45 public class EventAttributeTest {
46
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";
48
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()));
63
64         // Create an event and set the attribute value.
65         final Event eventWithValueSet = new Event();
66         setValue.accept(eventWithValueSet);
67
68         // then try get value and state.
69         assertThat(attribute.getValue(eventWithValueSet), is(expectedValue));
70         assertThat(attribute.getState(eventWithValueSet), is(expectedState));
71
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));
77         }
78     }
79
80     @Test
81     public void testGetNonExistingChannel() {
82         assertThat(EventAttribute.getByChannelName("unkownChannel", EventType.ARRIVAL), is(nullValue()));
83     }
84
85     @Test
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);
89     }
90
91     @Test
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);
95     }
96
97     @Test
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);
102     }
103
104     @Test
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);
109     }
110
111     @Test
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);
116     }
117
118     @Test
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);
123     }
124
125     @Test
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);
130     }
131
132     @Test
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);
137     }
138
139     @Test
140     public void testLine() {
141         String line = "RE60";
142         doTestEventAttribute("line", null, (Event e) -> e.setL(line), line, new StringType(line), EventType.DEPARTURE,
143                 true);
144     }
145
146     @Test
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);
154     }
155
156     @Test
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);
164     }
165
166     @Test
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);
174     }
175
176     @Test
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);
181     }
182
183     @Test
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);
188     }
189
190     @Test
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);
194     }
195
196     @Test
197     public void testDistantChange() {
198         doTestEventAttribute("distant-change", null, (Event e) -> e.setDc(42), 42, new DecimalType(42),
199                 EventType.DEPARTURE, true);
200     }
201
202     @Test
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);
208     }
209
210     @Test
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);
216     }
217
218     @Test
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);
232     }
233
234     @Test
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);
248     }
249
250     @Test
251     public void testMessages() {
252         String expectedOneMessage = "Verzögerungen im Betriebsablauf";
253         List<Message> messages = new ArrayList<>();
254         Message m1 = new Message();
255         m1.setC(99);
256         messages.add(m1);
257         doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
258                 new StringType(expectedOneMessage), EventType.DEPARTURE, true);
259
260         String expectedTwoMessages = "Verzögerungen im Betriebsablauf - keine Qualitätsmängel";
261         Message m2 = new Message();
262         m2.setC(88);
263         messages.add(m2);
264         doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
265                 new StringType(expectedTwoMessages), EventType.DEPARTURE, true);
266     }
267
268     @Test
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();
273         m1.setC(80);
274         messages.add(m1);
275         Message m2 = new Message();
276         m2.setC(80);
277         messages.add(m2);
278         Message m3 = new Message();
279         m3.setC(36);
280         messages.add(m3);
281         Message m4 = new Message();
282         m4.setC(80);
283         messages.add(m4);
284         Message m5 = new Message();
285         m5.setC(84);
286         messages.add(m5);
287
288         doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
289                 new StringType(expectedOneMessage), EventType.DEPARTURE, true);
290     }
291 }