|
| 1 | +package com.transloadit.sdk.integration; |
| 2 | + |
| 3 | +import com.transloadit.sdk.Assembly; |
| 4 | +import com.transloadit.sdk.AssemblyListener; |
| 5 | +import com.transloadit.sdk.Transloadit; |
| 6 | +import com.transloadit.sdk.response.AssemblyResponse; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | +import org.junit.jupiter.api.Assumptions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.io.OutputStream; |
| 15 | +import java.net.URL; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.*; |
| 27 | + |
| 28 | +class AssemblySseIntegrationTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + void sseStreamShouldCloseWithoutErrorsAfterAssemblyFinished() throws Exception { |
| 32 | + String key = System.getenv("TRANSLOADIT_KEY"); |
| 33 | + String secret = System.getenv("TRANSLOADIT_SECRET"); |
| 34 | + Assumptions.assumeTrue(key != null && !key.trim().isEmpty(), "TRANSLOADIT_KEY env var required"); |
| 35 | + Assumptions.assumeTrue(secret != null && !secret.trim().isEmpty(), "TRANSLOADIT_SECRET env var required"); |
| 36 | + |
| 37 | + Transloadit client = new Transloadit(key, secret); |
| 38 | + Assembly assembly = client.newAssembly(); |
| 39 | + |
| 40 | + Path tempFile = createTempUpload(); |
| 41 | + try { |
| 42 | + assembly.addFile(tempFile.toFile(), "file"); |
| 43 | + |
| 44 | + Map<String, Object> resizeStep = new HashMap<>(); |
| 45 | + resizeStep.put("use", ":original"); |
| 46 | + resizeStep.put("width", 64); |
| 47 | + resizeStep.put("height", 64); |
| 48 | + resizeStep.put("resize_strategy", "fit"); |
| 49 | + assembly.addStep("resize", "/image/resize", resizeStep); |
| 50 | + |
| 51 | + AtomicReference<AssemblyResponse> finishedResponse = new AtomicReference<>(); |
| 52 | + CompletableFuture<Void> finishedFuture = new CompletableFuture<>(); |
| 53 | + CompletableFuture<Exception> errorFuture = new CompletableFuture<>(); |
| 54 | + |
| 55 | + AssemblyListener listener = new AssemblyListener() { |
| 56 | + @Override |
| 57 | + public void onAssemblyFinished(AssemblyResponse response) { |
| 58 | + System.out.println("[AssemblySseIntegrationTest] assembly_finished event"); |
| 59 | + finishedResponse.set(response); |
| 60 | + finishedFuture.complete(null); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onError(Exception error) { |
| 65 | + System.out.println("[AssemblySseIntegrationTest] SSE error: " + error); |
| 66 | + errorFuture.complete(error); |
| 67 | + finishedFuture.completeExceptionally(error); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onMetadataExtracted() { |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onAssemblyUploadFinished() { |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onFileUploadFinished(JSONObject uploadInformation) { |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onFileUploadPaused(String name) { |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onFileUploadResumed(String name) { |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onFileUploadProgress(long uploadedBytes, long totalBytes) { |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onAssemblyProgress(JSONObject progress) { |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onAssemblyResultFinished(JSONArray result) { |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + assembly.setAssemblyListener(listener); |
| 104 | + |
| 105 | + AssemblyResponse initialResponse = assembly.save(true); |
| 106 | + assertNotNull(initialResponse.getId(), "Assembly ID should be present"); |
| 107 | + |
| 108 | + try { |
| 109 | + finishedFuture.get(5, TimeUnit.MINUTES); |
| 110 | + } catch (ExecutionException executionException) { |
| 111 | + Throwable cause = executionException.getCause(); |
| 112 | + if (cause instanceof Exception) { |
| 113 | + throw (Exception) cause; |
| 114 | + } |
| 115 | + throw executionException; |
| 116 | + } |
| 117 | + |
| 118 | + AssemblyResponse completed = finishedResponse.get(); |
| 119 | + assertNotNull(completed, "Assembly finished response missing"); |
| 120 | + assertTrue(completed.isFinished(), "Assembly should be finished"); |
| 121 | + assertEquals("ASSEMBLY_COMPLETED", completed.json().optString("ok")); |
| 122 | + |
| 123 | + try { |
| 124 | + Exception unexpected = errorFuture.get(30, TimeUnit.SECONDS); |
| 125 | + fail("Unexpected SSE error after completion: " + unexpected); |
| 126 | + } catch (TimeoutException ignore) { |
| 127 | + // expected: no error surfaced after assembly finished |
| 128 | + } |
| 129 | + } finally { |
| 130 | + try { |
| 131 | + Files.deleteIfExists(tempFile); |
| 132 | + } catch (IOException ignore) { |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static Path createTempUpload() throws IOException { |
| 138 | + Path file = Files.createTempFile("transloadit-sse-test", ".jpg"); |
| 139 | + URL source = new URL("https://demos.transloadit.com/inputs/chameleon.jpg"); |
| 140 | + try (InputStream input = source.openStream(); OutputStream output = Files.newOutputStream(file)) { |
| 141 | + byte[] buffer = new byte[8192]; |
| 142 | + int read; |
| 143 | + while ((read = input.read(buffer)) != -1) { |
| 144 | + output.write(buffer, 0, read); |
| 145 | + } |
| 146 | + } |
| 147 | + return file; |
| 148 | + } |
| 149 | +} |
0 commit comments