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.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
18 import java.util.ArrayList;
19 import java.util.Calendar;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import java.util.concurrent.ScheduledExecutorService;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.Mockito;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.openhab.binding.deutschebahn.internal.timetable.TimeproviderStub;
29 import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1ApiFactory;
30 import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1ApiStub;
31 import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1Impl.HttpCallable;
32 import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1ImplTestHelper;
33 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
34 import org.openhab.binding.deutschebahn.internal.timetable.dto.Timetable;
35 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.Channel;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingUID;
42 import org.openhab.core.thing.binding.ThingHandlerCallback;
43 import org.openhab.core.types.UnDefType;
46 * Tests for {@link DeutscheBahnTimetableHandler}.
48 * @author Sönke Küper - initial contribution.
51 public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHelper {
53 private static Configuration createConfig() {
54 final Configuration config = new Configuration();
55 config.put("accessToken", "letMeIn");
56 config.put("evaNo", "8000226");
57 config.put("trainFilter", "all");
61 private static Bridge mockBridge() {
62 final Bridge bridge = mock(Bridge.class);
63 when(bridge.getUID()).thenReturn(new ThingUID(DeutscheBahnBindingConstants.TIMETABLE_TYPE, "timetable"));
64 when(bridge.getConfiguration()).thenReturn(createConfig());
66 final List<Thing> things = new ArrayList<>();
67 things.add(DeutscheBahnTrainHandlerTest.mockThing(1));
68 things.add(DeutscheBahnTrainHandlerTest.mockThing(2));
69 things.add(DeutscheBahnTrainHandlerTest.mockThing(3));
70 when(things.get(0).getHandler()).thenReturn(mock(DeutscheBahnTrainHandler.class));
71 when(things.get(1).getHandler()).thenReturn(mock(DeutscheBahnTrainHandler.class));
72 when(things.get(2).getHandler()).thenReturn(mock(DeutscheBahnTrainHandler.class));
74 when(bridge.getThings()).thenReturn(things);
79 private DeutscheBahnTimetableHandler createAndInitHandler(final ThingHandlerCallback callback, final Bridge bridge)
81 return createAndInitHandler(callback, bridge, createApiWithTestdata().getApiFactory());
84 private DeutscheBahnTimetableHandler createAndInitHandler( //
85 final ThingHandlerCallback callback, //
86 final Bridge bridge, //
87 final TimetablesV1ApiFactory apiFactory) { //
88 final TimeproviderStub timeProvider = new TimeproviderStub();
89 timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 30);
91 final ScheduledExecutorService executorStub = Mockito.mock(ScheduledExecutorService.class);
92 doAnswer((InvocationOnMock invocation) -> {
93 ((Runnable) invocation.getArguments()[0]).run();
95 }).when(executorStub).execute(any(Runnable.class));
97 final DeutscheBahnTimetableHandler handler = new DeutscheBahnTimetableHandler(bridge, apiFactory, timeProvider,
99 handler.setCallback(callback);
100 handler.initialize();
105 public void testUpdateChannels() throws Exception {
106 final Bridge bridge = mockBridge();
107 final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
109 final DeutscheBahnTimetableHandler handler = createAndInitHandler(callback, bridge);
112 verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
113 verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
115 verifyThingUpdated(bridge, 0, "-5296516961807204721-2108160906-5");
116 verifyThingUpdated(bridge, 1, "-8364795265993682073-2108160911-6");
117 verifyThingUpdated(bridge, 2, "-2949440726131702047-2108160858-10");
123 private void verifyThingUpdated(final Bridge bridge, int offset, String stopId) {
124 final Thing train = bridge.getThings().get(offset);
125 final DeutscheBahnTrainHandler childHandler = (DeutscheBahnTrainHandler) train.getHandler();
126 verify(childHandler).updateChannels(argThat((TimetableStop stop) -> stop.getId().equals(stopId)));
130 public void testUpdateTrainsToUndefinedIfNoDataWasProvided() {
131 final Bridge bridge = mockBridge();
132 final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
134 final TimetablesV1ApiStub stubWithError = TimetablesV1ApiStub.createWithException();
136 final DeutscheBahnTimetableHandler handler = createAndInitHandler(callback, bridge,
137 (String authToken, HttpCallable httpCallable) -> stubWithError);
140 verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
141 verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.OFFLINE)));
143 verifyChannelsUpdatedToUndef(bridge, 0, callback);
144 verifyChannelsUpdatedToUndef(bridge, 1, callback);
145 verifyChannelsUpdatedToUndef(bridge, 2, callback);
152 private static void verifyChannelsUpdatedToUndef(Bridge bridge, int offset, ThingHandlerCallback callback) {
153 final Thing thing = bridge.getThings().get(offset);
154 for (Channel channel : thing.getChannels()) {
155 verify(callback).stateUpdated(eq(channel.getUID()), eq(UnDefType.UNDEF));
160 public void testUpdateTrainsToUndefinedIfNotEnoughDataWasProvided() {
161 final Bridge bridge = mockBridge();
162 final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
164 // Bridge contains 3 trains, but Timetable contains only 1 items, so two trains has to be updated to undef
166 final Timetable timetable = new Timetable();
167 TimetableStop stop01 = new TimetableStop();
168 stop01.setId("stop01id");
169 Event dp = new Event();
170 dp.setPt("2108161000");
172 timetable.getS().add(stop01);
174 final TimetablesV1ApiStub stubWithData = TimetablesV1ApiStub.createWithResult(timetable);
176 final DeutscheBahnTimetableHandler handler = createAndInitHandler(callback, bridge,
177 (String authToken, HttpCallable httpCallable) -> stubWithData);
180 verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
181 verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
183 verifyThingUpdated(bridge, 0, stop01.getId());
184 verifyChannelsUpdatedToUndef(bridge, 1, callback);
185 verifyChannelsUpdatedToUndef(bridge, 2, callback);