From d083edc2f3de855239f794d806f2025137519965 Mon Sep 17 00:00:00 2001 From: "julius.lautz" Date: Thu, 25 Nov 2021 10:59:47 +0100 Subject: [PATCH 1/3] added unit tests for the roster --- .../ch/unisg/roster/RosterApplication.java | 2 +- .../ch/unisg/roster/roster/domain/Roster.java | 13 +++- ...ewAssignmentToRosterServiceSystemTest.java | 77 +++++++++++++++++++ .../in/web/ApplyForTaskControllerTest.java | 69 +++++++++++++++++ .../mongodb/RosterPersistenceAdapterTest.java | 64 +++++++++++++++ .../AddNewAssignmentToRosterServiceTest.java | 72 +++++++++++++++++ .../roster/roster/domain/RosterTest.java | 41 ++++++++++ 7 files changed, 333 insertions(+), 5 deletions(-) create mode 100644 roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java create mode 100644 roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java create mode 100644 roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java create mode 100644 roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java create mode 100644 roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java diff --git a/roster/src/main/java/ch/unisg/roster/RosterApplication.java b/roster/src/main/java/ch/unisg/roster/RosterApplication.java index 58cd429..973e8f1 100644 --- a/roster/src/main/java/ch/unisg/roster/RosterApplication.java +++ b/roster/src/main/java/ch/unisg/roster/RosterApplication.java @@ -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) { diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java b/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java index a6b7f19..3893566 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/Roster.java @@ -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 getRosterMap(){ + return rosterMap.values(); + } + + public Collection> getAllTasksFromQueue(){ + return queues.values(); + } + } diff --git a/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java b/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java new file mode 100644 index 0000000..17dc478 --- /dev/null +++ b/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java @@ -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 request = new HttpEntity<>(jsonPayLoad, headers); + + return restTemplate.exchange( + "/tasks/apply/", + HttpMethod.POST, + request, + Object.class + ); + } + + + +} diff --git a/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java b/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java new file mode 100644 index 0000000..59b3e18 --- /dev/null +++ b/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java @@ -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))); + + } +} diff --git a/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java b/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java new file mode 100644 index 0000000..4dba278 --- /dev/null +++ b/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java @@ -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); + } +} diff --git a/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java b/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java new file mode 100644 index 0000000..a24525b --- /dev/null +++ b/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java @@ -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; + } +} + diff --git a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java new file mode 100644 index 0000000..4269759 --- /dev/null +++ b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java @@ -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 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> 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); + } +} From 801a520255bccad44d4998515eccea044fb75555 Mon Sep 17 00:00:00 2001 From: reynisson Date: Thu, 25 Nov 2021 12:02:04 +0100 Subject: [PATCH 2/3] Added TODOs --- .../roster/adapter/in/web/ApplyForTaskControllerTest.java | 1 + .../service/AddNewAssignmentToRosterServiceTest.java | 8 +++++--- .../java/ch/unisg/roster/roster/domain/RosterTest.java | 8 +++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java b/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java index 59b3e18..fc12efd 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java @@ -56,6 +56,7 @@ public class ApplyForTaskControllerTest { Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand)) .thenReturn(taskStub); + // TODO Add slash at the front mockMvc.perform(post("tasks/apply/") .contentType("application/json") .content(jsonPayLoad)) diff --git a/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java b/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java index a24525b..b93dd84 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java @@ -1,5 +1,6 @@ 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; @@ -12,6 +13,7 @@ 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,7 +41,7 @@ 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 ApplyForTaskCommand applyForTaskCommand = new ApplyForTaskCommand(newTask.getTaskType(), newRosterItem.getExecutorURI()); @@ -57,14 +59,14 @@ public class AddNewAssignmentToRosterServiceTest { RosterItem rosterItem = Mockito.mock(RosterItem.class); given(rosterItem.getTaskID()).willReturn(taskId); given(rosterItem.getTaskType()).willReturn(taskType); - given(rosterItem.getExecutorURI().getValue()).willReturn(URI.create(executorURI)); + given(rosterItem.getExecutorURI()).willReturn(new ExecutorURI(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.getTaskType()).willReturn(new ExecutorType(taskType)); given(task.getInputData()).willReturn(inputData); return task; } diff --git a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java index 4269759..ec6f5e1 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java @@ -18,13 +18,18 @@ public class RosterTest { Roster roster = Roster.getInstance(); Collection rosterMap = roster.getRosterMap(); rosterMap.clear(); + // TODO change test-type to upper case 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"); + // TODO test uri + + // TODO test id and type of Task task + + // TODO test that the task was removed from the Queue similar to below } @Test @@ -37,5 +42,6 @@ public class RosterTest { boolean test = roster.deleteTask("test-id", new ExecutorType("test-type")); assertThat(test).isEqualTo(true); + // TODO check that the queue has size 0 } } From 8ab87cda8270158d1ab98a751823b92d57c1f387 Mon Sep 17 00:00:00 2001 From: "julius.lautz" Date: Fri, 26 Nov 2021 13:07:10 +0100 Subject: [PATCH 3/3] fixed some of the tests --- ...ewAssignmentToRosterServiceSystemTest.java | 8 +++--- .../in/web/ApplyForTaskControllerTest.java | 14 +++++----- .../mongodb/RosterPersistenceAdapterTest.java | 18 ++++++------- .../AddNewAssignmentToRosterServiceTest.java | 6 +++-- .../roster/roster/domain/RosterTest.java | 27 +++++++++++-------- .../resources/application-test.properties | 2 ++ 6 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 roster/src/test/resources/application-test.properties diff --git a/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java b/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java index 17dc478..f274aef 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java @@ -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 request = new HttpEntity<>(jsonPayLoad, headers); return restTemplate.exchange( - "/tasks/apply/", + "/task/apply/", HttpMethod.POST, request, Object.class diff --git a/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java b/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java index fc12efd..4b5ee16 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java @@ -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,15 @@ public class ApplyForTaskControllerTest { Mockito.when(applyForTaskUseCase.applyForTask(applyForTaskCommand)) .thenReturn(taskStub); - // TODO Add slash at the front - mockMvc.perform(post("tasks/apply/") + + mockMvc.perform(post("/task/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))); } } diff --git a/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java b/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java index 4dba278..b6bc380 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/adapter/out/persistence/mongodb/RosterPersistenceAdapterTest.java @@ -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); diff --git a/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java b/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java index b93dd84..d089315 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/application/service/AddNewAssignmentToRosterServiceTest.java @@ -39,9 +39,11 @@ public class AddNewAssignmentToRosterServiceTest { @Test void assigningSucceeds(){ - Task newTask = givenATaskWithIdAndType("test-id", "test-type", "test-input"); - RosterItem newRosterItem = givenARosterItemWithIdAndTypeAndExecutorUri("test-id", "test-type", "test-uri"); + 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); ApplyForTaskCommand applyForTaskCommand = new ApplyForTaskCommand(newTask.getTaskType(), newRosterItem.getExecutorURI()); diff --git a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java index ec6f5e1..7fd1081 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/domain/RosterTest.java @@ -18,18 +18,23 @@ public class RosterTest { Roster roster = Roster.getInstance(); Collection rosterMap = roster.getRosterMap(); rosterMap.clear(); - // TODO change test-type to upper case - roster.addTaskToQueue(new Task("test-id", "test-type")); - Task task = roster.assignTaskToExecutor(new ExecutorType("test-type"), new ExecutorURI("Test-URI")); + Collection> 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")); assertThat(rosterMap.size()).isEqualTo(1); - assertThat(rosterMap.iterator().next().getTaskID()).isEqualTo("test-id"); - assertThat(rosterMap.iterator().next().getTaskType()).isEqualTo("test-type"); - // TODO test uri + 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"); - // TODO test id and type of Task task + assertThat(task.getTaskType().getValue().toString()).isEqualTo("TEST-TYPE"); + assertThat(task.getTaskID()).isEqualTo("TEST-ID"); - // TODO test that the task was removed from the Queue similar to below + 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 @@ -37,11 +42,11 @@ public class RosterTest { Roster roster = Roster.getInstance(); Collection> 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); - // TODO check that the queue has size 0 + assertThat(queues.size()).isEqualTo(1); } } diff --git a/roster/src/test/resources/application-test.properties b/roster/src/test/resources/application-test.properties new file mode 100644 index 0000000..e45b53d --- /dev/null +++ b/roster/src/test/resources/application-test.properties @@ -0,0 +1,2 @@ +spring.data.mongodb.uri=mongodb://127.0.0.1:27017 +spring.data.mongodb.database=tapas-tasks