articles-backend-app
87 строк · 3.3 Кб
1package by.andd3dfx.controllers;2
3import by.andd3dfx.ArticlesBackendAppApplication;4import by.andd3dfx.dto.LoggingSearchCriteria;5import by.andd3dfx.dto.MethodCallRecord;6import by.andd3dfx.services.ILoggingService;7import by.andd3dfx.util.TestUtil;8import org.junit.jupiter.api.BeforeEach;9import org.junit.jupiter.api.Test;10import org.springframework.beans.factory.annotation.Autowired;11import org.springframework.boot.test.context.SpringBootTest;12import org.springframework.boot.test.mock.mockito.MockBean;13import org.springframework.http.MediaType;14import org.springframework.test.context.web.WebAppConfiguration;15import org.springframework.test.web.servlet.MockMvc;16import org.springframework.web.context.WebApplicationContext;17
18import java.time.LocalDateTime;19import java.util.Arrays;20import java.util.HashMap;21import java.util.List;22
23import static org.mockito.BDDMockito.given;24import static org.mockito.Mockito.verify;25import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;26import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;27import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;28import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;29import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;30import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;31
32@SpringBootTest(classes = ArticlesBackendAppApplication.class)33@WebAppConfiguration
34class LoggingControllerTest {35
36private MockMvc mockMvc;37
38@MockBean39private ILoggingService loggingService;40
41@Autowired42private WebApplicationContext webApplicationContext;43
44@BeforeEach45public void setup() {46mockMvc = webAppContextSetup(webApplicationContext)47.build();48}49
50@Test51public void getLoggedRecords() throws Exception {52final List<MethodCallRecord> items = Arrays.asList(new MethodCallRecord());53
54final String timestamp = "2020-05-19T16:08:32";55final LoggingSearchCriteria criteria = new LoggingSearchCriteria();56criteria.setTimestamp(timestamp);57given(loggingService.getLoggedRecords(criteria)).willReturn(items);58
59mockMvc.perform(get("/api/v1/logs?timestamp=" + timestamp))60.andDo(print())61.andExpect(status().isOk())62.andExpect(content().json(TestUtil.asJsonString(items)));63
64verify(loggingService).getLoggedRecords(criteria);65}66
67@Test68public void addLoggingRecord() throws Exception {69final MethodCallRecord loggedRecord = new MethodCallRecord();70loggedRecord.setName("methodName");71loggedRecord.setArgs(new HashMap<>() {{72put("arg1", "Some value");73}});74loggedRecord.setTimestamp(LocalDateTime.parse("2020-05-19T16:08:32"));75loggedRecord.setResult("Result string");76loggedRecord.setIsSucceed(true);77
78mockMvc.perform(post("/api/v1/logs")79.content(TestUtil.asJsonString(loggedRecord))80.contentType(MediaType.APPLICATION_JSON)81.characterEncoding("UTF-8"))82.andDo(print())83.andExpect(status().isCreated());84
85verify(loggingService).addLoggingRecord(loggedRecord);86}87}
88