shunping commented on code in PR #37592:
URL: https://github.com/apache/beam/pull/37592#discussion_r2823283642


##########
sdks/java/extensions/google-cloud-platform-core/src/test/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtilParameterizedIT.java:
##########
@@ -297,4 +300,295 @@ public void testCreateAndRemoveBucket() throws 
IOException {
       }
     }
   }
+
+  private List<GcsPath> createTestBucketHelper(String bucketName) throws 
IOException {
+    final List<GcsPath> originPaths =
+        Arrays.asList(
+            
GcsPath.fromUri("gs://apache-beam-samples/shakespeare/kingrichardii.txt"),
+            
GcsPath.fromUri("gs://apache-beam-samples/shakespeare/kingrichardiii.txt"));
+
+    final List<GcsPath> testPaths =
+        originPaths.stream()
+            .map(o -> GcsPath.fromComponents(bucketName, o.getObject()))
+            .collect(Collectors.toList());
+
+    // create bucket and copy some initial files into there
+    if (experiment.equals("use_gcsutil_v2")) {
+      gcsUtil.createBucket(BucketInfo.of(bucketName));
+
+      gcsUtil.copyV2(originPaths, testPaths);
+    } else {
+      GcsOptions gcsOptions = options.as(GcsOptions.class);
+      gcsUtil.createBucket(gcsOptions.getProject(), new 
Bucket().setName(bucketName));
+
+      final List<String> originList =
+          originPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+      final List<String> testList =
+          testPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+      gcsUtil.copy(originList, testList);
+    }
+
+    return testPaths;
+  }
+
+  private void tearDownTestBucketHelper(String bucketName) {
+    try {
+      // use "**" in the pattern to match any characters including "/".
+      final List<GcsPath> paths =
+          gcsUtil.expand(GcsPath.fromUri(String.format("gs://%s/**", 
bucketName)));
+      if (experiment.equals("use_gcsutil_v2")) {
+        gcsUtil.remove(paths, MissingStrategy.SKIP_IF_MISSING);
+        gcsUtil.removeBucket(BucketInfo.of(bucketName));
+      } else {
+        
gcsUtil.remove(paths.stream().map(GcsPath::toString).collect(Collectors.toList()));
+        gcsUtil.removeBucket(new Bucket().setName(bucketName));
+      }
+    } catch (IOException e) {
+    }

Review Comment:
   Fixed.



##########
sdks/java/extensions/google-cloud-platform-core/src/test/java/org/apache/beam/sdk/extensions/gcp/util/GcsUtilParameterizedIT.java:
##########
@@ -297,4 +300,295 @@ public void testCreateAndRemoveBucket() throws 
IOException {
       }
     }
   }
+
+  private List<GcsPath> createTestBucketHelper(String bucketName) throws 
IOException {
+    final List<GcsPath> originPaths =
+        Arrays.asList(
+            
GcsPath.fromUri("gs://apache-beam-samples/shakespeare/kingrichardii.txt"),
+            
GcsPath.fromUri("gs://apache-beam-samples/shakespeare/kingrichardiii.txt"));
+
+    final List<GcsPath> testPaths =
+        originPaths.stream()
+            .map(o -> GcsPath.fromComponents(bucketName, o.getObject()))
+            .collect(Collectors.toList());
+
+    // create bucket and copy some initial files into there
+    if (experiment.equals("use_gcsutil_v2")) {
+      gcsUtil.createBucket(BucketInfo.of(bucketName));
+
+      gcsUtil.copyV2(originPaths, testPaths);
+    } else {
+      GcsOptions gcsOptions = options.as(GcsOptions.class);
+      gcsUtil.createBucket(gcsOptions.getProject(), new 
Bucket().setName(bucketName));
+
+      final List<String> originList =
+          originPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+      final List<String> testList =
+          testPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+      gcsUtil.copy(originList, testList);
+    }
+
+    return testPaths;
+  }
+
+  private void tearDownTestBucketHelper(String bucketName) {
+    try {
+      // use "**" in the pattern to match any characters including "/".
+      final List<GcsPath> paths =
+          gcsUtil.expand(GcsPath.fromUri(String.format("gs://%s/**", 
bucketName)));
+      if (experiment.equals("use_gcsutil_v2")) {
+        gcsUtil.remove(paths, MissingStrategy.SKIP_IF_MISSING);
+        gcsUtil.removeBucket(BucketInfo.of(bucketName));
+      } else {
+        
gcsUtil.remove(paths.stream().map(GcsPath::toString).collect(Collectors.toList()));
+        gcsUtil.removeBucket(new Bucket().setName(bucketName));
+      }
+    } catch (IOException e) {
+    }
+  }
+
+  @Test
+  public void testCopy() throws IOException {
+    final String existingBucket = "apache-beam-temp-bucket-12345";
+    final String nonExistentBucket = "my-random-test-bucket-12345";
+
+    try {
+      final List<GcsPath> srcPaths = createTestBucketHelper(existingBucket);
+      final List<GcsPath> dstPaths =
+          srcPaths.stream()
+              .map(o -> GcsPath.fromComponents(existingBucket, o.getObject() + 
".bak"))
+              .collect(Collectors.toList());
+      final List<GcsPath> errPaths =
+          srcPaths.stream()
+              .map(o -> GcsPath.fromComponents(nonExistentBucket, 
o.getObject()))
+              .collect(Collectors.toList());
+
+      assertNotExists(dstPaths.get(0));
+      assertNotExists(dstPaths.get(1));
+
+      if (experiment.equals("use_gcsutil_v2")) {
+        // (1) when the target files do not exist
+        gcsUtil.copyV2(srcPaths, dstPaths);
+        assertExists(dstPaths.get(0));
+        assertExists(dstPaths.get(1));
+
+        // (2) when the target files exist
+        // (2a) no exception on SAFE_OVERWRITE, ALWAYS_OVERWRITE, 
SKIP_IF_EXISTS
+        gcsUtil.copyV2(srcPaths, dstPaths);
+        gcsUtil.copy(srcPaths, dstPaths, OverwriteStrategy.ALWAYS_OVERWRITE);
+        gcsUtil.copy(srcPaths, dstPaths, OverwriteStrategy.SKIP_IF_EXISTS);
+
+        // (2b) raise exception on FAIL_IF_EXISTS
+        assertThrows(
+            FileAlreadyExistsException.class,
+            () -> gcsUtil.copy(srcPaths, dstPaths, 
OverwriteStrategy.FAIL_IF_EXISTS));
+
+        // (3) raise exception when the target bucket is nonexistent.
+        assertThrows(FileNotFoundException.class, () -> 
gcsUtil.copyV2(srcPaths, errPaths));
+
+        // (4) raise exception when the source files are nonexistent.
+        assertThrows(FileNotFoundException.class, () -> 
gcsUtil.copyV2(errPaths, dstPaths));
+      } else {
+        final List<String> srcList =
+            srcPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+        final List<String> dstList =
+            dstPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+        final List<String> errList =
+            errPaths.stream().map(o -> 
o.toString()).collect(Collectors.toList());
+
+        // (1) when the target files do not exist
+        gcsUtil.copy(srcList, dstList);
+        assertExists(dstPaths.get(0));
+        assertExists(dstPaths.get(1));
+
+        // (2) when the target files exist, no exception
+        gcsUtil.copy(srcList, dstList);
+
+        // (3) raise exception when the target bucket is nonexistent.
+        assertThrows(FileNotFoundException.class, () -> gcsUtil.copy(srcList, 
errList));
+
+        // (4) raise exception when the source files are nonexistent.
+        assertThrows(FileNotFoundException.class, () -> gcsUtil.copy(errList, 
dstList));
+      }
+    } catch (IOException e) {
+      throw e;

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to