Common library + roster service updates #52

Merged
Maece97 merged 7 commits from common into dev 2021-11-08 13:35:38 +00:00
19 changed files with 201 additions and 117 deletions
Showing only changes of commit 2905850943 - Show all commits

View File

@ -55,8 +55,8 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
if (response.body().equals("")) { if (response.body().equals("")) {
return null; return null;
} }
JSONObject responseBody = new JSONObject(response.body());
return new Task(new JSONObject(response.body()).getString("taskID")); return new Task(responseBody.getString("taskID"), responseBody.getString("input"));
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e); logger.log(Level.SEVERE, e.getLocalizedMessage(), e);

View File

@ -70,7 +70,7 @@ public abstract class ExecutorBase {
logger.info("Starting execution"); logger.info("Starting execution");
this.status = ExecutorStatus.EXECUTING; this.status = ExecutorStatus.EXECUTING;
task.setResult(execution()); task.setResult(execution(task.getInput()));
// TODO implement logic if execution was not successful // TODO implement logic if execution was not successful
executionFinishedEventPort.publishExecutionFinishedEvent( executionFinishedEventPort.publishExecutionFinishedEvent(
@ -84,6 +84,6 @@ public abstract class ExecutorBase {
* Implementation of the actual execution method of an executor * Implementation of the actual execution method of an executor
* @return the execution result * @return the execution result
**/ **/
protected abstract String execution(); protected abstract String execution(String... input);
} }

View File

@ -12,8 +12,12 @@ public class Task {
@Setter @Setter
private String result; private String result;
public Task(String taskID) { @Getter
private String[] input;
public Task(String taskID, String... input) {
this.taskID = taskID; this.taskID = taskID;
this.input = input;
} }
} }

View File

@ -19,19 +19,28 @@ public class Executor extends ExecutorBase {
@Override @Override
protected protected
String execution() { String execution(String... input) {
double result = Double.NaN;
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[2]);
String operation = input[1];
int a = 20;
int b = 20;
try { try {
TimeUnit.SECONDS.sleep(20); TimeUnit.SECONDS.sleep(20);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
int result = a + b; if (operation == "+") {
result = a + b;
} else if (operation == "*") {
result = a * b;
} else if (operation == "-") {
result = a - b;
}
return Integer.toString(result); return Double.toString(result);
} }
} }

View File

@ -75,6 +75,12 @@
<artifactId>org.eclipse.paho.client.mqttv3</artifactId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version> <version>1.2.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,8 +1,10 @@
package ch.unisg.tapastasks.tasks.adapter.in.web; package ch.unisg.tapastasks.tasks.adapter.in.web;
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation;
import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskCommand; import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskCommand;
import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase; import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -21,7 +23,7 @@ public class CompleteTaskWebController {
this.completeTaskUseCase = completeTaskUseCase; this.completeTaskUseCase = completeTaskUseCase;
} }
@PostMapping(path = "/tasks/completeTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) @PostMapping(path = "/tasks/completeTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> completeTask (@RequestBody Task task){ public ResponseEntity<String> completeTask (@RequestBody Task task){
try { try {
@ -32,9 +34,11 @@ public class CompleteTaskWebController {
Task updateATask = completeTaskUseCase.completeTask(command); Task updateATask = completeTaskUseCase.completeTask(command);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE);
return new ResponseEntity<>(TaskMediaType.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); return new ResponseEntity<>(TaskJsonRepresentation.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (ConstraintViolationException e) { } catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} }

View File

@ -1,9 +1,11 @@
package ch.unisg.tapastasks.tasks.adapter.in.web; package ch.unisg.tapastasks.tasks.adapter.in.web;
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation;
import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand;
import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -23,25 +25,26 @@ public class DeleteTaskWebController {
this.deleteClassUseCase = deleteClassUseCase; this.deleteClassUseCase = deleteClassUseCase;
} }
@PostMapping(path="/tasks/deleteTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) @PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> deleteTask (@RequestBody Task task){ public ResponseEntity<String> deleteTask (@RequestBody Task task){
try { try {
DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId()); DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId(), task.getOriginalTaskUri());
Optional<Task> deleteATask = deleteClassUseCase.deleteTask(command); Optional<Task> deleteATask = deleteClassUseCase.deleteTask(command);
// Check if the task with the given identifier exists // Check if the task with the given identifier exists
if (deleteATask.isEmpty()) { if (deleteATask.isEmpty()) {
// If not, through a 404 Not Found status code // If not, through a 404 Not Found status code
throw new ResponseStatusException(HttpStatus.NOT_FOUND); throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} }
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE);
return new ResponseEntity<>(TaskMediaType.serialize(deleteATask.get()), responseHeaders, HttpStatus.ACCEPTED); return new ResponseEntity<>(TaskJsonRepresentation.serialize(deleteATask.get()), responseHeaders, HttpStatus.ACCEPTED);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (ConstraintViolationException e) { } catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} }

View File

@ -1,8 +1,10 @@
package ch.unisg.tapastasks.tasks.adapter.in.web; package ch.unisg.tapastasks.tasks.adapter.in.web;
import ch.unisg.tapastasks.tasks.adapter.in.formats.TaskJsonRepresentation;
import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedCommand; import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedCommand;
import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase; import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -21,7 +23,7 @@ public class TaskAssignedWebController {
this.taskAssignedUseCase = taskAssignedUseCase; this.taskAssignedUseCase = taskAssignedUseCase;
} }
@PostMapping(path="/tasks/assignTask", consumes= {TaskMediaType.TASK_MEDIA_TYPE}) @PostMapping(path="/tasks/assignTask", consumes= {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> assignTask(@RequestBody Task task){ public ResponseEntity<String> assignTask(@RequestBody Task task){
try{ try{
TaskAssignedCommand command = new TaskAssignedCommand( TaskAssignedCommand command = new TaskAssignedCommand(
@ -31,9 +33,11 @@ public class TaskAssignedWebController {
Task updateATask = taskAssignedUseCase.assignTask(command); Task updateATask = taskAssignedUseCase.assignTask(command);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskJsonRepresentation.MEDIA_TYPE);
return new ResponseEntity<>(TaskMediaType.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); return new ResponseEntity<>(TaskJsonRepresentation.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (ConstraintViolationException e) { } catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} }

View File

@ -1,23 +0,0 @@
package ch.unisg.tapastasks.tasks.adapter.in.web;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import org.json.JSONObject;
final public class TaskMediaType {
public static final String TASK_MEDIA_TYPE = "application/json";
public static String serialize(Task task) {
JSONObject payload = new JSONObject();
payload.put("taskId", task.getTaskId().getValue());
payload.put("taskName", task.getTaskName().getValue());
payload.put("taskType", task.getTaskType().getValue());
payload.put("taskState", task.getTaskState().getValue());
payload.put("taskListName", TaskList.getTapasTaskList().getTaskListName().getValue());
payload.put("taskResult", task.getTaskResult().getValue());
return payload.toString();
}
private TaskMediaType() { }
}

View File

@ -0,0 +1,59 @@
package ch.unisg.tapastasks.tasks.adapter.out.web;
import ch.unisg.tapastasks.tasks.application.port.out.CanTaskBeDeletedPort;
import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
@Component
@Primary
public class CanTaskBeDeletedWebAdapter implements CanTaskBeDeletedPort {
// Base URI of the service interested in this event
//Todo: Add the right IP address
String server = null;
@Override
public void canTaskBeDeletedEvent(DeleteTaskEvent event){
var values = new HashMap<> () {{
put("taskId", event.taskId);
put("taskUri", event.taskUri);
}};
var objectMapper = new ObjectMapper();
String requestBody = null;
try {
requestBody = objectMapper.writeValueAsString(values);
} catch (JsonProcessingException e){
e.printStackTrace();
}
//Todo: Question: How do we include the URI from the DeleteTaskEvent? Do we even need it?
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"task"))
.header("Content-Type", "application/task+json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
//Todo: The following parameters probably need to be changed to get the right error code
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException e){
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -42,7 +42,7 @@ public class PublishNewTaskAddedEventWebAdapter implements NewTaskAddedEventPort
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"/task")) .uri(URI.create(server+"/task"))
.header("Content-Type", "application/json") .header("Content-Type", "application/task+json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody)) .POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build(); .build();

View File

@ -2,6 +2,7 @@ package ch.unisg.tapastasks.tasks.application.port.in;
import ch.unisg.tapastasks.common.SelfValidating; import ch.unisg.tapastasks.common.SelfValidating;
import ch.unisg.tapastasks.tasks.domain.Task.TaskId; import ch.unisg.tapastasks.tasks.domain.Task.TaskId;
import ch.unisg.tapastasks.tasks.domain.Task.OriginalTaskUri;
import lombok.Value; import lombok.Value;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -11,8 +12,12 @@ public class DeleteTaskCommand extends SelfValidating<DeleteTaskCommand> {
@NotNull @NotNull
private final TaskId taskId; private final TaskId taskId;
public DeleteTaskCommand(TaskId taskId){ @NotNull
private final OriginalTaskUri taskUri;
public DeleteTaskCommand(TaskId taskId, OriginalTaskUri taskUri){
this.taskId=taskId; this.taskId=taskId;
this.taskUri = taskUri;
this.validateSelf(); this.validateSelf();
} }
} }

View File

@ -0,0 +1,7 @@
package ch.unisg.tapastasks.tasks.application.port.out;
import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent;
public interface CanTaskBeDeletedPort {
void canTaskBeDeletedEvent(DeleteTaskEvent event);
}

View File

@ -19,17 +19,13 @@ public class CompleteTaskService implements CompleteTaskUseCase {
@Override @Override
public Task completeTask(CompleteTaskCommand command){ public Task completeTask(CompleteTaskCommand command){
// TODO Retrieve the task based on ID
TaskList taskList = TaskList.getTapasTaskList(); TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId()); Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
// TODO Update the status and result (and save?)
Task newTask = updatedTask.get(); Task newTask = updatedTask.get();
newTask.taskResult = new TaskResult(command.getTaskResult().getValue()); newTask.taskResult = new TaskResult(command.getTaskResult().getValue());
newTask.taskState = new TaskState(Task.State.EXECUTED); newTask.taskStatus = new TaskStatus(Task.Status.EXECUTED);
// TODO return the updated task
return newTask; return newTask;
} }
} }

View File

@ -19,10 +19,15 @@ public class DeleteTaskService implements DeleteTaskUseCase {
@Override @Override
public Optional<Task> deleteTask(DeleteTaskCommand command){ public Optional<Task> deleteTask(DeleteTaskCommand command){
// TODO check with assignment service if we can delte
TaskList taskList = TaskList.getTapasTaskList(); TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
Task newTask = updatedTask.get();
// TODO: Fill in the right condition into the if-statement and the else-statement
if (/*the task can be deleted*/){
return taskList.deleteTaskById(command.getTaskId()); return taskList.deleteTaskById(command.getTaskId());
} else {
/*send message back to TaskList that the task cannot be deleted*/
}
} }
} }

View File

@ -24,7 +24,7 @@ public class TaskAssignedService implements TaskAssignedUseCase {
// update the status to assigned // update the status to assigned
Task updatedTask = task.get(); Task updatedTask = task.get();
updatedTask.taskState = new TaskState(State.ASSIGNED); updatedTask.taskStatus = new TaskStatus(Status.ASSIGNED);
return updatedTask; return updatedTask;
} }

View File

@ -0,0 +1,11 @@
package ch.unisg.tapastasks.tasks.domain;
public class DeleteTaskEvent {
public String taskId;
public String taskUri;
public DeleteTaskEvent(String taskId, String taskUri){
this.taskId = taskId;
this.taskUri = taskUri;
}
}

View File

@ -21,40 +21,33 @@ public class Task {
@Getter @Getter
private final TaskType taskType; private final TaskType taskType;
@Getter @Getter @Setter
public TaskState taskState; // had to make public for CompleteTaskService public TaskStatus taskStatus; // had to make public for CompleteTaskService
@Getter @Getter
public TaskResult taskResult; // same as above public TaskResult taskResult; // same as above
// private final OriginalTaskUri originalTaskUri; @Getter
private final OriginalTaskUri originalTaskUri;
// @Getter @Setter @Getter @Setter
// private TaskStatus taskStatus; private ServiceProvider provider;
// @Getter @Setter @Getter @Setter
// private ServiceProvider provider; private InputData inputData;
// @Getter @Setter @Getter @Setter
// private InputData inputData; private OutputData outputData;
// @Getter @Setter
// private OutputData outputData;
public Task(TaskName taskName, TaskType taskType, OriginalTaskUri taskUri) { public Task(TaskName taskName, TaskType taskType, OriginalTaskUri taskUri) {
this.taskId = new TaskId(UUID.randomUUID().toString());
this.taskName = taskName; this.taskName = taskName;
this.taskType = taskType; this.taskType = taskType;
this.taskState = new TaskState(State.OPEN); this.taskStatus = new TaskStatus(Status.OPEN);
this.taskId = new TaskId(UUID.randomUUID().toString()); this.taskId = new TaskId(UUID.randomUUID().toString());
this.taskResult = new TaskResult(""); this.taskResult = new TaskResult("");
// this.originalTaskUri = taskUri; this.originalTaskUri = taskUri;
this.inputData = null;
// this.taskStatus = new TaskStatus(Status.OPEN); this.outputData = null;
// this.inputData = null;
// this.outputData = null;
} }
protected static Task createTaskWithNameAndType(TaskName name, TaskType type) { protected static Task createTaskWithNameAndType(TaskName name, TaskType type) {

View File

@ -75,41 +75,42 @@ public class TaskList {
} }
return Optional.empty(); return Optional.empty();
// public Task changeTaskStatusToAssigned(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider) }
// throws TaskNotFoundException { public Task changeTaskStatusToAssigned(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider)
// return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty()); throws TaskNotFoundException {
// } return changeTaskStatus(id, new Task.TaskStatus(Task.Status.ASSIGNED), serviceProvider, Optional.empty());
}
// public Task changeTaskStatusToRunning(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider) public Task changeTaskStatusToRunning(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider)
// throws TaskNotFoundException { throws TaskNotFoundException {
// return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty()); return changeTaskStatus(id, new Task.TaskStatus(Task.Status.RUNNING), serviceProvider, Optional.empty());
// } }
// public Task changeTaskStatusToExecuted(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider, public Task changeTaskStatusToExecuted(Task.TaskId id, Optional<Task.ServiceProvider> serviceProvider,
// Optional<Task.OutputData> outputData) throws TaskNotFoundException { Optional<Task.OutputData> outputData) throws TaskNotFoundException {
// return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData); return changeTaskStatus(id, new Task.TaskStatus(Task.Status.EXECUTED), serviceProvider, outputData);
// } }
// private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional<Task.ServiceProvider> serviceProvider, private Task changeTaskStatus(Task.TaskId id, Task.TaskStatus status, Optional<Task.ServiceProvider> serviceProvider,
// Optional<Task.OutputData> outputData) { Optional<Task.OutputData> outputData) {
// Optional<Task> taskOpt = retrieveTaskById(id); Optional<Task> taskOpt = retrieveTaskById(id);
// if (taskOpt.isEmpty()) { if (taskOpt.isEmpty()) {
// throw new TaskNotFoundException(); throw new TaskNotFoundException();
// } }
// Task task = taskOpt.get(); Task task = taskOpt.get();
// task.setTaskStatus(status); task.setTaskStatus(status);
// if (serviceProvider.isPresent()) { if (serviceProvider.isPresent()) {
// task.setProvider(serviceProvider.get()); task.setProvider(serviceProvider.get());
// } }
// if (outputData.isPresent()) { if (outputData.isPresent()) {
// task.setOutputData(outputData.get()); task.setOutputData(outputData.get());
// } }
// return task; return task;
} }
@Value @Value