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