]> git.basschouten.com Git - openhab-addons.git/blob
dd856c30cfc22de4b874fbf6cfc280dc5fdc3e71
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.timetable;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17 import static org.junit.jupiter.api.Assertions.assertEquals;
18
19 import java.util.Calendar;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.deutschebahn.internal.EventType;
26 import org.openhab.binding.deutschebahn.internal.TimetableStopFilter;
27 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
28
29 /**
30  * Tests for the {@link TimetableLoader}.
31  * 
32  * @author Sönke Küper - initial contribution
33  */
34 @NonNullByDefault
35 public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
36
37     @Test
38     public void testLoadRequiredStopCount() throws Exception {
39         final TimetablesApiTestModule timeTableTestModule = this.createApiWithTestdata();
40         final TimeproviderStub timeProvider = new TimeproviderStub();
41         final TimetableLoader loader = new TimetableLoader(timeTableTestModule.getApi(), TimetableStopFilter.ALL,
42                 EventType.DEPARTURE, timeProvider, EVA_LEHRTE, 20);
43
44         timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 30);
45
46         final List<TimetableStop> stops = loader.getTimetableStops();
47         assertThat(timeTableTestModule.getRequestedPlanUrls(),
48                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09",
49                         "https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/10",
50                         "https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/11"));
51         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
52                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
53         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
54
55         assertThat(stops, hasSize(21));
56         assertEquals("-5296516961807204721-2108160906-5", stops.get(0).getId());
57         assertEquals("-3222259045572671319-2108161155-1", stops.get(20).getId());
58
59         // when requesting again no further call to plan is made, because required stops are available.
60         final List<TimetableStop> stops02 = loader.getTimetableStops();
61         assertThat(stops02, hasSize(21));
62         assertThat(timeTableTestModule.getRequestedPlanUrls(), hasSize(3));
63         assertThat(timeTableTestModule.getRequestedFullChangesUrls(), hasSize(1));
64         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
65     }
66
67     @Test
68     public void testLoadNewDataIfRequired() throws Exception {
69         final TimetablesApiTestModule timeTableTestModule = this.createApiWithTestdata();
70         final TimeproviderStub timeProvider = new TimeproviderStub();
71         final TimetableLoader loader = new TimetableLoader(timeTableTestModule.getApi(), TimetableStopFilter.ALL,
72                 EventType.DEPARTURE, timeProvider, EVA_LEHRTE, 8);
73
74         timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 0);
75
76         final List<TimetableStop> stops = loader.getTimetableStops();
77         assertThat(timeTableTestModule.getRequestedPlanUrls(),
78                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
79         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
80                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
81         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
82
83         assertThat(stops, hasSize(8));
84         assertEquals("1763676552526687479-2108160847-6", stops.get(0).getId());
85         assertEquals("8681599812964340829-2108160955-1", stops.get(7).getId());
86
87         // Move clock ahead for 30 minutes, so that some of the fetched data is in past and new plan data must be
88         // requested
89         timeProvider.moveAhead(30 * 60);
90
91         final List<TimetableStop> stops02 = loader.getTimetableStops();
92         assertThat(stops02, hasSize(13));
93         assertThat(timeTableTestModule.getRequestedPlanUrls(),
94                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09",
95                         "https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/10"));
96         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
97                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226",
98                         "https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
99         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
100
101         assertEquals("-5296516961807204721-2108160906-5", stops02.get(0).getId());
102         assertEquals("-3376513334056532423-2108161055-1", stops02.get(12).getId());
103     }
104
105     @Test
106     public void testRequestUpdates() throws Exception {
107         final TimetablesApiTestModule timeTableTestModule = this.createApiWithTestdata();
108         final TimeproviderStub timeProvider = new TimeproviderStub();
109         final TimetableLoader loader = new TimetableLoader(timeTableTestModule.getApi(), TimetableStopFilter.ALL,
110                 EventType.DEPARTURE, timeProvider, EVA_LEHRTE, 1);
111
112         timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 30);
113
114         // First call - plan and full changes are requested.
115         loader.getTimetableStops();
116         assertThat(timeTableTestModule.getRequestedPlanUrls(),
117                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
118         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
119                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
120         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
121
122         // Changes are updated only every 30 seconds, so move clock ahead 20 seconds, no request is made
123         timeProvider.moveAhead(20);
124         loader.getTimetableStops();
125
126         assertThat(timeTableTestModule.getRequestedPlanUrls(),
127                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
128         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
129                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
130         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
131
132         // Move ahead 10 seconds, so recent changes are fetched
133         timeProvider.moveAhead(10);
134         loader.getTimetableStops();
135         assertThat(timeTableTestModule.getRequestedPlanUrls(),
136                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
137         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
138                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
139         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(),
140                 contains("https://api.deutschebahn.com/timetables/v1/rchg/8000226"));
141
142         // Move again ahead 30 seconds, recent changes are fetched again
143         timeProvider.moveAhead(30);
144         loader.getTimetableStops();
145         assertThat(timeTableTestModule.getRequestedPlanUrls(),
146                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
147         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
148                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
149         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(),
150                 contains("https://api.deutschebahn.com/timetables/v1/rchg/8000226",
151                         "https://api.deutschebahn.com/timetables/v1/rchg/8000226"));
152
153         // If recent change were not updated last 120 seconds the full changes must be requested
154         timeProvider.moveAhead(120);
155         loader.getTimetableStops();
156         assertThat(timeTableTestModule.getRequestedPlanUrls(),
157                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
158         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
159                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226",
160                         "https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
161         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(),
162                 contains("https://api.deutschebahn.com/timetables/v1/rchg/8000226",
163                         "https://api.deutschebahn.com/timetables/v1/rchg/8000226"));
164     }
165
166     @Test
167     public void testReturnOnlyArrivals() throws Exception {
168         final TimetablesApiTestModule timeTableTestModule = this.createApiWithTestdata();
169         final TimeproviderStub timeProvider = new TimeproviderStub();
170         final TimetableLoader loader = new TimetableLoader(timeTableTestModule.getApi(), TimetableStopFilter.ARRIVALS,
171                 EventType.ARRIVAL, timeProvider, EVA_LEHRTE, 20);
172
173         // Simulate that only one url is available
174         timeTableTestModule.addAvailableUrl("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09");
175
176         timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 0);
177
178         final List<TimetableStop> stops = loader.getTimetableStops();
179
180         // File contains 8 stops, but 2 are only departures
181         assertThat(stops, hasSize(6));
182         assertEquals("1763676552526687479-2108160847-6", stops.get(0).getId());
183         assertEquals("-735649762452915464-2108160912-6", stops.get(5).getId());
184     }
185
186     @Test
187     public void testReturnOnlyDepartures() throws Exception {
188         final TimetablesApiTestModule timeTableTestModule = this.createApiWithTestdata();
189         final TimeproviderStub timeProvider = new TimeproviderStub();
190         final TimetableLoader loader = new TimetableLoader(timeTableTestModule.getApi(), TimetableStopFilter.DEPARTURES,
191                 EventType.DEPARTURE, timeProvider, EVA_LEHRTE, 20);
192
193         // Simulate that only one url is available
194         timeTableTestModule.addAvailableUrl("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09");
195
196         timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 0);
197
198         final List<TimetableStop> stops = loader.getTimetableStops();
199
200         // File contains 8 stops, but 2 are only arrivals
201         assertThat(stops, hasSize(6));
202         assertEquals("-94442819435724762-2108160916-1", stops.get(0).getId());
203         assertEquals("8681599812964340829-2108160955-1", stops.get(5).getId());
204     }
205
206     @Test
207     public void testRemoveEntryOnlyIfChangedTimeIsInPast() throws Exception {
208         final TimetablesApiTestModule timeTableTestModule = this.createApiWithTestdata();
209         final TimeproviderStub timeProvider = new TimeproviderStub();
210         final TimetableLoader loader = new TimetableLoader(timeTableTestModule.getApi(), TimetableStopFilter.DEPARTURES,
211                 EventType.DEPARTURE, timeProvider, EVA_LEHRTE, 1);
212
213         timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 35);
214
215         final List<TimetableStop> stops = loader.getTimetableStops();
216         assertThat(timeTableTestModule.getRequestedPlanUrls(),
217                 contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
218         assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
219                 contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
220         assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
221
222         // Stop -5296516961807204721-2108160906-5 has its planned time at 9:34, but its included because its changed
223         // time is 9:42
224         assertThat(stops, hasSize(4));
225         assertEquals("-5296516961807204721-2108160906-5", stops.get(0).getId());
226         assertEquals("2108160942", stops.get(0).getDp().getCt());
227         assertEquals("8681599812964340829-2108160955-1", stops.get(3).getId());
228     }
229 }