DB: Migrate to Hafas client interface.

This commit is contained in:
Andreas Schildbach 2018-07-25 17:28:13 +02:00
parent 5ff1d2bd35
commit 88f0e12fcb
3 changed files with 52 additions and 65 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 the original author or authors.
* Copyright the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -21,6 +21,10 @@ import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import de.schildbach.pte.dto.Line;
import de.schildbach.pte.dto.Product;
import okhttp3.HttpUrl;
@ -28,18 +32,18 @@ import okhttp3.HttpUrl;
/**
* @author Andreas Schildbach
*/
public final class BahnProvider extends AbstractHafasLegacyProvider {
public final class BahnProvider extends AbstractHafasClientInterfaceProvider {
private static final HttpUrl API_BASE = HttpUrl.parse("https://reiseauskunft.bahn.de/bin/");
private static final Product[] PRODUCTS_MAP = { Product.HIGH_SPEED_TRAIN, Product.HIGH_SPEED_TRAIN,
Product.REGIONAL_TRAIN, Product.REGIONAL_TRAIN, Product.SUBURBAN_TRAIN, Product.BUS, Product.FERRY,
Product.SUBWAY, Product.TRAM, Product.ON_DEMAND, null, null, null, null };
public BahnProvider() {
super(NetworkId.DB, API_BASE, "dn", PRODUCTS_MAP);
setStationBoardEndpoint(HttpUrl.parse("https://mobile.bahn.de/bin/mobil/bhftafel.exe"));
setStationBoardHasStationTable(false);
setJsonGetStopsUseWeight(false);
public BahnProvider(final String apiAuthorization) {
super(NetworkId.DB, API_BASE, PRODUCTS_MAP);
setApiVersion("1.14");
setApiClient("{\"id\":\"DB\",\"v\":\"16040000\",\"type\":\"AND\",\"name\":\"DB Navigator\"}");
setApiAuthorization(apiAuthorization);
setRequestChecksumSalt("bdI8UVj40K5fvxwf".getBytes(Charsets.UTF_8));
}
@Override
@ -54,7 +58,6 @@ public final class BahnProvider extends AbstractHafasLegacyProvider {
final Matcher m = P_SPLIT_NAME_ONE_COMMA.matcher(name);
if (m.matches())
return new String[] { m.group(2), m.group(1) };
return super.splitStationName(name);
}
@ -63,7 +66,6 @@ public final class BahnProvider extends AbstractHafasLegacyProvider {
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(poi);
if (m.matches())
return new String[] { m.group(1), m.group(2) };
return super.splitStationName(poi);
}
@ -72,28 +74,17 @@ public final class BahnProvider extends AbstractHafasLegacyProvider {
final Matcher m = P_SPLIT_NAME_FIRST_COMMA.matcher(address);
if (m.matches())
return new String[] { m.group(1), m.group(2) };
return super.splitStationName(address);
}
private static final Pattern P_NORMALIZE_LINE_NAME_TRAM = Pattern.compile("str\\s+(.*)", Pattern.CASE_INSENSITIVE);
@Override
protected String normalizeLineName(final String lineName) {
final Matcher mTram = P_NORMALIZE_LINE_NAME_TRAM.matcher(lineName);
if (mTram.matches())
return mTram.group(1);
return super.normalizeLineName(lineName);
}
@Override
protected Product normalizeType(final String type) {
final String ucType = type.toUpperCase();
if ("N".equals(ucType))
return null;
return super.normalizeType(type);
protected Line newLine(final String operator, final Product product, final String name, final String number) {
if (product == Product.SUBURBAN_TRAIN && name != null && name.startsWith("S")
&& !(number != null && number.startsWith("S")))
return super.newLine(operator, product, name, "S" + Strings.nullToEmpty(number));
if (product == Product.SUBWAY && name != null && name.startsWith("U")
&& !(number != null && number.startsWith("U")))
return super.newLine(operator, product, name, "U" + Strings.nullToEmpty(number));
return super.newLine(operator, product, name, number);
}
}

View file

@ -44,13 +44,7 @@ import de.schildbach.pte.dto.TripOptions;
*/
public class BahnProviderLiveTest extends AbstractProviderLiveTest {
public BahnProviderLiveTest() {
super(new BahnProvider());
}
@Test
public void nearbyStations() throws Exception {
final NearbyLocationsResult result = queryNearbyStations(new Location(LocationType.STATION, "692991"));
print(result);
super(new BahnProvider(secretProperty("db.api_authorization")));
}
@Test
@ -62,10 +56,11 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
@Test
public void nearbyPOIsByCoordinate() throws Exception {
final NearbyLocationsResult result = queryNearbyLocations(EnumSet.of(LocationType.POI),
Location.coord(52525589, 13369548));
Location.coord(Point.fromDouble(52.5304903, 13.3791152)));
print(result);
assertThat(result.locations,
hasItem(new Location(LocationType.POI, "990416076", "Berlin", "Museum für Naturkunde")));
assertThat(result.locations, hasItem(new Location(LocationType.POI,
"A=4@O=Berlin, Museum für Naturkunde (Kultur und Unterhal@X=13380003@Y=52529724@u=0@U=104@L=991597061@",
"Berlin", "Museum für Naturkunde")));
}
@Test
@ -84,14 +79,16 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
public void suggestLocationsUmlaut() throws Exception {
final SuggestLocationsResult result = suggestLocations("Güntzelstr. (U)");
print(result);
assertEquals("Güntzelstr. (U)", result.getLocations().get(0).name);
assertThat(result.getLocations(),
hasItem(new Location(LocationType.STATION, "731371", "Berlin", "Güntzelstr. (U)")));
}
@Test
public void suggestLocationsIncomplete() throws Exception {
final SuggestLocationsResult result = suggestLocations("Dammt");
print(result);
assertEquals("Hamburg Dammtor", result.getLocations().get(0).name);
assertThat(result.getLocations(),
hasItem(new Location(LocationType.STATION, "8002548", null, "Hamburg Dammtor")));
}
@Test
@ -104,14 +101,16 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
public void suggestLocationsAddress() throws Exception {
final SuggestLocationsResult result = suggestLocations("München, Friedenstraße 2");
print(result);
assertEquals(LocationType.ADDRESS, result.getLocations().get(0).type);
assertEquals("Friedenstraße 2", result.getLocations().get(0).name);
assertThat(result.getLocations(), hasItem(new Location(LocationType.ADDRESS,
"A=2@O=München - Berg am Laim, Friedenstraße 2@X=11602251@Y=48123949@U=103@L=980857648@B=1@p=1378873973@",
"München - Berg am Laim", "Friedenstraße 2")));
}
@Test
public void shortTrip() throws Exception {
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8011160", null, "Berlin Hbf"),
null, new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"), new Date(), true, null);
final Location from = new Location(LocationType.STATION, "8011160", null, "Berlin Hbf");
final Location to = new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf");
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
print(laterResult);
@ -131,19 +130,15 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
"Tutzinger-Hof-Platz, Starnberg");
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
if (!result.context.canQueryLater())
return;
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
print(laterResult);
}
@Test
public void noTrips() throws Exception {
final QueryTripsResult result = queryTrips(
new Location(LocationType.STATION, "513729", null, "Schillerplatz, Kaiserslautern"), null,
new Location(LocationType.STATION, "403631", null, "Trippstadt Grundschule"), new Date(), true, null);
final Location from = new Location(LocationType.STATION, "513729", null, "Schillerplatz, Kaiserslautern");
final Location to = new Location(LocationType.STATION, "403631", null, "Trippstadt Grundschule");
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
}
@ -155,10 +150,6 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
"Starnberg, Possenhofener Straße 13");
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
if (!result.context.canQueryLater())
return;
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
print(laterResult);
}
@ -167,31 +158,35 @@ public class BahnProviderLiveTest extends AbstractProviderLiveTest {
public void tripsAcrossBorder() throws Exception {
final TripOptions options = new TripOptions(EnumSet.of(Product.BUS), null, WalkSpeed.NORMAL,
Accessibility.NEUTRAL, null);
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8506131", null, "Kreuzlingen"),
null, new Location(LocationType.STATION, "8003400", null, "Konstanz"), new Date(), true, options);
final Location from = new Location(LocationType.STATION, "8506131", null, "Kreuzlingen");
final Location to = new Location(LocationType.STATION, "8003400", null, "Konstanz");
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, options);
print(result);
assertEquals(QueryTripsResult.Status.OK, result.status);
}
@Test
public void tripsByCoordinate() throws Exception {
final QueryTripsResult result = queryTrips(Location.coord(52535576, 13422171), null,
Location.coord(52525589, 13369548), new Date(), true, null);
public void tripBetweenCoordinates() throws Exception {
final Location from = Location.coord(52535576, 13422171); // Berlin Marienburger Str.
final Location to = Location.coord(52525589, 13369548); // Berlin Hbf
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
}
@Test
public void tripsTooClose() throws Exception {
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"),
null, new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"), new Date(), true, null);
final Location location = new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf");
final QueryTripsResult result = queryTrips(location, null, location, new Date(), true, null);
print(result);
assertEquals(QueryTripsResult.Status.TOO_CLOSE, result.status);
}
@Test
public void tripsInvalidDate() throws Exception {
final QueryTripsResult result = queryTrips(new Location(LocationType.STATION, "8011160", null, "Berlin Hbf"),
null, new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf"), new Date(0), true, null);
final Location from = new Location(LocationType.STATION, "8011160", null, "Berlin Hbf");
final Location to = new Location(LocationType.STATION, "8010205", null, "Leipzig Hbf");
final Date date = new Date(System.currentTimeMillis() - 2 * 365 * 24 * 3600 * 1000l); // 2 years ago
final QueryTripsResult result = queryTrips(from, null, to, date, true, null);
print(result);
assertEquals(QueryTripsResult.Status.INVALID_DATE, result.status);
}

View file

@ -1,5 +1,6 @@
# Secrets are needed to run some of the live tests.
navitia.authorization =
db.api_authorization =
bvg.api_authorization =
nvv.api_authorization =
vbn.api_authorization =