allow getMessages with limit directly from Query instance

This commit is contained in:
Stefan Kalscheuer 2022-11-21 14:29:37 +01:00
parent 406fe076f1
commit 7684b2e089
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
3 changed files with 45 additions and 6 deletions

View File

@ -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

View File

@ -395,6 +395,20 @@ public class UraClient implements Serializable {
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.
*
@ -690,5 +704,17 @@ public class UraClient implements Serializable {
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);
}
}
}

View File

@ -349,12 +349,13 @@ public class UraClientTest {
@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"));
@ -365,6 +366,12 @@ public class UraClientTest {
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,
@ -377,19 +384,25 @@ public class UraClientTest {
@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