added unit tests for the roster

This commit is contained in:
julius.lautz 2021-11-25 10:59:47 +01:00 committed by reynisson
parent 767dcfa82a
commit d732ab47d7
6 changed files with 36 additions and 62 deletions

View File

@ -1,18 +1,14 @@
package ch.unisg.roster; package ch.unisg.roster;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository; import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository;
import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort; import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort;
import ch.unisg.roster.roster.domain.Roster; import ch.unisg.roster.roster.domain.Roster;
import ch.unisg.roster.roster.domain.RosterItem; import ch.unisg.roster.roster.domain.RosterItem;
import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
@ -30,20 +26,14 @@ public class RosterApplication {
private static ConfigurableEnvironment ENVIRONMENT; private static ConfigurableEnvironment ENVIRONMENT;
@Autowired private static LoadRosterItemPort loadRosterItemPort;
private LoadRosterItemPort loadRosterItemPort;
public static void main(String[] args) { public static void main(String[] args) {
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SpringApplication rosterApp = new SpringApplication(RosterApplication.class); SpringApplication rosterApp = new SpringApplication(RosterApplication.class);
ENVIRONMENT = rosterApp.run(args).getEnvironment(); ENVIRONMENT = rosterApp.run(args).getEnvironment();
bootstrapMarketplaceWithMqtt(); bootstrapMarketplaceWithMqtt();
initialiseRoster();
} }
/** /**
@ -62,9 +52,9 @@ public class RosterApplication {
} }
} }
@PostConstruct private static void initialiseRoster(){
private void initialiseRoster(){ List<RosterItem> rosterItemList = loadRosterItemPort.loadAllRosterItems();
Roster.getInstance().initialiseRoster(loadRosterItemPort.loadAllRosterItems()); Roster.getInstance().initialiseRoster(rosterItemList);
} }
} }

View File

@ -26,9 +26,9 @@ public class AddNewAssignmentToRosterServiceSystemTest {
@Test @Test
void addNewAssignmentToRosterService() throws JSONException { void addNewAssignmentToRosterService() throws JSONException {
String rosterItemId = "TEST-ID"; String rosterItemId = "test-id";
String executorType = "TEST-TYPE"; String executorType = "test-type";
String executorURI = "TEST-URI"; String executorURI = "test-URI";
ResponseEntity response = whenAddNewAssignmentToRoster(rosterItemId, executorType, executorURI); ResponseEntity response = whenAddNewAssignmentToRoster(rosterItemId, executorType, executorURI);
@ -65,7 +65,7 @@ public class AddNewAssignmentToRosterServiceSystemTest {
HttpEntity<String> request = new HttpEntity<>(jsonPayLoad, headers); HttpEntity<String> request = new HttpEntity<>(jsonPayLoad, headers);
return restTemplate.exchange( return restTemplate.exchange(
"/task/apply/", "/tasks/apply/",
HttpMethod.POST, HttpMethod.POST,
request, request,
Object.class Object.class

View File

@ -36,9 +36,9 @@ public class ApplyForTaskControllerTest {
@Test @Test
void testApplyForTask() throws Exception{ void testApplyForTask() throws Exception{
String executorType = "TEST-TYPE"; String executorType = "test-type";
String executorURI = "TEST-URI"; String executorURI = "test-uri";
String taskId = "TEST-ID"; String taskId = "test-id";
String jsonPayLoad = new JSONObject() String jsonPayLoad = new JSONObject()
.put("executorType", executorType ) .put("executorType", executorType )
@ -56,15 +56,14 @@ public class ApplyForTaskControllerTest {
Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand)) Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand))
.thenReturn(taskStub); .thenReturn(taskStub);
mockMvc.perform(post("tasks/apply/")
mockMvc.perform(post("/task/apply/")
.contentType("application/json") .contentType("application/json")
.content(jsonPayLoad)) .content(jsonPayLoad))
.andExpect(status().isCreated()); .andExpect(status().isCreated());
//TODO: No idea why this does not work yet
then(applyForTaskUseCase).should() then(applyForTaskUseCase).should()
.applyForTask(new ApplyForTaskCommand(new ExecutorType(executorType), new ExecutorURI(executorURI))); .applyForTask(new ApplyForTaskCommand(new ExecutorType(executorType),
new ExecutorURI(executorURI)));
} }
} }

View File

@ -21,21 +21,21 @@ public class RosterPersistenceAdapterTest {
private RosterRepository rosterRepository; private RosterRepository rosterRepository;
@Autowired @Autowired
private RosterPersistenceAdapter adapterUnderTest; private RosterPersistenceAdapter adapterunderTest;
@Test @Test
void addsNewRosterItem(){ void addsNewRosterItem(){
String taskId = "TEST-ID"; String taskId = "test-id";
String executorType = "TEST-TYPE"; String executorType = "test-type";
String executorURI = "TEST-URI"; String executorURI = "test-uri";
RosterItem testRosterItem = new RosterItem( RosterItem testRosterItem = new RosterItem(
taskId, taskId,
executorType, executorType,
new ExecutorURI(executorURI) new ExecutorURI(executorURI)
); );
adapterUnderTest.addRosterItem(testRosterItem); adapterunderTest.addRosterItem(testRosterItem);
MongoRosterDocument retrievedDoc = rosterRepository.findByTaskId(taskId); MongoRosterDocument retrievedDoc = rosterRepository.findByTaskId(taskId);
@ -48,14 +48,14 @@ public class RosterPersistenceAdapterTest {
@Test @Test
void retrievesRosterItem(){ void retrievesRosterItem(){
String taskId = "TEST-ID"; String taskId = "test-id";
String executorType = "TEST-TYPE"; String executorType = "test-type";
String executorURI = "TEST-URI"; String executorURI = "test-uri";
MongoRosterDocument mongoRosterDocument = new MongoRosterDocument(taskId, executorType, executorURI); MongoRosterDocument mongoRosterDocument = new MongoRosterDocument(taskId, executorType, executorURI);
rosterRepository.insert(mongoRosterDocument); rosterRepository.insert(mongoRosterDocument);
RosterItem retrievedRosterItem = adapterUnderTest.loadRosterItem(taskId); RosterItem retrievedRosterItem = adapterunderTest.loadRosterItem(taskId);
assertThat(retrievedRosterItem.getTaskID()).isEqualTo(taskId); assertThat(retrievedRosterItem.getTaskID()).isEqualTo(taskId);
assertThat(retrievedRosterItem.getTaskType()).isEqualTo(executorType); assertThat(retrievedRosterItem.getTaskType()).isEqualTo(executorType);

View File

@ -1,6 +1,5 @@
package ch.unisg.roster.roster.application.service; package ch.unisg.roster.roster.application.service;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.roster.roster.application.port.in.AddRosterItemPort; 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.ApplyForTaskCommand;
import ch.unisg.roster.roster.application.port.in.DeleteRosterItem; import ch.unisg.roster.roster.application.port.in.DeleteRosterItem;
@ -13,7 +12,6 @@ import ch.unisg.roster.roster.domain.RosterItem;
import ch.unisg.roster.roster.domain.Task; import ch.unisg.roster.roster.domain.Task;
import ch.unisg.roster.roster.domain.event.NewTaskEvent; import ch.unisg.roster.roster.domain.event.NewTaskEvent;
import ch.unisg.roster.roster.domain.event.TaskAssignedEvent; import ch.unisg.roster.roster.domain.event.TaskAssignedEvent;
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -39,11 +37,9 @@ public class AddNewAssignmentToRosterServiceTest {
@Test @Test
void assigningSucceeds(){ void assigningSucceeds(){
Task newTask = givenATaskWithIdAndType("TEST-ID", "TEST-TYPE", "TEST-INPUT"); Task newTask = givenATaskWithIdAndType("test-id", "test-type", "test-input");
RosterItem newRosterItem = givenARosterItemWithIdAndTypeAndExecutorUri("TEST-ID", "TEST-TYPE", "TEST-URI"); RosterItem newRosterItem = givenARosterItemWithIdAndTypeAndExecutorUri("test-id", "test-type", "test-uri");
// TODO Add task to queue
Roster roster = Roster.getInstance();
roster.addTaskToQueue(newTask);
ApplyForTaskCommand applyForTaskCommand = new ApplyForTaskCommand(newTask.getTaskType(), newRosterItem.getExecutorURI()); ApplyForTaskCommand applyForTaskCommand = new ApplyForTaskCommand(newTask.getTaskType(), newRosterItem.getExecutorURI());
@ -61,14 +57,14 @@ public class AddNewAssignmentToRosterServiceTest {
RosterItem rosterItem = Mockito.mock(RosterItem.class); RosterItem rosterItem = Mockito.mock(RosterItem.class);
given(rosterItem.getTaskID()).willReturn(taskId); given(rosterItem.getTaskID()).willReturn(taskId);
given(rosterItem.getTaskType()).willReturn(taskType); given(rosterItem.getTaskType()).willReturn(taskType);
given(rosterItem.getExecutorURI()).willReturn(new ExecutorURI(executorURI)); given(rosterItem.getExecutorURI().getValue()).willReturn(URI.create(executorURI));
return rosterItem; return rosterItem;
} }
private Task givenATaskWithIdAndType(String taskId, String taskType, String inputData) { private Task givenATaskWithIdAndType(String taskId, String taskType, String inputData) {
Task task = Mockito.mock(Task.class); Task task = Mockito.mock(Task.class);
given(task.getTaskID()).willReturn(taskId); given(task.getTaskID()).willReturn(taskId);
given(task.getTaskType()).willReturn(new ExecutorType(taskType)); given(task.getTaskType().getValue()).willReturn(taskType);
given(task.getInputData()).willReturn(inputData); given(task.getInputData()).willReturn(inputData);
return task; return task;
} }

View File

@ -18,23 +18,13 @@ public class RosterTest {
Roster roster = Roster.getInstance(); Roster roster = Roster.getInstance();
Collection<RosterItem> rosterMap = roster.getRosterMap(); Collection<RosterItem> rosterMap = roster.getRosterMap();
rosterMap.clear(); rosterMap.clear();
Collection<ArrayList<Task>> queues = roster.getAllTasksFromQueue(); roster.addTaskToQueue(new Task("test-id", "test-type"));
queues.clear(); Task task = roster.assignTaskToExecutor(new ExecutorType("test-type"), new ExecutorURI("Test-URI"));
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.size()).isEqualTo(1);
assertThat(rosterMap.iterator().next().getTaskID()).isEqualTo("TEST-ID"); assertThat(rosterMap.iterator().next().getTaskID()).isEqualTo("test-id");
assertThat(rosterMap.iterator().next().getTaskType()).isEqualTo("TEST-TYPE"); 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.getTaskID()).isEqualTo("TEST-ID");
boolean empty_queue = roster.deleteTask("TEST-ID", new ExecutorType("TEST-TYPE"));
// 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 @Test
@ -42,11 +32,10 @@ public class RosterTest {
Roster roster = Roster.getInstance(); Roster roster = Roster.getInstance();
Collection<ArrayList<Task>> queues = roster.getAllTasksFromQueue(); Collection<ArrayList<Task>> queues = roster.getAllTasksFromQueue();
queues.clear(); queues.clear();
roster.addTaskToQueue(new Task("TEST-ID", "TEST-TYPE")); roster.addTaskToQueue(new Task("test-id", "test-type"));
boolean test = roster.deleteTask("TEST-ID", new ExecutorType("TEST-TYPE")); boolean test = roster.deleteTask("test-id", new ExecutorType("test-type"));
assertThat(test).isEqualTo(true); assertThat(test).isEqualTo(true);
assertThat(queues.size()).isEqualTo(1);
} }
} }