added unit tests for the roster #89
|
@ -1,18 +1,14 @@
|
|||
package ch.unisg.roster;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository;
|
||||
import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort;
|
||||
import ch.unisg.roster.roster.domain.Roster;
|
||||
import ch.unisg.roster.roster.domain.RosterItem;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
@ -30,20 +26,14 @@ public class RosterApplication {
|
|||
|
||||
private static ConfigurableEnvironment ENVIRONMENT;
|
||||
|
||||
@Autowired
|
||||
private LoadRosterItemPort loadRosterItemPort;
|
||||
private static LoadRosterItemPort loadRosterItemPort;
|
||||
|
||||
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);
|
||||
ENVIRONMENT = rosterApp.run(args).getEnvironment();
|
||||
bootstrapMarketplaceWithMqtt();
|
||||
initialiseRoster();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,9 +52,9 @@ public class RosterApplication {
|
|||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void initialiseRoster(){
|
||||
Roster.getInstance().initialiseRoster(loadRosterItemPort.loadAllRosterItems());
|
||||
private static void initialiseRoster(){
|
||||
List<RosterItem> rosterItemList = loadRosterItemPort.loadAllRosterItems();
|
||||
Roster.getInstance().initialiseRoster(rosterItemList);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,9 +26,9 @@ public class AddNewAssignmentToRosterServiceSystemTest {
|
|||
@Test
|
||||
void addNewAssignmentToRosterService() throws JSONException {
|
||||
|
||||
String rosterItemId = "TEST-ID";
|
||||
String executorType = "TEST-TYPE";
|
||||
String executorURI = "TEST-URI";
|
||||
String rosterItemId = "test-id";
|
||||
String executorType = "test-type";
|
||||
String executorURI = "test-URI";
|
||||
|
||||
ResponseEntity response = whenAddNewAssignmentToRoster(rosterItemId, executorType, executorURI);
|
||||
|
||||
|
@ -65,7 +65,7 @@ public class AddNewAssignmentToRosterServiceSystemTest {
|
|||
HttpEntity<String> request = new HttpEntity<>(jsonPayLoad, headers);
|
||||
|
||||
return restTemplate.exchange(
|
||||
"/task/apply/",
|
||||
"/tasks/apply/",
|
||||
HttpMethod.POST,
|
||||
request,
|
||||
Object.class
|
||||
|
|
|
@ -36,9 +36,9 @@ public class ApplyForTaskControllerTest {
|
|||
@Test
|
||||
void testApplyForTask() throws Exception{
|
||||
|
||||
String executorType = "TEST-TYPE";
|
||||
String executorURI = "TEST-URI";
|
||||
String taskId = "TEST-ID";
|
||||
String executorType = "test-type";
|
||||
String executorURI = "test-uri";
|
||||
String taskId = "test-id";
|
||||
|
||||
String jsonPayLoad = new JSONObject()
|
||||
.put("executorType", executorType )
|
||||
|
@ -56,15 +56,14 @@ public class ApplyForTaskControllerTest {
|
|||
Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand))
|
||||
.thenReturn(taskStub);
|
||||
|
||||
|
||||
mockMvc.perform(post("/task/apply/")
|
||||
mockMvc.perform(post("tasks/apply/")
|
||||
.contentType("application/json")
|
||||
.content(jsonPayLoad))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
//TODO: No idea why this does not work yet
|
||||
then(applyForTaskUseCase).should()
|
||||
.applyForTask(new ApplyForTaskCommand(new ExecutorType(executorType), new ExecutorURI(executorURI)));
|
||||
.applyForTask(new ApplyForTaskCommand(new ExecutorType(executorType),
|
||||
new ExecutorURI(executorURI)));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,21 +21,21 @@ public class RosterPersistenceAdapterTest {
|
|||
private RosterRepository rosterRepository;
|
||||
|
||||
@Autowired
|
||||
private RosterPersistenceAdapter adapterUnderTest;
|
||||
private RosterPersistenceAdapter adapterunderTest;
|
||||
|
||||
@Test
|
||||
void addsNewRosterItem(){
|
||||
|
||||
String taskId = "TEST-ID";
|
||||
String executorType = "TEST-TYPE";
|
||||
String executorURI = "TEST-URI";
|
||||
String taskId = "test-id";
|
||||
String executorType = "test-type";
|
||||
String executorURI = "test-uri";
|
||||
|
||||
RosterItem testRosterItem = new RosterItem(
|
||||
taskId,
|
||||
executorType,
|
||||
new ExecutorURI(executorURI)
|
||||
);
|
||||
adapterUnderTest.addRosterItem(testRosterItem);
|
||||
adapterunderTest.addRosterItem(testRosterItem);
|
||||
|
||||
MongoRosterDocument retrievedDoc = rosterRepository.findByTaskId(taskId);
|
||||
|
||||
|
@ -48,14 +48,14 @@ public class RosterPersistenceAdapterTest {
|
|||
@Test
|
||||
void retrievesRosterItem(){
|
||||
|
||||
String taskId = "TEST-ID";
|
||||
String executorType = "TEST-TYPE";
|
||||
String executorURI = "TEST-URI";
|
||||
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);
|
||||
RosterItem retrievedRosterItem = adapterunderTest.loadRosterItem(taskId);
|
||||
|
||||
assertThat(retrievedRosterItem.getTaskID()).isEqualTo(taskId);
|
||||
assertThat(retrievedRosterItem.getTaskType()).isEqualTo(executorType);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
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.ApplyForTaskCommand;
|
||||
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.event.NewTaskEvent;
|
||||
import ch.unisg.roster.roster.domain.event.TaskAssignedEvent;
|
||||
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
|
@ -39,11 +37,9 @@ public class AddNewAssignmentToRosterServiceTest {
|
|||
@Test
|
||||
void assigningSucceeds(){
|
||||
|
||||
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);
|
||||
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());
|
||||
|
||||
|
@ -61,14 +57,14 @@ public class AddNewAssignmentToRosterServiceTest {
|
|||
RosterItem rosterItem = Mockito.mock(RosterItem.class);
|
||||
given(rosterItem.getTaskID()).willReturn(taskId);
|
||||
given(rosterItem.getTaskType()).willReturn(taskType);
|
||||
given(rosterItem.getExecutorURI()).willReturn(new ExecutorURI(executorURI));
|
||||
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()).willReturn(new ExecutorType(taskType));
|
||||
given(task.getTaskType().getValue()).willReturn(taskType);
|
||||
given(task.getInputData()).willReturn(inputData);
|
||||
return task;
|
||||
}
|
||||
|
|
|
@ -18,23 +18,13 @@ public class RosterTest {
|
|||
Roster roster = Roster.getInstance();
|
||||
Collection<RosterItem> rosterMap = roster.getRosterMap();
|
||||
rosterMap.clear();
|
||||
Collection<ArrayList<Task>> queues = roster.getAllTasksFromQueue();
|
||||
queues.clear();
|
||||
roster.addTaskToQueue(new Task("TEST-ID", "TEST-TYPE"));
|
||||
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.iterator().next().getTaskID()).isEqualTo("TEST-ID");
|
||||
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);
|
||||
assertThat(rosterMap.iterator().next().getTaskID()).isEqualTo("test-id");
|
||||
assertThat(rosterMap.iterator().next().getTaskType()).isEqualTo("test-type");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -42,11 +32,10 @@ public class RosterTest {
|
|||
Roster roster = Roster.getInstance();
|
||||
Collection<ArrayList<Task>> queues = roster.getAllTasksFromQueue();
|
||||
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(queues.size()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user
Let's make sure this is needed