|
| 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.IssueModel; |
| 24 | +import com.google.cloud.contactcenterinsights.v1.ListConversationsRequest; |
| 25 | +import com.google.cloud.contactcenterinsights.v1.ListConversationsResponse; |
| 26 | +import com.google.cloud.contactcenterinsights.v1.LocationName; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.PrintStream; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | + |
| 37 | +@RunWith(JUnit4.class) |
| 38 | +public class CreateIssueModelIT { |
| 39 | + |
| 40 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 41 | + private static final int MIN_CONVERSATION_COUNT = 10000; |
| 42 | + private ByteArrayOutputStream bout; |
| 43 | + private PrintStream out; |
| 44 | + private String issueModelName; |
| 45 | + private int conversationCount; |
| 46 | + |
| 47 | + private static void requireEnvVar(String varName) { |
| 48 | + assertNotNull(String.format(varName), String.format(varName)); |
| 49 | + } |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void checkRequirements() { |
| 53 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 54 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 55 | + } |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() throws IOException { |
| 59 | + bout = new ByteArrayOutputStream(); |
| 60 | + out = new PrintStream(bout); |
| 61 | + System.setOut(out); |
| 62 | + |
| 63 | + // Check if the project has the minimum number of conversations required to create |
| 64 | + // an issue model. See https://cloud.google.com/contact-center/insights/docs/topic-model. |
| 65 | + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { |
| 66 | + LocationName parent = LocationName.of(PROJECT_ID, "us-central1"); |
| 67 | + ListConversationsRequest.Builder listRequest = |
| 68 | + ListConversationsRequest.newBuilder().setParent(parent.toString()).setPageSize(1000); |
| 69 | + |
| 70 | + conversationCount = 0; |
| 71 | + while (conversationCount < MIN_CONVERSATION_COUNT) { |
| 72 | + ListConversationsResponse listResponse = |
| 73 | + client.listConversationsCallable().call(listRequest.build()); |
| 74 | + |
| 75 | + if (listResponse.getConversationsCount() == 0) { |
| 76 | + break; |
| 77 | + } |
| 78 | + conversationCount += listResponse.getConversationsCount(); |
| 79 | + |
| 80 | + if (listResponse.getNextPageToken().isEmpty()) { |
| 81 | + break; |
| 82 | + } |
| 83 | + listRequest.setPageToken(listResponse.getNextPageToken()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @After |
| 89 | + public void tearDown() throws Exception, IOException { |
| 90 | + if (conversationCount >= MIN_CONVERSATION_COUNT) { |
| 91 | + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { |
| 92 | + client.deleteIssueModelAsync(issueModelName); |
| 93 | + } |
| 94 | + } |
| 95 | + System.setOut(null); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testCreateIssueModel() throws Exception, IOException { |
| 100 | + if (conversationCount >= MIN_CONVERSATION_COUNT) { |
| 101 | + IssueModel issueModel = CreateIssueModel.createIssueModel(PROJECT_ID); |
| 102 | + issueModelName = issueModel.getName(); |
| 103 | + assertThat(bout.toString()).contains(issueModelName); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments