diff options
6 files changed, 100 insertions, 13 deletions
diff --git a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java index c713df6..c741e5c 100644 --- a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java +++ b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java @@ -3,6 +3,7 @@ package com.stileeducation.markr.controller; import com.stileeducation.markr.dto.AggregateResponseDTO; import com.stileeducation.markr.dto.ImportResponseDTO; import com.stileeducation.markr.dto.MCQTestResultsDTO; +import com.stileeducation.markr.exception.TestNotFoundException; import com.stileeducation.markr.service.TestResultsService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -34,11 +35,13 @@ public class TestResultsController { } @GetMapping(value = AGGREGATE_ENDPOINT, produces = "application/json") - public AggregateResponseDTO getAggregatedResults(@PathVariable("test-id") String testId) { - - // Should return 404 if test is not found. - - return testResultsService.aggregateTestResults(testId); + public ResponseEntity<AggregateResponseDTO> getAggregatedResults(@PathVariable("test-id") String testId) { + try { + AggregateResponseDTO response = testResultsService.aggregateTestResults(testId); + return ResponseEntity.ok(response); + } catch (TestNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } } @ExceptionHandler(MethodArgumentNotValidException.class) diff --git a/src/main/java/com/stileeducation/markr/exception/TestNotFoundException.java b/src/main/java/com/stileeducation/markr/exception/TestNotFoundException.java new file mode 100644 index 0000000..d4822de --- /dev/null +++ b/src/main/java/com/stileeducation/markr/exception/TestNotFoundException.java @@ -0,0 +1,7 @@ +package com.stileeducation.markr.exception; + +public class TestNotFoundException extends RuntimeException { + public TestNotFoundException(String message) { + super(message); + } +}
\ No newline at end of file diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java index 8dba13f..a06400e 100644 --- a/src/main/java/com/stileeducation/markr/service/TestResultsService.java +++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java @@ -7,6 +7,7 @@ import com.stileeducation.markr.dto.MCQTestResultsDTO; import com.stileeducation.markr.entity.Student; import com.stileeducation.markr.entity.Test; import com.stileeducation.markr.entity.TestResult; +import com.stileeducation.markr.exception.TestNotFoundException; import com.stileeducation.markr.repository.TestResultRepository; import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math3.stat.descriptive.rank.Percentile; @@ -43,12 +44,13 @@ public class TestResultsService { public AggregateResponseDTO aggregateTestResults(String testId) { AggregateResponseDTO aggregateResponseDTO = new AggregateResponseDTO(); - testService.findTest(testId).ifPresent(test -> { - List<TestResult> testResults = findAllByTestId(testId); - if (!testResults.isEmpty()) { - populateAggregateResponse(aggregateResponseDTO, test, testResults); - } - }); + Test test = testService.findTest(testId) + .orElseThrow(() -> new TestNotFoundException("Test with ID " + testId + " not found")); + + List<TestResult> testResults = findAllByTestId(testId); + if (!testResults.isEmpty()) { + populateAggregateResponse(aggregateResponseDTO, test, testResults); + } return aggregateResponseDTO; } diff --git a/src/test/java/com/stileeducation/markr/controller/AggregateEndpointTests.java b/src/test/java/com/stileeducation/markr/controller/AggregateEndpointTests.java new file mode 100644 index 0000000..7776a75 --- /dev/null +++ b/src/test/java/com/stileeducation/markr/controller/AggregateEndpointTests.java @@ -0,0 +1,74 @@ +package com.stileeducation.markr.controller; + +import com.stileeducation.markr.MarkrApplication; +import com.stileeducation.markr.dto.AggregateResponseDTO; +import com.stileeducation.markr.exception.TestNotFoundException; +import com.stileeducation.markr.service.TestResultsService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.BDDMockito.given; + +@SpringBootTest(classes = MarkrApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc +@ActiveProfiles("test") +public class AggregateEndpointTests { + + @Autowired + private TestRestTemplate restTemplate; + + @MockBean + private TestResultsService testResultsService; + + @Test + public void testGetAggregatedResults_notFound() throws Exception { + String testId = "1234"; + given(testResultsService.aggregateTestResults(testId)) + .willThrow(new TestNotFoundException("Test with ID " + testId + " not found")); + + String url = "/results/" + testId + "/aggregate"; + ResponseEntity<AggregateResponseDTO> response = restTemplate.getForEntity(url, AggregateResponseDTO.class); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertNull(response.getBody()); + } + + + @Test + public void testGetAggregatedResults() throws Exception { + String testId = "test123"; + AggregateResponseDTO mockResponse = new AggregateResponseDTO(); + mockResponse.setMin(10.0); + mockResponse.setMax(80.0); + mockResponse.setMean(45.0); + mockResponse.setStddev(25.0); + mockResponse.setP25(17.5); + mockResponse.setP50(25.0); + mockResponse.setP75(72.5); + mockResponse.setCount(6); + + given(testResultsService.aggregateTestResults(testId)).willReturn(mockResponse); + + String url = "/results/" + testId + "/aggregate"; + ResponseEntity<AggregateResponseDTO> response = restTemplate.getForEntity(url, AggregateResponseDTO.class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertEquals(mockResponse.getMin(), response.getBody().getMin()); + assertEquals(mockResponse.getMax(), response.getBody().getMax()); + assertEquals(mockResponse.getMean(), response.getBody().getMean()); + assertEquals(mockResponse.getStddev(), response.getBody().getStddev()); + assertEquals(mockResponse.getP25(), response.getBody().getP25()); + assertEquals(mockResponse.getP50(), response.getBody().getP50()); + assertEquals(mockResponse.getP75(), response.getBody().getP75()); + assertEquals(mockResponse.getCount(), response.getBody().getCount()); + } +} diff --git a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java index 3b491cc..efb20d6 100644 --- a/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java +++ b/src/test/java/com/stileeducation/markr/controller/TestResultsControllerTest.java @@ -47,6 +47,7 @@ public class TestResultsControllerTest { return stringWriter.toString(); } + @Test void testSupportedMediaType() throws Exception { // Given diff --git a/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java b/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java index 844c739..ad4e476 100644 --- a/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java +++ b/src/test/java/com/stileeducation/markr/service/TestResultsServiceTest.java @@ -151,7 +151,7 @@ class TestResultsServiceTest { @Test void calculateStandardDeviationOfTestResults() { double standardDeviationOfTestResults = testResultsService.calculateStandardDeviationOfTestResults(test1, test1Results); - assertEquals(25, standardDeviationOfTestResults, 0.01); + assertEquals(25.0, standardDeviationOfTestResults, 0.01); } @Test @@ -163,7 +163,7 @@ class TestResultsServiceTest { @Test void calculate50thPercentile() { double percentile = testResultsService.calculate50thPercentile(test1, test1Results); - assertEquals(45, percentile, 0.01); + assertEquals(45.0, percentile, 0.01); } @Test |
