|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.cloud.monitoring.v3.NotificationChannelServiceClient; |
| 22 | +import com.google.monitoring.v3.NotificationChannel; |
| 23 | +import com.google.monitoring.v3.ProjectName; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.PrintStream; |
| 28 | + |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.junit.runners.JUnit4; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for delete notification channel sample. |
| 38 | + */ |
| 39 | +@RunWith(JUnit4.class) |
| 40 | +@SuppressWarnings("checkstyle:abbreviationaswordinname") |
| 41 | +public class DeleteNotificationChannelIT { |
| 42 | + private ByteArrayOutputStream bout; |
| 43 | + private PrintStream out; |
| 44 | + private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT"; |
| 45 | + private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; |
| 46 | + private static String NOTIFICATION_CHANNEL_NAME = "channelname"; |
| 47 | + private static NotificationChannel NOTIFICATION_CHANNEL; |
| 48 | + |
| 49 | + private static String getProjectId() { |
| 50 | + String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME)); |
| 51 | + if (projectId == null) { |
| 52 | + projectId = System.getProperty(LEGACY_PROJECT_ENV_NAME, |
| 53 | + System.getenv(LEGACY_PROJECT_ENV_NAME)); |
| 54 | + } |
| 55 | + return projectId; |
| 56 | + } |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void setupClass() throws IOException { |
| 60 | + try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) { |
| 61 | + String projectId = getProjectId(); |
| 62 | + NOTIFICATION_CHANNEL = NotificationChannel.newBuilder() |
| 63 | + .setType("email") |
| 64 | + . putLabels( "email_address", "[email protected]") |
| 65 | + .build(); |
| 66 | + NotificationChannel channel = client.createNotificationChannel(ProjectName.of(projectId), |
| 67 | + NOTIFICATION_CHANNEL); |
| 68 | + NOTIFICATION_CHANNEL_NAME = channel.getName(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Before |
| 73 | + public void setUp() { |
| 74 | + bout = new ByteArrayOutputStream(); |
| 75 | + out = new PrintStream(bout); |
| 76 | + System.setOut(out); |
| 77 | + System.setProperty("projectId", DeleteNotificationChannelIT.getProjectId()); |
| 78 | + } |
| 79 | + |
| 80 | + @After |
| 81 | + public void tearDown() { |
| 82 | + System.setOut(null); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testDeleteNotificationChannel() throws Exception { |
| 87 | + // Act |
| 88 | + DeleteNotificationChannel.deleteNotificationChannel(NOTIFICATION_CHANNEL_NAME); |
| 89 | + // Assert |
| 90 | + String got = bout.toString(); |
| 91 | + assertThat(got).contains(NOTIFICATION_CHANNEL_NAME); |
| 92 | + } |
| 93 | +} |
0 commit comments