/** * Download the payload from the given path * * @param path the relative path of the payload in the {@link ExternalPayloadStorage} * @return the payload object * @throws ApplicationException in case of JSON parsing errors or download errors */ @SuppressWarnings("unchecked") public Map<String, Object> downloadPayload(String path) { try (InputStream inputStream = externalPayloadStorage.download(path)) { return objectMapper.readValue(IOUtils.toString(inputStream), Map.class); } catch (IOException e) { logger.error("Unable to download payload from external storage path: {}", path, e); throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, e); } }
@VisibleForTesting String uploadHelper(byte[] payloadBytes, long payloadSize, ExternalPayloadStorage.PayloadType payloadType) { ExternalStorageLocation location = externalPayloadStorage.getLocation(ExternalPayloadStorage.Operation.WRITE, payloadType, ""); externalPayloadStorage.upload(location.getPath(), new ByteArrayInputStream(payloadBytes), payloadSize); return location.getPath(); }
/** * Get external uri for the payload * * @param operation the type of {@link Operation} to be performed * @param payloadType the {@link PayloadType} at the external uri * @param path the path for which the external storage location is to be populated * @return the external uri at which the payload is stored/to be stored */ public ExternalStorageLocation getExternalStorageLocation(Operation operation, PayloadType payloadType, String path) { return externalPayloadStorage.getLocation(operation, payloadType, path); } }
/** * Get external uri for the payload * * @param operation the type of {@link Operation} to be performed * @param payloadType the {@link PayloadType} at the external uri * @param path the path for which the external storage location is to be populated * @return the external uri at which the payload is stored/to be stored */ public ExternalStorageLocation getExternalStorageLocation(Operation operation, PayloadType payloadType, String path) { return externalPayloadStorage.getLocation(operation, payloadType, path); } }
@Test public void testUploadHelper() { AtomicInteger uploadCount = new AtomicInteger(0); String path = "some/test/path.json"; ExternalStorageLocation location = new ExternalStorageLocation(); location.setPath(path); when(externalPayloadStorage.getLocation(any(), any(), any())).thenReturn(location); doAnswer(invocation -> { uploadCount.incrementAndGet(); return null; }).when(externalPayloadStorage).upload(anyString(), any(), anyLong()); assertEquals(path, externalPayloadStorageUtils.uploadHelper(new byte[]{}, 10L, ExternalPayloadStorage.PayloadType.TASK_OUTPUT)); assertEquals(1, uploadCount.get()); }
@Test public void testDownloadPayload() throws IOException { String path = "test/payload"; Map<String, Object> payload = new HashMap<>(); payload.put("key1", "value1"); payload.put("key2", 200); byte[] payloadBytes = objectMapper.writeValueAsString(payload).getBytes(); when(externalPayloadStorage.download(path)).thenReturn(new ByteArrayInputStream(payloadBytes)); Map<String, Object> result = externalPayloadStorageUtils.downloadPayload(path); assertNotNull(result); assertEquals(payload, result); }
@SuppressWarnings("unchecked") @Test public void testUploadTaskPayload() throws IOException { AtomicInteger uploadCount = new AtomicInteger(0); InputStream stream = ExternalPayloadStorageUtilsTest.class.getResourceAsStream("/payload.json"); Map<String, Object> payload = objectMapper.readValue(stream, Map.class); when(externalPayloadStorage.getLocation(ExternalPayloadStorage.Operation.WRITE, ExternalPayloadStorage.PayloadType.TASK_INPUT, "")).thenReturn(location); doAnswer(invocation -> { uploadCount.incrementAndGet(); return null; }).when(externalPayloadStorage).upload(anyString(), any(), anyLong()); Task task = new Task(); task.setInputData(payload); externalPayloadStorageUtils.verifyAndUpload(task, ExternalPayloadStorage.PayloadType.TASK_INPUT); assertNull(task.getInputData()); assertEquals(1, uploadCount.get()); assertNotNull(task.getExternalInputPayloadStoragePath()); }
/** * Download the payload from the given path * * @param path the relative path of the payload in the {@link ExternalPayloadStorage} * @return the payload object * @throws ApplicationException in case of JSON parsing errors or download errors */ @SuppressWarnings("unchecked") public Map<String, Object> downloadPayload(String path) { try (InputStream inputStream = externalPayloadStorage.download(path)) { return objectMapper.readValue(IOUtils.toString(inputStream), Map.class); } catch (IOException e) { logger.error("Unable to download payload from external storage path: {}", path, e); throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, e); } }
@SuppressWarnings("unchecked") @Test public void testUploadWorkflowPayload() throws IOException { AtomicInteger uploadCount = new AtomicInteger(0); InputStream stream = ExternalPayloadStorageUtilsTest.class.getResourceAsStream("/payload.json"); Map<String, Object> payload = objectMapper.readValue(stream, Map.class); when(externalPayloadStorage.getLocation(ExternalPayloadStorage.Operation.WRITE, ExternalPayloadStorage.PayloadType.WORKFLOW_OUTPUT, "")).thenReturn(location); doAnswer(invocation -> { uploadCount.incrementAndGet(); return null; }).when(externalPayloadStorage).upload(anyString(), any(), anyLong()); Workflow workflow = new Workflow(); workflow.setOutput(payload); externalPayloadStorageUtils.verifyAndUpload(workflow, ExternalPayloadStorage.PayloadType.WORKFLOW_OUTPUT); assertNull(workflow.getOutput()); assertEquals(1, uploadCount.get()); assertNotNull(workflow.getExternalOutputPayloadStoragePath()); }
@VisibleForTesting String uploadHelper(byte[] payloadBytes, long payloadSize, ExternalPayloadStorage.PayloadType payloadType) { ExternalStorageLocation location = externalPayloadStorage.getLocation(ExternalPayloadStorage.Operation.WRITE, payloadType, ""); externalPayloadStorage.upload(location.getPath(), new ByteArrayInputStream(payloadBytes), payloadSize); return location.getPath(); }