1919import static com .google .common .truth .Truth .assertThat ;
2020import static junit .framework .TestCase .assertNotNull ;
2121
22+ import com .google .api .core .ApiFuture ;
23+ import com .google .cloud .automl .v1beta1 .AutoMlClient ;
24+ import com .google .cloud .automl .v1beta1 .CreateDatasetRequest ;
25+ import com .google .cloud .automl .v1beta1 .Dataset ;
26+ import com .google .cloud .automl .v1beta1 .LocationName ;
27+ import com .google .cloud .automl .v1beta1 .TextExtractionDatasetMetadata ;
28+ import com .google .longrunning .Operation ;
2229import java .io .ByteArrayOutputStream ;
2330import java .io .IOException ;
2431import java .io .PrintStream ;
2532import java .util .UUID ;
33+ import java .util .concurrent .CancellationException ;
2634import java .util .concurrent .ExecutionException ;
35+ import java .util .concurrent .TimeUnit ;
2736import java .util .concurrent .TimeoutException ;
2837import org .junit .After ;
2938import org .junit .Before ;
@@ -56,20 +65,34 @@ public static void checkRequirements() {
5665 }
5766
5867 @ Before
59- public void setUp () throws InterruptedException , ExecutionException , IOException {
60- bout = new ByteArrayOutputStream ();
61- out = new PrintStream (bout );
62- System .setOut (out );
63-
64- // Create a dataset that can be used for the import test
68+ public void setUp ()
69+ throws IOException , InterruptedException , ExecutionException , TimeoutException {
70+ // Create a fake dataset to be deleted
6571 // Create a random dataset name with a length of 32 characters (max allowed by AutoML)
6672 // To prevent name collisions when running tests in multiple java versions at once.
6773 // AutoML doesn't allow "-", but accepts "_"
6874 String datasetName =
6975 String .format ("test_%s" , UUID .randomUUID ().toString ().replace ("-" , "_" ).substring (0 , 26 ));
70- LanguageEntityExtractionCreateDataset .createDataset (PROJECT_ID , datasetName );
71- String got = bout .toString ();
72- datasetId = got .split ("Dataset id: " )[1 ].split ("\n " )[0 ];
76+ try (AutoMlClient client = AutoMlClient .create ()) {
77+
78+ LocationName projectLocation = LocationName .of (PROJECT_ID , "us-central1" );
79+ TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata .newBuilder ().build ();
80+ Dataset dataset =
81+ Dataset .newBuilder ()
82+ .setDisplayName (datasetName )
83+ .setTextExtractionDatasetMetadata (metadata )
84+ .build ();
85+
86+ CreateDatasetRequest request =
87+ CreateDatasetRequest .newBuilder ()
88+ .setParent (projectLocation .toString ())
89+ .setDataset (dataset )
90+ .build ();
91+ ApiFuture <Dataset > future = client .createDatasetCallable ().futureCall (request );
92+ Dataset createdDataset = future .get (5 , TimeUnit .MINUTES );
93+ String [] names = createdDataset .getName ().split ("/" );
94+ datasetId = names [names .length - 1 ];
95+ }
7396
7497 bout = new ByteArrayOutputStream ();
7598 out = new PrintStream (bout );
@@ -85,9 +108,23 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
85108
86109 @ Test
87110 public void testImportDataset ()
88- throws IOException , ExecutionException , InterruptedException , TimeoutException {
89- ImportDataset .importDataset (PROJECT_ID , datasetId , BUCKET + "/entity-extraction/dataset.csv" );
111+ throws InterruptedException , ExecutionException , TimeoutException , IOException {
112+
113+ try {
114+ ImportDataset .importDataset (PROJECT_ID , datasetId , BUCKET + "/entity-extraction/dataset.csv" );
115+ } catch (CancellationException ex ) {
116+ // capture operation ID from output and wait for that operation to be finished.
117+ String fullOperationId = ex .getMessage ().split ("Operation name: " )[1 ].trim ();
118+ AutoMlClient client = AutoMlClient .create ();
119+ Operation importDatasetLro = client .getOperationsClient ().getOperation (fullOperationId );
120+ while (!importDatasetLro .getDone ()) {
121+ Thread .sleep (3000 );
122+ }
123+ // retry the import.
124+ ImportDataset .importDataset (PROJECT_ID , datasetId , BUCKET + "/entity-extraction/dataset.csv" );
125+ }
90126 String got = bout .toString ();
127+
91128 assertThat (got ).contains ("Dataset imported." );
92129 }
93130}
0 commit comments