Skip to content

Commit e892ed1

Browse files
committed
Cover generated resume runtime events
1 parent 1a3a085 commit e892ed1

1 file changed

Lines changed: 134 additions & 2 deletions

File tree

src/test/java/io/tus/java/client/TestGeneratedTusRuntimeEvents.java

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.mockserver.model.HttpResponse;
2020

2121
import static org.junit.Assert.assertArrayEquals;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertNull;
2224

2325
/**
2426
* Tests generated TUS client runtime event fixtures against the real uploader.
@@ -34,6 +36,8 @@ public class TestGeneratedTusRuntimeEvents extends MockServerProvider {
3436
"absolute",
3537
false,
3638
11,
39+
null,
40+
false,
3741
new GeneratedTusRuntimeEventMetadata[] {
3842
new GeneratedTusRuntimeEventMetadata(
3943
"filename",
@@ -71,6 +75,52 @@ public class TestGeneratedTusRuntimeEvents extends MockServerProvider {
7175
"chunk-complete:11:11:11",
7276
}
7377
),
78+
new GeneratedTusRuntimeEventCase(
79+
"resumeFromPreviousUpload",
80+
new GeneratedTusRuntimeEventInput(
81+
"hello world",
82+
"resume-contract",
83+
"stored",
84+
false,
85+
6,
86+
"contract-resume-fingerprint",
87+
true,
88+
new GeneratedTusRuntimeEventMetadata[0]
89+
),
90+
new GeneratedTusRuntimeEventRequest[] {
91+
new GeneratedTusRuntimeEventRequest(
92+
"HEAD",
93+
"upload",
94+
200,
95+
new GeneratedTusRuntimeEventHeader[] {
96+
new GeneratedTusRuntimeEventHeader(
97+
"Upload-Length",
98+
"11"
99+
),
100+
new GeneratedTusRuntimeEventHeader(
101+
"Upload-Offset",
102+
"5"
103+
),
104+
}
105+
),
106+
new GeneratedTusRuntimeEventRequest(
107+
"PATCH",
108+
"upload",
109+
204,
110+
new GeneratedTusRuntimeEventHeader[] {
111+
new GeneratedTusRuntimeEventHeader(
112+
"Upload-Offset",
113+
"11"
114+
),
115+
}
116+
),
117+
},
118+
new String[] {
119+
"progress:5:11",
120+
"progress:11:11",
121+
"chunk-complete:6:11:11",
122+
}
123+
),
74124
new GeneratedTusRuntimeEventCase(
75125
"relativeLocationResolution",
76126
new GeneratedTusRuntimeEventInput(
@@ -79,6 +129,8 @@ public class TestGeneratedTusRuntimeEvents extends MockServerProvider {
79129
"relative",
80130
true,
81131
11,
132+
null,
133+
false,
82134
new GeneratedTusRuntimeEventMetadata[] {
83135
new GeneratedTusRuntimeEventMetadata(
84136
"filename",
@@ -129,10 +181,17 @@ public void testSyncUploaderEmitsGeneratedProgressAndChunkEvents() throws Except
129181
final List<String> events = new ArrayList<String>();
130182
TusClient client = new TusClient();
131183
client.setUploadCreationURL(endpointUrlFor(testCase));
184+
GeneratedTusRuntimeEventUrlStore urlStore = urlStoreFor(testCase);
185+
if (urlStore != null) {
186+
client.enableResuming(urlStore);
187+
}
188+
if (testCase.input.removeFingerprintOnSuccess) {
189+
client.enableRemoveFingerprintOnSuccess();
190+
}
132191

133192
registerResponses(testCase);
134193

135-
TusUploader uploader = client.createUpload(uploadFor(testCase));
194+
TusUploader uploader = uploaderFor(client, testCase);
136195
uploader.setChunkSize(testCase.input.chunkSize);
137196
uploader.setProgressListener(new TusUploader.ProgressListener() {
138197
@Override
@@ -155,7 +214,17 @@ public void onChunkComplete(long chunkSize, long bytesAccepted, long bytesTotal)
155214
testCase.scenarioId,
156215
testCase.eventKeys,
157216
events.toArray(new String[events.size()]));
217+
assertStoredUploadState(testCase, urlStore);
218+
}
219+
}
220+
221+
private TusUploader uploaderFor(TusClient client, GeneratedTusRuntimeEventCase testCase)
222+
throws Exception {
223+
if (testCase.input.fingerprint != null) {
224+
return client.resumeUpload(uploadFor(testCase));
158225
}
226+
227+
return client.createUpload(uploadFor(testCase));
159228
}
160229

161230
private TusUpload uploadFor(GeneratedTusRuntimeEventCase testCase) {
@@ -164,6 +233,9 @@ private TusUpload uploadFor(GeneratedTusRuntimeEventCase testCase) {
164233
upload.setSize(content.length);
165234
upload.setInputStream(new ByteArrayInputStream(content));
166235
upload.setMetadata(metadataFor(testCase.input.metadata));
236+
if (testCase.input.fingerprint != null) {
237+
upload.setFingerprint(testCase.input.fingerprint);
238+
}
167239
return upload;
168240
}
169241

@@ -179,7 +251,7 @@ private void registerResponses(GeneratedTusRuntimeEventCase testCase) throws Exc
179251
for (GeneratedTusRuntimeEventRequest request : testCase.requests) {
180252
HttpRequest httpRequest = new HttpRequest()
181253
.withPath(pathFor(testCase, request));
182-
if (!"upload".equals(request.url)) {
254+
if (!"upload".equals(request.url) || "HEAD".equals(request.method)) {
183255
httpRequest.withMethod(request.method);
184256
}
185257

@@ -233,6 +305,41 @@ private URL endpointUrlFor(GeneratedTusRuntimeEventCase testCase) throws Excepti
233305
return mockServerURL;
234306
}
235307

308+
private GeneratedTusRuntimeEventUrlStore urlStoreFor(
309+
GeneratedTusRuntimeEventCase testCase) throws Exception {
310+
if (testCase.input.fingerprint == null) {
311+
return null;
312+
}
313+
314+
GeneratedTusRuntimeEventUrlStore store = new GeneratedTusRuntimeEventUrlStore();
315+
store.set(testCase.input.fingerprint, uploadUrlFor(testCase));
316+
return store;
317+
}
318+
319+
private void assertStoredUploadState(
320+
GeneratedTusRuntimeEventCase testCase,
321+
GeneratedTusRuntimeEventUrlStore urlStore) {
322+
if (urlStore == null) {
323+
return;
324+
}
325+
326+
URL storedUrl = urlStore.get(testCase.input.fingerprint);
327+
if (testCase.input.removeFingerprintOnSuccess) {
328+
assertNull(testCase.scenarioId, storedUrl);
329+
return;
330+
}
331+
332+
assertEquals(testCase.scenarioId, uploadUrlForUnchecked(testCase), storedUrl);
333+
}
334+
335+
private URL uploadUrlForUnchecked(GeneratedTusRuntimeEventCase testCase) {
336+
try {
337+
return uploadUrlFor(testCase);
338+
} catch (Exception error) {
339+
throw new AssertionError(error);
340+
}
341+
}
342+
236343
private static final class GeneratedTusRuntimeEventCase {
237344
final String scenarioId;
238345
final GeneratedTusRuntimeEventInput input;
@@ -257,6 +364,8 @@ private static final class GeneratedTusRuntimeEventInput {
257364
final String locationHeaderKind;
258365
final boolean endpointHasTrailingSlash;
259366
final int chunkSize;
367+
final String fingerprint;
368+
final boolean removeFingerprintOnSuccess;
260369
final GeneratedTusRuntimeEventMetadata[] metadata;
261370

262371
GeneratedTusRuntimeEventInput(
@@ -265,12 +374,16 @@ private static final class GeneratedTusRuntimeEventInput {
265374
String locationHeaderKind,
266375
boolean endpointHasTrailingSlash,
267376
int chunkSize,
377+
String fingerprint,
378+
boolean removeFingerprintOnSuccess,
268379
GeneratedTusRuntimeEventMetadata[] metadata) {
269380
this.content = content;
270381
this.uploadPath = uploadPath;
271382
this.locationHeaderKind = locationHeaderKind;
272383
this.endpointHasTrailingSlash = endpointHasTrailingSlash;
273384
this.chunkSize = chunkSize;
385+
this.fingerprint = fingerprint;
386+
this.removeFingerprintOnSuccess = removeFingerprintOnSuccess;
274387
this.metadata = metadata;
275388
}
276389
}
@@ -312,4 +425,23 @@ private static final class GeneratedTusRuntimeEventMetadata {
312425
this.value = value;
313426
}
314427
}
428+
429+
private static final class GeneratedTusRuntimeEventUrlStore implements TusURLStore {
430+
private final Map<String, URL> values = new LinkedHashMap<String, URL>();
431+
432+
@Override
433+
public URL get(String fingerprint) {
434+
return values.get(fingerprint);
435+
}
436+
437+
@Override
438+
public void set(String fingerprint, URL url) {
439+
values.put(fingerprint, url);
440+
}
441+
442+
@Override
443+
public void remove(String fingerprint) {
444+
values.remove(fingerprint);
445+
}
446+
}
315447
}

0 commit comments

Comments
 (0)