From aec0dff5477cafce865410381a722fedbac04ac1 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Tue, 23 Jul 2024 15:25:17 +1000 Subject: Implement simple /import endpoint and test --- .../com/stileeducation/markr/MarkrApplication.java | 8 ----- .../markr/controller/TestResultsController.java | 32 +++++++++++++++++ .../markr/service/TestResultsService.java | 7 ++++ .../markr/service/TestResultsServiceImpl.java | 15 ++++++++ .../markr/config/XmlMapperConfig.java | 14 ++++++++ .../controller/TestResultsControllerTest.java | 41 ++++++++++++++++++++++ 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/stileeducation/markr/controller/TestResultsController.java create mode 100644 src/main/java/com/stileeducation/markr/service/TestResultsService.java create mode 100644 src/main/java/com/stileeducation/markr/service/TestResultsServiceImpl.java create mode 100644 src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java create mode 100644 src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java (limited to 'src') diff --git a/src/main/java/com/stileeducation/markr/MarkrApplication.java b/src/main/java/com/stileeducation/markr/MarkrApplication.java index c98cfa6..ce721e9 100644 --- a/src/main/java/com/stileeducation/markr/MarkrApplication.java +++ b/src/main/java/com/stileeducation/markr/MarkrApplication.java @@ -2,18 +2,10 @@ package com.stileeducation.markr; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication -@RestController public class MarkrApplication { - @GetMapping(name = "/") - public String index() { - return "Mark - Marking as a Service"; - } - public static void main(String[] args) { SpringApplication.run(MarkrApplication.class, args); } diff --git a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java new file mode 100644 index 0000000..b1a5e63 --- /dev/null +++ b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java @@ -0,0 +1,32 @@ +package com.stileeducation.markr.controller; + +import com.stileeducation.markr.dto.MCQTestResultsDTO; +import com.stileeducation.markr.service.TestResultsService; +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.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +public class TestResultsController { + + public static final String IMPORT_ENDPOINT = "/import"; + + private final TestResultsService testResultsService; + + public TestResultsController(TestResultsService testResultsService) { + this.testResultsService = testResultsService; + } + + // TODO - update to consume text/xml+markr, consider return value + @PostMapping(value = IMPORT_ENDPOINT, consumes = "application/xml", produces = "application/json") + public ResponseEntity handleXmlRequest(@RequestBody MCQTestResultsDTO testResults) { + testResultsService.importTestResults(testResults); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + +} diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java new file mode 100644 index 0000000..810cffa --- /dev/null +++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java @@ -0,0 +1,7 @@ +package com.stileeducation.markr.service; + +import com.stileeducation.markr.dto.MCQTestResultsDTO; + +public interface TestResultsService { + MCQTestResultsDTO importTestResults(MCQTestResultsDTO mcqTestResults); +} diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsServiceImpl.java b/src/main/java/com/stileeducation/markr/service/TestResultsServiceImpl.java new file mode 100644 index 0000000..0d4cdc9 --- /dev/null +++ b/src/main/java/com/stileeducation/markr/service/TestResultsServiceImpl.java @@ -0,0 +1,15 @@ +package com.stileeducation.markr.service; + +import com.stileeducation.markr.dto.MCQTestResultsDTO; +import org.springframework.stereotype.Service; + +@Service +public class TestResultsServiceImpl implements TestResultsService { + + @Override + public MCQTestResultsDTO importTestResults(MCQTestResultsDTO testResults) { + System.out.println(testResults); + return testResults; + } + +} \ No newline at end of file diff --git a/src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java b/src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java new file mode 100644 index 0000000..470c1f9 --- /dev/null +++ b/src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java @@ -0,0 +1,14 @@ +package com.stileeducation.markr.config; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class XmlMapperConfig { + + @Bean + public XmlMapper xmlMapper() { + return new XmlMapper(); + } +} diff --git a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java new file mode 100644 index 0000000..4db9008 --- /dev/null +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -0,0 +1,41 @@ +package com.stileeducation.markr.controller; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.stileeducation.markr.dto.MCQTestResultsDTO; +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.boot.test.web.server.LocalServerPort; +import org.springframework.http.*; + +import static com.stileeducation.markr.controller.TestResultsController.IMPORT_ENDPOINT; +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class TestResultsControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private XmlMapper xmlMapper; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void testImport() throws Exception { + MCQTestResultsDTO request = new MCQTestResultsDTO(); + String requestXml = xmlMapper.writeValueAsString(request); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_XML); + + HttpEntity entity = new HttpEntity<>(requestXml, headers); + ResponseEntity response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, String.class); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); + } + +} -- cgit v1.2.3