19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static junit .framework .TestCase .assertNotNull ;
21
21
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 ;
22
29
import java .io .ByteArrayOutputStream ;
23
30
import java .io .IOException ;
24
31
import java .io .PrintStream ;
25
32
import java .util .UUID ;
33
+ import java .util .concurrent .CancellationException ;
26
34
import java .util .concurrent .ExecutionException ;
35
+ import java .util .concurrent .TimeUnit ;
27
36
import java .util .concurrent .TimeoutException ;
28
37
import org .junit .After ;
29
38
import org .junit .Before ;
@@ -56,20 +65,34 @@ public static void checkRequirements() {
56
65
}
57
66
58
67
@ 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
65
71
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
66
72
// To prevent name collisions when running tests in multiple java versions at once.
67
73
// AutoML doesn't allow "-", but accepts "_"
68
74
String datasetName =
69
75
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
+ }
73
96
74
97
bout = new ByteArrayOutputStream ();
75
98
out = new PrintStream (bout );
@@ -85,9 +108,23 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
85
108
86
109
@ Test
87
110
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
+ }
90
126
String got = bout .toString ();
127
+
91
128
assertThat (got ).contains ("Dataset imported." );
92
129
}
93
130
}
0 commit comments