Skip to content

Commit 81c4eae

Browse files
owenhuynOwen Huyn
andauthored
[SCC] Fix list notification configs test. (#9580)
Co-authored-by: Owen Huyn <[email protected]>
1 parent dd24a49 commit 81c4eae

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,28 @@ public static void main(String[] args) throws IOException {
4242
public static boolean deleteNotificationConfig(String parentId, String location,
4343
String notificationConfigId)
4444
throws IOException {
45+
return deleteNotificationConfig(String.format("projects/%s/locations/%s/notificationConfigs/%s",
46+
parentId,
47+
location,
48+
notificationConfigId));
49+
}
50+
51+
// Delete a notification config.
52+
// Ensure the ServiceAccount has the "securitycenter.notification.delete" permission
53+
public static boolean deleteNotificationConfig(String name)
54+
throws IOException {
4555
// Initialize client that will be used to send requests. This client only needs to be created
4656
// once, and can be reused for multiple requests. After completing all of your requests, call
4757
// the "close" method on the client to safely clean up any remaining background resources.
4858
try (SecurityCenterClient client = SecurityCenterClient.create()) {
4959

5060
DeleteNotificationConfigRequest request = DeleteNotificationConfigRequest.newBuilder()
51-
.setName(String.format("projects/%s/locations/%s/notificationConfigs/%s",
52-
parentId,
53-
location,
54-
notificationConfigId))
61+
.setName(name)
5562
.build();
5663

5764
client.deleteNotificationConfig(request);
5865

59-
System.out.printf("Deleted Notification config: %s%n", notificationConfigId);
66+
System.out.printf("Deleted Notification config: %s%n", name);
6067
}
6168
return true;
6269
}

security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static ImmutableList<NotificationConfig> listNotificationConfigs(String p
5252
.setParent(String.format("projects/%s/locations/%s",
5353
parentId,
5454
location))
55+
.setPageSize(500)
5556
.build();
5657

5758
ListNotificationConfigsPagedResponse response = client.listNotificationConfigs(

security-command-center/snippets/src/test/java/vtwo/NotificationIT.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121

22+
import com.google.api.gax.rpc.AlreadyExistsException;
2223
import com.google.cloud.pubsub.v1.TopicAdminClient;
2324
import com.google.cloud.securitycenter.v2.NotificationConfig;
2425
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
26+
import com.google.common.collect.ImmutableList;
2527
import com.google.pubsub.v1.ProjectTopicName;
2628
import java.io.ByteArrayOutputStream;
2729
import java.io.IOException;
2830
import java.io.PrintStream;
29-
import java.util.UUID;
3031
import org.junit.After;
3132
import org.junit.AfterClass;
3233
import org.junit.Before;
@@ -49,8 +50,8 @@ public class NotificationIT {
4950
private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID");
5051
private static final String LOCATION = "global";
5152
private static final String NOTIFICATION_RULE_CREATE =
52-
"random-notification-id-" + UUID.randomUUID();
53-
private static final String NOTIFICATION_TOPIC = "test-topic-" + UUID.randomUUID();
53+
"random-notification-id";
54+
private static final String NOTIFICATION_TOPIC = "test-topic-for-testing";
5455
private static final int MAX_ATTEMPT_COUNT = 3;
5556
private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes
5657
private static ByteArrayOutputStream stdOut;
@@ -76,8 +77,12 @@ public static void setUp() throws IOException, InterruptedException {
7677
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
7778
requireEnvVar("SCC_PROJECT_ID");
7879

79-
// Create pubsub topic.
80-
createPubSubTopic(PROJECT_ID, NOTIFICATION_TOPIC);
80+
try {
81+
// Create pubsub topic.
82+
createPubSubTopic(PROJECT_ID, NOTIFICATION_TOPIC);
83+
} catch (AlreadyExistsException ex) {
84+
System.out.printf("%s has already been created.", NOTIFICATION_TOPIC);
85+
}
8186

8287
// Create notification rules.
8388
NotificationConfig result = CreateNotification.createNotificationConfig(PROJECT_ID, LOCATION,
@@ -94,11 +99,13 @@ public static void cleanUp() throws IOException {
9499
stdOut = new ByteArrayOutputStream();
95100
System.setOut(new PrintStream(stdOut));
96101

97-
DeleteNotification.deleteNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE);
98-
assertThat(stdOut.toString()).contains(
99-
"Deleted Notification config: " + NOTIFICATION_RULE_CREATE);
102+
ImmutableList<NotificationConfig> notificationConfigs =
103+
ListNotification.listNotificationConfigs(
104+
PROJECT_ID, LOCATION);
100105

101-
deletePubSubTopic(PROJECT_ID, NOTIFICATION_TOPIC);
106+
for (NotificationConfig notificationConfig : notificationConfigs) {
107+
DeleteNotification.deleteNotificationConfig(notificationConfig.getName());
108+
}
102109

103110
stdOut = null;
104111
System.setOut(out);

0 commit comments

Comments
 (0)