|
| 1 | +package io.tus.java.example; |
| 2 | + |
| 3 | +import io.tus.java.client.TusClient; |
| 4 | +import io.tus.java.client.TusStartOptions; |
| 5 | +import io.tus.java.client.TusUpload; |
| 6 | + |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.net.URL; |
| 11 | + |
| 12 | +public final class Api2DevdockTusStartOptionValidation { |
| 13 | + /** |
| 14 | + * Run the API2 devdock start-option validation scenario. |
| 15 | + * |
| 16 | + * @param args Unused command-line arguments. |
| 17 | + * @throws Exception Thrown when the scenario cannot be executed. |
| 18 | + */ |
| 19 | + public static void main(String[] args) throws Exception { |
| 20 | + final JSONObject scenario = Api2DevdockScenario.loadScenario(); |
| 21 | + final JSONObject conformanceScenario = Api2DevdockScenario.conformanceScenario(scenario); |
| 22 | + final JSONObject result = validateStartOptions(conformanceScenario); |
| 23 | + |
| 24 | + Api2DevdockScenario.writeResult(result); |
| 25 | + System.out.println( |
| 26 | + "Java TUS SDK devdock scenario " |
| 27 | + + scenario.getString("scenarioId") |
| 28 | + + " rejected " |
| 29 | + + conformanceScenario.getJSONObject("completion").getString("reason") |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + private static JSONObject validateStartOptions(JSONObject conformanceScenario) |
| 34 | + throws Exception { |
| 35 | + final TusClient client = new TusClient(); |
| 36 | + final TusStartOptions options = startOptionsFor(conformanceScenario); |
| 37 | + |
| 38 | + try { |
| 39 | + client.validateStartOptions(options); |
| 40 | + } catch (IllegalArgumentException error) { |
| 41 | + final String expectedMessage = conformanceScenario |
| 42 | + .getJSONObject("completion") |
| 43 | + .getString("message"); |
| 44 | + if (!expectedMessage.equals(error.getMessage())) { |
| 45 | + throw new IllegalStateException( |
| 46 | + "expected start option validation error " |
| 47 | + + expectedMessage |
| 48 | + + ", got " |
| 49 | + + error.getMessage() |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + return new JSONObject() |
| 54 | + .put("errorCaught", true) |
| 55 | + .put("errorMessage", error.getMessage()) |
| 56 | + .put("requestCount", 0); |
| 57 | + } |
| 58 | + |
| 59 | + throw new IllegalStateException("start option validation scenario unexpectedly succeeded"); |
| 60 | + } |
| 61 | + |
| 62 | + private static TusStartOptions startOptionsFor(JSONObject conformanceScenario) |
| 63 | + throws Exception { |
| 64 | + final TusStartOptions options = new TusStartOptions(); |
| 65 | + final String endpointURL = Api2DevdockScenario.conformanceInputStringOptionOrNull( |
| 66 | + conformanceScenario, |
| 67 | + "endpointUrl" |
| 68 | + ); |
| 69 | + final String uploadURL = Api2DevdockScenario.conformanceInputStringOptionOrNull( |
| 70 | + conformanceScenario, |
| 71 | + "uploadUrl" |
| 72 | + ); |
| 73 | + final int parallelUploads = Api2DevdockScenario.conformanceInputIntegerOption( |
| 74 | + conformanceScenario, |
| 75 | + "parallelUploads", |
| 76 | + 1 |
| 77 | + ); |
| 78 | + final boolean uploadDataDuringCreation = |
| 79 | + Api2DevdockScenario.conformanceInputBooleanOption( |
| 80 | + conformanceScenario, |
| 81 | + "uploadDataDuringCreation", |
| 82 | + false |
| 83 | + ); |
| 84 | + final boolean uploadLengthDeferred = Api2DevdockScenario.conformanceInputBooleanOption( |
| 85 | + conformanceScenario, |
| 86 | + "uploadLengthDeferred", |
| 87 | + false |
| 88 | + ); |
| 89 | + |
| 90 | + options.setUpload(uploadFor(conformanceScenario, uploadLengthDeferred)); |
| 91 | + options.setParallelUploads(parallelUploads); |
| 92 | + options.setUploadDataDuringCreation(uploadDataDuringCreation); |
| 93 | + options.setUploadLengthDeferred(uploadLengthDeferred); |
| 94 | + if (endpointURL != null) { |
| 95 | + options.setEndpointURL(new URL(endpointURL)); |
| 96 | + } |
| 97 | + if (uploadURL != null) { |
| 98 | + options.setUploadURL(new URL(uploadURL)); |
| 99 | + } |
| 100 | + |
| 101 | + return options; |
| 102 | + } |
| 103 | + |
| 104 | + private static TusUpload uploadFor( |
| 105 | + JSONObject conformanceScenario, |
| 106 | + boolean uploadLengthDeferred |
| 107 | + ) { |
| 108 | + final byte[] content = Api2DevdockScenario.conformanceInputSourceBytes(conformanceScenario); |
| 109 | + final TusUpload upload = new TusUpload(); |
| 110 | + upload.setInputStream(new ByteArrayInputStream(content)); |
| 111 | + upload.setSize(content.length); |
| 112 | + upload.setUploadLengthDeferred(uploadLengthDeferred); |
| 113 | + return upload; |
| 114 | + } |
| 115 | + |
| 116 | + private Api2DevdockTusStartOptionValidation() { |
| 117 | + throw new IllegalStateException("Utility class"); |
| 118 | + } |
| 119 | +} |
0 commit comments