/
LocalStorageHelper.java
100 lines (93 loc) · 3.35 KB
/
LocalStorageHelper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2016 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 com.google.cloud.spi.ServiceRpcFactory;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.spi.v1.StorageRpc;
/**
* Utility to create an in-memory storage configuration for testing. Storage options can be obtained
* via the {@link #getOptions()} method. Returned options will point to FakeStorageRpc.
*
* <p>Note, the created in-memory storage configuration supports limited set of operations and is
* <b>not</b> thread-safe:
*
* <ul>
* <li>Supported operations
* <ul>
* <li>object create
* <li>object get
* <li>object delete
* <li>list the contents of a bucket
* </ul>
* <li>Unsupported operations
* <ul>
* <li>bucket create
* <li>bucket get
* <li>bucket delete
* <li>list all buckets
* <li>generations
* <li>file attributes
* <li>patch
* <li>continueRewrite
* <li>createBatch
* <li>checksums, etags
* <li>IAM operations
* </ul>
* </ul>
*
* {@link FakeStorageRpc#list(String, java.util.Map)} lists all the objects that have been created
* rather than the objects in the provided bucket. Since this class does not support creating,
* listing and deleting buckets, the parameter bucket here is not actually used and on serves as a
* placeholder.
*/
public final class LocalStorageHelper {
// used for testing. Will throw if you pass it an option.
private static final FakeStorageRpc instance = new FakeStorageRpc(true);
private LocalStorageHelper() {}
/**
* Returns a {@link StorageOptions} that use the static FakeStorageRpc instance, and resets it
* first so you start from a clean slate. That instance will throw if you pass it any option.
*/
public static StorageOptions getOptions() {
instance.reset();
return StorageOptions.newBuilder()
.setProjectId("fake-project-for-testing")
.setServiceRpcFactory(
new ServiceRpcFactory<StorageOptions>() {
@Override
public StorageRpc create(StorageOptions options) {
return instance;
}
})
.build();
}
/**
* Returns a {@link StorageOptions} that creates a new FakeStorageRpc instance with the given
* option.
*/
public static StorageOptions customOptions(final boolean throwIfOptions) {
return StorageOptions.newBuilder()
.setProjectId("fake-project-for-testing")
.setServiceRpcFactory(
new ServiceRpcFactory<StorageOptions>() {
@Override
public StorageRpc create(StorageOptions options) {
return new FakeStorageRpc(throwIfOptions);
}
})
.build();
}
}