From cadf081c4ba35b33307c38cddd08959b142edf23 Mon Sep 17 00:00:00 2001 From: Domrachev Denis Date: Wed, 20 Apr 2022 19:26:18 +0400 Subject: [PATCH] fix: check generation for nullability before incrementing it (#888) --- .../contrib/nio/testing/FakeStorageRpc.java | 5 +++- .../nio/testing/LocalStorageHelperTest.java | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java index 03d1e38d..bdc71b25 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java @@ -413,7 +413,10 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage // if this is a new file, set generation to 1, else increment the existing generation long generation = 1; if (metadata.containsKey(destKey)) { - generation = metadata.get(destKey).getGeneration() + 1; + Long storedGeneration = metadata.get(destKey).getGeneration(); + if (null != storedGeneration) { + generation = storedGeneration + 1; + } } checkGeneration(destKey, generationMatch); diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java index 0f186117..6cac05f8 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java @@ -156,4 +156,28 @@ public void testStorageOptionIsSerializable_customOptions() throws Exception { assertThat(ois.readObject()).isEqualTo(storageOptions); } } + + @Test + public void testCopyOperationOverwritesExistingFile() { + String bucket = "bucket"; + String original = "original"; + String replacement = "replacement"; + byte[] originalContent = "original content".getBytes(); + byte[] replacementContent = "replacement content".getBytes(); + + localStorageService.create(BlobInfo.newBuilder(bucket, original).build(), originalContent); + localStorageService.create( + BlobInfo.newBuilder(bucket, replacement).build(), replacementContent); + + final Storage.CopyRequest request = + Storage.CopyRequest.newBuilder() + .setSource(BlobId.of(bucket, replacement)) + .setTarget(BlobId.of(bucket, original)) + .build(); + + localStorageService.copy(request).getResult(); + + assertThat(localStorageService.readAllBytes(BlobId.of(bucket, original))) + .isEqualTo(replacementContent); + } }