public static TaskId generateTaskId() { return new TaskId(UUID.randomUUID()); }
@VisibleForTesting TaskId submit(Task task, Consumer<TaskId> callback) { TaskId taskId = TaskId.generateTaskId(); TaskExecutionDetails executionDetails = TaskExecutionDetails.from(task, taskId); idToExecutionDetails.put(taskId, executionDetails); idToFuture.put(taskId, executor.submit(() -> runWithMdc(executionDetails, task, callback))); return taskId; }
public UUID getTaskId() { return executionDetails.getTaskId().getValue(); }
private TaskManager.Status entryWithId(List<TaskExecutionDetails> list, TaskId taskId) { return list.stream() .filter(e -> e.getTaskId().equals(taskId)) .findFirst().get() .getStatus(); }
private TaskManager.Status entryWithId(List<TaskExecutionDetails> list, TaskId taskId) { return list.stream() .filter(e -> e.getTaskId().equals(taskId)) .findFirst().get() .getStatus(); }
public static TaskIdDto from(TaskId id) { return new TaskIdDto(id.getValue()); }
@Test public void respondShouldReturnCreatedWithTaskIdHeader() { Response response = mock(Response.class); TaskId taskId = new TaskId(UUID.fromString(UID_VALUE)); TaskIdDto.respond(response, taskId); verify(response).status(HttpStatus.CREATED_201); verify(response).header(LOCATION.asString(), "/tasks/" + UID_VALUE); verifyNoMoreInteractions(response); }
@Test public void getStatusShouldReturnUnknownWhenUnknownId() { TaskId unknownId = TaskId.generateTaskId(); assertThatThrownBy(() -> memoryTaskManager.getExecutionDetails(unknownId)) .isInstanceOf(TaskNotFoundException.class); }
private Object respondStatus(TaskId taskId, Supplier<TaskExecutionDetails> executionDetailsSupplier) { try { TaskExecutionDetails executionDetails = executionDetailsSupplier.get(); return ExecutionDetailsDto.from(executionDetails); } catch (TaskNotFoundException e) { throw ErrorResponder.builder() .message(String.format("%s can not be found", taskId.getValue())) .statusCode(HttpStatus.NOT_FOUND_404) .type(ErrorResponder.ErrorType.NOT_FOUND) .haltError(); } }
@Test public void respondShouldReturnCreatedWithTaskIdHeader() { Response response = mock(Response.class); TaskId taskId = new TaskId(UUID.fromString(UID_VALUE)); TaskIdDto.respond(response, taskId); verify(response).status(HttpStatus.CREATED_201); verify(response).header(LOCATION.asString(), "/tasks/" + UID_VALUE); verifyNoMoreInteractions(response); }
@Test public void getStatusShouldReturnUnknownWhenUnknownId() { TaskId unknownId = TaskId.generateTaskId(); assertThatThrownBy(() -> memoryTaskManager.getExecutionDetails(unknownId)) .isInstanceOf(TaskNotFoundException.class); }
public static TaskIdDto respond(Response response, TaskId taskId) { response.status(HttpStatus.CREATED_201); response.header(LOCATION.asString(), TasksRoutes.BASE + "/" + taskId.getValue()); return TaskIdDto.from(taskId); }
private TaskId getTaskId(Request req) { try { String id = req.params("id"); return new TaskId(UUID.fromString(id)); } catch (Exception e) { throw ErrorResponder.builder() .statusCode(HttpStatus.BAD_REQUEST_400) .cause(e) .type(ErrorResponder.ErrorType.INVALID_ARGUMENT) .message("Invalid task id") .haltError(); } } }
@Test public void listShouldReturnTaskDetailsWhenTaskInProgress() throws Exception { CountDownLatch taskInProgressLatch = new CountDownLatch(1); TaskId taskId = taskManager.submit(() -> { taskInProgressLatch.countDown(); await(); return Task.Result.COMPLETED; }); taskInProgressLatch.await(); when() .get() .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(1)) .body("[0].status", is(TaskManager.Status.IN_PROGRESS.getValue())) .body("[0].taskId", is(taskId.getValue().toString())) .body("[0].class", is(not(empty()))); }
@Test public void listShouldReturnTaskDetailsWhenTaskInProgress() throws Exception { CountDownLatch taskInProgressLatch = new CountDownLatch(1); TaskId taskId = taskManager.submit(() -> { taskInProgressLatch.countDown(); await(); return Task.Result.COMPLETED; }); taskInProgressLatch.await(); when() .get() .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(1)) .body("[0].status", is(TaskManager.Status.IN_PROGRESS.getValue())) .body("[0].taskId", is(taskId.getValue().toString())) .body("[0].class", is(not(empty()))); }
@Test public void getShouldReturnTaskDetails() throws Exception { CountDownLatch inProgressLatch = new CountDownLatch(1); TaskId taskId = taskManager.submit(() -> { inProgressLatch.countDown(); await(); return Task.Result.COMPLETED; }); inProgressLatch.await(); when() .get("/" + taskId.getValue()) .then() .statusCode(HttpStatus.OK_200) .body("status", is("inProgress")); }
@Test public void getAwaitShouldAwaitTaskCompletion() { TaskId taskId = taskManager.submit(() -> { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } return Task.Result.COMPLETED; }); when() .get("/" + taskId.getValue() + "/await") .then() .statusCode(HttpStatus.OK_200) .body("status", is("completed")); }
@Test public void getShouldReturnTaskDetails() throws Exception { CountDownLatch inProgressLatch = new CountDownLatch(1); TaskId taskId = taskManager.submit(() -> { inProgressLatch.countDown(); await(); return Task.Result.COMPLETED; }); inProgressLatch.await(); when() .get("/" + taskId.getValue()) .then() .statusCode(HttpStatus.OK_200) .body("status", is("inProgress")); }
@Test public void getAwaitShouldAwaitTaskCompletion() { TaskId taskId = taskManager.submit(() -> { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } return Task.Result.COMPLETED; }); when() .get("/" + taskId.getValue() + "/await") .then() .statusCode(HttpStatus.OK_200) .body("status", is("completed")); }