Skip to content

Commit

Permalink
Merge pull request #11 from md-fork/develop
Browse files Browse the repository at this point in the history
Add tests to WS module
  • Loading branch information
sciabarracom authored Apr 20, 2020
2 parents c08dfb1 + ffe3265 commit 8ffca7a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ hs_err_pid*
ws-sdk/ws/conf/dpppt-backend-sdk-ws-logback.xml

ws-sdk/ws/conf/dpppt-backend-sdk-ws.properties

*.iml
.idea
**/target/
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DPPPTController(DPPPTDataService dataService, EtagGeneratorInterface etag
return ResponseEntity.ok().build();

} else {
return new ResponseEntity<String>("No valid base64 key", HttpStatus.BAD_REQUEST);
return new ResponseEntity<>("No valid base64 key", HttpStatus.BAD_REQUEST);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.dpppt.backend.sdk.ws.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dpppt.backend.sdk.data.DPPPTDataService;
import org.dpppt.backend.sdk.data.EtagGeneratorInterface;
import org.dpppt.backend.sdk.model.ExposedOverview;
import org.dpppt.backend.sdk.model.Exposee;
import org.dpppt.backend.sdk.model.ExposeeAuthData;
import org.dpppt.backend.sdk.model.ExposeeRequest;
import org.dpppt.backend.sdk.ws.Application;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
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.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ActiveProfiles("dev")
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
public class DPPPTControllerTest {

@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper objectMapper;

@MockBean private DPPPTDataService dataService;
@MockBean private EtagGeneratorInterface etagGenerator;

@Captor private ArgumentCaptor<Exposee> exposeeCaptor;

@Test
void shouldSayHello() throws Exception {
mockMvc.perform(get("/v1")).andExpect(status().isOk())
.andExpect(content().string("Hello from DP3T WS"));
}

@Test
void shouldAddExposed() throws Exception {
// given
ExposeeRequest request = new ExposeeRequest();
request.setKey("key");
request.setOnset("onset");
request.setAuthData(new ExposeeAuthData());

// when
mockMvc.perform(post("/v1/exposed")
.content(objectMapper.writeValueAsString(request))
.contentType("application/json")
.header("User-Agent", "user-agent")
).andExpect(status().isOk())
.andExpect(content().string(""));

// then
verify(dataService, times(1)).upsertExposee(exposeeCaptor.capture(), anyString());

Exposee exposee = exposeeCaptor.getValue();
assertThat(exposee).isNotNull();
assertThat(exposee.getId()).isNull();
assertThat(exposee.getKey()).isEqualTo(request.getKey());
assertThat(exposee.getOnset()).isEqualTo(request.getOnset());
}

@Test
void shouldGetExposed() throws Exception {
// given
Exposee exposee = new Exposee();
exposee.setId(1);
exposee.setKey("key");
exposee.setOnset("onset");
String date = "2020-12-30";
when(dataService.getMaxExposedIdForDay(any())).thenReturn(10);
when(dataService.getSortedExposedForDay(any()))
.thenReturn(Collections.singletonList(exposee));

// when
mockMvc.perform(get("/v1/exposed/" + date))
.andExpect(status().isOk())
.andExpect(content().json(objectMapper.writeValueAsString(
new ExposedOverview(Collections.singletonList(exposee))))
);
}

}

0 comments on commit 8ffca7a

Please sign in to comment.