Skip to content

Commit

Permalink
changed the startTime and endTime req input format
Browse files Browse the repository at this point in the history
  • Loading branch information
riysaxen-amzn committed Mar 6, 2024
1 parent 0804c8b commit b9349c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.opensearch.securityanalytics.resthandler;

import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -50,12 +51,27 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
findingIds = Arrays.asList(request.param("findingIds").split(","));
}
Instant startTime = null;
if (request.param("startTime") != null) {
startTime = Instant.parse(request.param("startTime"));
String startTimeParam = request.param("startTime");
if (startTimeParam != null && !startTimeParam.isEmpty()) {
try {
startTime = Instant.ofEpochMilli(Long.parseLong(startTimeParam));
} catch (NumberFormatException | NullPointerException | DateTimeException e) {
// Handle the parsing error
// For example, log the error or provide a default value
startTime = Instant.now(); // Default value or fallback
}
}
Instant endTime= null;
if (request.param("endTime") != null) {
endTime = Instant.parse(request.param("endTime"));

Instant endTime = null;
String endTimeParam = request.param("endTime");
if (endTimeParam != null && !endTimeParam.isEmpty()) {
try {
endTime = Instant.ofEpochMilli(Long.parseLong(endTimeParam));
} catch (NumberFormatException | NullPointerException | DateTimeException e) {
// Handle the parsing error
// For example, log the error or provide a default value
endTime = Instant.now(); // Default value or fallback
}
}

Table table = new Table(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,18 +826,18 @@ public void testGetFindings_byStartTimeAndEndTime_success() throws IOException {

// Call GetFindings API for first detector by startTime and endTime
Map<String, String> params = new HashMap<>();
params.put("startTime", String.valueOf(startTime1));
params.put("startTime", String.valueOf(startTime1.toEpochMilli()));
Instant endTime1 = Instant.now();
params.put("endTime", String.valueOf(endTime1));
params.put("endTime", String.valueOf(endTime1.toEpochMilli()));
Response getFindingsResponse = makeRequest(client(), "GET", SecurityAnalyticsPlugin.FINDINGS_BASE_URI + "/_search", params, null);

Map<String, Object> getFindingsBody = entityAsMap(getFindingsResponse);
Assert.assertEquals(2, getFindingsBody.get("total_findings"));
// Call GetFindings API for second detector by startTime and endTime
params.clear();
params.put("startTime", String.valueOf(startTime2));
params.put("startTime", String.valueOf(startTime2.toEpochMilli()));
Instant endTime2 = Instant.now();
params.put("endTime", String.valueOf(endTime2));
params.put("endTime", String.valueOf(endTime2.toEpochMilli()));
getFindingsResponse = makeRequest(client(), "GET", SecurityAnalyticsPlugin.FINDINGS_BASE_URI + "/_search", params, null);
getFindingsBody = entityAsMap(getFindingsResponse);
Assert.assertEquals(1, getFindingsBody.get("total_findings"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void testGetFindings_success() {
0,
null
);
findingsService.getFindingsByDetectorId("detector_id123", table, new ActionListener<>() {
findingsService.getFindingsByDetectorId("detector_id123", table, null, null, null, null, null, new ActionListener<>() {
@Override
public void onResponse(GetFindingsResponse getFindingsResponse) {
assertEquals(2, (int)getFindingsResponse.getTotalFindings());
Expand Down Expand Up @@ -220,7 +220,7 @@ public void testGetFindings_getFindingsByMonitorIdFailure() {
0,
null
);
findingsService.getFindingsByDetectorId("detector_id123", table, new ActionListener<>() {
findingsService.getFindingsByDetectorId("detector_id123", table, null, null, null, null, null, new ActionListener<>() {
@Override
public void onResponse(GetFindingsResponse getFindingsResponse) {
fail("this test should've failed");
Expand Down Expand Up @@ -255,7 +255,7 @@ public void testGetFindings_getDetectorFailure() {
0,
null
);
findingsService.getFindingsByDetectorId("detector_id123", table, new ActionListener<>() {
findingsService.getFindingsByDetectorId("detector_id123", table, null, null, null, null, null, new ActionListener<>() {
@Override
public void onResponse(GetFindingsResponse getFindingsResponse) {
fail("this test should've failed");
Expand Down

0 comments on commit b9349c1

Please sign in to comment.