[deutschebahn] Fixed timing issue in test case (#12404)

Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
This commit is contained in:
Sönke Küper 2022-03-04 08:48:20 +01:00 committed by GitHub
parent 84687674d2
commit b209d9a51a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 29 deletions

View File

@ -50,7 +50,7 @@ public class DeutscheBahnHandlerFactory extends BaseThingHandlerFactory {
final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (TIMETABLE_TYPE.equals(thingTypeUID)) {
return new DeutscheBahnTimetableHandler((Bridge) thing, TimetablesV1Impl::new, Date::new);
return new DeutscheBahnTimetableHandler((Bridge) thing, TimetablesV1Impl::new, Date::new, null);
} else if (TRAIN_TYPE.equals(thingTypeUID)) {
return new DeutscheBahnTrainHandler(thing);
}

View File

@ -20,6 +20,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
@ -108,9 +109,11 @@ public class DeutscheBahnTimetableHandler extends BaseBridgeHandler {
private final Logger logger = LoggerFactory.getLogger(DeutscheBahnTimetableHandler.class);
private @Nullable TimetableLoader loader;
private TimetablesV1ApiFactory timetablesV1ApiFactory;
private final TimetablesV1ApiFactory timetablesV1ApiFactory;
private Supplier<Date> currentTimeProvider;
private final Supplier<Date> currentTimeProvider;
private final ScheduledExecutorService executorService;
/**
* Creates an new {@link DeutscheBahnTimetableHandler}.
@ -118,10 +121,12 @@ public class DeutscheBahnTimetableHandler extends BaseBridgeHandler {
public DeutscheBahnTimetableHandler( //
final Bridge bridge, //
final TimetablesV1ApiFactory timetablesV1ApiFactory, //
final Supplier<Date> currentTimeProvider) {
final Supplier<Date> currentTimeProvider, //
@Nullable final ScheduledExecutorService executorService) {
super(bridge);
this.timetablesV1ApiFactory = timetablesV1ApiFactory;
this.currentTimeProvider = currentTimeProvider;
this.executorService = executorService == null ? this.scheduler : executorService;
}
private List<TimetableStop> loadTimetable() {
@ -178,7 +183,7 @@ public class DeutscheBahnTimetableHandler extends BaseBridgeHandler {
this.updateStatus(ThingStatus.UNKNOWN);
this.scheduler.execute(() -> {
this.executorService.execute(() -> {
this.updateChannels();
this.restartJob();
});
@ -204,7 +209,7 @@ public class DeutscheBahnTimetableHandler extends BaseBridgeHandler {
try {
this.stopUpdateJob();
if (this.getThing().getStatus() == ThingStatus.ONLINE) {
this.updateJob = this.scheduler.scheduleWithFixedDelay(//
this.updateJob = this.executorService.scheduleWithFixedDelay(//
this::updateChannels, //
0L, //
UPDATE_INTERVAL_SECONDS, //

View File

@ -19,10 +19,12 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.openhab.binding.deutschebahn.internal.timetable.TimeproviderStub;
import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1ApiFactory;
import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1ApiStub;
@ -38,7 +40,6 @@ import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
/**
@ -47,7 +48,6 @@ import org.openhab.core.types.UnDefType;
* @author Sönke Küper - initial contribution.
*/
@NonNullByDefault
@Disabled("https://github.com/openhab/openhab-addons/issues/12235")
public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHelper {
private static Configuration createConfig() {
@ -84,11 +84,18 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
private DeutscheBahnTimetableHandler createAndInitHandler( //
final ThingHandlerCallback callback, //
final Bridge bridge, //
final TimetablesV1ApiFactory apiFactory) throws Exception { //
final TimetablesV1ApiFactory apiFactory) { //
final TimeproviderStub timeProvider = new TimeproviderStub();
timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 30);
final DeutscheBahnTimetableHandler handler = new DeutscheBahnTimetableHandler(bridge, apiFactory, timeProvider);
final ScheduledExecutorService executorStub = Mockito.mock(ScheduledExecutorService.class);
doAnswer((InvocationOnMock invocation) -> {
((Runnable) invocation.getArguments()[0]).run();
return null;
}).when(executorStub).execute(any(Runnable.class));
final DeutscheBahnTimetableHandler handler = new DeutscheBahnTimetableHandler(bridge, apiFactory, timeProvider,
executorStub);
handler.setCallback(callback);
handler.initialize();
return handler;
@ -103,8 +110,7 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
try {
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
verify(callback, timeout(1000)).statusUpdated(eq(bridge),
argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
verifyThingUpdated(bridge, 0, "-5296516961807204721-2108160906-5");
verifyThingUpdated(bridge, 1, "-8364795265993682073-2108160911-6");
@ -117,12 +123,11 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
private void verifyThingUpdated(final Bridge bridge, int offset, String stopId) {
final Thing train = bridge.getThings().get(offset);
final DeutscheBahnTrainHandler childHandler = (DeutscheBahnTrainHandler) train.getHandler();
verify(childHandler, timeout(1000))
.updateChannels(argThat((TimetableStop stop) -> stop.getId().equals(stopId)));
verify(childHandler).updateChannels(argThat((TimetableStop stop) -> stop.getId().equals(stopId)));
}
@Test
public void testUpdateTrainsToUndefinedIfNoDataWasProvided() throws Exception {
public void testUpdateTrainsToUndefinedIfNoDataWasProvided() {
final Bridge bridge = mockBridge();
final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
@ -133,28 +138,26 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
try {
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
verify(callback, timeout(1000)).statusUpdated(eq(bridge),
argThat(arg -> arg.getStatus().equals(ThingStatus.OFFLINE)));
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.OFFLINE)));
verifyChannelsUpdatedToUndef(bridge, 0, callback, UnDefType.UNDEF);
verifyChannelsUpdatedToUndef(bridge, 1, callback, UnDefType.UNDEF);
verifyChannelsUpdatedToUndef(bridge, 2, callback, UnDefType.UNDEF);
verifyChannelsUpdatedToUndef(bridge, 0, callback);
verifyChannelsUpdatedToUndef(bridge, 1, callback);
verifyChannelsUpdatedToUndef(bridge, 2, callback);
} finally {
handler.dispose();
}
}
private static void verifyChannelsUpdatedToUndef(Bridge bridge, int offset, ThingHandlerCallback callback,
State expectedState) {
private static void verifyChannelsUpdatedToUndef(Bridge bridge, int offset, ThingHandlerCallback callback) {
final Thing thing = bridge.getThings().get(offset);
for (Channel channel : thing.getChannels()) {
verify(callback).stateUpdated(eq(channel.getUID()), eq(expectedState));
verify(callback).stateUpdated(eq(channel.getUID()), eq(UnDefType.UNDEF));
}
}
@Test
public void testUpdateTrainsToUndefinedIfNotEnoughDataWasProvided() throws Exception {
public void testUpdateTrainsToUndefinedIfNotEnoughDataWasProvided() {
final Bridge bridge = mockBridge();
final ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
@ -175,12 +178,11 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
try {
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
verify(callback, timeout(1000)).statusUpdated(eq(bridge),
argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
verifyThingUpdated(bridge, 0, stop01.getId());
verifyChannelsUpdatedToUndef(bridge, 1, callback, UnDefType.UNDEF);
verifyChannelsUpdatedToUndef(bridge, 2, callback, UnDefType.UNDEF);
verifyChannelsUpdatedToUndef(bridge, 1, callback);
verifyChannelsUpdatedToUndef(bridge, 2, callback);
} finally {
handler.dispose();