24
24
import com .google .cloud .securitycentermanagement .v1 .EffectiveEventThreatDetectionCustomModule ;
25
25
import com .google .cloud .securitycentermanagement .v1 .EventThreatDetectionCustomModule ;
26
26
import com .google .cloud .securitycentermanagement .v1 .EventThreatDetectionCustomModule .EnablementState ;
27
- import com .google .cloud .securitycentermanagement .v1 .ListEventThreatDetectionCustomModulesRequest ;
28
- import com .google .cloud .securitycentermanagement .v1 .SecurityCenterManagementClient ;
29
27
import com .google .cloud .securitycentermanagement .v1 .SecurityCenterManagementClient .ListDescendantEventThreatDetectionCustomModulesPagedResponse ;
30
28
import com .google .cloud .securitycentermanagement .v1 .SecurityCenterManagementClient .ListEffectiveEventThreatDetectionCustomModulesPagedResponse ;
31
29
import com .google .cloud .securitycentermanagement .v1 .SecurityCenterManagementClient .ListEventThreatDetectionCustomModulesPagedResponse ;
32
30
import com .google .cloud .securitycentermanagement .v1 .ValidateEventThreatDetectionCustomModuleResponse ;
33
31
import com .google .cloud .testing .junit4 .MultipleAttemptsRule ;
34
32
import com .google .common .base .Strings ;
35
33
import java .io .IOException ;
34
+ import java .util .ArrayList ;
35
+ import java .util .List ;
36
36
import java .util .UUID ;
37
37
import java .util .regex .Matcher ;
38
38
import java .util .regex .Pattern ;
@@ -52,6 +52,7 @@ public class EventThreatDetectionCustomModuleTest {
52
52
"java_sample_etd_custom_module_test_" + UUID .randomUUID ();
53
53
private static final int MAX_ATTEMPT_COUNT = 3 ;
54
54
private static final int INITIAL_BACKOFF_MILLIS = 120000 ; // 2 minutes
55
+ private static List <String > createdCustomModuleIds = new ArrayList <>();
55
56
56
57
@ Rule
57
58
public final MultipleAttemptsRule multipleAttemptsRule =
@@ -71,36 +72,16 @@ public static void setUp() {
71
72
}
72
73
73
74
@ AfterClass
75
+ // Perform cleanup of all the custom modules created by the current execution of the test, after
76
+ // running tests
74
77
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 ();
100
84
}
101
- } catch (Exception e ) {
102
- System .err .println ("Failed to process cleanupExistingCustomModules." );
103
- e .printStackTrace ();
104
85
}
105
86
}
106
87
@@ -131,6 +112,7 @@ public void testCreateEventThreatDetectionCustomModule() throws IOException {
131
112
EventThreatDetectionCustomModule response =
132
113
CreateEventThreatDetectionCustomModule .createEventThreatDetectionCustomModule (
133
114
PROJECT_ID , CUSTOM_MODULE_DISPLAY_NAME );
115
+ createdCustomModuleIds .add (extractCustomModuleId (response .getName ()));
134
116
assertNotNull (response );
135
117
assertThat (response .getDisplayName ()).isEqualTo (CUSTOM_MODULE_DISPLAY_NAME );
136
118
}
@@ -148,8 +130,10 @@ public void testDeleteEventThreatDetectionCustomModule() throws IOException {
148
130
149
131
@ Test
150
132
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 ()));
153
137
ListEventThreatDetectionCustomModulesPagedResponse response =
154
138
ListEventThreatDetectionCustomModules .listEventThreatDetectionCustomModules (PROJECT_ID );
155
139
assertTrue (
@@ -163,6 +147,7 @@ public void testGetEventThreatDetectionCustomModule() throws IOException {
163
147
CreateEventThreatDetectionCustomModule .createEventThreatDetectionCustomModule (
164
148
PROJECT_ID , CUSTOM_MODULE_DISPLAY_NAME );
165
149
String customModuleId = extractCustomModuleId (response .getName ());
150
+ createdCustomModuleIds .add (customModuleId );
166
151
EventThreatDetectionCustomModule getCustomModuleResponse =
167
152
GetEventThreatDetectionCustomModule .getEventThreatDetectionCustomModule (
168
153
PROJECT_ID , customModuleId );
@@ -177,6 +162,7 @@ public void testUpdateEventThreatDetectionCustomModule() throws IOException {
177
162
CreateEventThreatDetectionCustomModule .createEventThreatDetectionCustomModule (
178
163
PROJECT_ID , CUSTOM_MODULE_DISPLAY_NAME );
179
164
String customModuleId = extractCustomModuleId (createCustomModuleResponse .getName ());
165
+ createdCustomModuleIds .add (customModuleId );
180
166
EventThreatDetectionCustomModule response =
181
167
UpdateEventThreatDetectionCustomModule .updateEventThreatDetectionCustomModule (
182
168
PROJECT_ID , customModuleId );
@@ -190,6 +176,7 @@ public void testGetEffectiveEventThreatDetectionCustomModule() throws IOExceptio
190
176
CreateEventThreatDetectionCustomModule .createEventThreatDetectionCustomModule (
191
177
PROJECT_ID , CUSTOM_MODULE_DISPLAY_NAME );
192
178
String customModuleId = extractCustomModuleId (createCustomModuleResponse .getName ());
179
+ createdCustomModuleIds .add (customModuleId );
193
180
EffectiveEventThreatDetectionCustomModule getEffectiveCustomModuleResponse =
194
181
GetEffectiveEventThreatDetectionCustomModule .getEffectiveEventThreatDetectionCustomModule (
195
182
PROJECT_ID , customModuleId );
@@ -202,8 +189,10 @@ public void testGetEffectiveEventThreatDetectionCustomModule() throws IOExceptio
202
189
203
190
@ Test
204
191
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 ()));
207
196
ListEffectiveEventThreatDetectionCustomModulesPagedResponse response =
208
197
ListEffectiveEventThreatDetectionCustomModules
209
198
.listEffectiveEventThreatDetectionCustomModules (PROJECT_ID );
@@ -214,8 +203,10 @@ public void testListEffectiveEventThreatDetectionCustomModules() throws IOExcept
214
203
215
204
@ Test
216
205
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 ()));
219
210
ListDescendantEventThreatDetectionCustomModulesPagedResponse response =
220
211
ListDescendantEventThreatDetectionCustomModules
221
212
.listDescendantEventThreatDetectionCustomModules (PROJECT_ID );
0 commit comments