diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskController.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskController.java deleted file mode 100644 index 144d557..0000000 --- a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskController.java +++ /dev/null @@ -1,33 +0,0 @@ -package ch.unisg.roster.roster.adapter.in.web; - -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -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.Task; - -@RestController -public class ApplyForTaskController { - private final ApplyForTaskUseCase applyForTaskUseCase; - - public ApplyForTaskController(ApplyForTaskUseCase applyForTaskUseCase) { - this.applyForTaskUseCase = applyForTaskUseCase; - } - - // TODO fix return type - /** - * Checks if task is available for the requesting executor. - * @return a task or null if no task found - **/ - @PostMapping(path = "/task/apply", consumes = {"application/json"}) - public Task applyForTask(@RequestBody ExecutorInfo executorInfo) { - - ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(), - executorInfo.getExecutorURI()); - - return applyForTaskUseCase.applyForTask(command); - } -} diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskWebController.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskWebController.java new file mode 100644 index 0000000..8fb10ee --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskWebController.java @@ -0,0 +1,65 @@ +package ch.unisg.roster.roster.adapter.in.web; + +import ch.unisg.roster.roster.domain.Roster; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.json.JSONObject; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +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.Task; +import org.springframework.web.server.ResponseStatusException; + +import javax.validation.ConstraintViolationException; + +@RestController +public class ApplyForTaskWebController { + private final ApplyForTaskUseCase applyForTaskUseCase; + + public ApplyForTaskWebController(ApplyForTaskUseCase applyForTaskUseCase) { + this.applyForTaskUseCase = applyForTaskUseCase; + } + + // TODO fix return type + /** + * Checks if task is available for the requesting executor. + * @return a task or null if no task found + **/ + @PostMapping(path = "/task/apply", consumes = {"application/json"}) + public ResponseEntity applyForTask (@RequestBody ExecutorInfo executorInfo) { + + ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(), + executorInfo.getExecutorURI()); + + Task task = applyForTaskUseCase.applyForTask(command); + + if (task == null) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + + try { + + String executorType = command.getTaskType().toString(); + String executorURI = command.getExecutorURI().toString(); + + String jsonPayLoad = new JSONObject() + .put("executorType", executorType) + .put("executorURI", executorURI) + .toString(); + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add("Content-Type", "application/json"); + + return new ResponseEntity<>(jsonPayLoad, responseHeaders, HttpStatus.OK); + } catch (ConstraintViolationException e) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); + } + + } +} 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 f5a7d8c..0e101ad 100644 --- a/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java +++ b/roster/src/test/java/ch/unisg/roster/roster/AddNewAssignmentToRosterServiceSystemTest.java @@ -3,6 +3,8 @@ package ch.unisg.roster.roster; import ch.unisg.roster.roster.application.port.in.AddRosterItemPort; import ch.unisg.roster.roster.domain.Roster; +import ch.unisg.roster.roster.domain.Task; +import ch.unisg.roster.roster.domain.valueobject.ExecutorType; import org.json.JSONObject; import org.json.JSONException; import org.junit.jupiter.api.Test; @@ -28,10 +30,12 @@ public class AddNewAssignmentToRosterServiceSystemTest { String rosterItemId = "TEST-ID"; String executorType = "TEST-TYPE"; - String executorURI = "http://localhost:6969"; + String executorURI = "TEST-URI"; ResponseEntity response = whenAddNewAssignmentToRoster(rosterItemId, executorType, executorURI); + System.out.println(response.getBody().toString()); + response.getBody(). JSONObject responseJson = new JSONObject(response.getBody().toString()); String respRosterItemId = responseJson.getString("rosterItemId"); String respExecutorType = responseJson.getString("executorType"); @@ -51,7 +55,10 @@ public class AddNewAssignmentToRosterServiceSystemTest { String executorType, String executorURI) throws JSONException { - Roster.getInstance().getRosterMap().clear(); + Roster roster = Roster.getInstance(); + roster.getRosterMap().clear(); + roster.addTaskToQueue(new Task(rosterItemId, new ExecutorType(executorType), executorURI)); + HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); 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/ApplyForTaskWebControllerTest.java similarity index 94% rename from roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskControllerTest.java rename to roster/src/test/java/ch/unisg/roster/roster/adapter/in/web/ApplyForTaskWebControllerTest.java index c06d96d..bcbd45c 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/ApplyForTaskWebControllerTest.java @@ -5,7 +5,6 @@ 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; @@ -21,8 +20,8 @@ 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 { +@WebMvcTest(controllers = ApplyForTaskWebController.class) +public class ApplyForTaskWebControllerTest { @Autowired private MockMvc mockMvc;