integration + fixes

This commit is contained in:
2021-10-17 15:38:04 +02:00
parent 8b0eea1270
commit f461c5f3cb
25 changed files with 252 additions and 186 deletions

View File

@@ -22,7 +22,7 @@ public class TaskCompletedController {
@PostMapping(path = "/task/completed", consumes = {"application/json"})
public ResponseEntity<Void> addNewTaskTaskToTaskList(@RequestBody Task task) {
TaskCompletedCommand command = new TaskCompletedCommand(task.getTaskID(), task.getTaskType(),
TaskCompletedCommand command = new TaskCompletedCommand(task.getTaskID(),
task.getStatus(), task.getResult());
taskCompletedUseCase.taskCompleted(command);

View File

@@ -0,0 +1,55 @@
package ch.unisg.assignment.assignment.adapter.out.web;
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.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import ch.unisg.assignment.assignment.application.port.out.GetAllExecutorInExecutorPoolByTypePort;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
@Component
@Primary
public class GetAllExecutorInExecutorPoolByTypeAdapter implements GetAllExecutorInExecutorPoolByTypePort {
@Override
public boolean doesExecutorTypeExist(ExecutorType type) {
String server = "http://127.0.0.1:8083";
Logger logger = Logger.getLogger(PublishNewTaskEventAdapter.class.getName());
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/executor-pool/GetAllExecutorInExecutorPoolByType/" + type.getValue()))
.header("Content-Type", "application/json")
.GET()
.build();
try {
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == HttpStatus.OK.value()) {
JSONArray jsonArray = new JSONArray(response.body().toString());
if (jsonArray.length() > 0) {
return true;
}
}
} catch (IOException | InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
}
return false;
}
}

View File

@@ -18,7 +18,8 @@ import ch.unisg.assignment.assignment.domain.event.NewTaskEvent;
@Primary
public class PublishNewTaskEventAdapter implements NewTaskEventPort {
String server = "http://127.0.0.1:8085";
String server = "http://127.0.0.1:8084";
String server2 = "http://127.0.0.1:8085";
Logger logger = Logger.getLogger(PublishNewTaskEventAdapter.class.getName());
@@ -32,6 +33,21 @@ public class PublishNewTaskEventAdapter implements NewTaskEventPort {
.build();
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
}
HttpClient client2 = HttpClient.newHttpClient();
HttpRequest request2 = HttpRequest.newBuilder()
.uri(URI.create(server2 + "/newtask/" + event.taskType.getValue()))
.GET()
.build();
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {

View File

@@ -19,7 +19,7 @@ import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent;
@Primary
public class PublishTaskAssignedEventAdapter implements TaskAssignedEventPort {
String server = "http://127.0.0.1:8085";
String server = "http://127.0.0.1:8081";
Logger logger = Logger.getLogger(PublishTaskAssignedEventAdapter.class.getName());
@@ -32,7 +32,7 @@ public class PublishTaskAssignedEventAdapter implements TaskAssignedEventPort {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/tasks/completeTask"))
.uri(URI.create(server + "/tasks/assignTask"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();

View File

@@ -2,7 +2,6 @@ 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,18 +13,14 @@ public class TaskCompletedCommand extends SelfValidating<TaskCompletedCommand>{
@NotNull
private final String taskID;
@NotNull
private final ExecutorType taskType;
@NotNull
private final String taskStatus;
@NotNull
private final String taskResult;
public TaskCompletedCommand(String taskID, ExecutorType taskType, String taskStatus, String taskResult) {
public TaskCompletedCommand(String taskID, String taskStatus, String taskResult) {
this.taskID = taskID;
this.taskType = taskType;
this.taskStatus = taskStatus;
this.taskResult = taskResult;
this.validateSelf();

View File

@@ -0,0 +1,9 @@
package ch.unisg.assignment.assignment.application.port.out;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
public interface GetAllExecutorInExecutorPoolByTypePort {
boolean doesExecutorTypeExist(ExecutorType type);
}

View File

@@ -9,6 +9,7 @@ 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.GetAllExecutorInExecutorPoolByTypePort;
import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort;
import ch.unisg.assignment.assignment.domain.Roster;
import ch.unisg.assignment.assignment.domain.Task;
@@ -21,14 +22,13 @@ import lombok.RequiredArgsConstructor;
public class NewTaskService implements NewTaskUseCase {
private final NewTaskEventPort newTaskEventPort;
private final GetAllExecutorInExecutorPoolByTypePort getAllExecutorInExecutorPoolByTypePort;
@Override
public boolean addNewTaskToQueue(NewTaskCommand command) {
// TODO Get availableTaskTypes from executor pool
List<String> availableTaskTypes = Arrays.asList("ADDITION", "ROBOT");
if (!availableTaskTypes.contains(command.getTaskType().getValue())) {
if (!getAllExecutorInExecutorPoolByTypePort.doesExecutorTypeExist(command.getTaskType())) {
return false;
}