@@ -1877,4 +1877,32 @@ public void testCreatingDetectorWithDynamicQueryIndexEnabledAndThenDisabled() th
18771877 Assert .assertEquals ("Create detector failed" , RestStatus .CREATED , restStatus (createResponse ));
18781878 Assert .assertTrue (doesIndexExist (DetectorMonitorConfig .getRuleIndex (randomDetectorType ()) + "-000001" ));
18791879 }
1880+
1881+ public void testDeletingDetectorEditedToHaveZeroRules () throws IOException {
1882+ String index = createTestIndex (randomIndex (), windowsIndexMapping ());
1883+
1884+ Request createMappingRequest = new Request ("POST" , SecurityAnalyticsPlugin .MAPPER_BASE_URI );
1885+ createMappingRequest .setJsonEntity (
1886+ "{ \" index_name\" :\" " + index + "\" ," +
1887+ " \" rule_topic\" :\" " + randomDetectorType () + "\" , " +
1888+ " \" partial\" :true" +
1889+ "}"
1890+ );
1891+ Response response = client ().performRequest (createMappingRequest );
1892+ assertEquals (HttpStatus .SC_OK , response .getStatusLine ().getStatusCode ());
1893+
1894+ // Create a test detector that has at least 1 rule.
1895+ Detector detector = randomDetectorWithTriggers (getRandomPrePackagedRules (), List .of (new DetectorTrigger (null , "test-trigger" , "1" , List .of (randomDetectorType ()), List .of (), List .of (), List .of (), List .of (), List .of ())));
1896+ String detectorId = createDetector (detector );
1897+
1898+ // Update the detector to have 0 rules. This should update the detector's 'monitor_id' attribute to be an empty array.
1899+ DetectorInput input = new DetectorInput ("detector with no rules" , List .of (index ), List .of (), List .of ());
1900+ Detector updatedDetector = randomDetectorWithInputs (List .of (input ));
1901+
1902+ Response updateResponse = makeRequest (client (), "PUT" , SecurityAnalyticsPlugin .DETECTOR_BASE_URI + "/" + detectorId , Collections .emptyMap (), toHttpEntity (updatedDetector ));
1903+ Assert .assertEquals ("Update detector failed" , RestStatus .OK , restStatus (updateResponse ));
1904+
1905+ Response deleteResponse = makeRequest (client (), "DELETE" , SecurityAnalyticsPlugin .DETECTOR_BASE_URI + "/" + detectorId , Collections .emptyMap (), null );
1906+ Assert .assertEquals ("Delete detector failed" , RestStatus .OK , restStatus (deleteResponse ));
1907+ }
18801908}
0 commit comments