8000 automl: move samples out of branch (#2325) · soumyakuberan/java-docs-samples@4b8751a · GitHub
[go: up one dir, main page]

10000
Skip to content

Commit 4b8751a

Browse files
automl: move samples out of branch (GoogleCloudPlatform#2325)
1 parent 4f95319 commit 4b8751a

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.automl;
18+
19+
// [START automl_get_operation_status_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.longrunning.Operation;
22+
23+
import java.io.IOException;
24+
25+
class GetOperationStatus {
26+
27+
static void getOperationStatus() throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]";
30+
getOperationStatus(operationFullId);
31+
}
32+
33+
// Get the status of an operation
34+
static void getOperationStatus(String operationFullId) throws IOException {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests. After completing all of your requests, call
37+
// the "close" method on the client to safely clean up any remaining background resources.
38+
try (AutoMlClient client = AutoMlClient.create()) {
39+
// Get the latest state of a long-running operation.
40+
Operation operation = client.getOperationsClient().getOperation(operationFullId);
41+
42+
// Display operation details.
43+
System.out.println("Operation details:");
44+
System.out.format("\tName: %s\n", operation.getName());
45+
System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl());
46+
System.out.format("\tDone: %s\n", operation.getDone());
47+
if (operation.hasResponse()) {
48+
System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl());
49+
}
50+
if (operation.hasError()) {
51+
System.out.println("\tResponse:");
52+
System.out.format("\t\tError code: %s\n", operation.getError().getCode());
53+
System.out.format("\t\tError message: %s\n", operation.getError().getMessage());
54+
}
55+
}
56+
}
57+
}
58+
// [END automl_get_operation_status_beta]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import 8000 static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.automl.v1beta1.AutoMlClient;
23+
import com.google.cloud.automl.v1beta1.LocationName;
24+
import com.google.longrunning.ListOperationsRequest;
25+
import com.google.longrunning.Operation;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
@RunWith(JUnit4.class)
39+
public class GetOperationStatusTest {
40+
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
41+
private String operationId;
42+
private ByteArrayOutputStream bout;
43+
private PrintStream out;
44+
45+
private static void requireEnvVar(String varName) {
46+
assertNotNull(
47+
System.getenv(varName),
48+
"Environment variable '%s' is required to perform these tests.".format(varName));
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
54+
requireEnvVar("AUTOML_PROJECT_ID");
55+
}
56+
57+
@Before
58+
public void setUp() throws IOException {
59+
// Use list operations to get a single operation id for the get call.
60+
try (AutoMlClient client = AutoMlClient.create()) {
61+
LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1");
62+
ListOperationsRequest request =
63+
ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build();
64+
Operation operation =
65+
client.getOperationsClient().listOperations(request).iterateAll().iterator().next();
66+
operationId = operation.getName();
67+
}
68+
69+
bout = new ByteArrayOutputStream();
70+
out = new PrintStream(bout);
71+
System.setOut(out);
72+
}
73+
74+
@After
75+
public void tearDown() {
76+
System.setOut(null);
77+
}
78+
79+
@Test
80+
public void testGetOperationStatus() throws IOException {
81+
GetOperationStatus.getOperationStatus(operationId);
82+
String got = bout.toString();
83+
assertThat(got).contains("Operation details:");
84+
}
85+
}

0 commit comments

Comments
 (0)
0