Skip to content

Commit 57fc720

Browse files
authored
fix(securitycenter) issue related to cleanup logic in testclass of ETD custom module (GoogleCloudPlatform#9973)
1 parent 42c928e commit 57fc720

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

security-command-center/snippets/src/test/java/management/api/EventThreatDetectionCustomModuleTest.java

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
import com.google.cloud.securitycentermanagement.v1.EffectiveEventThreatDetectionCustomModule;
2525
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
2626
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule.EnablementState;
27-
import com.google.cloud.securitycentermanagement.v1.ListEventThreatDetectionCustomModulesRequest;
28-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
2927
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListDescendantEventThreatDetectionCustomModulesPagedResponse;
3028
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEffectiveEventThreatDetectionCustomModulesPagedResponse;
3129
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEventThreatDetectionCustomModulesPagedResponse;
3230
import com.google.cloud.securitycentermanagement.v1.ValidateEventThreatDetectionCustomModuleResponse;
3331
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
3432
import com.google.common.base.Strings;
3533
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.List;
3636
import java.util.UUID;
3737
import java.util.regex.Matcher;
3838
import java.util.regex.Pattern;
@@ -52,6 +52,7 @@ public class EventThreatDetectionCustomModuleTest {
5252
"java_sample_etd_custom_module_test_" + UUID.randomUUID();
5353
private static final int MAX_ATTEMPT_COUNT = 3;
5454
private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes
55+
private static List<String> createdCustomModuleIds = new ArrayList<>();
5556

5657
@Rule
5758
public final MultipleAttemptsRule multipleAttemptsRule =
@@ -71,36 +72,16 @@ public static void setUp() {
7172
}
7273

7374
@AfterClass
75+
// Perform cleanup of all the custom modules created by the current execution of the test, after
76+
// running tests
7477
public static void cleanUp() throws IOException {
75-
// Perform cleanup after running tests
76-
cleanupExistingCustomModules();
77-
}
78-
79-
// cleanupExistingCustomModules clean up all the existing custom module
80-
private static void cleanupExistingCustomModules() throws IOException {
81-
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
82-
ListEventThreatDetectionCustomModulesRequest request =
83-
ListEventThreatDetectionCustomModulesRequest.newBuilder()
84-
.setParent(String.format("projects/%s/locations/global", PROJECT_ID))
85-
.build();
86-
ListEventThreatDetectionCustomModulesPagedResponse response =
87-
client.listEventThreatDetectionCustomModules(request);
88-
// Iterate over the response and delete custom module one by one which start with
89-
// java_sample_custom_module
90-
for (EventThreatDetectionCustomModule module : response.iterateAll()) {
91-
try {
92-
if (module.getDisplayName().startsWith("java_sample_etd_custom_module")) {
93-
String customModuleId = extractCustomModuleId(module.getName());
94-
deleteCustomModule(PROJECT_ID, customModuleId);
95-
}
96-
} catch (Exception e) {
97-
System.err.println("Failed to delete module: " + module.getDisplayName());
98-
e.printStackTrace();
99-
}
78+
for (String customModuleId : createdCustomModuleIds) {
79+
try {
80+
deleteCustomModule(PROJECT_ID, customModuleId);
81+
} catch (Exception e) {
82+
System.err.println("Failed to delete module: " + customModuleId);
83+
e.printStackTrace();
10084
}
101-
} catch (Exception e) {
102-
System.err.println("Failed to process cleanupExistingCustomModules.");
103-
e.printStackTrace();
10485
}
10586
}
10687

@@ -131,6 +112,7 @@ public void testCreateEventThreatDetectionCustomModule() throws IOException {
131112
EventThreatDetectionCustomModule response =
132113
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
133114
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
115+
createdCustomModuleIds.add(extractCustomModuleId(response.getName()));
134116
assertNotNull(response);
135117
assertThat(response.getDisplayName()).isEqualTo(CUSTOM_MODULE_DISPLAY_NAME);
136118
}
@@ -148,8 +130,10 @@ public void testDeleteEventThreatDetectionCustomModule() throws IOException {
148130

149131
@Test
150132
public void testListEventThreatDetectionCustomModules() throws IOException {
151-
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
152-
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
133+
EventThreatDetectionCustomModule createCustomModuleResponse =
134+
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
135+
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
136+
createdCustomModuleIds.add(extractCustomModuleId(createCustomModuleResponse.getName()));
153137
ListEventThreatDetectionCustomModulesPagedResponse response =
154138
ListEventThreatDetectionCustomModules.listEventThreatDetectionCustomModules(PROJECT_ID);
155139
assertTrue(
@@ -163,6 +147,7 @@ public void testGetEventThreatDetectionCustomModule() throws IOException {
163147
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
164148
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
165149
String customModuleId = extractCustomModuleId(response.getName());
150+
createdCustomModuleIds.add(customModuleId);
166151
EventThreatDetectionCustomModule getCustomModuleResponse =
167152
GetEventThreatDetectionCustomModule.getEventThreatDetectionCustomModule(
168153
PROJECT_ID, customModuleId);
@@ -177,6 +162,7 @@ public void testUpdateEventThreatDetectionCustomModule() throws IOException {
177162
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
178163
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
179164
String customModuleId = extractCustomModuleId(createCustomModuleResponse.getName());
165+
createdCustomModuleIds.add(customModuleId);
180166
EventThreatDetectionCustomModule response =
181167
UpdateEventThreatDetectionCustomModule.updateEventThreatDetectionCustomModule(
182168
PROJECT_ID, customModuleId);
@@ -190,6 +176,7 @@ public void testGetEffectiveEventThreatDetectionCustomModule() throws IOExceptio
190176
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
191177
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
192178
String customModuleId = extractCustomModuleId(createCustomModuleResponse.getName());
179+
createdCustomModuleIds.add(customModuleId);
193180
EffectiveEventThreatDetectionCustomModule getEffectiveCustomModuleResponse =
194181
GetEffectiveEventThreatDetectionCustomModule.getEffectiveEventThreatDetectionCustomModule(
195182
PROJECT_ID, customModuleId);
@@ -202,8 +189,10 @@ public void testGetEffectiveEventThreatDetectionCustomModule() throws IOExceptio
202189

203190
@Test
204191
public void testListEffectiveEventThreatDetectionCustomModules() throws IOException {
205-
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
206-
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
192+
EventThreatDetectionCustomModule createCustomModuleResponse =
193+
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
194+
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
195+
createdCustomModuleIds.add(extractCustomModuleId(createCustomModuleResponse.getName()));
207196
ListEffectiveEventThreatDetectionCustomModulesPagedResponse response =
208197
ListEffectiveEventThreatDetectionCustomModules
209198
.listEffectiveEventThreatDetectionCustomModules(PROJECT_ID);
@@ -214,8 +203,10 @@ public void testListEffectiveEventThreatDetectionCustomModules() throws IOExcept
214203

215204
@Test
216205
public void testListDescendantEventThreatDetectionCustomModules() throws IOException {
217-
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
218-
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
206+
EventThreatDetectionCustomModule createCustomModuleResponse =
207+
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
208+
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
209+
createdCustomModuleIds.add(extractCustomModuleId(createCustomModuleResponse.getName()));
219210
ListDescendantEventThreatDetectionCustomModulesPagedResponse response =
220211
ListDescendantEventThreatDetectionCustomModules
221212
.listDescendantEventThreatDetectionCustomModules(PROJECT_ID);

0 commit comments

Comments
 (0)