Skip to content

Commit

Permalink
search returns detector type in CAPS fix and integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Raj Chakravarthi <[email protected]>
  • Loading branch information
raj-chak committed Dec 1, 2022
1 parent a4a542d commit eac45d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private XContentBuilder createXContentBuilder(XContentBuilder builder, ToXConten
}
builder.field(TYPE_FIELD, type)
.field(NAME_FIELD, name)
.field(DETECTOR_TYPE_FIELD, detectorType);
.field(DETECTOR_TYPE_FIELD, detectorType.getDetectorType());

if (!secure) {
if (user == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void testCreatingADetector() throws IOException {
Assert.assertFalse(((Map<String, Object>) responseBody.get("detector")).containsKey("findings_index"));
Assert.assertFalse(((Map<String, Object>) responseBody.get("detector")).containsKey("alert_index"));

String detectorTypeInResponse = (String) ((Map<String, Object>)responseBody.get("detector")).get("detector_type");
Assert.assertEquals("Detector type incorrect", randomDetectorType(), detectorTypeInResponse);

String request = "{\n" +
" \"query\" : {\n" +
" \"match\":{\n" +
Expand Down Expand Up @@ -182,6 +185,9 @@ public void testGettingADetector() throws IOException {
Map<String, Object> responseBody = asMap(getResponse);
Assert.assertEquals(createdId, responseBody.get("_id"));
Assert.assertNotNull(responseBody.get("detector"));

String detectorTypeInResponse = (String) ((Map<String, Object>)responseBody.get("detector")).get("detector_type");
Assert.assertEquals("Detector type incorrect", randomDetectorType(), detectorTypeInResponse);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -218,6 +224,11 @@ public void testSearchingDetectors() throws IOException {
Map<String, Object> searchResponseHits = (Map) searchResponseBody.get("hits");
Map<String, Object> searchResponseTotal = (Map) searchResponseHits.get("total");
Assert.assertEquals(1, searchResponseTotal.get("value"));

List<Map<String, Object>> hits = ((List<Map<String, Object>>) ((Map<String, Object>) searchResponseBody.get("hits")).get("hits"));
Map<String, Object> hit = hits.get(0);
String detectorTypeInResponse = (String) ((Map<String, Object>) hit.get("_source")).get("detector_type");
Assert.assertEquals("Detector type incorrect", detectorTypeInResponse, randomDetectorType());
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -274,6 +285,9 @@ public void testCreatingADetectorWithCustomRules() throws IOException {
List<SearchHit> hits = executeSearch(Detector.DETECTORS_INDEX, request);
SearchHit hit = hits.get(0);

String detectorType = (String) ((Map<String, Object>) hit.getSourceAsMap().get("detector")).get("detector_type");
Assert.assertEquals("Detector type incorrect", detectorType, randomDetectorType());

String monitorId = ((List<String>) ((Map<String, Object>) hit.getSourceAsMap().get("detector")).get("monitor_id")).get(0);

indexDoc(index, "1", randomDoc());
Expand Down Expand Up @@ -430,6 +444,9 @@ public void testUpdateADetector() throws IOException {
Response updateResponse = makeRequest(client(), "PUT", SecurityAnalyticsPlugin.DETECTOR_BASE_URI + "/" + detectorId, Collections.emptyMap(), toHttpEntity(updatedDetector));
Assert.assertEquals("Update detector failed", RestStatus.OK, restStatus(updateResponse));

String detectorTypeInResponse = (String) ((Map<String, Object>) (asMap(updateResponse).get("detector"))).get("detector_type");
Assert.assertEquals("Detector type incorrect", randomDetectorType(), detectorTypeInResponse);

request = "{\n" +
" \"query\" : {\n" +
" \"match_all\":{\n" +
Expand Down

0 comments on commit eac45d7

Please sign in to comment.