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 @@ -119,15 +119,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());
}

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 @@ -322,6 +338,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}"));

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

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