[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(); final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (TIMETABLE_TYPE.equals(thingTypeUID)) { 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)) { } else if (TRAIN_TYPE.equals(thingTypeUID)) {
return new DeutscheBahnTrainHandler(thing); return new DeutscheBahnTrainHandler(thing);
} }

View File

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

View File

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