]> git.basschouten.com Git - openhab-addons.git/blob
1f11a0891b56cd166d24e4aa87a94a1d26526a30
[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.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.GregorianCalendar;
22 import java.util.List;
23 import java.util.function.Consumer;
24
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;
36
37 /**
38  * Tests Mapping from {@link Event} attribute values to openhab state values.
39  * 
40  * @author Sönke Küper - initial contribution.
41  */
42 @NonNullByDefault
43 @SuppressWarnings("unchecked")
44 public class EventAttributeTest {
45
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";
47
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()));
62
63         // Create an event and set the attribute value.
64         final Event eventWithValueSet = new Event();
65         setValue.accept(eventWithValueSet);
66
67         // then try get value and state.
68         assertThat(attribute.getValue(eventWithValueSet), is(expectedValue));
69         assertThat(attribute.getState(eventWithValueSet), is(expectedState));
70
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));
76         }
77     }
78
79     @Test
80     public void testGetNonExistingChannel() {
81         assertThat(EventAttribute.getByChannelName("unkownChannel", EventType.ARRIVAL), is(nullValue()));
82     }
83
84     @Test
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);
88     }
89
90     @Test
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);
94     }
95
96     @Test
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);
101     }
102
103     @Test
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);
108     }
109
110     @Test
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);
115     }
116
117     @Test
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);
122     }
123
124     @Test
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);
129     }
130
131     @Test
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);
136     }
137
138     @Test
139     public void testLine() {
140         String line = "RE60";
141         doTestEventAttribute("line", null, (Event e) -> e.setL(line), line, new StringType(line), EventType.DEPARTURE,
142                 true);
143     }
144
145     @Test
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);
153     }
154
155     @Test
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);
163     }
164
165     @Test
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);
173     }
174
175     @Test
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);
180     }
181
182     @Test
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);
187     }
188
189     @Test
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);
193     }
194
195     @Test
196     public void testDistantChange() {
197         doTestEventAttribute("distant-change", null, (Event e) -> e.setDc(42), 42, new DecimalType(42),
198                 EventType.DEPARTURE, true);
199     }
200
201     @Test
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);
207     }
208
209     @Test
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);
215     }
216
217     @Test
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);
227     }
228
229     @Test
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);
239     }
240
241     @Test
242     public void testMessages() {
243         String expectedOneMessage = "Verzögerungen im Betriebsablauf";
244         List<Message> messages = new ArrayList<>();
245         Message m1 = new Message();
246         m1.setC(99);
247         messages.add(m1);
248         doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
249                 new StringType(expectedOneMessage), EventType.DEPARTURE, true);
250
251         String expectedTwoMessages = "Verzögerungen im Betriebsablauf - keine Qualitätsmängel";
252         Message m2 = new Message();
253         m2.setC(88);
254         messages.add(m2);
255         doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
256                 new StringType(expectedTwoMessages), EventType.DEPARTURE, true);
257     }
258
259     @Test
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();
264         m1.setC(80);
265         messages.add(m1);
266         Message m2 = new Message();
267         m2.setC(80);
268         messages.add(m2);
269         Message m3 = new Message();
270         m3.setC(36);
271         messages.add(m3);
272         Message m4 = new Message();
273         m4.setC(80);
274         messages.add(m4);
275         Message m5 = new Message();
276         m5.setC(84);
277         messages.add(m5);
278
279         doTestEventAttribute("messages", null, (Event e) -> e.getM().addAll(messages), messages,
280                 new StringType(expectedOneMessage), EventType.DEPARTURE, true);
281     }
282 }