Skip to content

Commit

Permalink
Added null checks for queried event fields (#95)
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Shien <[email protected]>
  • Loading branch information
bshien authored Nov 2, 2024
1 parent 13bcfe3 commit 91099eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ public Optional<LatestEventData> queryLatestEvent(String repo, String userLogin,
Map<String, Object> latestDocument = topHits.getHits().getHits()[0].getSourceAsMap();
LatestEventData latestEventData = new LatestEventData();
latestEventData.setEventType(eventType);
latestEventData.setEventAction(latestDocument.get("action").toString());
latestEventData.setTimeLastEngaged(Instant.parse(latestDocument.get("created_at").toString()));
if(latestDocument.containsKey("action")){
latestEventData.setEventAction(latestDocument.get("action").toString());
}
if(latestDocument.containsKey("created_at")) {
latestEventData.setTimeLastEngaged(Instant.parse(latestDocument.get("created_at").toString()));
}
return Optional.of(latestEventData);
} else {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ public void testQueryLatestEvent() {
assertEquals(testEventOpt, latestEvent); // Modify expected result according to your logic
}

@Test
public void testQueryLatestEventNoActionOrCreatedAt() {
// Mock
OpenSearchUtil openSearchUtil = Mockito.mock(OpenSearchUtil.class);
SearchResponse eventsResponse = Mockito.mock(SearchResponse.class);
SearchHit searchHit = Mockito.mock(SearchHit.class);
SearchHits searchHits = Mockito.mock(SearchHits.class);
TopHits topHits = Mockito.mock(TopHits.class);
Aggregations aggregations = Mockito.mock(Aggregations.class);

when(openSearchUtil.search(any(SearchRequest.class))).thenReturn(eventsResponse);
when(eventsResponse.status()).thenReturn(RestStatus.OK);
when(eventsResponse.getAggregations()).thenReturn(aggregations);
when(aggregations.get("latest_event")).thenReturn(topHits);
when(topHits.getHits()).thenReturn(searchHits);
when(searchHits.getHits()).thenReturn(new SearchHit[]{searchHit});

Map<String, Object> sourceMap = new HashMap<>();
when(searchHit.getSourceAsMap()).thenReturn(sourceMap);

MaintainerMetrics maintainerMetrics = new MaintainerMetrics();

// Call method under test
String testRepo = "testRepo";
String testUserLogin = "testUserLogin";
String testEventType = "testEventType";
Optional<LatestEventData> latestEvent = maintainerMetrics.queryLatestEvent(testRepo, testUserLogin, testEventType, openSearchUtil);
LatestEventData testEvent = new LatestEventData();
testEvent.setEventType("testEventType");
Optional<LatestEventData> testEventOpt = Optional.of(testEvent);


// Assertions
assertEquals(testEventOpt, latestEvent); // Modify expected result according to your logic
}

@Test
public void testQueryLatestEventEmpty() {
// Mock
Expand Down

0 comments on commit 91099eb

Please sign in to comment.