|
3 | 3 | import com.transloadit.sdk.Assembly; |
4 | 4 | import com.transloadit.sdk.Transloadit; |
5 | 5 | import com.transloadit.sdk.response.AssemblyResponse; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileOutputStream; |
6 | 9 | import org.json.JSONArray; |
7 | 10 | import org.junit.jupiter.api.Assertions; |
8 | 11 | import org.junit.jupiter.api.Assumptions; |
@@ -50,4 +53,77 @@ void createAssemblyAndWaitForCompletion() throws Exception { |
50 | 53 | Assertions.assertNotNull(stepResult, "resize step result missing"); |
51 | 54 | Assertions.assertTrue(stepResult.length() > 0, "resize step result empty"); |
52 | 55 | } |
| 56 | + |
| 57 | + @Test |
| 58 | + void directUploadOriginalStepProducesResult() throws Exception { |
| 59 | + Assumptions.assumeTrue("1".equals(System.getenv("JAVA_SDK_E2E")), "set JAVA_SDK_E2E=1 to run"); |
| 60 | + |
| 61 | + String key = System.getenv("TRANSLOADIT_KEY"); |
| 62 | + String secret = System.getenv("TRANSLOADIT_SECRET"); |
| 63 | + Assumptions.assumeTrue(key != null && !key.trim().isEmpty(), "TRANSLOADIT_KEY env var required"); |
| 64 | + Assumptions.assumeTrue(secret != null && !secret.trim().isEmpty(), "TRANSLOADIT_SECRET env var required"); |
| 65 | + |
| 66 | + File upload = File.createTempFile("java-sdk-smartcdn", ".bin"); |
| 67 | + try (java.io.InputStream in = AssemblyIntegrationTest.class.getResourceAsStream("/chameleon.jpg"); |
| 68 | + FileOutputStream fos = new FileOutputStream(upload)) { |
| 69 | + if (in == null) { |
| 70 | + Assertions.fail("Embedded chameleon.jpg fixture missing"); |
| 71 | + } |
| 72 | + byte[] buffer = new byte[8192]; |
| 73 | + int read; |
| 74 | + while ((read = in.read(buffer)) != -1) { |
| 75 | + fos.write(buffer, 0, read); |
| 76 | + } |
| 77 | + long targetSize = 32L * 1024L * 1024L; |
| 78 | + long current = upload.length(); |
| 79 | + if (current < targetSize) { |
| 80 | + byte[] padding = new byte[8192]; |
| 81 | + while (current < targetSize) { |
| 82 | + long remaining = targetSize - current; |
| 83 | + int toWrite = (int) Math.min(padding.length, remaining); |
| 84 | + fos.write(padding, 0, toWrite); |
| 85 | + current += toWrite; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Transloadit client = new Transloadit(key, secret); |
| 91 | + Assembly assembly = client.newAssembly(); |
| 92 | + try { |
| 93 | + assembly.addFile(upload, "image"); |
| 94 | + |
| 95 | + Map<String, Object> resize = new HashMap<>(); |
| 96 | + resize.put("use", ":original"); |
| 97 | + resize.put("width", 32); |
| 98 | + resize.put("height", 32); |
| 99 | + resize.put("resize_strategy", "fit"); |
| 100 | + resize.put("format", "jpg"); |
| 101 | + resize.put("result", true); |
| 102 | + assembly.addStep("resize", "/image/resize", resize); |
| 103 | + |
| 104 | + AssemblyResponse response = assembly.save(true); |
| 105 | + String assemblyId = response.getId(); |
| 106 | + |
| 107 | + long deadline = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5); |
| 108 | + while (!response.isFinished() && System.currentTimeMillis() < deadline) { |
| 109 | + Thread.sleep(5000); |
| 110 | + response = client.getAssembly(assemblyId); |
| 111 | + } |
| 112 | + |
| 113 | + Assertions.assertTrue(response.isFinished(), "Assembly did not finish in time: " + assemblyId); |
| 114 | + String ok = response.json().optString("ok"); |
| 115 | + if (!"ASSEMBLY_COMPLETED".equals(ok)) { |
| 116 | + Assertions.fail("Assembly " + assemblyId + " finished with status=" + ok + " payload=" + response.json()); |
| 117 | + } |
| 118 | + |
| 119 | + JSONArray resizeResult = response.getStepResult("resize"); |
| 120 | + if (resizeResult == null) { |
| 121 | + Assertions.fail("resize step result missing for assembly " + assemblyId + " payload=" + response.json()); |
| 122 | + } |
| 123 | + Assertions.assertTrue(resizeResult.length() > 0, "resize step result empty for assembly " + assemblyId); |
| 124 | + } finally { |
| 125 | + upload.delete(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
53 | 129 | } |
0 commit comments