Skip to content

Commit 4e2bb27

Browse files
TrucHLeShabirmean
authored andcommitted
samples: enable pubsub notifications (#87)
1 parent 2938ba6 commit 4e2bb27

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

contact-center-insights/snippets/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@
4343
<version>1.1.3</version>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>google-cloud-pubsub</artifactId>
49+
<version>1.114.2</version>
50+
<scope>test</scope>
51+
</dependency>
4652
</dependencies>
4753
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// [START contactcenterinsights_enable_pubsub_notifications]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.cloud.contactcenterinsights.v1.Settings;
23+
import com.google.cloud.contactcenterinsights.v1.SettingsName;
24+
import com.google.protobuf.FieldMask;
25+
import java.io.IOException;
26+
27+
public class EnablePubSubNotifications {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my_project_id";
32+
String topicCreateConversation = "projects/my_project_id/topics/my_topic_id";
33+
String topicCreateAnalysis = "projects/my_project_id/topics/my_other_topic_id";
34+
35+
enablePubSubNotifications(projectId, topicCreateConversation, topicCreateAnalysis);
36+
}
37+
38+
public static void enablePubSubNotifications(
39+
String projectId, String topicCreateConversation, String topicCreateAnalysis)
40+
throws IOException {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests. After completing all of your requests, call
43+
// the "close" method on the client to safely clean up any remaining background resources.
44+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
45+
// Construct a settings resource.
46+
SettingsName name = SettingsName.of(projectId, "us-central1");
47+
Settings settings =
48+
Settings.newBuilder()
49+
.setName(name.toString())
50+
.putPubsubNotificationSettings("create-conversation", topicCreateConversation)
51+
.putPubsubNotificationSettings("create-analysis", topicCreateAnalysis)
52+
.build();
53+
54+
// Construct an update mask.
55+
FieldMask updateMask =
56+
FieldMask.newBuilder().addPaths("pubsub_notification_settings").build();
57+
58+
// Call the Insights client to enable Pub/Sub notifications.
59+
Settings response = client.updateSettings(settings, updateMask);
60+
System.out.printf("Enabled Pub/Sub notifications");
61+
}
62+
}
63+
}
64+
65+
// [END contactcenterinsights_enable_pubsub_notifications]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)