diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-07-25 20:36:11 +1000 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-07-25 20:36:11 +1000 |
| commit | aa9bdd514ab90d0da0391b879255a22c29450e9a (patch) | |
| tree | 9ddd1de0ab7e376ead06f55bdb32a6190d3647d5 /src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java | |
| parent | ec81d98e90f9fdb4dd12138a365fbbbb3a8efa5f (diff) | |
Validate import payload and return create/update stats
- Add validation to /import payload
- Move import logic to service bean
- Track whether entities have been created or update
- Report number of created and updated entities as return value for the
import endpoint
- Add some test coverage to exercise the validators
Diffstat (limited to 'src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java')
| -rw-r--r-- | src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java | 266 |
1 files changed, 247 insertions, 19 deletions
diff --git a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java index b486812..c0358c9 100644 --- a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -2,11 +2,11 @@ package com.stileeducation.markr.controller; import com.stileeducation.markr.MarkrApplication; import com.stileeducation.markr.converter.XmlMarkrMessageConverter; +import com.stileeducation.markr.dto.ImportResponseDTO; 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; @@ -17,29 +17,26 @@ import org.springframework.http.*; import org.springframework.test.context.ActiveProfiles; import java.io.IOException; -import java.io.InputStream; import java.io.StringWriter; import static com.stileeducation.markr.controller.TestResultsController.IMPORT_ENDPOINT; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = MarkrApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") public class TestResultsControllerTest { - private static MCQTestResultsDTO sampleResults; + private static String validPayload; + private static String invalidPayload; @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); - } + static void setup() throws IOException { + validPayload = new String(new ClassPathResource("sample-results.xml").getInputStream().readAllBytes(), UTF_8); + invalidPayload = new String(new ClassPathResource("invalid-payload.xml").getInputStream().readAllBytes(), UTF_8); } private static String toXmlString(MCQTestResultsDTO mcqTestResultsDTO) throws JAXBException { @@ -51,36 +48,267 @@ public class TestResultsControllerTest { } @Test - void testPost() throws Exception { + void testSupportedMediaType() throws Exception { // Given - String requestXml = toXmlString(sampleResults); - HttpHeaders headers = new HttpHeaders(); headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(validPayload, headers); // 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); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @Test void testUnsupportedMediaType() throws Exception { // Given - MCQTestResultsDTO request = new MCQTestResultsDTO(); - String requestXml = toXmlString(request); - HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_XML); + HttpEntity<String> entity = new HttpEntity<>(validPayload, headers); // 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 + void testInvalidImport() throws Exception { + // Given + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<String> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, String.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + } + + @Test + void testInvalidImport_MissingFirstName() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name></first-name> + <last-name>Alysander</last-name> + <student-number>002299</student-number> + <test-id>9863</test-id> + <summary-marks available="20" obtained="13"/> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_MissingLastName() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name></last-name> + <student-number>002299</student-number> + <test-id>9863</test-id> + <summary-marks available="20" obtained="13"/> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_MissingStudentNumber() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name>Alysander</last-name> + <student-number></student-number> + <test-id>9863</test-id> + <summary-marks available="20" obtained="13"/> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_MissingTestId() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name>Alysander</last-name> + <student-number>002299</student-number> + <test-id></test-id> + <summary-marks available="20" obtained="13"/> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_MissingSummaryMarks() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name>Alysander</last-name> + <student-number>002299</student-number> + <test-id>9863</test-id> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_MissingSummaryMarksAvailable() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name>Alysander</last-name> + <student-number>002299</student-number> + <test-id>9863</test-id> + <summary-marks obtained="13"/> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_MissingSummaryMarksObtained() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name>Alysander</last-name> + <student-number>002299</student-number> + <test-id>9863</test-id> + <summary-marks available="20"/> + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid payload"); + } + + @Test + void testInvalidImport_InvalidXmlFormat() throws Exception { + // Given + String invalidPayload = """ + <mcq-test-results> + <mcq-test-result scanned-on="2017-12-04T12:12:10+11:00"> + <first-name>KJ</first-name> + <last-name>Alysander</last-name> + <student-number>002299</student-number> + <test-id>9863</test-id> + <summary-marks available="20" obtained="1 + </mcq-test-result> + </mcq-test-results> + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity<String> entity = new HttpEntity<>(invalidPayload, headers); + + // When + ResponseEntity<ImportResponseDTO> response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getMessage()).isEqualTo("Invalid XML payload"); + } }
\ No newline at end of file |
