2323import org .opensearch .client .ResponseException ;
2424import org .opensearch .commons .alerting .model .IntervalSchedule ;
2525import org .opensearch .commons .alerting .model .Monitor .MonitorType ;
26+ import org .opensearch .commons .alerting .model .ScheduledJob ;
2627import org .opensearch .core .rest .RestStatus ;
2728import org .opensearch .core .xcontent .MediaTypeRegistry ;
2829import org .opensearch .search .SearchHit ;
@@ -72,10 +73,34 @@ public void testNewLogTypes() throws IOException {
7273 @ SuppressWarnings ("unchecked" )
7374 public void testDeletingADetector_MonitorNotExists () throws IOException {
7475 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 ());
76101
77102 // 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 );
79104 // both req params and req body are supported
80105 createMappingRequest .setJsonEntity (
81106 "{ \" index_name\" :\" " + index + "\" ," +
@@ -84,31 +109,39 @@ public void testDeletingADetector_MonitorNotExists() throws IOException {
84109 "}"
85110 );
86111
87- Response response = client ().performRequest (createMappingRequest );
112+ final Response response = client ().performRequest (createMappingRequest );
88113 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+ }
92120
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 " +
94130 " \" query\" : {\n " +
95131 " \" match\" :{\n " +
96- " \" _id\" : \" " + detectorId1 + "\" \n " +
132+ " \" _id\" : \" " + detectorId + "\" \n " +
97133 " }\n " +
98134 " }\n " +
99135 "}" ;
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+ }
108137
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 );
110141 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 );
112145 Assert .assertEquals (0 , hits .size ());
113146 }
114147
0 commit comments