-
Notifications
You must be signed in to change notification settings - Fork 31
/
FakeStorageRpcTest.java
68 lines (61 loc) · 2.68 KB
/
FakeStorageRpcTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage.contrib.nio.testing;
import static com.google.common.truth.Truth.assertThat;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobWriteOption;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper.FakeStorageRpcFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public final class FakeStorageRpcTest {
@Test
public void overwritingAnObjectOverwritesItsContent() throws IOException {
Storage storage =
StorageOptions.http()
.setServiceRpcFactory(new FakeStorageRpcFactory())
.build()
.getService();
try (WriteChannel writer =
storage.writer(
BlobInfo.newBuilder(BlobId.of("bucket", "obj", 0L)).build(),
BlobWriteOption.generationMatch())) {
writer.write(ByteBuffer.wrap("abc".getBytes(StandardCharsets.UTF_8)));
}
Blob gen1 = storage.get(BlobId.of("bucket", "obj"));
// get existing generation
String gen1read1 = new String(gen1.getContent(), StandardCharsets.UTF_8);
assertThat(gen1read1).isEqualTo("abc");
System.out.println("gen1read1 = " + gen1read1);
// start an upload that will overwrite the existing generation
WriteChannel writer = storage.writer(gen1, BlobWriteOption.generationMatch());
writer.write(ByteBuffer.wrap("def".getBytes(StandardCharsets.UTF_8)));
// make sure we can still read the existing generations value after starting but before closing
String gen1read2 = new String(gen1.getContent(), StandardCharsets.UTF_8);
assertThat(gen1read2).isEqualTo("abc");
writer.close();
Blob gen2 = storage.get(BlobId.of("bucket", "obj"));
String gen2read1 = new String(gen2.getContent(), StandardCharsets.UTF_8);
System.out.println("gen2read1 = " + gen2read1);
assertThat(gen2read1).isEqualTo("def");
}
}