Skip to content

Commit

Permalink
allow getMessages with limit directly from Query instance
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Nov 21, 2022
1 parent 406fe07 commit 7684b2e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file.
* Updated dependencies

### Fixed
* Querying trips with limit directly from `Query` instance (#18)
* Querying trips and messages with limit directly from `Query` instance (#18)

### Misc
* Tested with JDK 19
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/de/stklcode/pubtrans/ura/UraClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,20 @@ public List<Message> getMessages(final Query query) throws UraClientException {
return getMessages(query, null);
}


/**
* Get list of messages with limit.
* If forStops() has been called, those will be used as filter.
*
* @param limit Maximum number of results.
* @return List of trips.
* @throws UraClientException Error with API communication.
* @since 2.0.4
*/
public List<Message> getMessages(final Integer limit) throws UraClientException {
return getMessages(new Query(), limit);
}

/**
* Get list of messages for given stopIDs with result limit.
*
Expand Down Expand Up @@ -690,5 +704,17 @@ public AsyncUraTripReader getTripsStream(List<Consumer<Trip>> consumers) throws
public List<Message> getMessages() throws UraClientException {
return UraClient.this.getMessages(this);
}

/**
* Get trips for set filters.
*
* @param limit Maximum number of results.
* @return List of matching messages.
* @throws UraClientException Error with API communication.
* @since 2.0.4
*/
public List<Message> getMessages(final Integer limit) throws UraClientException {
return UraClient.this.getMessages(this, limit);
}
}
}
23 changes: 18 additions & 5 deletions src/test/java/de/stklcode/pubtrans/ura/UraClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,13 @@ public void getTripsForStopAndLine() throws UraClientException {

@Test
public void getMessages() throws UraClientException {
UraClient uraClient = new UraClient(wireMock.baseUrl());

// Mock the HTTP call.
mockHttpToFile(1, "instant_V1_messages.txt");

// Get messages without filter and verify some values.
List<Message> messages = new UraClient(wireMock.baseUrl())
.getMessages();
List<Message> messages = uraClient.getMessages();
assertThat(messages, hasSize(2));
assertThat(messages.get(0).getStop().getId(), is("100707"));
assertThat(messages.get(0).getUuid(), is("016e1231d4e30014_100707"));
Expand All @@ -365,6 +366,12 @@ public void getMessages() throws UraClientException {
assertThat(messages.get(0).getText(), is("Sehr geehrte Fahrgäste, wegen Strassenbauarbeiten kann diese Haltestelle nicht von den Bussen der Linien 17, 44 und N2 angefahren werden."));
assertThat(messages.get(1).getText(), is("Sehr geehrte Fahrgäste, diese Haltestelle wird vorübergehend von den Linien 47, 147 und N3 nicht angefahren."));

// With limit.
messages = uraClient.getMessages(1);
assertThat(messages, hasSize(1));
messages = uraClient.getMessages(3);
assertThat(messages, hasSize(2));

mockHttpToException();
UraClientException exception = assertThrows(
UraClientException.class,
Expand All @@ -377,19 +384,25 @@ public void getMessages() throws UraClientException {

@Test
public void getMessagesForStop() throws UraClientException {
UraClient uraClient = new UraClient(wireMock.baseUrl(), "/interfaces/ura/instant_V2", "/interfaces/ura/stream");

// Mock the HTTP call.
mockHttpToFile(2, "instant_V2_messages_stop.txt");

// Get trips for stop ID 100707 (Berensberger Str.) and verify some values.
List<Message> messages = new UraClient(wireMock.baseUrl(), "/interfaces/ura/instant_V2", "/interfaces/ura/stream")
.forStops("100707")
.getMessages();
List<Message> messages = uraClient.forStops("100707").getMessages();
assertThat(messages, hasSize(1));
assertThat(messages.stream().filter(t -> !t.getStop().getId().equals("100707")).findAny(), is(Optional.empty()));
assertThat(messages.get(0).getUuid(), is("016e1231d4e30014_100707"));
assertThat(messages.get(0).getType(), is(0));
assertThat(messages.get(0).getPriority(), is(3));
assertThat(messages.get(0).getText(), is("Sehr geehrte Fahrgäste, wegen Strassenbauarbeiten kann diese Haltestelle nicht von den Bussen der Linien 17, 44 und N2 angefahren werden."));

// With limit.
messages = uraClient.forStops("100707").getMessages(0);
assertThat(messages, hasSize(0));
messages = uraClient.forStops("100707").getMessages(2);
assertThat(messages, hasSize(1));
}

@Test
Expand Down

0 comments on commit 7684b2e

Please sign in to comment.