added unit tests for the roster

This commit is contained in:
julius.lautz 2021-11-25 10:59:47 +01:00
parent 6c118a82c0
commit d083edc2f3
7 changed files with 333 additions and 5 deletions

View File

@ -26,7 +26,7 @@ public class RosterApplication {
private static ConfigurableEnvironment ENVIRONMENT;
private static final LoadRosterItemPort loadRosterItemPort;
private static LoadRosterItemPort loadRosterItemPort;
public static void main(String[] args) {

View File

@ -1,9 +1,6 @@
package ch.unisg.roster.roster.domain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -90,4 +87,12 @@ public class Roster {
}
}
public Collection<RosterItem> getRosterMap(){
return rosterMap.values();
}
public Collection<ArrayList<Task>> getAllTasksFromQueue(){
return queues.values();
}
}

View File

@ -0,0 +1,77 @@
package ch.unisg.roster.roster;
import ch.unisg.roster.roster.application.port.in.AddRosterItemPort;
import ch.unisg.roster.roster.domain.Roster;
import org.json.JSONObject;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import static org.assertj.core.api.BDDAssertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class AddNewAssignmentToRosterServiceSystemTest {
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private AddRosterItemPort addRosterItemPort;
@Test
void addNewAssignmentToRosterService() throws JSONException {
String rosterItemId = "test-id";
String executorType = "test-type";
String executorURI = "test-URI";
ResponseEntity response = whenAddNewAssignmentToRoster(rosterItemId, executorType, executorURI);
JSONObject responseJson = new JSONObject(response.getBody().toString());
String respRosterItemId = responseJson.getString("rosterItemId");
String respExecutorType = responseJson.getString("executorType");
String respExecutorURI = responseJson.getString("executorURI");
then(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
then(respRosterItemId).isEqualTo(rosterItemId);
then(respExecutorType).isEqualTo(executorType);
then(respExecutorURI).isEqualTo(executorURI);
then(Roster.getInstance().getRosterMap().size()).isEqualTo(1);
}
private ResponseEntity whenAddNewAssignmentToRoster(
String rosterItemId,
String executorType,
String executorURI) throws JSONException {
Roster.getInstance().getRosterMap().clear();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
String jsonPayLoad = new JSONObject()
.put("rosterItemId", rosterItemId)
.put("executorType", executorType)
.put("executorURI", executorURI)
.toString();
HttpEntity<String> request = new HttpEntity<>(jsonPayLoad, headers);
return restTemplate.exchange(
"/tasks/apply/",
HttpMethod.POST,
request,
Object.class
);
}
}

View File

@ -0,0 +1,69 @@
package ch.unisg.roster.roster.adapter.in.web;
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.domain.ExecutorInfo;
import ch.unisg.roster.roster.domain.RosterItem;
import ch.unisg.roster.roster.domain.Task;
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
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.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(controllers = ApplyForTaskController.class)
public class ApplyForTaskControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ApplyForTaskUseCase applyForTaskUseCase;
@MockBean
RosterRepository rosterRepository;
@Test
void testApplyForTask() throws Exception{
String executorType = "test-type";
String executorURI = "test-uri";
String taskId = "test-id";
String jsonPayLoad = new JSONObject()
.put("executorType", executorType )
.put("executorUri",executorURI)
.toString();
RosterItem rosterItem = new RosterItem(taskId, executorType,
new ExecutorURI(executorURI));
Task taskStub = new Task(taskId, executorType);
ApplyForTaskCommand applyForTaskCommand = new ApplyForTaskCommand(new ExecutorType(executorType),
new ExecutorURI(executorURI));
Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand))
.thenReturn(taskStub);
mockMvc.perform(post("tasks/apply/")
.contentType("application/json")
.content(jsonPayLoad))
.andExpect(status().isCreated());
then(applyForTaskUseCase).should()
.applyForTask(new ApplyForTaskCommand(new ExecutorType(executorType),
new ExecutorURI(executorURI)));
}
}

View File

@ -0,0 +1,64 @@
package ch.unisg.roster.roster.adapter.out.persistence.mongodb;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.roster.roster.domain.RosterItem;
import ch.unisg.roster.roster.domain.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureDataMongo
@Import({RosterPersistenceAdapter.class, RosterMapper.class})
public class RosterPersistenceAdapterTest {
@Autowired
private RosterRepository rosterRepository;
@Autowired
private RosterPersistenceAdapter adapterunderTest;
@Test
void addsNewRosterItem(){
String taskId = "test-id";
String executorType = "test-type";
String executorURI = "test-uri";
RosterItem testRosterItem = new RosterItem(
taskId,
executorType,
new ExecutorURI(executorURI)
);
adapterunderTest.addRosterItem(testRosterItem);
MongoRosterDocument retrievedDoc = rosterRepository.findByTaskId(taskId);
assertThat(retrievedDoc.taskId).isEqualTo(taskId);
assertThat(retrievedDoc.executorURI).isEqualTo(executorURI);
assertThat(retrievedDoc.taskType).isEqualTo(executorType);
}
@Test
void retrievesRosterItem(){
String taskId = "test-id";
String executorType = "test-type";
String executorURI = "test-uri";
MongoRosterDocument mongoRosterDocument = new MongoRosterDocument(taskId, executorType, executorURI);
rosterRepository.insert(mongoRosterDocument);
RosterItem retrievedRosterItem = adapterunderTest.loadRosterItem(taskId);
assertThat(retrievedRosterItem.getTaskID()).isEqualTo(taskId);
assertThat(retrievedRosterItem.getTaskType()).isEqualTo(executorType);
assertThat(retrievedRosterItem.getExecutorURI()).isEqualTo(executorURI);
}
}

View File

@ -0,0 +1,72 @@
package ch.unisg.roster.roster.application.service;
import ch.unisg.roster.roster.application.port.in.AddRosterItemPort;
import ch.unisg.roster.roster.application.port.in.ApplyForTaskCommand;
import ch.unisg.roster.roster.application.port.in.DeleteRosterItem;
import ch.unisg.roster.roster.application.port.in.NewTaskCommand;
import ch.unisg.roster.roster.application.port.out.NewTaskEventPort;
import ch.unisg.roster.roster.application.port.out.TaskAssignedEventPort;
import ch.unisg.roster.roster.application.port.out.TaskCompletedEventPort;
import ch.unisg.roster.roster.domain.Roster;
import ch.unisg.roster.roster.domain.RosterItem;
import ch.unisg.roster.roster.domain.Task;
import ch.unisg.roster.roster.domain.event.NewTaskEvent;
import ch.unisg.roster.roster.domain.event.TaskAssignedEvent;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.net.URI;
import java.util.Optional;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;
public class AddNewAssignmentToRosterServiceTest {
//private final NewTaskEventPort newTaskEventPort = Mockito.mock(NewTaskEventPort.class);
private final TaskAssignedEventPort taskAssignedEventPort = Mockito.mock(TaskAssignedEventPort.class);
private final AddRosterItemPort addRosterItemPort = Mockito.mock(AddRosterItemPort.class);
//private final TaskCompletedEventPort taskCompletedEventPort = Mockito.mock(TaskCompletedEventPort.class)
//private final DeleteRosterItem deleteRosterItem = Mockito.mock(DeleteRosterItem.class);
//private final NewTaskService newTaskService = new NewTaskService(newTaskEventPort);
private final ApplyForTaskService applyForTaskService = new ApplyForTaskService(taskAssignedEventPort,addRosterItemPort);
//private final TaskCompletedService taskCompletedService = new TaskCompletedService(taskCompletedEventPort, deleteRosterItem);
@Test
void assigningSucceeds(){
Task newTask = givenATaskWithIdAndType("test-id", "test-type", "test-input");
RosterItem newRosterItem = givenARosterItemWithIdAndTypeAndExecutorUri("test-id", "test-type", "test-uri");
ApplyForTaskCommand applyForTaskCommand = new ApplyForTaskCommand(newTask.getTaskType(), newRosterItem.getExecutorURI());
Task assignedTask = applyForTaskService.applyForTask(applyForTaskCommand);
assertThat(assignedTask).isNotNull();
then(taskAssignedEventPort).should(times(1))
.publishTaskAssignedEvent(any(TaskAssignedEvent.class));
then(addRosterItemPort).should(times(1));
}
private RosterItem givenARosterItemWithIdAndTypeAndExecutorUri(String taskId, String taskType,
String executorURI){
RosterItem rosterItem = Mockito.mock(RosterItem.class);
given(rosterItem.getTaskID()).willReturn(taskId);
given(rosterItem.getTaskType()).willReturn(taskType);
given(rosterItem.getExecutorURI().getValue()).willReturn(URI.create(executorURI));
return rosterItem;
}
private Task givenATaskWithIdAndType(String taskId, String taskType, String inputData) {
Task task = Mockito.mock(Task.class);
given(task.getTaskID()).willReturn(taskId);
given(task.getTaskType().getValue()).willReturn(taskType);
given(task.getInputData()).willReturn(inputData);
return task;
}
}

View File

@ -0,0 +1,41 @@
package ch.unisg.roster.roster.domain;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import static org.assertj.core.api.Assertions.*;
public class RosterTest {
@Test
void addAssignmentToRosterMapTest(){
Roster roster = Roster.getInstance();
Collection<RosterItem> rosterMap = roster.getRosterMap();
rosterMap.clear();
roster.addTaskToQueue(new Task("test-id", "test-type"));
Task task = roster.assignTaskToExecutor(new ExecutorType("test-type"), new ExecutorURI("Test-URI"));
assertThat(rosterMap.size()).isEqualTo(1);
assertThat(rosterMap.iterator().next().getTaskID()).isEqualTo("test-id");
assertThat(rosterMap.iterator().next().getTaskType()).isEqualTo("test-type");
}
@Test
void removeTaskFromQueue(){
Roster roster = Roster.getInstance();
Collection<ArrayList<Task>> queues = roster.getAllTasksFromQueue();
queues.clear();
roster.addTaskToQueue(new Task("test-id", "test-type"));
boolean test = roster.deleteTask("test-id", new ExecutorType("test-type"));
assertThat(test).isEqualTo(true);
}
}