15
15
import static com .google .common .truth .Truth .assertThat ;
16
16
import static com .google .common .truth .Truth .assertWithMessage ;
17
17
18
+ import com .google .cloud .batch .v1 .Job ;
18
19
import com .google .cloud .storage .Blob ;
19
20
import com .google .cloud .storage .Bucket ;
20
21
import com .google .cloud .storage .BucketInfo ;
21
22
import com .google .cloud .storage .Storage ;
23
+ import com .google .cloud .storage .StorageClass ;
22
24
import com .google .cloud .storage .StorageOptions ;
23
25
import java .io .ByteArrayOutputStream ;
24
26
import java .io .IOException ;
25
27
import java .io .PrintStream ;
26
- import java .util .Arrays ;
27
- import java .util .Base64 ;
28
+ import java .nio .charset .StandardCharsets ;
28
29
import java .util .UUID ;
29
30
import java .util .concurrent .ExecutionException ;
30
31
import java .util .concurrent .TimeUnit ;
@@ -44,7 +45,6 @@ public class BatchBucketIT {
44
45
private static final String REGION = "europe-north1" ;
45
46
private static String SCRIPT_JOB_NAME ;
46
47
private static String BUCKET_NAME ;
47
-
48
48
private ByteArrayOutputStream stdOut ;
49
49
50
50
// Check if the required environment variables are set.
@@ -81,15 +81,31 @@ public static void cleanup()
81
81
ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
82
82
System .setOut (new PrintStream (stdOut ));
83
83
84
+ // Delete bucket.
85
+ Storage storage = StorageOptions .newBuilder ().setProjectId (PROJECT_ID ).build ().getService ();
86
+ Bucket bucket = storage .get (BUCKET_NAME );
87
+ for (Blob blob : storage .list (bucket .getName ()).iterateAll ()) {
88
+ storage .delete (blob .getBlobId ());
89
+ }
90
+ storage .delete (bucket .getName ());
91
+ System .out .println ("Bucket " + bucket .getName () + " was deleted" );
92
+
93
+ // Delete job.
84
94
DeleteJob .deleteJob (PROJECT_ID , REGION , SCRIPT_JOB_NAME );
85
95
86
96
stdOut .close ();
87
97
System .setOut (out );
88
98
}
89
99
90
100
private static void createBucket (String bucketName ) {
91
- Storage storage = StorageOptions .getDefaultInstance ().getService ();
92
- storage .create (BucketInfo .of (bucketName ));
101
+ Storage storage = StorageOptions .newBuilder ().setProjectId (PROJECT_ID ).build ().getService ();
102
+ StorageClass storageClass = StorageClass .COLDLINE ;
103
+ String location = "US" ;
104
+ storage .create (
105
+ BucketInfo .newBuilder (bucketName )
106
+ .setStorageClass (storageClass )
107
+ .setLocation (location )
108
+ .build ());
93
109
}
94
110
95
111
@ Before
@@ -109,19 +125,22 @@ public void testBucketJob()
109
125
throws IOException , ExecutionException , InterruptedException , TimeoutException {
110
126
CreateWithMountedBucket .createScriptJobWithBucket (PROJECT_ID , REGION , SCRIPT_JOB_NAME ,
111
127
BUCKET_NAME );
128
+ Job job = Util .getJob (PROJECT_ID , REGION , SCRIPT_JOB_NAME );
129
+ Util .waitForJobCompletion (job );
112
130
assertThat (stdOut .toString ()).contains ("Successfully created the job" );
113
131
testBucketContent ();
114
132
}
115
133
116
134
public void testBucketContent () {
117
135
String fileNameTemplate = "output_task_%s.txt" ;
118
- String fileContentTemplate = "Hello world from task {task_number}. \n " ;
136
+ String fileContentTemplate ;
119
137
120
138
Storage storage = StorageOptions .getDefaultInstance ().getService ();
121
139
Bucket bucket = storage .get (BUCKET_NAME );
122
140
for (int i = 0 ; i < 4 ; i ++) {
141
+ fileContentTemplate = String .format ("Hello world from task %s.\n " , i );
123
142
Blob blob = bucket .get (String .format (fileNameTemplate , i ));
124
- String content = Arrays . toString ( Base64 . getDecoder (). decode ( blob .getContent ()) );
143
+ String content = new String ( blob .getContent (), StandardCharsets . UTF_8 );
125
144
assertThat (fileContentTemplate ).matches (content );
126
145
}
127
146
}
0 commit comments