|
21 | 21 | import com.google.cloud.secretmanager.v1.AddSecretVersionRequest; |
22 | 22 | import com.google.cloud.secretmanager.v1.CreateSecretRequest; |
23 | 23 | import com.google.cloud.secretmanager.v1.DeleteSecretRequest; |
24 | | -import com.google.cloud.secretmanager.v1.DestroySecretVersionRequest; |
25 | 24 | import com.google.cloud.secretmanager.v1.DisableSecretVersionRequest; |
26 | | -import com.google.cloud.secretmanager.v1.EnableSecretVersionRequest; |
27 | 25 | import com.google.cloud.secretmanager.v1.ProjectName; |
28 | 26 | import com.google.cloud.secretmanager.v1.Replication; |
29 | 27 | import com.google.cloud.secretmanager.v1.Secret; |
|
37 | 35 | import java.io.ByteArrayOutputStream; |
38 | 36 | import java.io.IOException; |
39 | 37 | import java.io.PrintStream; |
| 38 | +import java.nio.charset.StandardCharsets; |
40 | 39 | import java.util.Arrays; |
| 40 | +import java.util.Base64; |
| 41 | +import java.util.HashMap; |
41 | 42 | import java.util.List; |
| 43 | +import java.util.Map; |
42 | 44 | import java.util.Random; |
43 | 45 | import org.junit.After; |
44 | 46 | import org.junit.AfterClass; |
|
48 | 50 | import org.junit.Test; |
49 | 51 | import org.junit.runner.RunWith; |
50 | 52 | import org.junit.runners.JUnit4; |
| 53 | +import secretmanager.ConsumeEventNotification.PubSubMessage; |
51 | 54 |
|
52 | | -/** Integration (system) tests for {@link Snippets}. */ |
| 55 | +/** |
| 56 | + * Integration (system) tests for {@link Snippets}. |
| 57 | + */ |
53 | 58 | @RunWith(JUnit4.class) |
54 | 59 | @SuppressWarnings("checkstyle:AbbreviationAsWordInName") |
55 | 60 | public class SnippetsIT { |
| 61 | + |
56 | 62 | private static final String IAM_USER = |
57 | 63 | "serviceAccount:iam-samples@java-docs-samples-testing.iam.gserviceaccount.com"; |
58 | 64 | private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
@@ -122,7 +128,7 @@ public static void afterAll() throws IOException { |
122 | 128 |
|
123 | 129 | private static String randomSecretId() { |
124 | 130 | Random random = new Random(); |
125 | | - return "java-" + String.valueOf(random.nextLong()); |
| 131 | + return "java-" + random.nextLong(); |
126 | 132 | } |
127 | 133 |
|
128 | 134 | private static Secret createSecret() throws IOException { |
@@ -376,12 +382,29 @@ public void testUpdateSecret() throws IOException { |
376 | 382 |
|
377 | 383 | assertThat(stdOut.toString()).contains("Updated secret"); |
378 | 384 | } |
379 | | - |
| 385 | + |
380 | 386 | @Test |
381 | 387 | public void testUpdateSecretWithAlias() throws IOException { |
382 | 388 | SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName()); |
383 | 389 | UpdateSecretWithAlias.updateSecret(name.getProject(), name.getSecret()); |
384 | 390 |
|
385 | 391 | assertThat(stdOut.toString()).contains("test"); |
386 | 392 | } |
| 393 | + |
| 394 | + @Test |
| 395 | + public void testConsumeEventNotification() { |
| 396 | + String message = "hello!"; |
| 397 | + byte[] base64Bytes = Base64.getEncoder().encode(message.getBytes(StandardCharsets.UTF_8)); |
| 398 | + Map<String, String> attributes = new HashMap<>(); |
| 399 | + attributes.put("eventType", "SECRET_UPDATE"); |
| 400 | + attributes.put("secretId", "projects/p/secrets/s"); |
| 401 | + |
| 402 | + PubSubMessage pubSubMessage = new PubSubMessage(); |
| 403 | + pubSubMessage.setData(base64Bytes); |
| 404 | + pubSubMessage.setAttributes(attributes); |
| 405 | + |
| 406 | + String log = ConsumeEventNotification.accept(pubSubMessage); |
| 407 | + assertThat(log).isEqualTo( |
| 408 | + "Received SECRET_UPDATE for projects/p/secrets/s. New metadata: hello!"); |
| 409 | + } |
387 | 410 | } |
0 commit comments