From 39e7e3344bf0827fe9563dc1f08d18084316fd6f Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 26 Jul 2024 14:46:26 +1000 Subject: Simplify entity model builders --- .../java/com/stileeducation/markr/util/StudentBuilder.java | 12 +----------- src/test/java/com/stileeducation/markr/util/TestBuilder.java | 11 ----------- .../com/stileeducation/markr/util/TestResultBuilder.java | 8 ++++---- 3 files changed, 5 insertions(+), 26 deletions(-) (limited to 'src/test') diff --git a/src/test/java/com/stileeducation/markr/util/StudentBuilder.java b/src/test/java/com/stileeducation/markr/util/StudentBuilder.java index 5189f19..897d84a 100644 --- a/src/test/java/com/stileeducation/markr/util/StudentBuilder.java +++ b/src/test/java/com/stileeducation/markr/util/StudentBuilder.java @@ -1,17 +1,13 @@ package com.stileeducation.markr.util; import com.stileeducation.markr.entity.Student; -import com.stileeducation.markr.entity.TestResult; - -import java.util.HashSet; -import java.util.Set; public class StudentBuilder { private Long id; private String firstName; private String lastName; private String studentNumber; - private Set testResults = new HashSet<>(); + public StudentBuilder withId(Long id) { this.id = id; @@ -33,18 +29,12 @@ public class StudentBuilder { return this; } - public StudentBuilder withTestResults(Set testResults) { - this.testResults = testResults; - return this; - } - public Student build() { Student student = new Student(); student.setId(id); student.setFirstName(firstName); student.setLastName(lastName); student.setStudentNumber(studentNumber); - student.setTestResults(testResults); return student; } } \ No newline at end of file diff --git a/src/test/java/com/stileeducation/markr/util/TestBuilder.java b/src/test/java/com/stileeducation/markr/util/TestBuilder.java index 5c513f3..e247f67 100644 --- a/src/test/java/com/stileeducation/markr/util/TestBuilder.java +++ b/src/test/java/com/stileeducation/markr/util/TestBuilder.java @@ -1,16 +1,11 @@ package com.stileeducation.markr.util; import com.stileeducation.markr.entity.Test; -import com.stileeducation.markr.entity.TestResult; - -import java.util.HashSet; -import java.util.Set; public class TestBuilder { private Long id; private String testId; private Integer marksAvailable; - private Set testResults = new HashSet<>(); public TestBuilder withId(Long id) { this.id = id; @@ -27,17 +22,11 @@ public class TestBuilder { return this; } - public TestBuilder withTestResults(Set testResults) { - this.testResults = testResults; - return this; - } - public Test build() { Test test = new Test(); test.setId(id); test.setTestId(testId); test.setMarksAvailable(marksAvailable); - test.setTestResults(testResults); return test; } } diff --git a/src/test/java/com/stileeducation/markr/util/TestResultBuilder.java b/src/test/java/com/stileeducation/markr/util/TestResultBuilder.java index db26e15..70fcecd 100644 --- a/src/test/java/com/stileeducation/markr/util/TestResultBuilder.java +++ b/src/test/java/com/stileeducation/markr/util/TestResultBuilder.java @@ -8,7 +8,7 @@ public class TestResultBuilder { private Long id; private Student student; private Test test; - private Integer marksAwarded; + private Integer marksObtained; public TestResultBuilder withId(Long id) { this.id = id; @@ -25,12 +25,12 @@ public class TestResultBuilder { return this; } - public TestResultBuilder withMarksAwarded(Integer marksAwarded) { - this.marksAwarded = marksAwarded; + public TestResultBuilder withMarksObtained(Integer marksObtained) { + this.marksObtained = marksObtained; return this; } public TestResult build() { - return new TestResult(id, student, test, marksAwarded); + return new TestResult(id, student, test, marksObtained); } } \ No newline at end of file -- cgit v1.2.3 From 7d4d645eb24a30888825f6cdf50cb3eec6ef59f7 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 26 Jul 2024 15:07:54 +1000 Subject: Add test cases for multiple submissions Add test cases which exercise the logic for updating the marks available and marks obtained. Update the service method so that they are transactional and reset the transient 'updated' and 'created' flags after reloading entites to ensure accurate update/create reporting. --- .../controller/TestResultsControllerTest.java | 249 ++++++++++++++++++--- .../markr/service/TestResultsServiceTest.java | 12 +- 2 files changed, 219 insertions(+), 42 deletions(-) (limited to 'src/test') diff --git a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java index efb20d6..cd1f3cb 100644 --- a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -4,6 +4,8 @@ import com.stileeducation.markr.MarkrApplication; import com.stileeducation.markr.converter.XmlMarkrMessageConverter; import com.stileeducation.markr.dto.ImportResponseDTO; import com.stileeducation.markr.dto.MCQTestResultsDTO; +import com.stileeducation.markr.repository.TestResultRepository; +import com.stileeducation.markr.service.TestService; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Marshaller; @@ -33,6 +35,11 @@ public class TestResultsControllerTest { @Autowired private TestRestTemplate restTemplate; + @Autowired + private TestResultRepository testResultRepository; + @Autowired + private TestService testService; + @BeforeAll static void setup() throws IOException { validPayload = new String(new ClassPathResource("sample-results.xml").getInputStream().readAllBytes(), UTF_8); @@ -95,11 +102,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - + - Alysander - 002299 - 9863 + Szukalski + 001122 + 2024001 @@ -123,11 +130,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ + + Szymon - 002299 - 9863 + 001122 + 2024001 @@ -151,11 +158,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ - Alysander + + Szymon + Szukalski - 9863 + 2024001 @@ -179,10 +186,10 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ - Alysander - 002299 + + Szymon + Szukalski + 2024001 @@ -207,11 +214,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ - Alysander - 002299 - 9863 + + Szymon + Szukalski + 001122 + 2024001 """; @@ -234,11 +241,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ - Alysander - 002299 - 9863 + + Szymon + Szukalski + 001122 + 2024001 @@ -262,11 +269,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ - Alysander - 002299 - 9863 + + Szymon + Szukalski + 001122 + 2024001 @@ -290,11 +297,11 @@ public class TestResultsControllerTest { // Given String invalidPayload = """ - - KJ - Alysander - 002299 - 9863 + + Szymon + Szukalski + 001122 + 2024001 + + John + Doe + 002233 + 202402 + + + + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity entity = new HttpEntity<>(validPayload, headers); + + // When + ResponseEntity response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getStatus()).isEqualTo("success"); + assertThat(response.getBody().getMessage()).isEqualTo("Import completed successfully"); + assertThat(response.getBody().getData().getStudentsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getStudentsUpdated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getTestsUpdated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestResultsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getTestResultsUpdated()).isEqualTo(0); + } + + + @Test + void testSubsequentSubmitReportsNoCreatedOrUpdatedEntities() throws Exception { + // Given + String validPayload = """ + + + John + Doe + 003344 + 202403 + + + + John + Doe + 003344 + 202403 + + + + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity entity = new HttpEntity<>(validPayload, headers); + + // Initial Submit + restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // When + ResponseEntity response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getStatus()).isEqualTo("success"); + assertThat(response.getBody().getMessage()).isEqualTo("Import completed successfully"); + assertThat(response.getBody().getData().getStudentsCreated()).isEqualTo(0); + assertThat(response.getBody().getData().getStudentsUpdated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestsCreated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestsUpdated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestResultsCreated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestResultsUpdated()).isEqualTo(0); + } + + + @Test + void testMarksAvailableAndObtainedAreUpdated() throws Exception { + // Given + String payload = """ + + + John + Doe + 004455 + 202404 + + + + John + Doe + 004455 + 202404 + + + + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity entity = new HttpEntity<>(payload, headers); + + // When + ResponseEntity response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getStatus()).isEqualTo("success"); + assertThat(response.getBody().getMessage()).isEqualTo("Import completed successfully"); + + assertThat(response.getBody().getData().getStudentsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getStudentsUpdated()).isEqualTo(0); + + assertThat(response.getBody().getData().getTestsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getTestsUpdated()).isEqualTo(1); + + assertThat(response.getBody().getData().getTestResultsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getTestResultsUpdated()).isEqualTo(1); + } + + @Test + void testMarksAvailableAndObtainedAreNotUpdatedWhenFirstSubmissionIsHigher() throws Exception { + // Given + String payload = """ + + + John + Doe + 005566 + 202405 + + + + John + Doe + 005566 + 202405 + + + + """; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); + HttpEntity entity = new HttpEntity<>(payload, headers); + + // When + ResponseEntity response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); + + // Then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getBody().getStatus()).isEqualTo("success"); + assertThat(response.getBody().getMessage()).isEqualTo("Import completed successfully"); + assertThat(response.getBody().getData().getStudentsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getStudentsUpdated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getTestsUpdated()).isEqualTo(0); + assertThat(response.getBody().getData().getTestResultsCreated()).isEqualTo(1); + assertThat(response.getBody().getData().getTestResultsUpdated()).isEqualTo(0); + } + } \ No newline at end of file diff --git a/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java b/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java index ad4e476..f9a8871 100644 --- a/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java +++ b/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java @@ -89,37 +89,37 @@ class TestResultsServiceTest { testResult1 = new TestResultBuilder() .withStudent(student1) .withTest(test1) - .withMarksAwarded(10) + .withMarksObtained(10) .build(); testResult2 = new TestResultBuilder() .withStudent(student2) .withTest(test1) - .withMarksAwarded(20) + .withMarksObtained(20) .build(); testResult3 = new TestResultBuilder() .withStudent(student3) .withTest(test1) - .withMarksAwarded(40) + .withMarksObtained(40) .build(); testResult4 = new TestResultBuilder() .withStudent(student4) .withTest(test1) - .withMarksAwarded(50) + .withMarksObtained(50) .build(); testResult5 = new TestResultBuilder() .withStudent(student3) .withTest(test1) - .withMarksAwarded(70) + .withMarksObtained(70) .build(); testResult6 = new TestResultBuilder() .withStudent(student4) .withTest(test1) - .withMarksAwarded(80) + .withMarksObtained(80) .build(); test1Results = List.of(testResult1, testResult2, testResult3, testResult4, testResult5, testResult6); -- cgit v1.2.3 From b59d2f0c722d2e21194ed8d66a17be8128ba7b39 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 26 Jul 2024 15:15:06 +1000 Subject: Remove unused variables and tidy comments --- .../com/stileeducation/markr/controller/TestResultsControllerTest.java | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/test') diff --git a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java index cd1f3cb..40de1a5 100644 --- a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -382,9 +382,6 @@ public class TestResultsControllerTest { headers.setContentType(XmlMarkrMessageConverter.MEDIA_TYPE); HttpEntity entity = new HttpEntity<>(validPayload, headers); - // Initial Submit - restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); - // When ResponseEntity response = restTemplate.postForEntity(IMPORT_ENDPOINT, entity, ImportResponseDTO.class); -- cgit v1.2.3