Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ public Single<ListSessionsResponse> listSessions(String appName, String userId)
.map(
listSessionsResponseMap ->
parseListSessionsResponse(listSessionsResponseMap, appName, userId))
.defaultIfEmpty(ListSessionsResponse.builder().build());
.defaultIfEmpty(ListSessionsResponse.builder().sessions(new ArrayList<>()).build());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The builder() already initializes the session with an empty list so we can probably skip this here and in line 138.

}

private ListSessionsResponse parseListSessionsResponse(
JsonNode listSessionsResponseMap, String appName, String userId) {
JsonNode sessionsNode = listSessionsResponseMap.get("sessions");
if (sessionsNode == null || sessionsNode.isNull() || sessionsNode.isEmpty()) {
return ListSessionsResponse.builder().sessions(new ArrayList<>()).build();
}
List<Map<String, Object>> apiSessions =
objectMapper.convertValue(
listSessionsResponseMap.get("sessions"),
new TypeReference<List<Map<String, Object>>>() {});
objectMapper.convertValue(sessionsNode, new TypeReference<List<Map<String, Object>>>() {});

List<Session> sessions = new ArrayList<>();
for (Map<String, Object> apiSession : apiSessions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -37,6 +39,20 @@
public class VertexAiSessionServiceTest {

private static final ObjectMapper mapper = JsonBaseModel.getMapper();
private static final MediaType JSON_MEDIA_TYPE =
MediaType.parse("application/json; charset=utf-8");

private static ApiResponse apiResponseJson(String json) {
return new ApiResponse() {
@Override
public ResponseBody getResponseBody() {
return ResponseBody.create(JSON_MEDIA_TYPE, json);
}

@Override
public void close() {}
};
}

private static final String MOCK_SESSION_STRING_1 =
"""
Expand Down Expand Up @@ -319,6 +335,24 @@ public void listSessions_empty() {
.isEmpty();
}

@Test
public void listSessions_missingSessionsField_returnsEmpty() {
when(mockApiClient.request("GET", "reasoningEngines/123/sessions?filter=user_id=userX", ""))
.thenReturn(apiResponseJson("{}"));

assertThat(vertexAiSessionService.listSessions("123", "userX").blockingGet().sessions())
.isEmpty();
}

@Test
public void listSessions_nullSessionsField_returnsEmpty() {
when(mockApiClient.request("GET", "reasoningEngines/123/sessions?filter=user_id=userY", ""))
.thenReturn(apiResponseJson("{\"sessions\": null}"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about updating this to something like:
.thenAnswer(new MockApiAnswer(...)) and adjusting MockApiAnswer implementation to respond with given rawApiResponse?

This way we could avoid importing a new lib (and using a non-trivial, realistic media type to mock ApiResponse) for the purpose of those tests


assertThat(vertexAiSessionService.listSessions("123", "userY").blockingGet().sessions())
.isEmpty();
}

@Test
public void listEvents_empty() {
assertThat(vertexAiSessionService.listEvents("789", "user1", "3").blockingGet().events())
Expand Down
Loading