diff options
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java | 14 | ||||
| -rw-r--r-- | src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java | 62 |
2 files changed, 55 insertions, 21 deletions
diff --git a/src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java b/src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java deleted file mode 100644 index 470c1f9..0000000 --- a/src/test/java/com/stileeducation/markr/config/XmlMapperConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -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 index 41a6837..3d21c25 100644 --- a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -1,31 +1,74 @@ package com.stileeducation.markr.controller; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.stileeducation.markr.converter.XmlMarkrMessageConverter; import com.stileeducation.markr.dto.AggregatedTestResultsDTO; import com.stileeducation.markr.dto.MCQTestResultsDTO; +import jakarta.xml.bind.JAXBContext; +import jakarta.xml.bind.JAXBException; +import jakarta.xml.bind.Marshaller; +import jakarta.xml.bind.Unmarshaller; +import org.junit.jupiter.api.BeforeAll; 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.core.io.ClassPathResource; import org.springframework.http.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + 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 { - @Autowired - private XmlMapper xmlMapper; + private static MCQTestResultsDTO sampleResults; @Autowired private TestRestTemplate restTemplate; + @BeforeAll + static void setup() throws JAXBException, IOException { + ClassPathResource resource = new ClassPathResource("sample-results.xml"); + try (InputStream inputStream = resource.getInputStream()) { + JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + sampleResults = (MCQTestResultsDTO) unmarshaller.unmarshal(inputStream); + } + } + + private static String toXmlString(MCQTestResultsDTO mcqTestResultsDTO) throws JAXBException { + JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class); + Marshaller marshaller = jaxbContext.createMarshaller(); + StringWriter stringWriter = new StringWriter(); + marshaller.marshal(mcqTestResultsDTO, stringWriter); + return stringWriter.toString(); + } + @Test - public void testImport() throws Exception { + void testPost() throws Exception { + // Given + String requestXml = toXmlString(sampleResults); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + + // When + HttpEntity<String> entity = new HttpEntity<>(requestXml, headers); + ResponseEntity<String> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, String.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); + } + + @Test + void testUnsupportedMediaType() throws Exception { // Given MCQTestResultsDTO request = new MCQTestResultsDTO(); - String requestXml = xmlMapper.writeValueAsString(request); + String requestXml = toXmlString(request); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_XML); @@ -35,7 +78,7 @@ public class TestResultsControllerTest { ResponseEntity<String> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, String.class); // Then - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE); } @Test @@ -45,7 +88,11 @@ public class TestResultsControllerTest { String url = "/results/" + testId + "/aggregate"; // When - ResponseEntity<AggregatedTestResultsDTO> response = restTemplate.getForEntity(url, AggregatedTestResultsDTO.class); + ResponseEntity<AggregatedTestResultsDTO> response = + restTemplate + .getForEntity( + url, + AggregatedTestResultsDTO.class); // Then assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); @@ -62,4 +109,5 @@ public class TestResultsControllerTest { assertThat(results.getCount()).isEqualTo(1); } + } |
