-
Notifications
You must be signed in to change notification settings - Fork 68
/
ParallelCompositeUploadBlobWriteSessionConfigTest.java
113 lines (98 loc) · 4.43 KB
/
ParallelCompositeUploadBlobWriteSessionConfigTest.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
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* 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;
import static com.google.cloud.storage.TestUtils.assertAll;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import com.google.cloud.storage.MetadataField.PartRange;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartMetadataFieldDecorator;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartNamingStrategy;
import com.google.common.truth.StringSubject;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import org.junit.Test;
public final class ParallelCompositeUploadBlobWriteSessionConfigTest {
@Test
public void partNameStrategy_noPrefix() throws Exception {
PartNamingStrategy strategy = PartNamingStrategy.noPrefix();
String fmt = strategy.fmtName("object23", PartRange.of(1, 32));
assertAll(
// random digest to spread over keyspace
() -> assertField(fmt, 0).hasLength(22),
// name digest
() -> assertField(fmt, 1).hasLength(22),
() -> assertField(fmt, 2).isEqualTo("0001-0032.part"));
}
@Test
public void partNameStrategy_prefix() throws Exception {
PartNamingStrategy strategy = PartNamingStrategy.prefix("asdf");
String fmt = strategy.fmtName("301object23", PartRange.of(1, 96));
assertAll(
// random digest with prefix to spread over keyspace
// digest is 22, prefix is 4, slash is 1
() -> assertField(fmt, 0).hasLength(22 + 5),
// name digest
() -> assertField(fmt, 1).hasLength(22),
() -> assertField(fmt, 2).isEqualTo("0001-0096.part"),
() -> assertThat(fmt).startsWith("asdf/"));
}
@Test
public void partNameStrategy_prefix_stillWorksWithFmtPattern() throws Exception {
PartNamingStrategy strategy = PartNamingStrategy.prefix("[%s]");
String fmt = strategy.fmtName("301object23", PartRange.of(1, 96));
assertAll(
// random digest with prefix to spread over keyspace
// digest is 22, prefix is 4, slash is 1
() -> assertField(fmt, 0).hasLength(22 + 5),
// name digest
() -> assertField(fmt, 1).hasLength(22),
() -> assertField(fmt, 2).isEqualTo("0001-0096.part"),
() -> assertThat(fmt).startsWith("[%s]/"));
}
@Test
public void partNameStrategy_objectNamePrefix() throws Exception {
// Creating an object level prefix without specifying an additional prefix will append the
// object name to the beginning of the part name.
PartNamingStrategy strategy = PartNamingStrategy.useObjectNameAsPrefix();
String fmt = strategy.fmtName("a/b/obj", PartRange.of(1, 96));
assertAll(
// random digest with prefix to spread over keyspace
// digest is 22, objectName is 7, slash is 1
() -> assertField(fmt, 0).hasLength(22 + 8),
// name digest
() -> assertField(fmt, 1).hasLength(22),
() -> assertField(fmt, 2).isEqualTo("0001-0096.part"),
() -> assertThat(fmt).startsWith("a/b/obj"));
}
@Test
public void partMetadataFieldDecorator_customTime() {
BlobInfo.Builder testBlob = BlobInfo.newBuilder("testBlob", "testBucket");
Duration duration = Duration.ofSeconds(30);
TestClock clock = TestClock.tickBy(Instant.EPOCH, Duration.ofSeconds(1));
OffsetDateTime expected =
OffsetDateTime.from(Instant.EPOCH.plus(duration).atZone(ZoneId.of("Z")));
PartMetadataFieldDecorator.setCustomTimeInFuture(duration).newInstance(clock).apply(testBlob);
assertThat(expected).isEqualTo(testBlob.build().getCustomTimeOffsetDateTime());
}
private static StringSubject assertField(String fmt, int idx) {
String[] split = fmt.split(";");
String s = split[idx];
return assertWithMessage(String.format("Formatted name '%s', field[%d] = %s", fmt, idx, s))
.that(s);
}
}