articles-backend-app

Форк
0
/
LoggingControllerTest.java 
87 строк · 3.3 Кб
1
package by.andd3dfx.controllers;
2

3
import by.andd3dfx.ArticlesBackendAppApplication;
4
import by.andd3dfx.dto.LoggingSearchCriteria;
5
import by.andd3dfx.dto.MethodCallRecord;
6
import by.andd3dfx.services.ILoggingService;
7
import by.andd3dfx.util.TestUtil;
8
import org.junit.jupiter.api.BeforeEach;
9
import org.junit.jupiter.api.Test;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.boot.test.context.SpringBootTest;
12
import org.springframework.boot.test.mock.mockito.MockBean;
13
import org.springframework.http.MediaType;
14
import org.springframework.test.context.web.WebAppConfiguration;
15
import org.springframework.test.web.servlet.MockMvc;
16
import org.springframework.web.context.WebApplicationContext;
17

18
import java.time.LocalDateTime;
19
import java.util.Arrays;
20
import java.util.HashMap;
21
import java.util.List;
22

23
import static org.mockito.BDDMockito.given;
24
import static org.mockito.Mockito.verify;
25
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
28
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
29
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
31

32
@SpringBootTest(classes = ArticlesBackendAppApplication.class)
33
@WebAppConfiguration
34
class LoggingControllerTest {
35

36
    private MockMvc mockMvc;
37

38
    @MockBean
39
    private ILoggingService loggingService;
40

41
    @Autowired
42
    private WebApplicationContext webApplicationContext;
43

44
    @BeforeEach
45
    public void setup() {
46
        mockMvc = webAppContextSetup(webApplicationContext)
47
                .build();
48
    }
49

50
    @Test
51
    public void getLoggedRecords() throws Exception {
52
        final List<MethodCallRecord> items = Arrays.asList(new MethodCallRecord());
53

54
        final String timestamp = "2020-05-19T16:08:32";
55
        final LoggingSearchCriteria criteria = new LoggingSearchCriteria();
56
        criteria.setTimestamp(timestamp);
57
        given(loggingService.getLoggedRecords(criteria)).willReturn(items);
58

59
        mockMvc.perform(get("/api/v1/logs?timestamp=" + timestamp))
60
            .andDo(print())
61
            .andExpect(status().isOk())
62
            .andExpect(content().json(TestUtil.asJsonString(items)));
63

64
        verify(loggingService).getLoggedRecords(criteria);
65
    }
66

67
    @Test
68
    public void addLoggingRecord() throws Exception {
69
        final MethodCallRecord loggedRecord = new MethodCallRecord();
70
        loggedRecord.setName("methodName");
71
        loggedRecord.setArgs(new HashMap<>() {{
72
            put("arg1", "Some value");
73
        }});
74
        loggedRecord.setTimestamp(LocalDateTime.parse("2020-05-19T16:08:32"));
75
        loggedRecord.setResult("Result string");
76
        loggedRecord.setIsSucceed(true);
77

78
        mockMvc.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

85
        verify(loggingService).addLoggingRecord(loggedRecord);
86
    }
87
}
88

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.