Skip to content
Permalink
Browse files
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
  • Loading branch information
hosainnet committed Jan 27, 2021
1 parent 2c70858 commit 1328de4adf15450f055cae0506ffc97a97309b33
@@ -16,6 +16,7 @@

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

import com.google.api.client.util.DateTime;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.ServiceAccount;
import com.google.api.services.storage.model.StorageObject;
@@ -29,8 +30,10 @@
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.file.FileAlreadyExistsException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -70,6 +73,9 @@
@NotThreadSafe
class FakeStorageRpc extends StorageRpcTestBase {

private static final SimpleDateFormat RFC_3339_FORMATTER =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

// fullname -> metadata
Map<String, StorageObject> metadata = new HashMap<>();
// fullname -> contents
@@ -95,6 +101,7 @@ public StorageObject create(StorageObject object, InputStream content, Map<Optio
throws StorageException {
potentiallyThrow(options);
String key = fullname(object);
object.setUpdated(now());
metadata.put(key, object);
try {
contents.put(key, com.google.common.io.ByteStreams.toByteArray(content));
@@ -369,6 +376,7 @@ public StorageObject writeWithResponse(
futureContents.remove(uploadId);
if (metadata.containsKey(uploadId)) {
storageObject = metadata.get(uploadId);
storageObject.setUpdated(now());
Long generation = storageObject.getGeneration();
if (null == generation) {
generation = Long.valueOf(0);
@@ -414,6 +422,7 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage

rewriteRequest.target.setGeneration(generation);
rewriteRequest.target.setSize(BigInteger.valueOf(data.length));
rewriteRequest.target.setUpdated(metadata.get(sourceKey).getUpdated());

metadata.put(destKey, rewriteRequest.target);

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

private static DateTime now() {
return DateTime.parseRfc3339(RFC_3339_FORMATTER.format(new Date()));
}

private String fullname(StorageObject so) {
return (so.getBucket() + "/" + so.getName());
}
@@ -52,11 +52,8 @@ public void before() throws IOException {
BlobId id = BlobId.of(testBucket, sourceFile);
BlobInfo info = BlobInfo.newBuilder(id).build();

WriteChannel writer = localStorageService.writer(info);
try {
try (WriteChannel writer = localStorageService.writer(info)) {
writer.write(ByteBuffer.wrap(payloadBytes));
} finally {
writer.close();
}
}

@@ -105,4 +102,19 @@ public void testCopyIncrementsGenerations() {
assertThat(obj.getGeneration()).isEqualTo(2);
assertThat(obj.getSize()).isEqualTo(7);
}

@Test
public void testWriteNewFileSetsUpdateTime() {
Blob obj = localStorageService.get(BlobId.of(testBucket, sourceFile));

assertThat(obj.getUpdateTime()).isNotNull();
}

@Test
public void testCreateNewFileSetsUpdateTime() {
BlobInfo info = BlobInfo.newBuilder(BlobId.of(testBucket, "newFile")).build();
Blob obj = localStorageService.create(info);

assertThat(obj.getUpdateTime()).isNotNull();
}
}

0 comments on commit 1328de4

Please sign in to comment.