|
| 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