diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-07-25 12:07:39 +1000 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-07-25 12:07:39 +1000 |
| commit | 7429ce2c63f467cc6e0f742c644b53dcdd5a635b (patch) | |
| tree | a927d91137f761065661b06e6da92ef17869d8df /src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java | |
| parent | 1df19fd955eb951f5d31fb205963b2f4e794c039 (diff) | |
Specify Spring profile for tests and set configuration class context
Diffstat (limited to 'src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java')
| -rw-r--r-- | src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java | 143 |
1 files changed, 58 insertions, 85 deletions
diff --git a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java index 3d21c25..b486812 100644 --- a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -1,7 +1,7 @@ package com.stileeducation.markr.controller; +import com.stileeducation.markr.MarkrApplication; 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; @@ -14,6 +14,7 @@ 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 org.springframework.test.context.ActiveProfiles; import java.io.IOException; import java.io.InputStream; @@ -22,92 +23,64 @@ 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) +@SpringBootTest(classes = MarkrApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") public class TestResultsControllerTest { - private static MCQTestResultsDTO sampleResults; + private static MCQTestResultsDTO sampleResults; - @Autowired - private TestRestTemplate restTemplate; + @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); + @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 - 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 = toXmlString(request); - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_XML); - - // When - HttpEntity<String> entity = new HttpEntity<>(requestXml, headers); - ResponseEntity<String> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, String.class); - - // Then - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE); - } - - @Test - public void testAggregate() throws Exception { - // Given - String testId = "123"; // Example test ID - String url = "/results/" + testId + "/aggregate"; - - // When - ResponseEntity<AggregatedTestResultsDTO> response = - restTemplate - .getForEntity( - url, - AggregatedTestResultsDTO.class); - - // Then - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - - AggregatedTestResultsDTO results = response.getBody(); - assertThat(results).isNotNull(); - assertThat(results.getMean()).isEqualTo(65.0); - assertThat(results.getStddev()).isEqualTo(0.0); - assertThat(results.getMin()).isEqualTo(65.0); - assertThat(results.getMax()).isEqualTo(65.0); - assertThat(results.getP25()).isEqualTo(65.0); - assertThat(results.getP50()).isEqualTo(65.0); - assertThat(results.getP75()).isEqualTo(65.0); - assertThat(results.getCount()).isEqualTo(1); - } - - -} + + 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 + 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 = toXmlString(request); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_XML); + + // When + HttpEntity<String> entity = new HttpEntity<>(requestXml, headers); + ResponseEntity<String> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, String.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE); + } + +}
\ No newline at end of file |
