Skip to content

Commit

Permalink
feat: set storage update time in FakeStorageRpc
Browse files Browse the repository at this point in the history
  • Loading branch information
hosainnet committed Jul 19, 2020
1 parent 65d448e commit 87f095a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions .mvn/jvm.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Xmx1024m -XX:CICompilerCount=1 -XX:TieredStopAtLevel=1 -Djava.security.egd=file:/dev/./urandom
1 change: 1 addition & 0 deletions .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-DaltSnapshotDeploymentRepository=repo.spring.io::default::https://repo.spring.io/libs-snapshot-local -P spring
Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
1 change: 1 addition & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -70,6 +73,8 @@
@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
Expand All @@ -95,6 +100,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));
Expand Down Expand Up @@ -357,6 +363,7 @@ public void write(
futureContents.remove(uploadId);
if (metadata.containsKey(uploadId)) {
StorageObject storageObject = metadata.get(uploadId);
storageObject.setUpdated(now());
Long generation = storageObject.getGeneration();
if (null == generation) {
generation = Long.valueOf(0);
Expand Down Expand Up @@ -401,6 +408,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);

Expand All @@ -414,6 +422,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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ public void before() throws IOException {
BlobId id = BlobId.of(testBucket, sourceFile);
BlobInfo info = BlobInfo.newBuilder(id).build();

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

@After
Expand Down Expand Up @@ -105,4 +102,20 @@ 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 87f095a

Please sign in to comment.