|
| 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.Analysis; |
| 23 | +import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient; |
| 24 | +import com.google.cloud.contactcenterinsights.v1.Conversation; |
| 25 | +import com.google.cloud.contactcenterinsights.v1.ConversationDataSource; |
| 26 | +import com.google.cloud.contactcenterinsights.v1.CreateConversationRequest; |
| 27 | +import com.google.cloud.contactcenterinsights.v1.DeleteConversationRequest; |
| 28 | +import com.google.cloud.contactcenterinsights.v1.GcsSource; |
| 29 | +import com.google.cloud.contactcenterinsights.v1.LocationName; |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.PrintStream; |
| 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 CreateAnalysisIT { |
| 42 | + |
| 43 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 44 | + private static final String TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"; |
| 45 | + private static final String AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"; |
| 46 | + private ByteArrayOutputStream bout; |
| 47 | + private PrintStream out; |
| 48 | + private String conversationName; |
| 49 | + |
| 50 | + private static void requireEnvVar(String varName) { |
| 51 | + assertNotNull(String.format(varName), String.format(varName)); |
| 52 | + } |
| 53 | + |
| 54 | + @BeforeClass |
| 55 | + public static void checkRequirements() { |
| 56 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 57 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 58 | + } |
| 59 | + |
| 60 | + @Before |
| 61 | + public void setUp() throws IOException { |
| 62 | + bout = new ByteArrayOutputStream(); |
| 63 | + out = new PrintStream(bout); |
| 64 | + System.setOut(out); |
| 65 | + |
| 66 | + // Create a conversation. |
| 67 | + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { |
| 68 | + LocationName parent = LocationName.of(PROJECT_ID, "us-central1"); |
| 69 | + |
| 70 | + Conversation conversation = |
| 71 | + Conversation.newBuilder() |
| 72 | + .setDataSource( |
| 73 | + ConversationDataSource.newBuilder() |
| 74 | + .setGcsSource( |
| 75 | + GcsSource.newBuilder() |
| 76 | + .setTranscriptUri(TRANSCRIPT_URI) |
| 77 | + .setAudioUri(AUDIO_URI) |
| 78 | + .build()) |
| 79 | + .build()) |
| 80 | + .setMedium(Conversation.Medium.CHAT) |
| 81 | + .build(); |
| 82 | + |
| 83 | + CreateConversationRequest request = |
| 84 | + CreateConversationRequest.newBuilder() |
| 85 | + .setParent(parent.toString()) |
| 86 | + .setConversation(conversation) |
| 87 | + .build(); |
| 88 | + |
| 89 | + Conversation response = client.createConversation(request); |
| 90 | + conversationName = response.getName(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @After |
| 95 | + public void tearDown() throws Exception, IOException { |
| 96 | + // Delete the conversation. |
| 97 | + try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) { |
| 98 | + DeleteConversationRequest request = |
| 99 | + DeleteConversationRequest.newBuilder().setName(conversationName).setForce(true).build(); |
| 100 | + client.deleteConversation(request); |
| 101 | + } |
| 102 | + System.setOut(null); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testCreateAnalysis() throws Exception, IOException { |
| 107 | + Analysis analysis = CreateAnalysis.createAnalysis(conversationName); |
| 108 | + assertThat(bout.toString()).contains(analysis.getName()); |
| 109 | + } |
| 110 | +} |
0 commit comments