From b9349c192e2687c00a4dffbf0a9786b82b33ee75 Mon Sep 17 00:00:00 2001 From: Riya Saxena Date: Wed, 6 Mar 2024 07:31:14 -0800 Subject: [PATCH] changed the startTime and endTime req input format --- .../resthandler/RestGetFindingsAction.java | 26 +++++++++++++++---- .../securityanalytics/findings/FindingIT.java | 8 +++--- .../findings/FindingServiceTests.java | 6 ++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/opensearch/securityanalytics/resthandler/RestGetFindingsAction.java b/src/main/java/org/opensearch/securityanalytics/resthandler/RestGetFindingsAction.java index f908caad4..b0c966732 100644 --- a/src/main/java/org/opensearch/securityanalytics/resthandler/RestGetFindingsAction.java +++ b/src/main/java/org/opensearch/securityanalytics/resthandler/RestGetFindingsAction.java @@ -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; @@ -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( diff --git a/src/test/java/org/opensearch/securityanalytics/findings/FindingIT.java b/src/test/java/org/opensearch/securityanalytics/findings/FindingIT.java index 710089268..cad71d0e5 100644 --- a/src/test/java/org/opensearch/securityanalytics/findings/FindingIT.java +++ b/src/test/java/org/opensearch/securityanalytics/findings/FindingIT.java @@ -826,18 +826,18 @@ public void testGetFindings_byStartTimeAndEndTime_success() throws IOException { // Call GetFindings API for first detector by startTime and endTime Map 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 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")); diff --git a/src/test/java/org/opensearch/securityanalytics/findings/FindingServiceTests.java b/src/test/java/org/opensearch/securityanalytics/findings/FindingServiceTests.java index 57fa793b5..e60870b1a 100644 --- a/src/test/java/org/opensearch/securityanalytics/findings/FindingServiceTests.java +++ b/src/test/java/org/opensearch/securityanalytics/findings/FindingServiceTests.java @@ -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()); @@ -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"); @@ -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");