Skip to content

Commit 1e2b698

Browse files
authored
Merge pull request #3 from manuelprinz/refactoring-tests
Refactoring tests
2 parents a16a4d7 + 27c3325 commit 1e2b698

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
2428
<dependency>
2529
<groupId>org.springframework.boot</groupId>
2630
<artifactId>spring-boot-starter-webflux</artifactId>

src/main/java/org/bih/aft/controller/QueryController.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ public QueryController( QueryUseCase queryService) {
2727
path = "/federate",
2828
consumes = "application/json",
2929
produces = "application/json")
30-
public ResponseEntity<Object> federateQuery(@RequestBody String json) {
30+
public ResponseEntity<Object> federateQuery(@RequestBody AQLinput aQlQuery) {
3131
log.info("Query received");
32-
ObjectMapper objectMapper = new ObjectMapper();
3332
try {
34-
AQLinput aQlQuery = objectMapper.readValue(json, AQLinput.class);
3533
return new ResponseEntity<>(queryUseCaseService.federate(aQlQuery), HttpStatus.OK);
36-
} catch (JsonProcessingException e) {
37-
return new ResponseEntity<>("{ \"message\" : \"Json malformed\" }", HttpStatus.BAD_REQUEST);
3834
}catch (InvalidCountQuery invalidCountException){
3935
return new ResponseEntity<>("{ \"message\" : "+invalidCountException.getMessage()+" }", HttpStatus.BAD_REQUEST);
4036
}

src/main/java/org/bih/aft/service/DefaultWebQueryService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import org.springframework.http.HttpHeaders;
1010
import org.springframework.http.MediaType;
1111
import org.springframework.http.ResponseEntity;
12+
import org.springframework.stereotype.Service;
1213
import org.springframework.web.client.ResourceAccessException;
1314
import org.springframework.web.client.RestTemplate;
1415

1516
@Slf4j
17+
@Service
1618
public class DefaultWebQueryService implements QueryService {
1719

1820
@Override
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.bih.aft.controller;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.bih.aft.exceptions.InvalidCountQuery;
5+
import org.bih.aft.ports.QueryUseCase;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
9+
import org.springframework.boot.test.mock.mockito.MockBean;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
12+
import static org.mockito.ArgumentMatchers.any;
13+
import static org.mockito.ArgumentMatchers.isNull;
14+
import static org.mockito.Mockito.when;
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
17+
18+
@WebMvcTest(controllers = QueryController.class)
19+
class QueryControllerTest {
20+
@Autowired
21+
private MockMvc mockMvc;
22+
23+
@Autowired
24+
private ObjectMapper objectMapper;
25+
26+
@MockBean
27+
private QueryUseCase queryUseCase;
28+
29+
@Test
30+
void whenValidInput_thenReturns200() throws Exception {
31+
String body = "{\"aql\": \"Select e FROM EHR e\" }";
32+
mockMvc.perform(post("/query/local").contentType("application/json").content(body))
33+
.andExpect(status().isOk())
34+
.andExpect(content().string(""));
35+
}
36+
37+
@Test
38+
void whenServiceThrowsInvalidCountQuery_thenReturn400AndSetMessage() throws Exception {
39+
String body = "{\"aql\": \"Select e FROM EHR e\" }";
40+
41+
when(queryUseCase.local(any())).thenThrow(new InvalidCountQuery("asdasd"));
42+
43+
mockMvc.perform(post("/query/local").contentType("application/json").content(body))
44+
.andExpect(status().isBadRequest())
45+
.andExpect(jsonPath("$.message").value("asdasd"));
46+
}
47+
}

0 commit comments

Comments
 (0)