diff --git a/contact-center-insights/pom.xml b/contact-center-insights/pom.xml new file mode 100644 index 00000000000..3ae7e6e5010 --- /dev/null +++ b/contact-center-insights/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + com.example.contactcenterinsights + contact-center-insights-snippets + jar + Google CCAI Insights Snippets + https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/contact-center-insights + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 1.8 + 1.8 + UTF-8 + + + + + + com.google.cloud + google-cloud-contact-center-insights + 2.3.9 + + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.1.3 + test + + + com.google.cloud + google-cloud-bigquery + 2.17.1 + test + + + com.google.cloud + google-cloud-pubsub + 1.120.20 + test + + + diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateAnalysis.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateAnalysis.java new file mode 100644 index 00000000000..0b4293b921d --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateAnalysis.java @@ -0,0 +1,51 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_create_analysis] + +import com.google.cloud.contactcenterinsights.v1.Analysis; +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import java.io.IOException; + +public class CreateAnalysis { + + public static void main(String[] args) throws Exception, IOException { + // TODO(developer): Replace this variable before running the sample. + String conversationName = + "projects/my_project_id/locations/us-central1/conversations/my_conversation_id"; + + createAnalysis(conversationName); + } + + public static Analysis createAnalysis(String conversationName) throws Exception, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct an analysis. + Analysis analysis = Analysis.newBuilder().build(); + + // Call the Insights client to create an analysis. + Analysis response = client.createAnalysisAsync(conversationName, analysis).get(); + System.out.printf("Created %s%n", response.getName()); + return response; + } + } +} + +// [END contactcenterinsights_create_analysis] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateConversation.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateConversation.java new file mode 100644 index 00000000000..cd03bd1535e --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateConversation.java @@ -0,0 +1,78 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_create_conversation] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Conversation; +import com.google.cloud.contactcenterinsights.v1.ConversationDataSource; +import com.google.cloud.contactcenterinsights.v1.CreateConversationRequest; +import com.google.cloud.contactcenterinsights.v1.GcsSource; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import java.io.IOException; + +public class CreateConversation { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my_project_id"; + String transcriptUri = "gs://cloud-samples-data/ccai/chat_sample.json"; + String audioUri = "gs://cloud-samples-data/ccai/voice_6912.txt"; + + createConversation(projectId, transcriptUri, audioUri); + } + + public static Conversation createConversation( + String projectId, String transcriptUri, String audioUri) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a parent resource. + LocationName parent = LocationName.of(projectId, "us-central1"); + + // Construct a conversation. + Conversation conversation = + Conversation.newBuilder() + .setDataSource( + ConversationDataSource.newBuilder() + .setGcsSource( + GcsSource.newBuilder() + .setTranscriptUri(transcriptUri) + .setAudioUri(audioUri) + .build()) + .build()) + .setMedium(Conversation.Medium.CHAT) + .build(); + + // Construct a request. + CreateConversationRequest request = + CreateConversationRequest.newBuilder() + .setParent(parent.toString()) + .setConversation(conversation) + .build(); + + // Call the Insights client to create a conversation. + Conversation response = client.createConversation(request); + System.out.printf("Created %s%n", response.getName()); + return response; + } + } +} + +// [END contactcenterinsights_create_conversation] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateConversationWithTtl.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateConversationWithTtl.java new file mode 100644 index 00000000000..55649165c10 --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateConversationWithTtl.java @@ -0,0 +1,80 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_create_conversation_with_ttl] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Conversation; +import com.google.cloud.contactcenterinsights.v1.ConversationDataSource; +import com.google.cloud.contactcenterinsights.v1.CreateConversationRequest; +import com.google.cloud.contactcenterinsights.v1.GcsSource; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import com.google.protobuf.Duration; +import java.io.IOException; + +public class CreateConversationWithTtl { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my_project_id"; + String transcriptUri = "gs://cloud-samples-data/ccai/chat_sample.json"; + String audioUri = "gs://cloud-samples-data/ccai/voice_6912.txt"; + + createConversationWithTtl(projectId, transcriptUri, audioUri); + } + + public static Conversation createConversationWithTtl( + String projectId, String transcriptUri, String audioUri) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a parent resource. + LocationName parent = LocationName.of(projectId, "us-central1"); + + // Construct a conversation. + Conversation conversation = + Conversation.newBuilder() + .setDataSource( + ConversationDataSource.newBuilder() + .setGcsSource( + GcsSource.newBuilder() + .setTranscriptUri(transcriptUri) + .setAudioUri(audioUri) + .build()) + .build()) + .setMedium(Conversation.Medium.CHAT) + .setTtl(Duration.newBuilder().setSeconds(86400).build()) + .build(); + + // Construct a request. + CreateConversationRequest request = + CreateConversationRequest.newBuilder() + .setParent(parent.toString()) + .setConversation(conversation) + .build(); + + // Call the Insights client to create a conversation. + Conversation response = client.createConversation(request); + System.out.printf("Created %s%n", response.getName()); + return response; + } + } +} + +// [END contactcenterinsights_create_conversation_with_ttl] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateIssueModel.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateIssueModel.java new file mode 100644 index 00000000000..0a3612b0fb6 --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreateIssueModel.java @@ -0,0 +1,59 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_create_issue_model] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.IssueModel; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import java.io.IOException; + +public class CreateIssueModel { + + public static void main(String[] args) throws Exception, IOException { + // TODO(developer): Replace this variable before running the sample. + String projectId = "my_project_id"; + + createIssueModel(projectId); + } + + public static IssueModel createIssueModel(String projectId) throws Exception, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a parent resource. + LocationName parent = LocationName.of(projectId, "us-central1"); + + // Construct an issue model. + IssueModel issueModel = + IssueModel.newBuilder() + .setDisplayName("my-model") + .setInputDataConfig( + IssueModel.InputDataConfig.newBuilder().setFilter("medium=\"CHAT\"").build()) + .build(); + + // Call the Insights client to create an issue model. + IssueModel response = client.createIssueModelAsync(parent, issueModel).get(); + System.out.printf("Created %s%n", response.getName()); + return response; + } + } +} + +// [END contactcenterinsights_create_issue_model] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOf.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOf.java new file mode 100644 index 00000000000..ec8d75a3778 --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOf.java @@ -0,0 +1,96 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_create_phrase_matcher_all_of] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.ExactMatchConfig; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import com.google.cloud.contactcenterinsights.v1.PhraseMatchRule; +import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleConfig; +import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleGroup; +import com.google.cloud.contactcenterinsights.v1.PhraseMatcher; +import java.io.IOException; + +public class CreatePhraseMatcherAllOf { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace this variable before running the sample. + String projectId = "my_project_id"; + + createPhraseMatcherAllOf(projectId); + } + + public static PhraseMatcher createPhraseMatcherAllOf(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a phrase matcher that matches all of its rule groups. + PhraseMatcher.Builder phraseMatcher = + PhraseMatcher.newBuilder() + .setDisplayName("NON_SHIPPING_PHONE_SERVICE") + .setTypeValue(1) + .setActive(true); + + // Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity. + PhraseMatchRuleGroup.Builder ruleGroup1 = PhraseMatchRuleGroup.newBuilder().setTypeValue(2); + + String[] words1 = {"PHONE", "CELLPHONE"}; + for (String w : words1) { + PhraseMatchRule.Builder rule = + PhraseMatchRule.newBuilder() + .setQuery(w) + .setConfig( + PhraseMatchRuleConfig.newBuilder() + .setExactMatchConfig(ExactMatchConfig.newBuilder().build()) + .build()); + ruleGroup1.addPhraseMatchRules(rule.build()); + } + phraseMatcher.addPhraseMatchRuleGroups(ruleGroup1.build()); + + // Construct another rule group to not match the word "SHIPPING" or "DELIVERY", + // ignoring case sensitivity. + PhraseMatchRuleGroup.Builder ruleGroup2 = PhraseMatchRuleGroup.newBuilder().setTypeValue(1); + + String[] words2 = {"SHIPPING", "DELIVERY"}; + for (String w : words2) { + PhraseMatchRule.Builder rule = + PhraseMatchRule.newBuilder() + .setQuery(w) + .setNegated(true) + .setConfig( + PhraseMatchRuleConfig.newBuilder() + .setExactMatchConfig(ExactMatchConfig.newBuilder().build()) + .build()); + ruleGroup2.addPhraseMatchRules(rule.build()); + } + phraseMatcher.addPhraseMatchRuleGroups(ruleGroup2.build()); + + // Construct a parent resource. + LocationName parent = LocationName.of(projectId, "us-central1"); + + // Call the Insights client to create a phrase matcher. + PhraseMatcher response = client.createPhraseMatcher(parent, phraseMatcher.build()); + System.out.printf("Created %s%n", response.getName()); + return response; + } + } +} + +// [END contactcenterinsights_create_phrase_matcher_all_of] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOf.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOf.java new file mode 100644 index 00000000000..c1d0bcf3953 --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOf.java @@ -0,0 +1,78 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_create_phrase_matcher_any_of] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.ExactMatchConfig; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import com.google.cloud.contactcenterinsights.v1.PhraseMatchRule; +import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleConfig; +import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleGroup; +import com.google.cloud.contactcenterinsights.v1.PhraseMatcher; +import java.io.IOException; + +public class CreatePhraseMatcherAnyOf { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace this variable before running the sample. + String projectId = "my_project_id"; + + createPhraseMatcherAnyOf(projectId); + } + + public static PhraseMatcher createPhraseMatcherAnyOf(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a phrase matcher that matches any of its rule groups. + PhraseMatcher.Builder phraseMatcher = + PhraseMatcher.newBuilder() + .setDisplayName("PHONE_SERVICE") + .setTypeValue(2) + .setActive(true); + + // Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity. + PhraseMatchRuleGroup.Builder ruleGroup = PhraseMatchRuleGroup.newBuilder().setTypeValue(2); + + String[] words = {"PHONE", "CELLPHONE"}; + for (String w : words) { + PhraseMatchRule.Builder rule = + PhraseMatchRule.newBuilder() + .setQuery(w) + .setConfig( + PhraseMatchRuleConfig.newBuilder() + .setExactMatchConfig(ExactMatchConfig.newBuilder().build()) + .build()); + ruleGroup.addPhraseMatchRules(rule.build()); + } + phraseMatcher.addPhraseMatchRuleGroups(ruleGroup.build()); + + // Construct a parent resource. + LocationName parent = LocationName.of(projectId, "us-central1"); + + // Call the Insights client to create a phrase matcher. + PhraseMatcher response = client.createPhraseMatcher(parent, phraseMatcher.build()); + System.out.printf("Created %s%n", response.getName()); + return response; + } + } +} + +// [END contactcenterinsights_create_phrase_matcher_any_of] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/EnablePubSubNotifications.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/EnablePubSubNotifications.java new file mode 100644 index 00000000000..6518985347d --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/EnablePubSubNotifications.java @@ -0,0 +1,65 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_enable_pubsub_notifications] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Settings; +import com.google.cloud.contactcenterinsights.v1.SettingsName; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +public class EnablePubSubNotifications { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my_project_id"; + String topicCreateConversation = "projects/my_project_id/topics/my_topic_id"; + String topicCreateAnalysis = "projects/my_project_id/topics/my_other_topic_id"; + + enablePubSubNotifications(projectId, topicCreateConversation, topicCreateAnalysis); + } + + public static void enablePubSubNotifications( + String projectId, String topicCreateConversation, String topicCreateAnalysis) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a settings resource. + SettingsName name = SettingsName.of(projectId, "us-central1"); + Settings settings = + Settings.newBuilder() + .setName(name.toString()) + .putPubsubNotificationSettings("create-conversation", topicCreateConversation) + .putPubsubNotificationSettings("create-analysis", topicCreateAnalysis) + .build(); + + // Construct an update mask. + FieldMask updateMask = + FieldMask.newBuilder().addPaths("pubsub_notification_settings").build(); + + // Call the Insights client to enable Pub/Sub notifications. + Settings response = client.updateSettings(settings, updateMask); + System.out.printf("Enabled Pub/Sub notifications"); + } + } +} + +// [END contactcenterinsights_enable_pubsub_notifications] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java new file mode 100644 index 00000000000..c5e8de145dc --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/ExportToBigquery.java @@ -0,0 +1,90 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_export_to_bigquery] + +import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsSettings; +import com.google.cloud.contactcenterinsights.v1.ExportInsightsDataRequest; +import com.google.cloud.contactcenterinsights.v1.ExportInsightsDataResponse; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import java.io.IOException; +import org.threeten.bp.Duration; + +public class ExportToBigquery { + + public static void main(String[] args) throws Exception, IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my_project_id"; + String bigqueryProjectId = "my_bigquery_project_id"; + String bigqueryDataset = "my_bigquery_dataset"; + String bigqueryTable = "my_bigquery_table"; + + exportToBigquery(projectId, bigqueryProjectId, bigqueryDataset, bigqueryTable); + } + + public static void exportToBigquery( + String projectId, String bigqueryProjectId, String bigqueryDataset, String bigqueryTable) + throws Exception, IOException { + // Set the operation total polling timeout to 24 hours instead of the 5-minute default. + // Other values are copied from the default values of {@link ContactCenterInsightsStubSettings}. + ContactCenterInsightsSettings.Builder clientSettings = + ContactCenterInsightsSettings.newBuilder(); + clientSettings + .exportInsightsDataOperationSettings() + .setPollingAlgorithm( + OperationTimedPollAlgorithm.create( + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.ofMillis(5000L)) + .setRetryDelayMultiplier(1.5) + .setMaxRetryDelay(Duration.ofMillis(45000L)) + .setInitialRpcTimeout(Duration.ZERO) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.ZERO) + .setTotalTimeout(Duration.ofHours(24L)) + .build())); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = + ContactCenterInsightsClient.create(clientSettings.build())) { + // Construct an export request. + LocationName parent = LocationName.of(projectId, "us-central1"); + ExportInsightsDataRequest request = + ExportInsightsDataRequest.newBuilder() + .setParent(parent.toString()) + .setBigQueryDestination( + ExportInsightsDataRequest.BigQueryDestination.newBuilder() + .setProjectId(bigqueryProjectId) + .setDataset(bigqueryDataset) + .setTable(bigqueryTable) + .build()) + .setFilter("agent_id=\"007\"") + .build(); + + // Call the Insights client to export data to BigQuery. + ExportInsightsDataResponse response = client.exportInsightsDataAsync(request).get(); + System.out.printf("Exported data to BigQuery"); + } + } +} + +// [END contactcenterinsights_export_to_bigquery] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/GetOperation.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/GetOperation.java new file mode 100644 index 00000000000..19721bca9b0 --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/GetOperation.java @@ -0,0 +1,49 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_get_operation] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.longrunning.Operation; +import com.google.longrunning.OperationsClient; +import java.io.IOException; + +public class GetOperation { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace this variable before running the sample. + String operationName = "projects/my_project_id/locations/us-central1/operations/12345"; + + getOperation(operationName); + } + + public static Operation getOperation(String operationName) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + OperationsClient operationsClient = client.getOperationsClient(); + Operation operation = operationsClient.getOperation(operationName); + + System.out.printf("Got operation %s%n", operation.getName()); + return operation; + } + } +} + +// [END contactcenterinsights_get_operation] diff --git a/contact-center-insights/src/main/java/com/example/contactcenterinsights/SetProjectTtl.java b/contact-center-insights/src/main/java/com/example/contactcenterinsights/SetProjectTtl.java new file mode 100644 index 00000000000..9e4b2a732eb --- /dev/null +++ b/contact-center-insights/src/main/java/com/example/contactcenterinsights/SetProjectTtl.java @@ -0,0 +1,60 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +// [START contactcenterinsights_set_project_ttl] + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Settings; +import com.google.cloud.contactcenterinsights.v1.SettingsName; +import com.google.protobuf.Duration; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +public class SetProjectTtl { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace this variable before running the sample. + String projectId = "my_project_id"; + + setProjectTtl(projectId); + } + + public static void setProjectTtl(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Construct a settings resource. + SettingsName name = SettingsName.of(projectId, "us-central1"); + Settings settings = + Settings.newBuilder() + .setName(name.toString()) + .setConversationTtl(Duration.newBuilder().setSeconds(86400).build()) + .build(); + + // Construct an update mask. + FieldMask updateMask = FieldMask.newBuilder().addPaths("conversation_ttl").build(); + + // Call the Insights client to set a project-level TTL. + Settings response = client.updateSettings(settings, updateMask); + System.out.printf("Set TTL for all incoming conversations to 1 day"); + } + } +} + +// [END contactcenterinsights_set_project_ttl] diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateAnalysisIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateAnalysisIT.java new file mode 100644 index 00000000000..8eb7fd9592a --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateAnalysisIT.java @@ -0,0 +1,110 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.Analysis; +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Conversation; +import com.google.cloud.contactcenterinsights.v1.ConversationDataSource; +import com.google.cloud.contactcenterinsights.v1.CreateConversationRequest; +import com.google.cloud.contactcenterinsights.v1.DeleteConversationRequest; +import com.google.cloud.contactcenterinsights.v1.GcsSource; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateAnalysisIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"; + private static final String AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"; + private ByteArrayOutputStream bout; + private PrintStream out; + private String conversationName; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a conversation. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + LocationName parent = LocationName.of(PROJECT_ID, "us-central1"); + + Conversation conversation = + Conversation.newBuilder() + .setDataSource( + ConversationDataSource.newBuilder() + .setGcsSource( + GcsSource.newBuilder() + .setTranscriptUri(TRANSCRIPT_URI) + .setAudioUri(AUDIO_URI) + .build()) + .build()) + .setMedium(Conversation.Medium.CHAT) + .build(); + + CreateConversationRequest request = + CreateConversationRequest.newBuilder() + .setParent(parent.toString()) + .setConversation(conversation) + .build(); + + Conversation response = client.createConversation(request); + conversationName = response.getName(); + } + } + + @After + public void tearDown() throws Exception, IOException { + // Delete the conversation. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + DeleteConversationRequest request = + DeleteConversationRequest.newBuilder().setName(conversationName).setForce(true).build(); + client.deleteConversation(request); + } + System.setOut(null); + } + + @Test + public void testCreateAnalysis() throws Exception, IOException { + Analysis analysis = CreateAnalysis.createAnalysis(conversationName); + assertThat(bout.toString()).contains(analysis.getName()); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateConversationIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateConversationIT.java new file mode 100644 index 00000000000..894914fe7c1 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateConversationIT.java @@ -0,0 +1,76 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Conversation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateConversationIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"; + private static final String AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"; + private ByteArrayOutputStream bout; + private PrintStream out; + private String conversationName; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + client.deleteConversation(conversationName); + } + System.setOut(null); + } + + @Test + public void testCreateConversation() throws IOException { + Conversation conversation = + CreateConversation.createConversation(PROJECT_ID, TRANSCRIPT_URI, AUDIO_URI); + conversationName = conversation.getName(); + assertThat(bout.toString()).contains(conversationName); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateConversationWithTtlIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateConversationWithTtlIT.java new file mode 100644 index 00000000000..349795e6056 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateConversationWithTtlIT.java @@ -0,0 +1,76 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Conversation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateConversationWithTtlIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"; + private static final String AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"; + private ByteArrayOutputStream bout; + private PrintStream out; + private String conversationName; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + client.deleteConversation(conversationName); + } + System.setOut(null); + } + + @Test + public void testCreateConversationWithTtl() throws IOException { + Conversation conversation = + CreateConversationWithTtl.createConversationWithTtl(PROJECT_ID, TRANSCRIPT_URI, AUDIO_URI); + conversationName = conversation.getName(); + assertThat(bout.toString()).contains(conversationName); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateIssueModelIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateIssueModelIT.java new file mode 100644 index 00000000000..60e4b9be8c6 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreateIssueModelIT.java @@ -0,0 +1,106 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.IssueModel; +import com.google.cloud.contactcenterinsights.v1.ListConversationsRequest; +import com.google.cloud.contactcenterinsights.v1.ListConversationsResponse; +import com.google.cloud.contactcenterinsights.v1.LocationName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateIssueModelIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final int MIN_CONVERSATION_COUNT = 10000; + private ByteArrayOutputStream bout; + private PrintStream out; + private String issueModelName; + private int conversationCount; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Check if the project has the minimum number of conversations required to create + // an issue model. See https://cloud.google.com/contact-center/insights/docs/topic-model. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + LocationName parent = LocationName.of(PROJECT_ID, "us-central1"); + ListConversationsRequest.Builder listRequest = + ListConversationsRequest.newBuilder().setParent(parent.toString()).setPageSize(1000); + + conversationCount = 0; + while (conversationCount < MIN_CONVERSATION_COUNT) { + ListConversationsResponse listResponse = + client.listConversationsCallable().call(listRequest.build()); + + if (listResponse.getConversationsCount() == 0) { + break; + } + conversationCount += listResponse.getConversationsCount(); + + if (listResponse.getNextPageToken().isEmpty()) { + break; + } + listRequest.setPageToken(listResponse.getNextPageToken()); + } + } + } + + @After + public void tearDown() throws Exception, IOException { + if (conversationCount >= MIN_CONVERSATION_COUNT) { + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + client.deleteIssueModelAsync(issueModelName); + } + } + System.setOut(null); + } + + @Test + public void testCreateIssueModel() throws Exception, IOException { + if (conversationCount >= MIN_CONVERSATION_COUNT) { + IssueModel issueModel = CreateIssueModel.createIssueModel(PROJECT_ID); + issueModelName = issueModel.getName(); + assertThat(bout.toString()).contains(issueModelName); + } + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOfIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOfIT.java new file mode 100644 index 00000000000..971150d0b78 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreatePhraseMatcherAllOfIT.java @@ -0,0 +1,73 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.PhraseMatcher; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreatePhraseMatcherAllOfIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + private String phraseMatcherName; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + client.deletePhraseMatcher(phraseMatcherName); + } + System.setOut(null); + } + + @Test + public void testCreatePhraseMatcherAllOf() throws IOException { + PhraseMatcher phraseMatcher = CreatePhraseMatcherAllOf.createPhraseMatcherAllOf(PROJECT_ID); + phraseMatcherName = phraseMatcher.getName(); + assertThat(bout.toString()).contains(phraseMatcherName); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOfIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOfIT.java new file mode 100644 index 00000000000..ccea1df8c62 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/CreatePhraseMatcherAnyOfIT.java @@ -0,0 +1,73 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.PhraseMatcher; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreatePhraseMatcherAnyOfIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + private String phraseMatcherName; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + client.deletePhraseMatcher(phraseMatcherName); + } + System.setOut(null); + } + + @Test + public void testCreatePhraseMatcherAnyOf() throws IOException { + PhraseMatcher phraseMatcher = CreatePhraseMatcherAnyOf.createPhraseMatcherAnyOf(PROJECT_ID); + phraseMatcherName = phraseMatcher.getName(); + assertThat(bout.toString()).contains(phraseMatcherName); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/EnablePubSubNotificationsIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/EnablePubSubNotificationsIT.java new file mode 100644 index 00000000000..5e3188c6e68 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/EnablePubSubNotificationsIT.java @@ -0,0 +1,109 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Settings; +import com.google.cloud.contactcenterinsights.v1.SettingsName; +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.protobuf.FieldMask; +import com.google.pubsub.v1.Topic; +import com.google.pubsub.v1.TopicName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class EnablePubSubNotificationsIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + private String conversationTopic; + private String analysisTopic; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create Pub/Sub topics. + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + String conversationTopicId = + String.format("create-conversation-%s", UUID.randomUUID().toString()); + String analysisTopicId = String.format("create-analysis-%s", UUID.randomUUID().toString()); + + conversationTopic = TopicName.of(PROJECT_ID, conversationTopicId).toString(); + analysisTopic = TopicName.of(PROJECT_ID, analysisTopicId).toString(); + String[] topicNames = {conversationTopic, analysisTopic}; + + for (String topicName : topicNames) { + Topic topic = topicAdminClient.createTopic(topicName); + } + } + } + + @After + public void tearDown() throws IOException { + // Disable Pub/Sub notifications. + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + SettingsName name = SettingsName.of(PROJECT_ID, "us-central1"); + Settings settings = + Settings.newBuilder().setName(name.toString()).clearPubsubNotificationSettings().build(); + + FieldMask updateMask = + FieldMask.newBuilder().addPaths("pubsub_notification_settings").build(); + + Settings response = client.updateSettings(settings, updateMask); + } + + // Delete Pub/Sub topics. + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.deleteTopic(conversationTopic); + topicAdminClient.deleteTopic(analysisTopic); + } + System.setOut(null); + } + + @Test + public void testEnablePubSubNotifications() throws IOException { + EnablePubSubNotifications.enablePubSubNotifications( + PROJECT_ID, conversationTopic, analysisTopic); + assertThat(bout.toString()).contains("Enabled Pub/Sub notifications"); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/ExportToBigqueryIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/ExportToBigqueryIT.java new file mode 100644 index 00000000000..a7b68acf8e4 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/ExportToBigqueryIT.java @@ -0,0 +1,111 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import com.google.cloud.bigquery.DatasetInfo; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.Table; +import com.google.cloud.bigquery.TableDefinition; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ExportToBigqueryIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BIGQUERY_PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String GCLOUD_TESTS_PREFIX = "java_samples_tests"; + private ByteArrayOutputStream bout; + private PrintStream out; + private String bigqueryDatasetId; + private String bigqueryTableId; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws BigQueryException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Generate BigQuery table and dataset IDs. + bigqueryDatasetId = + String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_")); + bigqueryTableId = + String.format("%s_%s", GCLOUD_TESTS_PREFIX, UUID.randomUUID().toString().replace("-", "_")); + + // Create a BigQuery dataset. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + DatasetInfo datasetInfo = + DatasetInfo.newBuilder(DatasetId.of(BIGQUERY_PROJECT_ID, bigqueryDatasetId)).build(); + Dataset dataset = bigquery.create(datasetInfo); + + // Create a BigQuery table under the created dataset. + Schema schema = Schema.of(new ArrayList<>()); + TableDefinition tableDefinition = StandardTableDefinition.of(schema); + TableInfo tableInfo = + TableInfo.newBuilder(TableId.of(bigqueryDatasetId, bigqueryTableId), tableDefinition) + .build(); + Table table = bigquery.create(tableInfo); + } + + @After + public void tearDown() throws BigQueryException { + // Delete the BigQuery dataset and table. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + boolean success = + bigquery.delete( + DatasetId.of(PROJECT_ID, bigqueryDatasetId), + BigQuery.DatasetDeleteOption.deleteContents()); + System.setOut(null); + } + + @Test + public void testExportToBigquery() throws Exception, IOException { + ExportToBigquery.exportToBigquery( + PROJECT_ID, BIGQUERY_PROJECT_ID, bigqueryDatasetId, bigqueryTableId); + assertThat(bout.toString()).contains("Exported data to BigQuery"); + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/GetOperationIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/GetOperationIT.java new file mode 100644 index 00000000000..17e9abdb110 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/GetOperationIT.java @@ -0,0 +1,79 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.rpc.ApiException; +import com.google.longrunning.Operation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetOperationIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + private String conversationName; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + System.setOut(null); + } + + @Test + public void testGetOperation() throws IOException { + // TODO(developer): Replace this variable with your operation name. + String operationName = + String.format("projects/%s/locations/us-central1/operations/12345", PROJECT_ID); + + try { + Operation operation = GetOperation.getOperation(operationName); + assertThat(bout.toString()).contains(operation.getName()); + } catch (ApiException exception) { + if (!exception.getMessage().contains("not found")) { + throw exception; + } + } + } +} diff --git a/contact-center-insights/src/test/java/com/example/contactcenterinsights/SetProjectTtlIT.java b/contact-center-insights/src/test/java/com/example/contactcenterinsights/SetProjectTtlIT.java new file mode 100644 index 00000000000..adb973d3414 --- /dev/null +++ b/contact-center-insights/src/test/java/com/example/contactcenterinsights/SetProjectTtlIT.java @@ -0,0 +1,84 @@ +/* + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.contactcenterinsights; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; +import com.google.cloud.contactcenterinsights.v1.Settings; +import com.google.cloud.contactcenterinsights.v1.SettingsName; +import com.google.protobuf.Duration; +import com.google.protobuf.FieldMask; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SetProjectTtlIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull(String.format(varName), String.format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { + // Clear project-level TTL. + SettingsName name = SettingsName.of(PROJECT_ID, "us-central1"); + Settings settings = + Settings.newBuilder() + .setName(name.toString()) + .setConversationTtl(Duration.newBuilder().build()) + .build(); + + FieldMask updateMask = FieldMask.newBuilder().addPaths("conversation_ttl").build(); + + Settings response = client.updateSettings(settings, updateMask); + } + System.setOut(null); + } + + @Test + public void testSetProjecTtl() throws IOException { + SetProjectTtl.setProjectTtl(PROJECT_ID); + assertThat(bout.toString()).contains("Set TTL for all incoming conversations to 1 day"); + } +}