Skip to content

Commit

Permalink
Merge pull request #3 from manuelprinz/refactoring-tests
Browse files Browse the repository at this point in the history
Refactoring tests
  • Loading branch information
SevKohler authored Apr 21, 2024
2 parents a16a4d7 + 27c3325 commit 1e2b698
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/bih/aft/controller/QueryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ public QueryController( QueryUseCase queryService) {
path = "/federate",
consumes = "application/json",
produces = "application/json")
public ResponseEntity<Object> federateQuery(@RequestBody String json) {
public ResponseEntity<Object> federateQuery(@RequestBody AQLinput aQlQuery) {
log.info("Query received");
ObjectMapper objectMapper = new ObjectMapper();
try {
AQLinput aQlQuery = objectMapper.readValue(json, AQLinput.class);
return new ResponseEntity<>(queryUseCaseService.federate(aQlQuery), HttpStatus.OK);
} catch (JsonProcessingException e) {
return new ResponseEntity<>("{ \"message\" : \"Json malformed\" }", HttpStatus.BAD_REQUEST);
}catch (InvalidCountQuery invalidCountException){
return new ResponseEntity<>("{ \"message\" : "+invalidCountException.getMessage()+" }", HttpStatus.BAD_REQUEST);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/bih/aft/service/DefaultWebQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class DefaultWebQueryService implements QueryService {

@Override
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/bih/aft/controller/QueryControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.bih.aft.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.bih.aft.exceptions.InvalidCountQuery;
import org.bih.aft.ports.QueryUseCase;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(controllers = QueryController.class)
class QueryControllerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@MockBean
private QueryUseCase queryUseCase;

@Test
void whenValidInput_thenReturns200() throws Exception {
String body = "{\"aql\": \"Select e FROM EHR e\" }";
mockMvc.perform(post("/query/local").contentType("application/json").content(body))
.andExpect(status().isOk())
.andExpect(content().string(""));
}

@Test
void whenServiceThrowsInvalidCountQuery_thenReturn400AndSetMessage() throws Exception {
String body = "{\"aql\": \"Select e FROM EHR e\" }";

when(queryUseCase.local(any())).thenThrow(new InvalidCountQuery("asdasd"));

mockMvc.perform(post("/query/local").contentType("application/json").content(body))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").value("asdasd"));
}
}

0 comments on commit 1e2b698

Please sign in to comment.