diff --git a/security-command-center/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java b/security-command-center/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java index b89f766909d..3a5349b892f 100644 --- a/security-command-center/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java +++ b/security-command-center/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java @@ -53,6 +53,7 @@ private FindingSnippets() { * * @param sourceName The source for the finding. */ + // [START securitycenter_create_finding] static Finding createFinding(SourceName sourceName, String findingId) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ @@ -89,12 +90,14 @@ static Finding createFinding(SourceName sourceName, String findingId) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_create_finding] /** * Create a finding with source properties under a source. * * @param sourceName The source for the finding. */ + // [START securitycenter_create_finding_with_source_properties] static Finding createFindingWithSourceProperties(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ @@ -139,12 +142,14 @@ static Finding createFindingWithSourceProperties(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_create_finding_with_source_properties] /** * Update a finding's source properties. * * @param findingName The finding to update. */ + // [START securitycenter_update_finding_source_properties] static Finding updateFinding(FindingName findingName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // FindingName findingName = FindingName.of(/*organization=*/"123234324", @@ -184,12 +189,14 @@ static Finding updateFinding(FindingName findingName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_update_finding_source_properties] /** * Updates a finding's state to INACTIVE. * * @param findingName The finding to update. */ + // [START securitycenter_update_finding_state] static Finding setFindingState(FindingName findingName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // FindingName findingName = FindingName.of(/*organization=*/"123234324", @@ -213,12 +220,14 @@ static Finding setFindingState(FindingName findingName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_update_finding_state] /** * List all findings under an organization. * * @param organizationName The source to list all findings for. */ + // [START securitycenter_list_all_findings] static ImmutableList listAllFindings(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Input parameters for SourceName must be in one of the following formats: @@ -249,12 +258,14 @@ static ImmutableList listAllFindings(OrganizationName organi throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_list_all_findings] /** * List filtered findings under a source. * * @param sourceName The source to list filtered findings for. */ + // [START securitycenter_list_filtered_findings] static ImmutableList listFilteredFindings(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -283,6 +294,7 @@ static ImmutableList listFilteredFindings(SourceName sourceN throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_list_filtered_findings] /** * List findings at a specific time under a source. @@ -329,6 +341,7 @@ static ImmutableList listFindingsAtTime(SourceName sourceNam * * @param sourceName The source to create a finding for. */ + // [START securitycenter_test_iam] static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324", @@ -349,6 +362,7 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_test_iam] /** * Group all findings under an organization across all sources by their specified properties (e.g. @@ -356,6 +370,7 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { * * @param organizationName The organization to group all findings for. */ + // [START securitycenter_group_all_findings] static ImmutableList groupFindings(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Input parameters for 'SourceName' must be in one of the following formats: @@ -385,6 +400,7 @@ static ImmutableList groupFindings(OrganizationName organizationNam throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_group_all_findings] /** * Group findings under an organization and a source by their specified properties (e.g. @@ -392,6 +408,7 @@ static ImmutableList groupFindings(OrganizationName organizationNam * * @param sourceName The source to limit the findings to. */ + // [START securitycenter_group_findings_with_source] static ImmutableList groupFindingsWithSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -418,6 +435,7 @@ static ImmutableList groupFindingsWithSource(SourceName sourceName) throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_group_findings_with_source] /** * Group active findings under an organization and a source by their specified properties (e.g. @@ -425,6 +443,7 @@ static ImmutableList groupFindingsWithSource(SourceName sourceName) * * @param sourceName The source to limit the findings to. */ + // [START securitycenter_group_active_findings_with_source] static ImmutableList groupActiveFindingsWithSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -454,6 +473,7 @@ static ImmutableList groupActiveFindingsWithSource(SourceName sourc throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_group_active_findings_with_source] /** * Group active findings under an organization and a source by their specified properties (e.g. @@ -461,6 +481,7 @@ static ImmutableList groupActiveFindingsWithSource(SourceName sourc * * @param sourceName The source to limit the findings to. */ + // [START securitycenter_group_active_findings_with_source_at_time] static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -497,6 +518,7 @@ static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_group_active_findings_with_source_at_time] /** * Group active findings under an organization and a source by their state_changes @@ -504,6 +526,7 @@ static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName * * @param sourceName The source to limit the findings to. */ + // [START securitycenter_group_active_findings_with_source_and_compare_duration] static ImmutableList groupActiveFindingsWithSourceAndCompareDuration( SourceName sourceName, Duration duration) { try (SecurityCenterClient client = SecurityCenterClient.create()) { @@ -538,4 +561,5 @@ static ImmutableList groupActiveFindingsWithSourceAndCompareDuratio throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_group_active_findings_with_source_and_compare_duration] } diff --git a/security-command-center/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java b/security-command-center/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java index 1553e461a23..5224e49016c 100644 --- a/security-command-center/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java +++ b/security-command-center/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java @@ -46,6 +46,7 @@ private SourceSnippets() { * * @param organizationName The organization for the source. */ + // [START securitycenter_create_source] static Source createSource(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to create a source in an organization. @@ -68,12 +69,14 @@ static Source createSource(OrganizationName organizationName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_create_source] /** * List sources under an organization. * * @param organizationName The organization for the source. */ + // [START securitycenter_list_sources] static ImmutableList listSources(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to list sources in an organization, project, or folder. @@ -98,12 +101,14 @@ static ImmutableList listSources(OrganizationName organizationName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_list_sources] /** * Update a source under an organization. * * @param sourceName The source to update. */ + // [START securitycenter_update_source] static Source updateSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to update a source. @@ -128,12 +133,14 @@ static Source updateSource(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_update_source] /** * Get a source under an organization. * * @param sourceName The source to get. */ + // [START securitycenter_get_source] static Source getSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to get a source. @@ -151,12 +158,14 @@ static Source getSource(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_get_source] /** * Set IAM policy for a source. * * @param sourceName The source to set IAM Policy for. */ + // [START securitycenter_set_source_iam] static Policy setIamPolicySource(SourceName sourceName, String userEmail) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // userEmail = "someuser@domain.com" @@ -184,12 +193,14 @@ static Policy setIamPolicySource(SourceName sourceName, String userEmail) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_set_source_iam] /** * Get IAM policy for a source. * * @param sourceName The source to set IAM Policy for. */ + // [START securitycenter_get_source_iam] static Policy getIamPolicySource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to get IAM policy for a source. @@ -207,5 +218,6 @@ static Policy getIamPolicySource(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } + // [END securitycenter_get_source_iam] -} \ No newline at end of file +} diff --git a/security-command-center/snippets/src/test/java/MuteFindingIT.java b/security-command-center/snippets/src/test/java/MuteFindingIT.java index 5b162332df1..9910ef150e8 100644 --- a/security-command-center/snippets/src/test/java/MuteFindingIT.java +++ b/security-command-center/snippets/src/test/java/MuteFindingIT.java @@ -25,6 +25,8 @@ import com.google.cloud.securitycenter.v1.Finding.State; import com.google.cloud.securitycenter.v1.ListFindingsRequest; import com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult; +import com.google.cloud.securitycenter.v1.ListMuteConfigsRequest; +import com.google.cloud.securitycenter.v1.MuteConfig; import com.google.cloud.securitycenter.v1.SecurityCenterClient; import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse; import com.google.cloud.securitycenter.v1.Source; @@ -76,16 +78,41 @@ public static void requireEnvVar(String envVarName) { .isNotEmpty(); } + private static void cleanUpExistingMuteRules() { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + ListMuteConfigsRequest request = + ListMuteConfigsRequest.newBuilder() + .setParent(String.format("projects/%s", PROJECT_ID)) + .build(); + for (MuteConfig muteConfig : client.listMuteConfigs(request).iterateAll()) { + String name = muteConfig.getName(); + String muteConfigId = name.substring(name.lastIndexOf('/') + 1); + if (muteConfigId.startsWith("random-mute-id-")) { + try { + client.deleteMuteConfig(name); + } catch (Exception e) { + System.err.println("Failed to delete mute rule: " + name); + e.printStackTrace(); + } + } + } + } catch (Exception e) { + System.err.println("Warning: Pre-test cleanup of mute rules failed: " + e.getMessage()); + } + } + @BeforeClass public static void setUp() throws IOException { - final PrintStream out = System.out; - stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("SCC_PROJECT_ID"); requireEnvVar("SCC_PROJECT_ORG_ID"); + cleanUpExistingMuteRules(); + + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + // Create mute rules. CreateMuteRule.createMuteRule(String.format("projects/%s", PROJECT_ID), MUTE_RULE_CREATE); CreateMuteRule.createMuteRule(String.format("projects/%s", PROJECT_ID), MUTE_RULE_UPDATE); @@ -106,10 +133,19 @@ public static void cleanUp() { final PrintStream out = System.out; stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_CREATE); - assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_CREATE); - DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_UPDATE); - assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_UPDATE); + try { + DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_CREATE); + } catch (Exception e) { + System.err.println("Failed to delete mute rule: " + MUTE_RULE_CREATE); + e.printStackTrace(); + } + try { + DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_UPDATE); + } catch (Exception e) { + System.err.println("Failed to delete mute rule: " + MUTE_RULE_UPDATE); + e.printStackTrace(); + } + cleanUpExistingMuteRules(); stdOut = null; System.setOut(out); } diff --git a/security-command-center/snippets/src/test/java/management/api/EventThreatDetectionCustomModuleTest.java b/security-command-center/snippets/src/test/java/management/api/EventThreatDetectionCustomModuleTest.java index cbce3e4632e..ab47e2a7aaa 100644 --- a/security-command-center/snippets/src/test/java/management/api/EventThreatDetectionCustomModuleTest.java +++ b/security-command-center/snippets/src/test/java/management/api/EventThreatDetectionCustomModuleTest.java @@ -34,7 +34,7 @@ import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.StreamSupport; +import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; @@ -47,7 +47,7 @@ public class EventThreatDetectionCustomModuleTest { // TODO(Developer): Replace the below variable private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); private static final String CUSTOM_MODULE_DISPLAY_NAME = - "java_sample_etd_custom_module_test_" + UUID.randomUUID(); + "java_sample_etd_custom_module_test_" + UUID.randomUUID().toString().replace("-", "_"); private static final int MAX_ATTEMPT_COUNT = 3; private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes private static List createdCustomModuleIds = new ArrayList<>(); @@ -67,6 +67,10 @@ public static void requireEnvVar(String envVarName) { public static void setUp() { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("SCC_PROJECT_ID"); + cleanUpExistingCustomModules(); + } + + private static void cleanUpExistingCustomModules() { try { ImmutableList response = ListEventThreatDetectionCustomModules @@ -83,10 +87,9 @@ public static void setUp() { } } - @AfterClass - // Perform cleanup of all the custom modules created by the current execution of the test, after - // running tests - public static void cleanUp() throws IOException { + @After + // Perform cleanup of custom modules created by the current test method execution + public void tearDown() throws IOException { for (String customModuleId : createdCustomModuleIds) { try { deleteCustomModule(PROJECT_ID, customModuleId); @@ -95,6 +98,13 @@ public static void cleanUp() throws IOException { e.printStackTrace(); } } + createdCustomModuleIds.clear(); + } + + @AfterClass + // Final cleanup sweep after all tests in class have run + public static void cleanUp() throws IOException { + cleanUpExistingCustomModules(); } // extractCustomModuleID extracts the custom module Id from the full name and below regex will @@ -135,9 +145,14 @@ public void testDeleteEventThreatDetectionCustomModule() throws IOException { CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule( PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME); String customModuleId = extractCustomModuleId(response.getName()); - assertTrue( + createdCustomModuleIds.add(customModuleId); + boolean isDeleted = DeleteEventThreatDetectionCustomModule.deleteEventThreatDetectionCustomModule( - PROJECT_ID, customModuleId)); + PROJECT_ID, customModuleId); + if (isDeleted) { + createdCustomModuleIds.remove(customModuleId); + } + assertTrue(isDeleted); } @Test diff --git a/security-command-center/snippets/src/test/java/management/api/SecurityHealthAnalyticsCustomModuleTest.java b/security-command-center/snippets/src/test/java/management/api/SecurityHealthAnalyticsCustomModuleTest.java index 852e2617986..d7972e85d76 100644 --- a/security-command-center/snippets/src/test/java/management/api/SecurityHealthAnalyticsCustomModuleTest.java +++ b/security-command-center/snippets/src/test/java/management/api/SecurityHealthAnalyticsCustomModuleTest.java @@ -31,9 +31,10 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.StreamSupport; +import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; @@ -45,7 +46,8 @@ public class SecurityHealthAnalyticsCustomModuleTest { // TODO(Developer): Replace the below variable private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); - private static final String CUSTOM_MODULE_DISPLAY_NAME = "java_sample_custom_module_test"; + private static final String CUSTOM_MODULE_DISPLAY_NAME = + "java_sample_custom_module_test_" + UUID.randomUUID().toString().replace("-", "_"); private static final int MAX_ATTEMPT_COUNT = 3; private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes private static List createdCustomModuleIds = new ArrayList<>(); @@ -65,12 +67,17 @@ public static void requireEnvVar(String envVarName) { public static void setUp() throws InterruptedException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("SCC_PROJECT_ID"); + cleanUpExistingCustomModules(); + } + + private static void cleanUpExistingCustomModules() { try { ImmutableList response = ListSecurityHealthAnalyticsCustomModules .listSecurityHealthAnalyticsCustomModules(PROJECT_ID); for (SecurityHealthAnalyticsCustomModule module : response) { - if (CUSTOM_MODULE_DISPLAY_NAME.equals(module.getDisplayName())) { + if (module.getDisplayName() != null + && module.getDisplayName().startsWith("java_sample_custom_module_test")) { deleteCustomModule(PROJECT_ID, extractCustomModuleId(module.getName())); } } @@ -80,10 +87,9 @@ public static void setUp() throws InterruptedException { } } - @AfterClass - // Perform cleanup of all the custom modules created by the current execution of the test, after - // running tests - public static void cleanUp() throws IOException { + @After + // Perform cleanup of custom modules created by the current test method execution + public void tearDown() throws IOException { for (String customModuleId : createdCustomModuleIds) { try { deleteCustomModule(PROJECT_ID, customModuleId); @@ -92,6 +98,13 @@ public static void cleanUp() throws IOException { e.printStackTrace(); } } + createdCustomModuleIds.clear(); + } + + @AfterClass + // Final cleanup sweep after all tests in class have run + public static void cleanUp() throws IOException { + cleanUpExistingCustomModules(); } // extractCustomModuleID extracts the custom module Id from the full name and below regex will @@ -132,9 +145,14 @@ public void testDeleteSecurityHealthAnalyticsCustomModule() throws IOException { CreateSecurityHealthAnalyticsCustomModule.createSecurityHealthAnalyticsCustomModule( PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME); String customModuleId = extractCustomModuleId(response.getName()); - assertTrue( + createdCustomModuleIds.add(customModuleId); + boolean isDeleted = DeleteSecurityHealthAnalyticsCustomModule.deleteSecurityHealthAnalyticsCustomModule( - PROJECT_ID, customModuleId)); + PROJECT_ID, customModuleId); + if (isDeleted) { + createdCustomModuleIds.remove(customModuleId); + } + assertTrue(isDeleted); } @Test diff --git a/security-command-center/snippets/src/test/java/vtwo/MuteFindingIT.java b/security-command-center/snippets/src/test/java/vtwo/MuteFindingIT.java index f5028bf406e..c7bdb2c55f2 100644 --- a/security-command-center/snippets/src/test/java/vtwo/MuteFindingIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/MuteFindingIT.java @@ -23,6 +23,7 @@ import com.google.cloud.securitycenter.v2.Finding.Mute; import com.google.cloud.securitycenter.v2.ListFindingsRequest; import com.google.cloud.securitycenter.v2.ListFindingsResponse.ListFindingsResult; +import com.google.cloud.securitycenter.v2.ListMuteConfigsRequest; import com.google.cloud.securitycenter.v2.MuteConfig; import com.google.cloud.securitycenter.v2.SecurityCenterClient; import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListFindingsPagedResponse; @@ -85,16 +86,41 @@ public static void requireEnvVar(String envVarName) { .isNotEmpty(); } + private static void cleanUpExistingMuteRules() { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + ListMuteConfigsRequest request = + ListMuteConfigsRequest.newBuilder() + .setParent(String.format("projects/%s/locations/%s", PROJECT_ID, LOCATION)) + .build(); + for (MuteConfig muteConfig : client.listMuteConfigs(request).iterateAll()) { + String name = muteConfig.getName(); + String muteConfigId = name.substring(name.lastIndexOf('/') + 1); + if (muteConfigId.startsWith("random-mute-id-")) { + try { + client.deleteMuteConfig(name); + } catch (Exception e) { + System.err.println("Failed to delete mute rule: " + name); + e.printStackTrace(); + } + } + } + } catch (Exception e) { + System.err.println("Warning: Pre-test cleanup of mute rules failed: " + e.getMessage()); + } + } + @BeforeClass public static void setUp() throws IOException, InterruptedException { - final PrintStream out = System.out; - stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("SCC_PROJECT_ID"); requireEnvVar("SCC_PROJECT_ORG_ID"); + cleanUpExistingMuteRules(); + + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + // Create mute rules. CreateMuteRule.createMuteRule(PROJECT_ID, LOCATION, MUTE_RULE_CREATE); CreateMuteRule.createMuteRule(PROJECT_ID, LOCATION, MUTE_RULE_UPDATE); @@ -131,10 +157,19 @@ public static void cleanUp() throws IOException { final PrintStream out = System.out; stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - DeleteMuteRule.deleteMuteRule(PROJECT_ID, LOCATION, MUTE_RULE_CREATE); - assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_CREATE); - DeleteMuteRule.deleteMuteRule(PROJECT_ID, LOCATION, MUTE_RULE_UPDATE); - assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_UPDATE); + try { + DeleteMuteRule.deleteMuteRule(PROJECT_ID, LOCATION, MUTE_RULE_CREATE); + } catch (Exception e) { + System.err.println("Failed to delete mute rule: " + MUTE_RULE_CREATE); + e.printStackTrace(); + } + try { + DeleteMuteRule.deleteMuteRule(PROJECT_ID, LOCATION, MUTE_RULE_UPDATE); + } catch (Exception e) { + System.err.println("Failed to delete mute rule: " + MUTE_RULE_UPDATE); + e.printStackTrace(); + } + cleanUpExistingMuteRules(); stdOut = null; System.setOut(out); }