[deutschebahn] DB API Marketplace adjustments (#12786)

* Changes API URL and authentication Headers for DB API Marketplace.
* Fixed some typos and code style findings.

Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
This commit is contained in:
Sönke Küper
2022-05-26 13:09:09 +02:00
committed by GitHub
parent a30bb59400
commit 76001902a3
22 changed files with 256 additions and 236 deletions

View File

@@ -12,8 +12,13 @@
*/
package org.openhab.binding.deutschebahn.internal;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Calendar;
@@ -172,7 +177,7 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
final TimetablesV1ApiStub stubWithError = TimetablesV1ApiStub.createWithException();
final DeutscheBahnTimetableHandler handler = createAndInitHandler(callback, bridge,
(String authToken, HttpCallable httpCallable) -> stubWithError);
(String clientId, String clientSecret, HttpCallable httpCallable) -> stubWithError);
try {
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));
@@ -212,7 +217,7 @@ public class DeutscheBahnTimetableHandlerTest implements TimetablesV1ImplTestHel
final TimetablesV1ApiStub stubWithData = TimetablesV1ApiStub.createWithResult(timetable);
final DeutscheBahnTimetableHandler handler = createAndInitHandler(callback, bridge,
(String authToken, HttpCallable httpCallable) -> stubWithData);
(String clientId, String clientSecret, HttpCallable httpCallable) -> stubWithData);
try {
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN)));

View File

@@ -45,11 +45,11 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
final List<TimetableStop> stops = loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09",
"https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/10",
"https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/11"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/10",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/11"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
assertThat(stops, hasSize(21));
@@ -75,9 +75,9 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
final List<TimetableStop> stops = loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
assertThat(stops, hasSize(8));
@@ -91,11 +91,11 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
final List<TimetableStop> stops02 = loader.getTimetableStops();
assertThat(stops02, hasSize(13));
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09",
"https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/10"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/10"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226",
"https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
assertEquals("-5296516961807204721-2108160906-5", stops02.get(0).getId());
@@ -114,9 +114,9 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
// First call - plan and full changes are requested.
loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
// Changes are updated only every 30 seconds, so move clock ahead 20 seconds, no request is made
@@ -124,43 +124,43 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
// Move ahead 10 seconds, so recent changes are fetched
timeProvider.moveAhead(10);
loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/rchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/rchg/8000226"));
// Move again ahead 30 seconds, recent changes are fetched again
timeProvider.moveAhead(30);
loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/rchg/8000226",
"https://api.deutschebahn.com/timetables/v1/rchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/rchg/8000226",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/rchg/8000226"));
// If recent change were not updated last 120 seconds the full changes must be requested
timeProvider.moveAhead(120);
loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226",
"https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/rchg/8000226",
"https://api.deutschebahn.com/timetables/v1/rchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/rchg/8000226",
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/rchg/8000226"));
}
@Test
@@ -171,7 +171,8 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
EventType.ARRIVAL, timeProvider, EVA_LEHRTE, 20);
// Simulate that only one url is available
timeTableTestModule.addAvailableUrl("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09");
timeTableTestModule.addAvailableUrl(
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09");
timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 0);
@@ -191,7 +192,8 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
EventType.DEPARTURE, timeProvider, EVA_LEHRTE, 20);
// Simulate that only one url is available
timeTableTestModule.addAvailableUrl("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09");
timeTableTestModule.addAvailableUrl(
"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09");
timeProvider.time = new GregorianCalendar(2021, Calendar.AUGUST, 16, 9, 0);
@@ -214,9 +216,9 @@ public class TimetableLoaderTest implements TimetablesV1ImplTestHelper {
final List<TimetableStop> stops = loader.getTimetableStops();
assertThat(timeTableTestModule.getRequestedPlanUrls(),
contains("https://api.deutschebahn.com/timetables/v1/plan/8000226/210816/09"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/8000226/210816/09"));
assertThat(timeTableTestModule.getRequestedFullChangesUrls(),
contains("https://api.deutschebahn.com/timetables/v1/fchg/8000226"));
contains("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/8000226"));
assertThat(timeTableTestModule.getRequestedRecentChangesUrls(), empty());
// Stop -5296516961807204721-2108160906-5 has its planned time at 9:34, but its included because its changed

View File

@@ -39,11 +39,11 @@ import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1Impl.Http
public class TimetableStubHttpCallable implements HttpCallable {
private static final Pattern PLAN_URL_PATTERN = Pattern
.compile("https://api.deutschebahn.com/timetables/v1/plan/(\\d+)/(\\d+)/(\\d+)");
.compile("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/(\\d+)/(\\d+)/(\\d+)");
private static final Pattern FULL_CHANGES_URL_PATTERN = Pattern
.compile("https://api.deutschebahn.com/timetables/v1/fchg/(\\d+)");
.compile("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/(\\d+)");
private static final Pattern RECENT_CHANGES_URL_PATTERN = Pattern
.compile("https://api.deutschebahn.com/timetables/v1/rchg/(\\d+)");
.compile("https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/rchg/(\\d+)");
private final File testdataDir;
private final List<String> requestedPlanUrls;

View File

@@ -12,14 +12,10 @@
*/
package org.openhab.binding.deutschebahn.internal.timetable;
import java.net.URISyntaxException;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.deutschebahn.internal.timetable.TimetablesV1Impl.HttpCallable;
import org.xml.sax.SAXException;
/**
* Testmodule that contains the {@link TimetablesV1Api} and {@link TimetableStubHttpCallable}.
@@ -59,13 +55,6 @@ public final class TimetablesApiTestModule {
}
public TimetablesV1ApiFactory getApiFactory() {
return new TimetablesV1ApiFactory() {
@Override
public TimetablesV1Api create(String authToken, HttpCallable httpCallable)
throws JAXBException, SAXException, URISyntaxException {
return api;
}
};
return (String clientId, String clientSecret, HttpCallable httpCallable) -> api;
}
}

View File

@@ -56,7 +56,7 @@ public class TimetablesV1ImplTest implements TimetablesV1ImplTestHelper {
public void testGetDataForHannoverHBF() throws Exception {
TimetablesV1Api timeTableApi = createApiWithTestdata().getApi();
Date time = new GregorianCalendar(2021, Calendar.OCTOBER, 14, 11, 00).getTime();
Date time = new GregorianCalendar(2021, Calendar.OCTOBER, 14, 11, 0).getTime();
Timetable timeTable = timeTableApi.getPlan(EVA_HANNOVER_HBF, time);
assertNotNull(timeTable);

View File

@@ -29,7 +29,8 @@ public interface TimetablesV1ImplTestHelper {
public static final String EVA_LEHRTE = "8000226";
public static final String EVA_HANNOVER_HBF = "8000152";
public static final String AUTH_TOKEN = "354c8161cd7fb0936c840240280c131e";
public static final String CLIENT_ID = "bdwrpmxuo6157jrekftlbcc6ju9awo";
public static final String CLIENT_SECRET = "354c8161cd7fb0936c840240280c131e";
/**
* Creates an {@link TimetablesApiTestModule} that uses http response data from file system.
@@ -49,7 +50,7 @@ public interface TimetablesV1ImplTestHelper {
assertNotNull(timetablesData);
final File testDataDir = new File(timetablesData.toURI());
final TimetableStubHttpCallable httpStub = new TimetableStubHttpCallable(testDataDir);
final TimetablesV1Impl timeTableApi = new TimetablesV1Impl(AUTH_TOKEN, httpStub);
final TimetablesV1Impl timeTableApi = new TimetablesV1Impl(CLIENT_ID, CLIENT_SECRET, httpStub);
return new TimetablesApiTestModule(timeTableApi, httpStub);
}
}