Skip to content

Commit 4244988

Browse files
authored
test: update ITJsonResumable*Test to use new FakeHttpServer#createUri method (googleapis#3384)
1 parent 932c6ee commit 4244988

File tree

4 files changed

+114
-98
lines changed

4 files changed

+114
-98
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/FakeHttpServer.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static io.grpc.netty.shaded.io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
2121
import static io.grpc.netty.shaded.io.netty.handler.codec.http.HttpHeaderValues.CLOSE;
2222

23+
import com.google.api.client.http.UriTemplate;
2324
import com.google.api.gax.retrying.RetrySettings;
2425
import com.google.cloud.NoCredentials;
2526
import com.google.cloud.storage.it.runner.registry.Registry;
@@ -48,6 +49,7 @@
4849
import java.net.InetSocketAddress;
4950
import java.net.URI;
5051
import java.time.Duration;
52+
import java.util.Map;
5153

5254
final class FakeHttpServer implements AutoCloseable {
5355

@@ -64,12 +66,24 @@ private FakeHttpServer(
6466
this.httpStorageOptions = httpStorageOptions;
6567
}
6668

67-
public HttpStorageOptions getHttpStorageOptions() {
68-
return httpStorageOptions;
69+
/**
70+
* overload which calls {@link #createUri(String, Map, boolean)} with {@code createUri(template,
71+
* params, false)}
72+
*/
73+
public URI createUri(String template, Map<String, String> params) {
74+
return createUri(template, params, false);
6975
}
7076

71-
public URI getEndpoint() {
72-
return endpoint;
77+
/** Decorator for {@link UriTemplate#expand(String, String, Object, boolean)} */
78+
public URI createUri(
79+
String template, Map<String, String> params, boolean addUnusedParamsAsQueryParams) {
80+
String expand =
81+
UriTemplate.expand(endpoint.toString(), template, params, addUnusedParamsAsQueryParams);
82+
return URI.create(expand);
83+
}
84+
85+
public HttpStorageOptions getHttpStorageOptions() {
86+
return httpStorageOptions;
7387
}
7488

7589
@Override

google-cloud-storage/src/test/java/com/google/cloud/storage/ITJsonResumableSessionPutTaskTest.java

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.net.URI;
4949
import java.nio.ByteBuffer;
5050
import java.nio.charset.StandardCharsets;
51-
import java.util.Locale;
5251
import java.util.UUID;
5352
import java.util.concurrent.atomic.AtomicLong;
5453
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -95,14 +94,14 @@ public void emptyObjectHappyPath() throws Exception {
9594
};
9695

9796
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
98-
URI endpoint = fakeHttpServer.getEndpoint();
99-
String uploadUrl =
100-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
97+
URI uri =
98+
fakeHttpServer.createUri(
99+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
101100

102101
JsonResumableSessionPutTask task =
103102
new JsonResumableSessionPutTask(
104103
httpClientContext,
105-
jsonResumableWrite(uploadUrl),
104+
jsonResumableWrite(uri),
106105
RewindableContent.empty(),
107106
HttpContentRange.of(ByteRangeSpec.explicitClosed(0L, 0L), 0));
108107

@@ -142,16 +141,16 @@ public void scenario7() throws Exception {
142141
};
143142

144143
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
145-
URI endpoint = fakeHttpServer.getEndpoint();
146-
String uploadUrl =
147-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
144+
URI uri =
145+
fakeHttpServer.createUri(
146+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
148147

149148
AtomicLong confirmedBytes = new AtomicLong(-1L);
150149

151150
JsonResumableSessionPutTask task =
152151
new JsonResumableSessionPutTask(
153152
httpClientContext,
154-
jsonResumableWrite(uploadUrl),
153+
jsonResumableWrite(uri),
155154
RewindableContent.empty(),
156155
HttpContentRange.of(ByteRangeSpec.explicitClosed(0L, 10L)));
157156

@@ -224,16 +223,16 @@ public void scenario1() throws Exception {
224223
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler);
225224
TmpFile tmpFile =
226225
DataGenerator.base64Characters().tempFile(temp.newFolder().toPath(), _256KiBL)) {
227-
URI endpoint = fakeHttpServer.getEndpoint();
228-
String uploadUrl =
229-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
226+
URI uri =
227+
fakeHttpServer.createUri(
228+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
230229

231230
AtomicLong confirmedBytes = new AtomicLong(-1L);
232231

233232
JsonResumableSessionPutTask task =
234233
new JsonResumableSessionPutTask(
235234
httpClientContext,
236-
jsonResumableWrite(uploadUrl),
235+
jsonResumableWrite(uri),
237236
RewindableContent.of(tmpFile.getPath()),
238237
HttpContentRange.of(ByteRangeSpec.explicit(0L, _256KiBL)));
239238

@@ -294,16 +293,16 @@ public void scenario2() throws Exception {
294293
};
295294

296295
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
297-
URI endpoint = fakeHttpServer.getEndpoint();
298-
String uploadUrl =
299-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
296+
URI uri =
297+
fakeHttpServer.createUri(
298+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
300299

301300
AtomicLong confirmedBytes = new AtomicLong(-1L);
302301

303302
JsonResumableSessionPutTask task =
304303
new JsonResumableSessionPutTask(
305304
httpClientContext,
306-
jsonResumableWrite(uploadUrl),
305+
jsonResumableWrite(uri),
307306
RewindableContent.empty(),
308307
HttpContentRange.of(_256KiBL));
309308

@@ -364,16 +363,16 @@ public void scenario3() throws Exception {
364363
};
365364

366365
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
367-
URI endpoint = fakeHttpServer.getEndpoint();
368-
String uploadUrl =
369-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
366+
URI uri =
367+
fakeHttpServer.createUri(
368+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
370369

371370
AtomicLong confirmedBytes = new AtomicLong(-1L);
372371

373372
JsonResumableSessionPutTask task =
374373
new JsonResumableSessionPutTask(
375374
httpClientContext,
376-
jsonResumableWrite(uploadUrl),
375+
jsonResumableWrite(uri),
377376
RewindableContent.empty(),
378377
HttpContentRange.of(_512KiBL));
379378

@@ -445,22 +444,21 @@ public void scenario4() throws Exception {
445444
};
446445

447446
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
448-
URI endpoint = fakeHttpServer.getEndpoint();
449-
String uploadUrl =
450-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
447+
URI uri =
448+
fakeHttpServer.createUri(
449+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
451450

452451
JsonResumableSessionPutTask task =
453452
new JsonResumableSessionPutTask(
454453
httpClientContext,
455-
jsonResumableWrite(uploadUrl),
454+
jsonResumableWrite(uri),
456455
RewindableContent.empty(),
457456
HttpContentRange.of(_256KiBL));
458457

459458
ResumableOperationResult<@Nullable StorageObject> operationResult = task.call();
460459
StorageObject call = operationResult.getObject();
461460
assertThat(call).isNotNull();
462-
assertThat(call.getMetadata())
463-
.containsEntry("upload_id", uploadUrl.substring(endpoint.toString().length() - 1));
461+
assertThat(call.getMetadata()).containsEntry("upload_id", uri.getPath());
464462
assertThat(operationResult.getPersistedSize()).isEqualTo(_256KiBL);
465463
}
466464
}
@@ -526,16 +524,16 @@ public void scenario4_1() throws Exception {
526524
};
527525

528526
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
529-
URI endpoint = fakeHttpServer.getEndpoint();
530-
String uploadUrl =
531-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
527+
URI uri =
528+
fakeHttpServer.createUri(
529+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
532530

533531
AtomicLong confirmedBytes = new AtomicLong(-1L);
534532

535533
JsonResumableSessionPutTask task =
536534
new JsonResumableSessionPutTask(
537535
httpClientContext,
538-
jsonResumableWrite(uploadUrl),
536+
jsonResumableWrite(uri),
539537
RewindableContent.empty(),
540538
HttpContentRange.of(_512KiBL));
541539

@@ -607,16 +605,16 @@ public void scenario4_2() throws Exception {
607605
};
608606

609607
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
610-
URI endpoint = fakeHttpServer.getEndpoint();
611-
String uploadUrl =
612-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
608+
URI uri =
609+
fakeHttpServer.createUri(
610+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
613611

614612
AtomicLong confirmedBytes = new AtomicLong(-1L);
615613

616614
JsonResumableSessionPutTask task =
617615
new JsonResumableSessionPutTask(
618616
httpClientContext,
619-
jsonResumableWrite(uploadUrl),
617+
jsonResumableWrite(uri),
620618
RewindableContent.empty(),
621619
HttpContentRange.of(_128KiBL));
622620

@@ -686,16 +684,16 @@ public void scenario5() throws Exception {
686684
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler);
687685
TmpFile tmpFile =
688686
DataGenerator.base64Characters().tempFile(temp.newFolder().toPath(), _256KiBL)) {
689-
URI endpoint = fakeHttpServer.getEndpoint();
690-
String uploadUrl =
691-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
687+
URI uri =
688+
fakeHttpServer.createUri(
689+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
692690

693691
AtomicLong confirmedBytes = new AtomicLong(-1L);
694692

695693
JsonResumableSessionPutTask task =
696694
new JsonResumableSessionPutTask(
697695
httpClientContext,
698-
jsonResumableWrite(uploadUrl),
696+
jsonResumableWrite(uri),
699697
RewindableContent.of(tmpFile.getPath()),
700698
HttpContentRange.of(ByteRangeSpec.explicit(_512KiBL, _768KiBL)));
701699

@@ -719,16 +717,16 @@ public void _503_emptyBody() throws Exception {
719717
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler);
720718
TmpFile tmpFile =
721719
DataGenerator.base64Characters().tempFile(temp.newFolder().toPath(), _256KiBL)) {
722-
URI endpoint = fakeHttpServer.getEndpoint();
723-
String uploadUrl =
724-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
720+
URI uri =
721+
fakeHttpServer.createUri(
722+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
725723

726724
AtomicLong confirmedBytes = new AtomicLong(-1L);
727725

728726
JsonResumableSessionPutTask task =
729727
new JsonResumableSessionPutTask(
730728
httpClientContext,
731-
jsonResumableWrite(uploadUrl),
729+
jsonResumableWrite(uri),
732730
RewindableContent.of(tmpFile.getPath()),
733731
HttpContentRange.of(ByteRangeSpec.explicit(_512KiBL, _768KiBL)));
734732

@@ -761,16 +759,16 @@ public void jsonParseFailure() throws Exception {
761759
};
762760

763761
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
764-
URI endpoint = fakeHttpServer.getEndpoint();
765-
String uploadUrl =
766-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
762+
URI uri =
763+
fakeHttpServer.createUri(
764+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
767765

768766
AtomicLong confirmedBytes = new AtomicLong(-1L);
769767

770768
JsonResumableSessionPutTask task =
771769
new JsonResumableSessionPutTask(
772770
httpClientContext,
773-
jsonResumableWrite(uploadUrl),
771+
jsonResumableWrite(uri),
774772
RewindableContent.empty(),
775773
HttpContentRange.of(0));
776774

@@ -802,14 +800,14 @@ public void jsonDeserializationOnlyAttemptedWhenContentPresent() throws Exceptio
802800
};
803801

804802
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
805-
URI endpoint = fakeHttpServer.getEndpoint();
806-
String uploadUrl =
807-
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
803+
URI uri =
804+
fakeHttpServer.createUri(
805+
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));
808806

809807
JsonResumableSessionPutTask task =
810808
new JsonResumableSessionPutTask(
811809
httpClientContext,
812-
jsonResumableWrite(uploadUrl),
810+
jsonResumableWrite(uri),
813811
RewindableContent.empty(),
814812
HttpContentRange.of(0));
815813

@@ -878,7 +876,7 @@ public void repeatedRewindsToTheSameLocationWork() {
878876
assertThat(buf2.position()).isEqualTo(13);
879877
}
880878

881-
static @NonNull JsonResumableWrite jsonResumableWrite(String uploadUrl) {
882-
return JsonResumableWrite.of(new StorageObject(), ImmutableMap.of(), uploadUrl, 0);
879+
static @NonNull JsonResumableWrite jsonResumableWrite(URI uploadUrl) {
880+
return JsonResumableWrite.of(new StorageObject(), ImmutableMap.of(), uploadUrl.toString(), 0);
883881
}
884882
}

0 commit comments

Comments
 (0)