|
| 1 | +/* |
| 2 | + * Copyright 2021 Google Inc. |
| 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.contactcenterinsights; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; |
| 23 | +import com.google.cloud.contactcenterinsights.v1.Settings; |
| 24 | +import com.google.cloud.contactcenterinsights.v1.SettingsName; |
| 25 | +import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 26 | +import com.google.protobuf.FieldMask; |
| 27 | +import com.google.pubsub.v1.Topic; |
| 28 | +import com.google.pubsub.v1.TopicName; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.PrintStream; |
| 32 | +import java.util.UUID; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.junit.runners.JUnit4; |
| 39 | + |
| 40 | +@RunWith(JUnit4.class) |
| 41 | +public class EnablePubSubNotificationsIT { |
| 42 | + |
| 43 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 44 | + private ByteArrayOutputStream bout; |
| 45 | + private PrintStream out; |
| 46 | + private String conversationTopic; |
| 47 | + private String analysisTopic; |
| 48 | + |
| 49 | + private static void requireEnvVar(String varName) { |
| 50 | + assertNotNull(String.format(varName), String.format(varName)); |
| 51 | + } |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void checkRequirements() { |
| 55 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 56 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void setUp() throws IOException { |
| 61 | + bout = new ByteArrayOutputStream(); |
| 62 | + out = new PrintStream(bout); |
| 63 | + System.setOut(out); |
| 64 | + |
| 65 | + // Create Pub/Sub topics. |
| 66 | + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { |
| 67 | + String conversationTopicId = |
| 68 | + String.format("create-conversation-%s", UUID.randomUUID().toString()); |
| 69 | + String analysisTopicId = String.format("create-analysis-%s", UUID.randomUUID().toString()); |
| 70 | + |
| 71 | + conversationTopic = TopicName.of(PROJECT_ID, conversationTopicId).toString(); |
| 72 | + analysisTopic = TopicName.of(PROJECT_ID, analysisTopicId).toString(); |
| 73 | + String[] topicNames = {conversationTopic, analysisTopic}; |
| 74 | + |
| 75 | + for (String topicName : topicNames) { |
| 76 | + Topic topic = topicAdminClient.createTopic(topicName); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @After |
| 82 | + public void tearDown() throws IOException { |
| 83 | + // Disable Pub/Sub notifications. |
| 84 | + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { |
| 85 | + SettingsName name = SettingsName.of(PROJECT_ID, "us-central1"); |
| 86 | + Settings settings = |
| 87 | + Settings.newBuilder().setName(name.toString()).clearPubsubNotificationSettings().build(); |
| 88 | + |
| 89 | + FieldMask updateMask = |
| 90 | + FieldMask.newBuilder().addPaths("pubsub_notification_settings").build(); |
| 91 | + |
| 92 | + Settings response = client.updateSettings(settings, updateMask); |
| 93 | + } |
| 94 | + |
| 95 | + // Delete Pub/Sub topics. |
| 96 | + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { |
| 97 | + topicAdminClient.deleteTopic(conversationTopic); |
| 98 | + topicAdminClient.deleteTopic(analysisTopic); |
| 99 | + } |
| 100 | + System.setOut(null); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testEnablePubSubNotifications() throws IOException { |
| 105 | + EnablePubSubNotifications.enablePubSubNotifications( |
| 106 | + PROJECT_ID, conversationTopic, analysisTopic); |
| 107 | + assertThat(bout.toString()).contains("Enabled Pub/Sub notifications"); |
| 108 | + } |
| 109 | +} |
0 commit comments