Code clean up and test fixes

This commit is contained in:
reynisson
2021-12-23 14:14:32 +00:00
parent 4bbe71729c
commit edc2279434
43 changed files with 216 additions and 261 deletions

View File

@@ -29,7 +29,7 @@ public class ApplyForTaskWebController {
**/
@PostMapping(path = "/task/apply", consumes = {"application/json"})
public Task applyForTask (@RequestBody ExecutorInfo executorInfo) {
logger.info("Roster | Execuor applying for task");
logger.info("Roster | Executor applying for task");
ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(),
executorInfo.getExecutorURI());

View File

@@ -5,5 +5,5 @@ import org.springframework.stereotype.Repository;
@Repository
public interface RosterRepository extends MongoRepository<MongoRosterDocument,String>{
public MongoRosterDocument findByTaskId(String taskId);
MongoRosterDocument findByTaskId(String taskId);
}

View File

@@ -12,10 +12,10 @@ public class Roster {
private static final Roster roster = new Roster();
// Queues which hold all the tasks which need to be assigned | Will be replaced by message queue later
private HashMap<String, ArrayList<Task>> queues = new HashMap<>();
private final HashMap<String, ArrayList<Task>> queues = new HashMap<>();
// Roster witch holds information about which executor is assigned to a task
private HashMap<String, RosterItem> rosterMap = new HashMap<>();
private final HashMap<String, RosterItem> rosterMap = new HashMap<>();
Logger logger = Logger.getLogger(Roster.class.getName());

View File

@@ -6,13 +6,13 @@ import lombok.Getter;
public class RosterItem {
@Getter
private String taskID;
private final String taskID;
@Getter
private String taskType;
private final String taskType;
@Getter
private ExecutorURI executorURI;
private final ExecutorURI executorURI;
public RosterItem(String taskID, String taskType, ExecutorURI executorURI) {
this.taskID = taskID;

View File

@@ -28,41 +28,40 @@ public class AddNewAssignmentToRosterServiceSystemTest {
@Test
void addNewAssignmentToRosterService() throws JSONException {
String rosterItemId = "TEST-ID";
String taskId = "TEST-ID";
String executorType = "TEST-TYPE";
String inputData = "TEST-DATA";
String executorURI = "TEST-URI";
ResponseEntity response = whenAddNewAssignmentToRoster(rosterItemId, executorType, executorURI);
System.out.println(response.getBody().toString());
ResponseEntity response = whenAddNewAssignmentToRoster(taskId, executorType, inputData, executorURI);
JSONObject responseJson = new JSONObject(response.getBody().toString());
String respExecutorType = responseJson.getString("executorType");
String respExecutorURI = responseJson.getString("executorURI");
String respTaskId = responseJson.getString("taskID");
String respTaskType = responseJson.getJSONObject("taskType").getString("value");
String respInputData = responseJson.getString("inputData");
then(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
then(respExecutorType).isEqualTo(executorType);
then(respExecutorURI).isEqualTo(executorURI);
then(response.getStatusCode()).isEqualTo(HttpStatus.OK);
then(respTaskId).isEqualTo(respTaskId);
then(respTaskType).isEqualTo(executorType);
then(respInputData).isEqualTo(inputData);
then(Roster.getInstance().getRosterMap().size()).isEqualTo(1);
}
private ResponseEntity whenAddNewAssignmentToRoster(
String rosterItemId,
String taskId,
String executorType,
String inputData,
String executorURI) throws JSONException {
Roster roster = Roster.getInstance();
roster.getRosterMap().clear();
roster.addTaskToQueue(new Task(rosterItemId, new ExecutorType(executorType), executorURI));
roster.addTaskToQueue(new Task(taskId, new ExecutorType(executorType), inputData));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
String jsonPayLoad = new JSONObject()
.put("rosterItemId", rosterItemId)
.put("executorType", executorType)
.put("executorURI", executorURI)
.toString();

View File

@@ -5,6 +5,7 @@ import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository;
import ch.unisg.roster.roster.application.port.in.ApplyForTaskCommand;
import ch.unisg.roster.roster.application.port.in.ApplyForTaskUseCase;
import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort;
import ch.unisg.roster.roster.domain.RosterItem;
import ch.unisg.roster.roster.domain.Task;
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
@@ -15,7 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -29,6 +30,9 @@ public class ApplyForTaskWebControllerTest {
@MockBean
private ApplyForTaskUseCase applyForTaskUseCase;
@MockBean
private LoadRosterItemPort loadRosterItemPort;
@MockBean
RosterRepository rosterRepository;
@@ -55,14 +59,13 @@ public class ApplyForTaskWebControllerTest {
Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand))
.thenReturn(taskStub);
mockMvc.perform(post("/task/apply/")
.contentType("application/json")
.content(jsonPayLoad))
.andExpect(status().is2xxSuccessful());
then(applyForTaskUseCase).should()
.applyForTask(new ApplyForTaskCommand(new ExecutorType(executorType), new ExecutorURI(executorURI)));
.applyForTask(eq(new ApplyForTaskCommand(new ExecutorType(executorType), new ExecutorURI(executorURI))));
}
}

View File

@@ -38,7 +38,6 @@ public class RosterPersistenceAdapterTest {
new ExecutorURI(executorURI)
);
adapterUnderTest.addRosterItem(testRosterItem);
MongoRosterDocument retrievedDoc = rosterRepository.findByTaskId(taskId);
@@ -46,7 +45,6 @@ public class RosterPersistenceAdapterTest {
assertThat(retrievedDoc.taskId).isEqualTo(taskId);
assertThat(retrievedDoc.executorURI).isEqualTo(executorURI);
assertThat(retrievedDoc.taskType).isEqualTo(executorType);
}
@Test

View File

@@ -41,7 +41,6 @@ public class AddNewAssignmentToRosterServiceTest {
Task newTask = givenATaskWithIdAndType("TEST-ID", "TEST-TYPE", "TEST-INPUT");
RosterItem newRosterItem = givenARosterItemWithIdAndTypeAndExecutorUri("TEST-ID", "TEST-TYPE", "TEST-URI");
// TODO Add task to queue
Roster roster = Roster.getInstance();
roster.addTaskToQueue(newTask);
@@ -50,6 +49,8 @@ public class AddNewAssignmentToRosterServiceTest {
Task assignedTask = applyForTaskService.applyForTask(applyForTaskCommand);
assertThat(assignedTask).isNotNull();
// Checks that the first (and only) task type has no tasks
assertThat(roster.getAllTasksFromQueue().stream().findFirst().get()).hasSize(0);
then(taskAssignedEventPort).should(times(1))
.publishTaskAssignedEvent(any(TaskAssignedEvent.class));

View File

@@ -28,13 +28,8 @@ public class RosterTest {
assertThat(rosterMap.iterator().next().getTaskType()).isEqualTo("TEST-TYPE");
assertThat(rosterMap.iterator().next().getExecutorURI().getValue().toString()).isEqualTo("TEST-URI");
assertThat(task.getTaskType().getValue().toString()).isEqualTo("TEST-TYPE");
assertThat(task.getTaskType().getValue()).isEqualTo("TEST-TYPE");
assertThat(task.getTaskID()).isEqualTo("TEST-ID");
boolean empty_queue = roster.deleteTask("TEST-ID");
// TODO test that the task was removed from the Queue similar to below --> I don't know if it actually gets deleted or not
//assertThat(empty_queue).isEqualTo(true);
//assertThat(queues.size()).isEqualTo(0);
}
@Test
@@ -45,8 +40,8 @@ public class RosterTest {
roster.addTaskToQueue(new Task("TEST-ID", "TEST-TYPE"));
boolean test = roster.deleteTask("TEST-ID");
// TODO Fix assert for queue
assertThat(test).isEqualTo(true);
assertThat(queues.size()).isEqualTo(1);
// Checks that the first (and only) task type has no tasks
assertThat(queues.stream().findFirst().get()).hasSize(0);
}
}