blob: 3d21c25cceb23b8ec1a26fc8a2edb46245752079 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package com.stileeducation.markr.controller;
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 {
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
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);
}
}
|