From 46969deb0f9df05669c7efb0757cc372b171d63a Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 17 Oct 2021 10:33:26 +0200 Subject: [PATCH] added valueObjects & CustomErrors --- .../in/web/ApplyForTaskController.java | 15 +++------ .../adapter/in/web/NewTaskController.java | 15 +++------ .../in/web/WebControllerExceptionHandler.java | 31 +++++++++++++++++++ .../out/web/PublishNewTaskEventAdapter.java | 4 +-- .../web/PublishTaskAssignedEventAdapter.java | 2 +- .../web/PublishTaskCompletedEventAdapter.java | 2 +- .../port/in/ApplyForTaskCommand.java | 11 ++++--- .../application/port/in/NewTaskCommand.java | 8 +++-- .../port/in/TaskCompletedCommand.java | 5 +-- .../port/out/NewTaskEventPort.java | 2 +- .../port/out/TaskAssignedEventPort.java | 2 +- .../port/out/TaskCompletedEventPort.java | 2 +- .../service/ApplyForTaskService.java | 2 +- .../application/service/NewTaskService.java | 6 ++-- .../service/TaskCompletedService.java | 2 +- .../assignment/domain/ExecutorInfo.java | 9 ++++-- .../assignment/domain/NewTaskEvent.java | 9 ------ .../assignment/assignment/domain/Roster.java | 22 +++++++------ .../assignment/domain/RosterItem.java | 8 +++-- .../assignment/assignment/domain/Task.java | 10 +++++- .../assignment/domain/event/NewTaskEvent.java | 11 +++++++ .../domain/{ => event}/TaskAssignedEvent.java | 2 +- .../{ => event}/TaskCompletedEvent.java | 8 ++--- .../domain/valueobject/ExecutorType.java | 12 +++++++ .../domain/valueobject/IP4Adress.java | 23 ++++++++++++++ .../assignment/domain/valueobject/Port.java | 17 ++++++++++ .../common/exception/ErrorResponse.java | 13 ++++++++ .../common/exception/InvalidIP4Exception.java | 7 +++++ .../exception/PortOutOfRangeException.java | 7 +++++ .../web/ExecutionFinishedEventAdapter.java | 5 --- .../out/web/NotifyExecutorPoolAdapter.java | 28 +++++------------ executor2/pom.xml | 6 ++++ .../in/web/TaskAvailableController.java | 9 +++--- .../service/TaskAvailableService.java | 2 ++ 34 files changed, 214 insertions(+), 103 deletions(-) create mode 100644 assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/WebControllerExceptionHandler.java delete mode 100644 assignment/src/main/java/ch/unisg/assignment/assignment/domain/NewTaskEvent.java create mode 100644 assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/NewTaskEvent.java rename assignment/src/main/java/ch/unisg/assignment/assignment/domain/{ => event}/TaskAssignedEvent.java (73%) rename assignment/src/main/java/ch/unisg/assignment/assignment/domain/{ => event}/TaskCompletedEvent.java (58%) create mode 100644 assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/ExecutorType.java create mode 100644 assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/IP4Adress.java create mode 100644 assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/Port.java create mode 100644 assignment/src/main/java/ch/unisg/assignment/common/exception/ErrorResponse.java create mode 100644 assignment/src/main/java/ch/unisg/assignment/common/exception/InvalidIP4Exception.java create mode 100644 assignment/src/main/java/ch/unisg/assignment/common/exception/PortOutOfRangeException.java diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/ApplyForTaskController.java b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/ApplyForTaskController.java index e83e74d..1d0111d 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/ApplyForTaskController.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/ApplyForTaskController.java @@ -1,12 +1,8 @@ package ch.unisg.assignment.assignment.adapter.in.web; -import javax.validation.ConstraintViolationException; - -import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskCommand; import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskUseCase; @@ -23,14 +19,11 @@ public class ApplyForTaskController { @PostMapping(path = "/task/apply", consumes = {"application/json"}) public Task applyForTask(@RequestBody ExecutorInfo executorInfo) { - try { - ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(), - executorInfo.getIp(), executorInfo.getPort()); - return applyForTaskUseCase.applyForTask(command); + ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(), + executorInfo.getIp(), executorInfo.getPort()); + + return applyForTaskUseCase.applyForTask(command); - } catch (ConstraintViolationException e) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); - } } } diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/NewTaskController.java b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/NewTaskController.java index d95fdd5..18bad8f 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/NewTaskController.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/NewTaskController.java @@ -1,13 +1,10 @@ package ch.unisg.assignment.assignment.adapter.in.web; -import javax.validation.ConstraintViolationException; - import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; import ch.unisg.assignment.assignment.application.port.in.NewTaskCommand; import ch.unisg.assignment.assignment.application.port.in.NewTaskUseCase; @@ -22,11 +19,9 @@ public class NewTaskController { } @PostMapping(path = "/task", consumes = {"application/json"}) - public ResponseEntity addNewTaskTaskToTaskList(@RequestBody Task task) { - try { - NewTaskCommand command = new NewTaskCommand( - task.getTaskID(), task.getTaskType() - ); + public ResponseEntity newTaskController(@RequestBody Task task) { + + NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskType()); boolean success = newTaskUseCase.addNewTaskToQueue(command); @@ -34,8 +29,6 @@ public class NewTaskController { return new ResponseEntity<>(HttpStatus.CREATED); } return new ResponseEntity<>(HttpStatus.CONFLICT); - } catch (ConstraintViolationException e) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); - } + } } diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/WebControllerExceptionHandler.java b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/WebControllerExceptionHandler.java new file mode 100644 index 0000000..08a0895 --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/in/web/WebControllerExceptionHandler.java @@ -0,0 +1,31 @@ +package ch.unisg.assignment.assignment.adapter.in.web; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import ch.unisg.assignment.common.exception.ErrorResponse; +import ch.unisg.assignment.common.exception.InvalidIP4Exception; +import ch.unisg.assignment.common.exception.PortOutOfRangeException; + +@ControllerAdvice +public class WebControllerExceptionHandler { + + @ExceptionHandler(PortOutOfRangeException.class) + public ResponseEntity handleException(PortOutOfRangeException e){ + + ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage()); + return new ResponseEntity<>(error, error.getHttpStatus()); + + } + + @ExceptionHandler(InvalidIP4Exception.class) + public ResponseEntity handleException(InvalidIP4Exception e){ + + ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage()); + return new ResponseEntity<>(error, error.getHttpStatus()); + + } + +} diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishNewTaskEventAdapter.java b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishNewTaskEventAdapter.java index c08651b..b764faa 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishNewTaskEventAdapter.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishNewTaskEventAdapter.java @@ -10,7 +10,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort; -import ch.unisg.assignment.assignment.domain.NewTaskEvent; +import ch.unisg.assignment.assignment.domain.event.NewTaskEvent; @Component @Primary @@ -23,7 +23,7 @@ public class PublishNewTaskEventAdapter implements NewTaskEventPort { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(server + "/newtask/" + event.taskType)) + .uri(URI.create(server + "/newtask/" + event.taskType.getValue())) .GET() .build(); diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskAssignedEventAdapter.java b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskAssignedEventAdapter.java index 7f38d60..99879f7 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskAssignedEventAdapter.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskAssignedEventAdapter.java @@ -11,7 +11,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import ch.unisg.assignment.assignment.application.port.out.TaskAssignedEventPort; -import ch.unisg.assignment.assignment.domain.TaskAssignedEvent; +import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent; @Component @Primary diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskCompletedEventAdapter.java b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskCompletedEventAdapter.java index d63d710..59a27ce 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskCompletedEventAdapter.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/adapter/out/web/PublishTaskCompletedEventAdapter.java @@ -11,7 +11,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import ch.unisg.assignment.assignment.application.port.out.TaskCompletedEventPort; -import ch.unisg.assignment.assignment.domain.TaskCompletedEvent; +import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent; @Component @Primary diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/ApplyForTaskCommand.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/ApplyForTaskCommand.java index 76d0e41..df36d58 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/ApplyForTaskCommand.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/ApplyForTaskCommand.java @@ -2,6 +2,9 @@ package ch.unisg.assignment.assignment.application.port.in; import javax.validation.constraints.NotNull; +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; +import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress; +import ch.unisg.assignment.assignment.domain.valueobject.Port; import ch.unisg.assignment.common.SelfValidating; import lombok.EqualsAndHashCode; import lombok.Value; @@ -11,16 +14,16 @@ import lombok.Value; public class ApplyForTaskCommand extends SelfValidating{ @NotNull - private final String taskType; + private final ExecutorType taskType; @NotNull - private final String executorIP; + private final IP4Adress executorIP; @NotNull - private final int executorPort; + private final Port executorPort; - public ApplyForTaskCommand(String taskType, String executorIP, int executorPort) { + public ApplyForTaskCommand(ExecutorType taskType, IP4Adress executorIP, Port executorPort) { this.taskType = taskType; this.executorIP = executorIP; this.executorPort = executorPort; diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/NewTaskCommand.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/NewTaskCommand.java index 98f4579..ab6838e 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/NewTaskCommand.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/NewTaskCommand.java @@ -2,20 +2,22 @@ package ch.unisg.assignment.assignment.application.port.in; import javax.validation.constraints.NotNull; +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; import ch.unisg.assignment.common.SelfValidating; - +import lombok.EqualsAndHashCode; import lombok.Value; @Value +@EqualsAndHashCode(callSuper=false) public class NewTaskCommand extends SelfValidating { @NotNull private final String taskID; @NotNull - private final String taskType; + private final ExecutorType taskType; - public NewTaskCommand(String taskID, String taskType) { + public NewTaskCommand(String taskID, ExecutorType taskType) { this.taskID = taskID; this.taskType = taskType; this.validateSelf(); diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/TaskCompletedCommand.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/TaskCompletedCommand.java index 474456d..e324e89 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/TaskCompletedCommand.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/in/TaskCompletedCommand.java @@ -2,6 +2,7 @@ package ch.unisg.assignment.assignment.application.port.in; import javax.validation.constraints.NotNull; +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; import ch.unisg.assignment.common.SelfValidating; import lombok.EqualsAndHashCode; import lombok.Value; @@ -14,7 +15,7 @@ public class TaskCompletedCommand extends SelfValidating{ private final String taskID; @NotNull - private final String taskType; + private final ExecutorType taskType; @NotNull private final String taskStatus; @@ -22,7 +23,7 @@ public class TaskCompletedCommand extends SelfValidating{ @NotNull private final String taskResult; - public TaskCompletedCommand(String taskID, String taskType, String taskStatus, String taskResult) { + public TaskCompletedCommand(String taskID, ExecutorType taskType, String taskStatus, String taskResult) { this.taskID = taskID; this.taskType = taskType; this.taskStatus = taskStatus; diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/NewTaskEventPort.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/NewTaskEventPort.java index 521af1a..909a9ba 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/NewTaskEventPort.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/NewTaskEventPort.java @@ -1,6 +1,6 @@ package ch.unisg.assignment.assignment.application.port.out; -import ch.unisg.assignment.assignment.domain.NewTaskEvent; +import ch.unisg.assignment.assignment.domain.event.NewTaskEvent; public interface NewTaskEventPort { void publishNewTaskEvent(NewTaskEvent event); diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskAssignedEventPort.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskAssignedEventPort.java index 0cd1cae..fefd4a1 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskAssignedEventPort.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskAssignedEventPort.java @@ -1,6 +1,6 @@ package ch.unisg.assignment.assignment.application.port.out; -import ch.unisg.assignment.assignment.domain.TaskAssignedEvent; +import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent; public interface TaskAssignedEventPort { void publishTaskAssignedEvent(TaskAssignedEvent taskAssignedEvent); diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskCompletedEventPort.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskCompletedEventPort.java index 37b4ff3..43a8aa5 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskCompletedEventPort.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/port/out/TaskCompletedEventPort.java @@ -1,6 +1,6 @@ package ch.unisg.assignment.assignment.application.port.out; -import ch.unisg.assignment.assignment.domain.TaskCompletedEvent; +import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent; public interface TaskCompletedEventPort { void publishTaskCompleted(TaskCompletedEvent event); diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/ApplyForTaskService.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/ApplyForTaskService.java index dac9a70..0593a30 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/ApplyForTaskService.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/ApplyForTaskService.java @@ -9,7 +9,7 @@ import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskUseCase; import ch.unisg.assignment.assignment.application.port.out.TaskAssignedEventPort; import ch.unisg.assignment.assignment.domain.Roster; import ch.unisg.assignment.assignment.domain.Task; -import ch.unisg.assignment.assignment.domain.TaskAssignedEvent; +import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/NewTaskService.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/NewTaskService.java index b662cd2..069ee29 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/NewTaskService.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/NewTaskService.java @@ -10,9 +10,9 @@ import org.springframework.stereotype.Component; import ch.unisg.assignment.assignment.application.port.in.NewTaskCommand; import ch.unisg.assignment.assignment.application.port.in.NewTaskUseCase; import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort; -import ch.unisg.assignment.assignment.domain.NewTaskEvent; import ch.unisg.assignment.assignment.domain.Roster; import ch.unisg.assignment.assignment.domain.Task; +import ch.unisg.assignment.assignment.domain.event.NewTaskEvent; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @@ -26,9 +26,9 @@ public class NewTaskService implements NewTaskUseCase { public boolean addNewTaskToQueue(NewTaskCommand command) { // TODO Get availableTaskTypes from executor pool - List availableTaskTypes = Arrays.asList("addition", "robot"); + List availableTaskTypes = Arrays.asList("ADDITION", "ROBOT"); - if (!availableTaskTypes.contains(command.getTaskType())) { + if (!availableTaskTypes.contains(command.getTaskType().getValue())) { return false; } diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/TaskCompletedService.java b/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/TaskCompletedService.java index 1295336..c8273ff 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/TaskCompletedService.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/application/service/TaskCompletedService.java @@ -8,7 +8,7 @@ import ch.unisg.assignment.assignment.application.port.in.TaskCompletedCommand; import ch.unisg.assignment.assignment.application.port.in.TaskCompletedUseCase; import ch.unisg.assignment.assignment.application.port.out.TaskCompletedEventPort; import ch.unisg.assignment.assignment.domain.Roster; -import ch.unisg.assignment.assignment.domain.TaskCompletedEvent; +import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/ExecutorInfo.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/ExecutorInfo.java index 3b7432c..6b19dcc 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/ExecutorInfo.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/ExecutorInfo.java @@ -1,18 +1,21 @@ package ch.unisg.assignment.assignment.domain; +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; +import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress; +import ch.unisg.assignment.assignment.domain.valueobject.Port; import lombok.Getter; import lombok.Setter; public class ExecutorInfo { @Getter @Setter - private String ip; + private IP4Adress ip; @Getter @Setter - private int port; + private Port port; @Getter @Setter - private String executorType; + private ExecutorType executorType; } diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/NewTaskEvent.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/NewTaskEvent.java deleted file mode 100644 index 57084ee..0000000 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/NewTaskEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package ch.unisg.assignment.assignment.domain; - -public class NewTaskEvent { - public String taskType; - - public NewTaskEvent(String taskType) { - this.taskType = taskType; - } -} diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Roster.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Roster.java index 054c094..521a748 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Roster.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Roster.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; +import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress; +import ch.unisg.assignment.assignment.domain.valueobject.Port; + public class Roster { private static final Roster roster = new Roster(); @@ -19,25 +23,25 @@ public class Roster { private Roster() {} public void addTaskToQueue(Task task) { - if (queues.containsKey(task.getTaskType().toUpperCase())) { - queues.get(task.getTaskType().toUpperCase()).add(task); + if (queues.containsKey(task.getTaskType().getValue())) { + queues.get(task.getTaskType().getValue()).add(task); } else { - queues.put(task.getTaskType().toUpperCase(), new ArrayList<>(Arrays.asList(task))); + queues.put(task.getTaskType().getValue(), new ArrayList<>(Arrays.asList(task))); } } - public Task assignTaskToExecutor(String taskType, String executorIP, int executorPort) { - if (!queues.containsKey(taskType.toUpperCase())) { + public Task assignTaskToExecutor(ExecutorType taskType, IP4Adress executorIP, Port executorPort) { + if (!queues.containsKey(taskType.getValue())) { return null; } - if (queues.get(taskType.toUpperCase()).isEmpty()) { + if (queues.get(taskType.getValue()).isEmpty()) { return null; } - Task task = queues.get(taskType.toUpperCase()).remove(0); + Task task = queues.get(taskType.getValue()).remove(0); - rosterMap.put(task.getTaskID(), new RosterItem(task.getTaskID(), task.getTaskType(), - executorIP, executorPort)); + rosterMap.put(task.getTaskID(), new RosterItem(task.getTaskID(), + task.getTaskType().getValue(), executorIP, executorPort)); return task; } diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/RosterItem.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/RosterItem.java index 6e050d4..2c3bb52 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/RosterItem.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/RosterItem.java @@ -1,5 +1,7 @@ package ch.unisg.assignment.assignment.domain; +import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress; +import ch.unisg.assignment.assignment.domain.valueobject.Port; import lombok.Getter; public class RosterItem { @@ -11,13 +13,13 @@ public class RosterItem { private String taskType; @Getter - private String executorIP; + private IP4Adress executorIP; @Getter - private int executorPort; + private Port executorPort; - public RosterItem(String taskID, String taskType, String executorIP, int executorPort) { + public RosterItem(String taskID, String taskType, IP4Adress executorIP, Port executorPort) { this.taskID = taskID; this.taskType = taskType; this.executorIP = executorIP; diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Task.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Task.java index 6db5111..55fb00d 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Task.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/Task.java @@ -1,5 +1,6 @@ package ch.unisg.assignment.assignment.domain; +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; import lombok.Getter; import lombok.Setter; @@ -9,7 +10,7 @@ public class Task { private String taskID; @Getter - private String taskType; + private ExecutorType taskType; @Getter @Setter @@ -20,8 +21,15 @@ public class Task { private String status; public Task(String taskID, String taskType) { + this.taskID = taskID; + this.taskType = new ExecutorType(taskType); + } + + public Task(String taskID, ExecutorType taskType) { this.taskID = taskID; this.taskType = taskType; } + public Task() {}; + } diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/NewTaskEvent.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/NewTaskEvent.java new file mode 100644 index 0000000..34e7f0b --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/NewTaskEvent.java @@ -0,0 +1,11 @@ +package ch.unisg.assignment.assignment.domain.event; + +import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType; + +public class NewTaskEvent { + public final ExecutorType taskType; + + public NewTaskEvent(ExecutorType taskType) { + this.taskType = taskType; + } +} diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/TaskAssignedEvent.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/TaskAssignedEvent.java similarity index 73% rename from assignment/src/main/java/ch/unisg/assignment/assignment/domain/TaskAssignedEvent.java rename to assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/TaskAssignedEvent.java index 8acd144..ef20f43 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/TaskAssignedEvent.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/TaskAssignedEvent.java @@ -1,4 +1,4 @@ -package ch.unisg.assignment.assignment.domain; +package ch.unisg.assignment.assignment.domain.event; public class TaskAssignedEvent { public String taskID; diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/TaskCompletedEvent.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/TaskCompletedEvent.java similarity index 58% rename from assignment/src/main/java/ch/unisg/assignment/assignment/domain/TaskCompletedEvent.java rename to assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/TaskCompletedEvent.java index 89f26ae..432a8f0 100644 --- a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/TaskCompletedEvent.java +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/event/TaskCompletedEvent.java @@ -1,11 +1,11 @@ -package ch.unisg.assignment.assignment.domain; +package ch.unisg.assignment.assignment.domain.event; public class TaskCompletedEvent { - public String taskID; + public final String taskID; - public String status; + public final String status; - public String result; + public final String result; public TaskCompletedEvent(String taskID, String status, String result) { this.taskID = taskID; diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/ExecutorType.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/ExecutorType.java new file mode 100644 index 0000000..bc5f467 --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/ExecutorType.java @@ -0,0 +1,12 @@ +package ch.unisg.assignment.assignment.domain.valueobject; + +import lombok.Value; + +@Value +public class ExecutorType { + private String value; + + public ExecutorType(String type) { + this.value = type.toUpperCase(); + } +} diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/IP4Adress.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/IP4Adress.java new file mode 100644 index 0000000..cd23b6b --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/IP4Adress.java @@ -0,0 +1,23 @@ +package ch.unisg.assignment.assignment.domain.valueobject; + +import ch.unisg.assignment.common.exception.InvalidIP4Exception; +import lombok.Value; + +@Value +public class IP4Adress { + private String value; + + public IP4Adress(String ip4) throws InvalidIP4Exception { + if (ip4.equalsIgnoreCase("localhost") || + ip4.matches("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$")) { + this.value = ip4; + } else { + throw new InvalidIP4Exception(); + } + } +} + + + + + diff --git a/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/Port.java b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/Port.java new file mode 100644 index 0000000..a66dbbd --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/assignment/domain/valueobject/Port.java @@ -0,0 +1,17 @@ +package ch.unisg.assignment.assignment.domain.valueobject; + +import ch.unisg.assignment.common.exception.PortOutOfRangeException; +import lombok.Value; + +@Value +public class Port { + private int value; + + public Port(int port) throws PortOutOfRangeException { + if (1024 <= port && port <= 65535) { + this.value = port; + } else { + throw new PortOutOfRangeException(); + } + } +} diff --git a/assignment/src/main/java/ch/unisg/assignment/common/exception/ErrorResponse.java b/assignment/src/main/java/ch/unisg/assignment/common/exception/ErrorResponse.java new file mode 100644 index 0000000..2fb834e --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/common/exception/ErrorResponse.java @@ -0,0 +1,13 @@ +package ch.unisg.assignment.common.exception; + +import org.springframework.http.HttpStatus; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +@Data +@RequiredArgsConstructor +public class ErrorResponse { + private final HttpStatus httpStatus; + private final String message; +} diff --git a/assignment/src/main/java/ch/unisg/assignment/common/exception/InvalidIP4Exception.java b/assignment/src/main/java/ch/unisg/assignment/common/exception/InvalidIP4Exception.java new file mode 100644 index 0000000..fecbfcb --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/common/exception/InvalidIP4Exception.java @@ -0,0 +1,7 @@ +package ch.unisg.assignment.common.exception; + +public class InvalidIP4Exception extends Exception { + public InvalidIP4Exception() { + super("IP4 is invalid"); + } +} diff --git a/assignment/src/main/java/ch/unisg/assignment/common/exception/PortOutOfRangeException.java b/assignment/src/main/java/ch/unisg/assignment/common/exception/PortOutOfRangeException.java new file mode 100644 index 0000000..2772256 --- /dev/null +++ b/assignment/src/main/java/ch/unisg/assignment/common/exception/PortOutOfRangeException.java @@ -0,0 +1,7 @@ +package ch.unisg.assignment.common.exception; + +public class PortOutOfRangeException extends Exception { + public PortOutOfRangeException() { + super("Port is out of available range (1024-65535)"); + } +} diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java index e688868..7408740 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/ExecutionFinishedEventAdapter.java @@ -5,14 +5,9 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.util.HashMap; - -import com.fasterxml.jackson.databind.ObjectMapper; import org.json.JSONObject; -import com.fasterxml.jackson.core.JsonProcessingException; - import ch.unisg.executorBase.executor.application.port.out.ExecutionFinishedEventPort; import ch.unisg.executorBase.executor.domain.ExecutionFinishedEvent; diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/NotifyExecutorPoolAdapter.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/NotifyExecutorPoolAdapter.java index feeca69..bf465f7 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/NotifyExecutorPoolAdapter.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/NotifyExecutorPoolAdapter.java @@ -3,11 +3,8 @@ package ch.unisg.executorBase.executor.adapter.out.web; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; -import java.util.HashMap; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import org.json.JSONObject; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @@ -17,31 +14,22 @@ import ch.unisg.executorBase.executor.domain.ExecutorType; @Component @Primary public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort { - - //This is the base URI of the service interested in this event (in my setup, running locally as separate Spring Boot application) + String server = "http://127.0.0.1:8083"; @Override public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) { - var values = new HashMap() {{ - put("ip", ip); - put("port", Integer.toString(port)); - put("executorType", executorType.toString()); - }}; - - var objectMapper = new ObjectMapper(); - String requestBody = null; - try { - requestBody = objectMapper.writeValueAsString(values); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } + String body = new JSONObject() + .put("executorType", executorType) + .put("ip", ip) + .put("port", port) + .toString(); HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(server+"/executor/new/")) - .POST(HttpRequest.BodyPublishers.ofString(requestBody)) + .POST(HttpRequest.BodyPublishers.ofString(body)) .build(); /** Needs the other service running diff --git a/executor2/pom.xml b/executor2/pom.xml index 95c67ff..86634c6 100644 --- a/executor2/pom.xml +++ b/executor2/pom.xml @@ -45,6 +45,12 @@ executorBase 0.0.1-SNAPSHOT + + + org.json + json + 20210307 + diff --git a/executor2/src/main/java/ch/unisg/executor2/executor/adapter/in/web/TaskAvailableController.java b/executor2/src/main/java/ch/unisg/executor2/executor/adapter/in/web/TaskAvailableController.java index db1b4b5..a14b58f 100644 --- a/executor2/src/main/java/ch/unisg/executor2/executor/adapter/in/web/TaskAvailableController.java +++ b/executor2/src/main/java/ch/unisg/executor2/executor/adapter/in/web/TaskAvailableController.java @@ -1,5 +1,7 @@ package ch.unisg.executor2.executor.adapter.in.web; +import java.util.concurrent.CompletableFuture; + import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -25,12 +27,9 @@ public class TaskAvailableController { if (ExecutorType.contains(taskType.toUpperCase())) { TaskAvailableCommand command = new TaskAvailableCommand( ExecutorType.valueOf(taskType.toUpperCase())); - taskAvailableUseCase.newTaskAvailable(command); + CompletableFuture.runAsync(() -> taskAvailableUseCase.newTaskAvailable(command)); } - // Add the content type as a response header - HttpHeaders responseHeaders = new HttpHeaders(); - - return new ResponseEntity<>("OK", responseHeaders, HttpStatus.OK); + return new ResponseEntity<>("OK", new HttpHeaders(), HttpStatus.OK); } } diff --git a/executor2/src/main/java/ch/unisg/executor2/executor/application/service/TaskAvailableService.java b/executor2/src/main/java/ch/unisg/executor2/executor/application/service/TaskAvailableService.java index 6fa918d..39db6ab 100644 --- a/executor2/src/main/java/ch/unisg/executor2/executor/application/service/TaskAvailableService.java +++ b/executor2/src/main/java/ch/unisg/executor2/executor/application/service/TaskAvailableService.java @@ -1,5 +1,6 @@ package ch.unisg.executor2.executor.application.service; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import ch.unisg.executor2.executor.domain.Executor; @@ -16,6 +17,7 @@ import javax.transaction.Transactional; public class TaskAvailableService implements TaskAvailableUseCase { @Override + @Async public void newTaskAvailable(TaskAvailableCommand command) { Executor executor = Executor.getExecutor();