23
23
import org .opensearch .client .ResponseException ;
24
24
import org .opensearch .commons .alerting .model .IntervalSchedule ;
25
25
import org .opensearch .commons .alerting .model .Monitor .MonitorType ;
26
+ import org .opensearch .commons .alerting .model .ScheduledJob ;
26
27
import org .opensearch .core .rest .RestStatus ;
27
28
import org .opensearch .core .xcontent .MediaTypeRegistry ;
28
29
import org .opensearch .search .SearchHit ;
@@ -72,10 +73,34 @@ public void testNewLogTypes() throws IOException {
72
73
@ SuppressWarnings ("unchecked" )
73
74
public void testDeletingADetector_MonitorNotExists () throws IOException {
74
75
updateClusterSetting (ENABLE_WORKFLOW_USAGE .getKey (), "false" );
75
- String index = createTestIndex (randomIndex (), windowsIndexMapping ());
76
+ final String detectorId = setupDetector ();
77
+ final Map <String , Object > detectorSourceAsMap = getDetectorSourceAsMap (detectorId );
78
+
79
+ final String monitorId = ((List <String >) detectorSourceAsMap .get ("monitor_id" )).get (0 );
80
+ final Response deleteMonitorResponse = deleteAlertingMonitor (monitorId );
81
+ assertEquals (200 , deleteMonitorResponse .getStatusLine ().getStatusCode ());
82
+ entityAsMap (deleteMonitorResponse );
83
+
84
+ validateDetectorDeletion (detectorId );
85
+ }
86
+
87
+ public void testDeletingADetector_WorkflowUsageEnabled_WorkflowDoesntExist () throws IOException {
88
+ final String detectorId = setupDetector ();
89
+ final Map <String , Object > detectorSourceAsMap = getDetectorSourceAsMap (detectorId );
90
+
91
+ final String workflowId = ((List <String >) detectorSourceAsMap .get ("workflow_ids" )).get (0 );
92
+ final Response deleteWorkflowResponse = deleteAlertingWorkflow (workflowId );
93
+ assertEquals (200 , deleteWorkflowResponse .getStatusLine ().getStatusCode ());
94
+ entityAsMap (deleteWorkflowResponse );
95
+
96
+ validateDetectorDeletion (detectorId );
97
+ }
98
+
99
+ private String setupDetector () throws IOException {
100
+ final String index = createTestIndex (randomIndex (), windowsIndexMapping ());
76
101
77
102
// Execute CreateMappingsAction to add alias mapping for index
78
- Request createMappingRequest = new Request ("POST" , SecurityAnalyticsPlugin .MAPPER_BASE_URI );
103
+ final Request createMappingRequest = new Request ("POST" , SecurityAnalyticsPlugin .MAPPER_BASE_URI );
79
104
// both req params and req body are supported
80
105
createMappingRequest .setJsonEntity (
81
106
"{ \" index_name\" :\" " + index + "\" ," +
@@ -84,31 +109,39 @@ public void testDeletingADetector_MonitorNotExists() throws IOException {
84
109
"}"
85
110
);
86
111
87
- Response response = client ().performRequest (createMappingRequest );
112
+ final Response response = client ().performRequest (createMappingRequest );
88
113
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 (), List .of ())));
91
- String detectorId1 = createDetector (detector1 );
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 (), List .of ());
117
+ final Detector detector = randomDetectorWithTriggers (getRandomPrePackagedRules (), List .of (detectorTrigger ));
118
+ return createDetector (detector );
119
+ }
92
120
93
- String request = "{\n " +
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