added valueObjects & CustomErrors

This commit is contained in:
2021-10-17 10:33:26 +02:00
parent a61f111879
commit 46969deb0f
34 changed files with 214 additions and 103 deletions

View File

@@ -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());
}
}
}

View File

@@ -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<Void> addNewTaskTaskToTaskList(@RequestBody Task task) {
try {
NewTaskCommand command = new NewTaskCommand(
task.getTaskID(), task.getTaskType()
);
public ResponseEntity<Void> 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());
}
}
}

View File

@@ -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<ErrorResponse> handleException(PortOutOfRangeException e){
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
return new ResponseEntity<>(error, error.getHttpStatus());
}
@ExceptionHandler(InvalidIP4Exception.class)
public ResponseEntity<ErrorResponse> handleException(InvalidIP4Exception e){
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
return new ResponseEntity<>(error, error.getHttpStatus());
}
}

View File

@@ -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();

View File

@@ -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

View File

@@ -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

View File

@@ -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<ApplyForTaskCommand>{
@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;

View File

@@ -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<NewTaskCommand> {
@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();

View File

@@ -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<TaskCompletedCommand>{
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<TaskCompletedCommand>{
@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;

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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

View File

@@ -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<String> availableTaskTypes = Arrays.asList("addition", "robot");
List<String> availableTaskTypes = Arrays.asList("ADDITION", "ROBOT");
if (!availableTaskTypes.contains(command.getTaskType())) {
if (!availableTaskTypes.contains(command.getTaskType().getValue())) {
return false;
}

View File

@@ -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

View File

@@ -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;
}

View File

@@ -1,9 +0,0 @@
package ch.unisg.assignment.assignment.domain;
public class NewTaskEvent {
public String taskType;
public NewTaskEvent(String taskType) {
this.taskType = taskType;
}
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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() {};
}

View File

@@ -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;
}
}

View File

@@ -1,4 +1,4 @@
package ch.unisg.assignment.assignment.domain;
package ch.unisg.assignment.assignment.domain.event;
public class TaskAssignedEvent {
public String taskID;

View File

@@ -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;

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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;
}

View File

@@ -0,0 +1,7 @@
package ch.unisg.assignment.common.exception;
public class InvalidIP4Exception extends Exception {
public InvalidIP4Exception() {
super("IP4 is invalid");
}
}

View File

@@ -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)");
}
}