1919import static com .google .common .truth .Truth .assertThat ;
2020import static junit .framework .TestCase .assertNotNull ;
2121
22+ import com .google .api .gax .longrunning .OperationFuture ;
23+ import com .google .cloud .automl .v1 .AutoMlClient ;
24+ import com .google .cloud .automl .v1 .DatasetName ;
25+ import com .google .cloud .automl .v1 .GcsSource ;
26+ import com .google .cloud .automl .v1 .InputConfig ;
27+ import com .google .cloud .automl .v1 .OperationMetadata ;
28+ import com .google .protobuf .Empty ;
2229import java .io .ByteArrayOutputStream ;
2330import java .io .IOException ;
2431import java .io .PrintStream ;
32+ import java .util .Arrays ;
33+ import java .util .concurrent .ExecutionException ;
34+ import java .util .concurrent .TimeUnit ;
2535import org .junit .After ;
2636import org .junit .Before ;
2737import org .junit .BeforeClass ;
3242@ RunWith (JUnit4 .class )
3343public class GetOperationStatusTest {
3444 private static final String PROJECT_ID = System .getenv ("AUTOML_PROJECT_ID" );
35- private String operationId ;
45+ private static final String DATASET_ID = "TRL5394674636845744128" ;
46+ private static final String BUCKET = "gs://translate_data_exported/translation.csv" ;
47+
48+ private String operationFullName ;
3649 private ByteArrayOutputStream bout ;
3750 private PrintStream out ;
3851
@@ -49,30 +62,48 @@ public static void checkRequirements() {
4962 }
5063
5164 @ Before
52- public void setUp () throws IOException {
65+ public void setUp () throws IOException , ExecutionException , InterruptedException {
5366 bout = new ByteArrayOutputStream ();
5467 out = new PrintStream (bout );
5568 System .setOut (out );
5669
57- ListOperationStatus .listOperationStatus (PROJECT_ID );
58- String got = bout .toString ();
59- operationId = got .split ("\n " )[1 ].split (":" )[1 ].trim ();
60- assertThat (got ).contains ("Operation details:" );
70+ // start a export data into dataset and cancel it before it finishes.
71+ try (AutoMlClient client = AutoMlClient .create ()) {
72+ // Get the complete path of the dataset.
73+ DatasetName datasetFullId = DatasetName .of (PROJECT_ID , "us-central1" , DATASET_ID );
74+
75+ GcsSource gcsSource =
76+ GcsSource .newBuilder ().addAllInputUris (Arrays .asList (BUCKET .split ("," ))).build ();
77+
78+ InputConfig inputConfig = InputConfig .newBuilder ().setGcsSource (gcsSource ).build ();
79+
80+ // Start the import LRO job
81+ OperationFuture <Empty , OperationMetadata > operation =
82+ client .importDataAsync (datasetFullId , inputConfig );
83+
84+ operationFullName = operation .getName ();
85+ }
6186
6287 bout = new ByteArrayOutputStream ();
6388 out = new PrintStream (bout );
6489 System .setOut (out );
6590 }
6691
67- @ After
68- public void tearDown () {
69- System .setOut (null );
70- }
71-
7292 @ Test
7393 public void testGetOperationStatus () throws IOException {
74- GetOperationStatus .getOperationStatus (operationId );
94+ GetOperationStatus .getOperationStatus (operationFullName );
7595 String got = bout .toString ();
7696 assertThat (got ).contains ("Operation details:" );
7797 }
98+
99+ @ After
100+ public void tearDown () throws IOException , InterruptedException {
101+ try (AutoMlClient client = AutoMlClient .create ()) {
102+ // terminate export data LRO.
103+ client .getOperationsClient ().cancelOperation (operationFullName );
104+ client .getOperationsClient ().awaitTermination (5 , TimeUnit .SECONDS );
105+ }
106+
107+ System .setOut (null );
108+ }
78109}
0 commit comments