Skip to content

Commit

Permalink
Add a first attempt on SSE testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cdr-chakotay committed Aug 28, 2024
1 parent cde87d3 commit 115cc4c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Uncomment following line if you want to use the local java-sdk
// for the example instead of pulling the JARs from JCenter.
// This is useful for debugging and testing new features.
//include ':examples'
include ':examples'
rootProject.name = 'transloadit'
97 changes: 61 additions & 36 deletions src/test/java/com/transloadit/sdk/AssemblyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import org.json.JSONStringer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -17,14 +18,12 @@
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Objects;

import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.RegexBody.regex;

/**
Expand All @@ -42,19 +41,31 @@ public class AssemblyTest extends MockHttpService {
* Links to {@link Assembly} instance to perform the tests on.
*/
private Assembly assembly;

/**
* Indicates if the {@link Assembly} has been finished.
* Keeps track of events fired by the {@link AssemblyListener}
*/
private boolean assemblyFinished;
private HashMap<String, Boolean> emittedEvents = new HashMap<String, Boolean>() {{
put("ASSEMBLY_ERROR", false);
put("ASSEMBLY_META_DATA_EXTRACTED", false);
put("ASSEMBLY_INSTRUCTION_UPLOAD_FINISHED", false);
put("ASSEMBLY_FILE_UPLOAD_FINISHED", false);
put("ASSEMBLY_FILE_UPLOAD_PAUSED", false);
put("ASSEMBLY_FILE_UPLOAD_RESUMED", false);
put("ASSEMBLY_FILE_UPLOAD_PROGRESS", false);
put("ASSEMBLY_PROGRESS", false);
put("ASSEMBLY_RESULT_FINISHED", false);
put("ASSEMBLY_FINISHED", false);
}};

/**
* Assigns a new {@link Assembly} instance to the {@link AssemblyTest#assembly} variable before each individual test
* and resets the mockServerClient. Also sets {@link AssemblyTest#assemblyFinished} {@code = false}.
* and resets the mockServerClient. Also sets {@link AssemblyTest#state} {@code = ASSEMBLY_NOT_STARTED}.
*/
@BeforeEach
public void setUp() {
assembly = newAssemblyWithoutID();
assemblyFinished = false;

mockServerClient.reset();
}

Expand Down Expand Up @@ -131,7 +142,7 @@ public void removeFile() {
*/
@Test
public void save() throws IOException, LocalOperationException, RequestException {
mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies").withMethod("POST")
// content from the file uploaded is present
.withBody(regex("[\\w\\W]*Permission is hereby granted, free of charge[\\w\\W]*")))
Expand All @@ -151,7 +162,7 @@ public void save() throws IOException, LocalOperationException, RequestException
*/
@Test
public void saveWithInputStream() throws Exception {
mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies").withMethod("POST")
// content from the file uploaded is present
.withBody(regex("[\\w\\W]*Permission is hereby granted, free of charge[\\w\\W]*")))
Expand All @@ -172,13 +183,13 @@ public void saveWithInputStream() throws Exception {
*/
@Test
public void saveTillComplete() throws Exception {
mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies").withMethod("POST")
// content from the file uploaded is present
.withBody(regex("[\\w\\W]*Permission is hereby granted, free of charge[\\w\\W]*")))
.respond(HttpResponse.response().withBody(getJson("assembly_executing.json")));

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies/76fe5df1c93a0a530f3e583805cf98b4").withMethod("GET"))
.respond(HttpResponse.response().withBody(getJson("resumable_assembly_complete.json")));

Expand All @@ -200,9 +211,10 @@ public void saveTillComplete() throws Exception {
@Test
public void saveWithTus() throws Exception {
MockTusAssembly assembly = new MockTusAssembly(transloadit);

assembly.addFile(new File("LICENSE"), "file_name");

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies")
.withMethod("POST")
.withBody(regex("[\\w\\W]*tus_num_expected_upload_files\"\\r\\nContent-Length: 1"
Expand All @@ -223,28 +235,40 @@ public void saveWithTus() throws Exception {
* if Test resources "resumable_assembly.json" or "resumable_assembly_complete.json" are missing.
*/
@Test
public void saveWithTusTillSocketComplete() throws Exception {
public void saveWithTusListenSSE() throws Exception {
String sseBody = getJson("sse_response_body");
MockTusAssembly assembly = getMockTusAssembly();

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies")
.withMethod("POST")
.withBody(regex("[\\w\\W]*tus_num_expected_upload_files\"\\r\\nContent-Length: 1"
+ "\\r\\n\\r\\n1[\\w\\W]*")))
.respond(HttpResponse.response().withBody(getJson("resumable_assembly.json")));

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/ws20013").withMethod("GET").withHeader("Accept", "text/event-stream"))
.respond(HttpResponse.response().withBody(sseBody));
// When the assembly is finished (finished status)
/*mockServerClient.when(HttpRequest.request()
.withPath("/assemblies/02ce6150ea2811e6a35a8d1e061a5b71").withMethod("GET"))
.respond(HttpResponse.response().withBody(getJson("resumable_assembly_complete.json")));
.respond(HttpResponse.response().withBody(getJson("resumable_assembly_complete.json")));*/

AssemblyResponse response = assembly.save(true);

Assertions.assertEquals(response.json().get("ok"), "ASSEMBLY_UPLOADING");
Assertions.assertFalse(assemblyFinished);
Assertions.assertTrue(assembly.emitted.containsKey("assembly_connect"));
Assertions.assertEquals(emittedEvents.get("ASSEMBLY_FINISHED"), false);
//Assertions.assertTrue(assembly.emitted.containsKey("assembly_connect"));
// emit that assembly is complete
// assembly.getSocket("").emit("assembly_finished");
Assertions.assertTrue(assemblyFinished);


HttpRequest[] recordedRequests = mockServerClient.retrieveRecordedRequests(
request()
);

System.out.println("lol");

}

private @NotNull MockTusAssembly getMockTusAssembly() {
Expand All @@ -253,55 +277,56 @@ public void saveWithTusTillSocketComplete() throws Exception {
assembly.setAssemblyListener(new AssemblyListener() {
@Override
public void onAssemblyFinished(AssemblyResponse response) {
assemblyFinished = true;
emittedEvents.put("ASSEMBLY_FINISHED", true);
Assertions.assertEquals(response.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71");
Assertions.assertEquals(response.json().get("ok"), "ASSEMBLY_COMPLETED");
}

@Override
public void onError(Exception error) {
emittedEvents.put("ASSEMBLY_ERROR", true);
System.err.println("No Mockserver Response");
}

@Override
public void onMetadataExtracted() {

emittedEvents.put("ASSEMBLY_META_DATA_EXTRACTED", true);
}

@Override
public void onAssemblyUploadFinished() {

emittedEvents.put("ASSEMBLY_INSTRUCTION_UPLOAD_FINISHED", true);
}

@Override
public void onFileUploadFinished(String fileName, JSONObject uploadInformation) {

emittedEvents.put("ASSEMBLY_FILE_UPLOAD_FINISHED", true);
}

@Override
public void onFileUploadPaused(String name) {

emittedEvents.put("ASSEMBLY_FILE_UPLOAD_PAUSED", true);
}

@Override
public void onFileUploadResumed(String name) {

emittedEvents.put("ASSEMBLY_FILE_UPLOAD_RESUMED", true);
}

@Override
public void onFileUploadProgress(long uploadedBytes, long totalBytes) {

emittedEvents.put("ASSEMBLY_FILE_UPLOAD_PROGRESS", false);
}

@Override
public void onAssemblyProgress(double combinedProgress, JSONObject progressPerOriginalFile) {

System.out.println("Boeing707-800");
emittedEvents.put("ASSEMBLY_PROGRESS", true);
}

@Override
public void onAssemblyResultFinished(String stepName, JSONObject result) {


emittedEvents.put("ASSEMBLY_RESULT_FINISHED", true);
}

});
Expand All @@ -322,14 +347,14 @@ public void saveWithTusTillComplete() throws Exception {
assembly.addFile(new File("LICENSE"), "file_name");
assembly.setShouldWaitForCompletion(true);

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies")
.withMethod("POST")
.withBody(regex("[\\w\\W]*tus_num_expected_upload_files\"\\r\\nContent-Length: 1"
+ "\\r\\n\\r\\n1[\\w\\W]*")))
.respond(HttpResponse.response().withBody(getJson("resumable_assembly.json")));

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies/02ce6150ea2811e6a35a8d1e061a5b71").withMethod("GET"))
.respond(HttpResponse.response().withBody(getJson("resumable_assembly_complete.json")));

Expand All @@ -349,7 +374,7 @@ public void saveWithInputStreamAndTus() throws Exception {
MockTusAssembly assembly = new MockTusAssembly(transloadit);
assembly.addFile(Files.newInputStream(new File("LICENSE").toPath()), "file_name");

mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies")
.withMethod("POST")
.withBody(regex("[\\w\\W]*tus_num_expected_upload_files\"\\r\\nContent-Length: 1"
Expand All @@ -370,12 +395,12 @@ public void saveWithInputStreamAndTus() throws Exception {
public void testRetryRateLimit() throws Exception {
int retries = 2;
// let it retry twice
mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies").withMethod("POST"), Times.exactly(retries))
.respond(HttpResponse.response().withStatusCode(413).withBody(getJson("rate_limit_reached.json")));

// let it pass
mockServerClient.when(HttpRequest.request()
mockServerClient.when(request()
.withPath("/assemblies").withMethod("POST"))
.respond(HttpResponse.response().withBody(getJson("assembly_executing.json")));

Expand Down
4 changes: 0 additions & 4 deletions src/test/java/com/transloadit/sdk/MockTusAssembly.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.transloadit.sdk;

import io.socket.client.Manager;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
* This class serves as a Mock to {@link Assembly}, which can be used to test
* Assemblies with resumability enabled and provides a suitable {@link MockSocket}.
*/
public class MockTusAssembly extends Assembly {
private Socket socket;
/**
* Stores emitted information.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/__files/resumable_assembly.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"assembly_url": "http://localhost:9040/assemblies/assemblies/02ce6150ea2811e6a35a8d1e061a5b71",
"assembly_ssl_url": "http://localhost:9040/assemblies/02ce6150ea2811e6a35a8d1e061a5b71",
"tus_url": "http://localhost:9040/resumable/files",
"update_stream_url": "https://api2-kranj.transloadit.com/ws20013?assembly=3c9a6dda5546479dbf66a200310490e1"
"update_stream_url": "http://localhost:9040/ws20013?assembly=02ce6150ea2811e6a35a8d1e061a5b71"
}
25 changes: 25 additions & 0 deletions src/test/resources/__files/sse_response_body
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
id: 3c951d1310dd4f829a6fb696a48a43e1

event: assembly_execution_progress
data: {"progress_combined":null,"progress_per_original_file":[]}

data: assembly_uploading_finished

event: assembly_execution_progress
data: {"progress_combined":null,"progress_per_original_file":[]}

event: assembly_upload_finished
data: {"id":"77bfd6d1378a4289a83ff8777848dce8","name":"lesereihe.doc","basename":"lesereihe","ext":"doc","size":61440,"mime":"application/msword","type":"office","field":"file","md5hash":"154a9349b8f9111865a07ed0a7050f55","original_id":"77bfd6d1378a4289a83ff8777848dce8","original_basename":"lesereihe","original_name":"lesereihe.doc","original_path":"/","original_md5hash":"154a9349b8f9111865a07ed0a7050f55","from_batch_import":false,"is_tus_file":true,"tus_upload_url":"https://api2-samho.transloadit.com/resumable/files/3c951d1310dd4f829a6fb696a48a43e1_38f7dd7616eb554ca069264d3e0ee17a","url":"http://tmp-eu-west-1.transloadit.net/6ed8698028ac11e4a6abc57a5f066e81/3c951d1310dd4f829a6fb696a48a43e1/77bfd6d1378a4289a83ff8777848dce8.doc","ssl_url":"https://tmp-eu-west-1.transloadit.net/6ed8698028ac11e4a6abc57a5f066e81/3c951d1310dd4f829a6fb696a48a43e1/77bfd6d1378a4289a83ff8777848dce8.doc","meta":{},"user_meta":{},"as":null,"is_temp_url":true}

data: assembly_upload_meta_data_extracted

event: assembly_execution_progress
data: {"progress_combined":0,"progress_per_original_file":[{"original_id":"77bfd6d1378a4289a83ff8777848dce8","progress":0}]}

event: assembly_result_finished
data: ["avatar",{"id":"1ddf6ddf086444e6894717aebe0757e7","name":"lesereihe.pdf","basename":"lesereihe","ext":"pdf","size":126266,"mime":"application/pdf","type":"pdf","field":"file","md5hash":"3ec28058ac524ed8cff0d4da477913a6","original_id":"77bfd6d1378a4289a83ff8777848dce8","original_basename":"lesereihe","original_name":"lesereihe.doc","original_path":"/","original_md5hash":"154a9349b8f9111865a07ed0a7050f55","from_batch_import":false,"is_tus_file":false,"tus_upload_url":null,"url":"http://tmp-eu-west-1.transloadit.net/6ed8698028ac11e4a6abc57a5f066e81/3c951d1310dd4f829a6fb696a48a43e1/1ddf6ddf086444e6894717aebe0757e7.pdf","ssl_url":"https://tmp-eu-west-1.transloadit.net/6ed8698028ac11e4a6abc57a5f066e81/3c951d1310dd4f829a6fb696a48a43e1/1ddf6ddf086444e6894717aebe0757e7.pdf","meta":{"page_count":2,"width":1238,"height":1750,"page_size":"595.304 x 841.89 pts (A4)","title":"Lesereihe%202007%3A","author":"Lugsteinhof","producer":"LibreOffice 7.5","creator":"Writer","create_date":"2024:08:14 13:43:02Z","modify_date":null},"user_meta":{},"as":null,"queue":"live","queue_time":0,"exec_time":2.86,"cost":1048576,"is_temp_url":true}]

event: assembly_execution_progress
data: {"progress_combined":100,"progress_per_original_file":[]}

data: assembly_finished

0 comments on commit 115cc4c

Please sign in to comment.