@@ -72,10 +72,34 @@ public void testNewLogTypes() throws IOException {
72
72
@ SuppressWarnings ("unchecked" )
73
73
public void testDeletingADetector_MonitorNotExists () throws IOException {
74
74
updateClusterSetting (ENABLE_WORKFLOW_USAGE .getKey (), "false" );
75
- String index = createTestIndex (randomIndex (), windowsIndexMapping ());
75
+ final String detectorId = setupDetector ();
76
+ final Map <String , Object > detectorSourceAsMap = getDetectorSourceAsMap (detectorId );
77
+
78
+ final String monitorId = ((List <String >) detectorSourceAsMap .get ("monitor_id" )).get (0 );
79
+ final Response deleteMonitorResponse = deleteAlertingMonitor (monitorId );
80
+ assertEquals (200 , deleteMonitorResponse .getStatusLine ().getStatusCode ());
81
+ entityAsMap (deleteMonitorResponse );
82
+
83
+ validateDetectorDeletion (detectorId );
84
+ }
85
+
86
+ public void testDeletingADetector_WorkflowUsageEnabled_WorkflowDoesntExist () throws IOException {
87
+ final String detectorId = setupDetector ();
88
+ final Map <String , Object > detectorSourceAsMap = getDetectorSourceAsMap (detectorId );
89
+
90
+ final String workflowId = ((List <String >) detectorSourceAsMap .get ("workflow_ids" )).get (0 );
91
+ final Response deleteWorkflowResponse = deleteAlertingWorkflow (workflowId );
92
+ assertEquals (200 , deleteWorkflowResponse .getStatusLine ().getStatusCode ());
93
+ entityAsMap (deleteWorkflowResponse );
94
+
95
+ validateDetectorDeletion (detectorId );
96
+ }
97
+
98
+ private String setupDetector () throws IOException {
99
+ final String index = createTestIndex (randomIndex (), windowsIndexMapping ());
76
100
77
101
// Execute CreateMappingsAction to add alias mapping for index
78
- Request createMappingRequest = new Request ("POST" , SecurityAnalyticsPlugin .MAPPER_BASE_URI );
102
+ final Request createMappingRequest = new Request ("POST" , SecurityAnalyticsPlugin .MAPPER_BASE_URI );
79
103
// both req params and req body are supported
80
104
createMappingRequest .setJsonEntity (
81
105
"{ \" index_name\" :\" " + index + "\" ," +
@@ -84,31 +108,40 @@ public void testDeletingADetector_MonitorNotExists() throws IOException {
84
108
"}"
85
109
);
86
110
87
- Response response = client ().performRequest (createMappingRequest );
111
+ final Response response = client ().performRequest (createMappingRequest );
88
112
assertEquals (HttpStatus .SC_OK , response .getStatusLine ().getStatusCode ());
89
- // Create detector #1 of type test_windows
90
- Detector detector1 = randomDetectorWithTriggers (getRandomPrePackagedRules (), List .of (new DetectorTrigger (null , "test-trigger" , "1" , List .of (randomDetectorType ()), List .of (), List .of (), List .of (), List .of ())));
91
- String detectorId1 = createDetector (detector1 );
92
113
93
- String request = "{\n " +
114
+ // Create detector of type test_windows
115
+ final DetectorTrigger detectorTrigger = new DetectorTrigger (null , "test-trigger" , "1" , List .of (randomDetectorType ()),
116
+ List .of (), List .of (), List .of (), List .of ());
117
+ final Detector detector = randomDetectorWithTriggers (getRandomPrePackagedRules (), List .of (detectorTrigger ));
118
+ return createDetector (detector );
119
+ }
120
+
121
+ private Map <String , Object > getDetectorSourceAsMap (final String detectorId ) throws IOException {
122
+ final String request = getDetectorQuery (detectorId );
123
+ final List <SearchHit > hits = executeSearch (Detector .DETECTORS_INDEX , request );
124
+ final SearchHit hit = hits .get (0 );
125
+ return (Map <String , Object >) hit .getSourceAsMap ().get ("detector" );
126
+ }
127
+
128
+ private String getDetectorQuery (final String detectorId ) {
129
+ return "{\n " +
94
130
" \" query\" : {\n " +
95
131
" \" match\" :{\n " +
96
- " \" _id\" : \" " + detectorId1 + "\" \n " +
132
+ " \" _id\" : \" " + detectorId + "\" \n " +
97
133
" }\n " +
98
134
" }\n " +
99
135
"}" ;
100
- List <SearchHit > hits = executeSearch (Detector .DETECTORS_INDEX , request );
101
- SearchHit hit = hits .get (0 );
102
-
103
- String monitorId = ((List <String >) ((Map <String , Object >) hit .getSourceAsMap ().get ("detector" )).get ("monitor_id" )).get (0 );
104
-
105
- Response deleteMonitorResponse = deleteAlertingMonitor (monitorId );
106
- assertEquals (200 , deleteMonitorResponse .getStatusLine ().getStatusCode ());
107
- entityAsMap (deleteMonitorResponse );
136
+ }
108
137
109
- Response deleteResponse = makeRequest (client (), "DELETE" , SecurityAnalyticsPlugin .DETECTOR_BASE_URI + "/" + detectorId1 , Collections .emptyMap (), null );
138
+ private void validateDetectorDeletion (final String detectorId ) throws IOException {
139
+ final Response deleteResponse = makeRequest (client (), "DELETE" , SecurityAnalyticsPlugin .DETECTOR_BASE_URI + "/" + detectorId ,
140
+ Collections .emptyMap (), null );
110
141
Assert .assertEquals ("Delete detector failed" , RestStatus .OK , restStatus (deleteResponse ));
111
- hits = executeSearch (Detector .DETECTORS_INDEX , request );
142
+
143
+ final String request = getDetectorQuery (detectorId );
144
+ final List <SearchHit > hits = executeSearch (Detector .DETECTORS_INDEX , request );
112
145
Assert .assertEquals (0 , hits .size ());
113
146
}
114
147
0 commit comments