Skip to content

Commit c2f231d

Browse files
TrucHLeShabirmean
authored andcommitted
samples: get operation (#83)
1 parent d06d9e4 commit c2f231d

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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_get_operation]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.longrunning.Operation;
23+
import com.google.longrunning.OperationsClient;
24+
import java.io.IOException;
25+
26+
public class GetOperation {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace this variable before running the sample.
30+
String operationName = "projects/my_project_id/locations/us-central1/operations/12345";
31+
32+
getOperation(operationName);
33+
}
34+
35+
public static Operation getOperation(String operationName) throws IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
40+
OperationsClient operationsClient = client.getOperationsClient();
41+
Operation operation = operationsClient.getOperation(operationName);
42+
43+
System.out.printf("Got operation %s%n", operation.getName());
44+
return operation;
45+
}
46+
}
47+
}
48+
49+
// [END contactcenterinsights_get_operation]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.api.gax.rpc.ApiException;
23+
import com.google.longrunning.Operation;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
@RunWith(JUnit4.class)
35+
public class GetOperationIT {
36+
37+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
private String conversationName;
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(String.format(varName), String.format(varName));
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() throws IOException {
61+
System.setOut(null);
62+
}
63+
64+
@Test
65+
public void testGetOperation() throws IOException {
66+
// TODO(developer): Replace this variable with your operation name.
67+
String operationName =
68+
String.format("projects/%s/locations/us-central1/operations/12345", PROJECT_ID);
69+
70+
try {
71+
Operation operation = GetOperation.getOperation(operationName);
72+
assertThat(bout.toString()).contains(operation.getName());
73+
} catch (ApiException exception) {
74+
if (!exception.getMessage().contains("not found")) {
75+
throw exception;
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)