Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop predictions #21

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface GTFSRealTimeInterface {

export interface StopDeparturePredictionsInterface {
getStopPredictions(
stopId?: string | null,
stopId: string,
): Promise<any>;
}

Expand Down
46 changes: 44 additions & 2 deletions src/ResponseDataDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import {StopTimeUpdate} from "./domain/gtfs-rt/entity/trip-update/StopTimeUpdate
import {Arrival} from "./domain/gtfs-rt/entity/trip-update/Arrival";
import {Trip as TripUpdateTrip} from "./domain/gtfs-rt/entity/trip-update/Trip";
import {VehicleId} from "./domain/gtfs-rt/entity/trip-update/VehicleId";
import {Response as StopPredictionResponse} from "./domain/stop-departure-prediction/Response";
import {Departure} from "./domain/stop-departure-prediction/Departure";
import {Origin} from "./domain/stop-departure-prediction/Origin";
import {Destination} from "./domain/stop-departure-prediction/Destination";
import {Entity as StopPredictionEntity} from "./domain/stop-departure-prediction/Entity";
import {Arrival as StopPredictionArrival} from "./domain/stop-departure-prediction/Arrival";

export class ResponseDataDecorator implements MetlinkHttpClientInterface {
private readonly httpClient: MetlinkHttpClientInterface;
Expand Down Expand Up @@ -343,8 +349,44 @@ export class ResponseDataDecorator implements MetlinkHttpClientInterface {
}


public async getStopPredictions(stopId: string | null): Promise<any> {

public async getStopPredictions(stopId: string): Promise<any> {
const response = await this.httpClient.getStopPredictions(stopId);

return new StopPredictionResponse(
response.data.farezone,
response.data.closed,
response.data.departures.map((departure:any) => {
return new StopPredictionEntity(
departure.stop_id,
departure.service_id,
departure.direction,
departure.operator,
new Origin(
departure.origin.stop_id,
departure.origin.name
),
new Destination(
departure.destination.stop_id,
departure.destination.name
),
departure.delay,
departure.vehicle_id,
departure.name,
new StopPredictionArrival(
departure.arrival.aimed,
departure.arrival.expected
),
new Departure(
departure.departure.aimed,
departure.departure.expected
),
departure.status,
departure.monitored,
departure.wheelchair_accessible,
departure.trip_id
);
})
);
}

public async getTripCancellations(query: TripCancellationQueryInterface | null): Promise<any> {
Expand Down
18 changes: 17 additions & 1 deletion src/domain/stop-departure-prediction/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ export class Entity {
private readonly _wheelChairAccessible: boolean;
private readonly _tripId: string;

constructor(stopId: string, serviceId: string, direction: string, operator: string, origin: Origin, destination: Destination, delay: string, vehicleId: string, name: string, arrival: Arrival, departure: Departure, status: string, monitored: boolean, wheelChairAccessible: boolean, tripId: string) {
constructor(
stopId: string,
serviceId: string,
direction: string,
operator: string,
origin: Origin,
destination: Destination,
delay: string,
vehicleId: string,
name: string,
arrival: Arrival,
departure: Departure,
status: string,
monitored: boolean,
wheelChairAccessible: boolean,
tripId: string
) {
this._stopId = stopId;
this._serviceId = serviceId;
this._direction = direction;
Expand Down
13 changes: 6 additions & 7 deletions tests/integration/MetlinkHttpClient/StopPredictions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {MetlinkHttpClientInterface} from "../../../src/Contracts";

describe("Integration: Metlink Http Client: Stop Predictions", () => {


function getSchema(): {} {
return {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down Expand Up @@ -59,10 +60,9 @@ describe("Integration: Metlink Http Client: Stop Predictions", () => {
},
"delay": {
"type": "string",
"pattern": "^PT\\d+M\\d+S$"
},
"vehicle_id": {
"type": "string"
"type": ["string","null"]
},
"name": {
"type": "string"
Expand All @@ -74,7 +74,7 @@ describe("Integration: Metlink Http Client: Stop Predictions", () => {
"type": "string"
},
"expected": {
"type": "string"
"type": ["string", "null"]
}
},
"required": ["aimed", "expected"]
Expand All @@ -86,14 +86,13 @@ describe("Integration: Metlink Http Client: Stop Predictions", () => {
"type": "string"
},
"expected": {
"type": "string"
"type": ["string", "null"]
}
},
"required": ["aimed", "expected"]
},
"status": {
"type": "string",
"enum": ["on time", "delayed", "cancelled"]
"type": ["string", "null"]
},
"monitored": {
"type": "boolean"
Expand Down Expand Up @@ -136,7 +135,7 @@ describe("Integration: Metlink Http Client: Stop Predictions", () => {

test("getStopPredictions", async () => {
const client: MetlinkHttpClientInterface = MetlinkHttpClientBuilder.buildWithAxios(getMetlinkToken())
const response = await client.getStopPredictions();
const response = await client.getStopPredictions("5515");

const result = SchemaValidator.validate(response.data, getSchema());
expect(result.isValid).toBeTruthy();
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/ResponseDataDecorator/StopPredictions.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import axios from 'axios';
import MockAdapter from "axios-mock-adapter";
import {MetlinkHttpClientInterface} from "../../../src/Contracts";
import {ClientBuilder} from "../ClientBuilder";
import {Response} from "../../../src/domain/stop-departure-prediction/Response";

const mock: MockAdapter = new MockAdapter(axios);

describe.skip("Response Data Decorator: Stop predictions", () => {
describe("Response Data Decorator: Stop predictions", () => {

const STOP_ID: string = "5515";


afterEach(function (): void {
mock.reset();
Expand Down Expand Up @@ -50,10 +56,23 @@ describe.skip("Response Data Decorator: Stop predictions", () => {
]
];

function getPath(): string {
return "/stop-predictions";
function getPath(urlSearchParams: URLSearchParams | null = null): string {
let query: string = "";

if (urlSearchParams) {
query = "?"+urlSearchParams?.toString();
}

return "/stop-predictions" + query;
}


it.each(dataSet)("getStopPredictions", async (mockData) => {
mock.onGet(getPath(new URLSearchParams({"stop_id": STOP_ID}))).replyOnce(200, mockData);

const client: MetlinkHttpClientInterface = ClientBuilder.getHttpClientWithResponseDataDecorator(axios);
const entity: Response = await client.getStopPredictions(STOP_ID);

expect(entity).toBeInstanceOf(Response)
});
});