Skip to content
This repository was archived by the owner on Apr 2, 2026. It is now read-only.

Commit 1328de4

Browse files
authored
fix: Set storage update time in FakeStorageRpc (#174)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-storage-nio/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #173
1 parent 2c70858 commit 1328de4

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.storage.contrib.nio.testing;
1818

19+
import com.google.api.client.util.DateTime;
1920
import com.google.api.services.storage.model.Bucket;
2021
import com.google.api.services.storage.model.ServiceAccount;
2122
import com.google.api.services.storage.model.StorageObject;
@@ -29,8 +30,10 @@
2930
import java.io.OutputStream;
3031
import java.math.BigInteger;
3132
import java.nio.file.FileAlreadyExistsException;
33+
import java.text.SimpleDateFormat;
3234
import java.util.ArrayList;
3335
import java.util.Arrays;
36+
import java.util.Date;
3437
import java.util.HashMap;
3538
import java.util.List;
3639
import java.util.Map;
@@ -70,6 +73,9 @@
7073
@NotThreadSafe
7174
class FakeStorageRpc extends StorageRpcTestBase {
7275

76+
private static final SimpleDateFormat RFC_3339_FORMATTER =
77+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
78+
7379
// fullname -> metadata
7480
Map<String, StorageObject> metadata = new HashMap<>();
7581
// fullname -> contents
@@ -95,6 +101,7 @@ public StorageObject create(StorageObject object, InputStream content, Map<Optio
95101
throws StorageException {
96102
potentiallyThrow(options);
97103
String key = fullname(object);
104+
object.setUpdated(now());
98105
metadata.put(key, object);
99106
try {
100107
contents.put(key, com.google.common.io.ByteStreams.toByteArray(content));
@@ -369,6 +376,7 @@ public StorageObject writeWithResponse(
369376
futureContents.remove(uploadId);
370377
if (metadata.containsKey(uploadId)) {
371378
storageObject = metadata.get(uploadId);
379+
storageObject.setUpdated(now());
372380
Long generation = storageObject.getGeneration();
373381
if (null == generation) {
374382
generation = Long.valueOf(0);
@@ -414,6 +422,7 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage
414422

415423
rewriteRequest.target.setGeneration(generation);
416424
rewriteRequest.target.setSize(BigInteger.valueOf(data.length));
425+
rewriteRequest.target.setUpdated(metadata.get(sourceKey).getUpdated());
417426

418427
metadata.put(destKey, rewriteRequest.target);
419428

@@ -427,6 +436,10 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage
427436
data.length);
428437
}
429438

439+
private static DateTime now() {
440+
return DateTime.parseRfc3339(RFC_3339_FORMATTER.format(new Date()));
441+
}
442+
430443
private String fullname(StorageObject so) {
431444
return (so.getBucket() + "/" + so.getName());
432445
}

google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ public void before() throws IOException {
5252
BlobId id = BlobId.of(testBucket, sourceFile);
5353
BlobInfo info = BlobInfo.newBuilder(id).build();
5454

55-
WriteChannel writer = localStorageService.writer(info);
56-
try {
55+
try (WriteChannel writer = localStorageService.writer(info)) {
5756
writer.write(ByteBuffer.wrap(payloadBytes));
58-
} finally {
59-
writer.close();
6057
}
6158
}
6259

@@ -105,4 +102,19 @@ public void testCopyIncrementsGenerations() {
105102
assertThat(obj.getGeneration()).isEqualTo(2);
106103
assertThat(obj.getSize()).isEqualTo(7);
107104
}
105+
106+
@Test
107+
public void testWriteNewFileSetsUpdateTime() {
108+
Blob obj = localStorageService.get(BlobId.of(testBucket, sourceFile));
109+
110+
assertThat(obj.getUpdateTime()).isNotNull();
111+
}
112+
113+
@Test
114+
public void testCreateNewFileSetsUpdateTime() {
115+
BlobInfo info = BlobInfo.newBuilder(BlobId.of(testBucket, "newFile")).build();
116+
Blob obj = localStorageService.create(info);
117+
118+
assertThat(obj.getUpdateTime()).isNotNull();
119+
}
108120
}

0 commit comments

Comments
 (0)