yanderong
2026-10-02 f50643366a89ea4548cabddf3cbc783605a6f167
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.ruoyi.other.controller;
 
import com.ruoyi.other.domain.TPlatformConfig;
import com.ruoyi.other.service.TPlatformConfigService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
class TPlatformConfigControllerTest {
 
    private TPlatformConfigService service;
    private MockMvc mvc;
 
    @BeforeEach
    void setUp() {
        service = Mockito.mock(TPlatformConfigService.class);
        mvc = MockMvcBuilders.standaloneSetup(new TPlatformConfigController(service)).build();
    }
 
    @Test
    @SuppressWarnings({"rawtypes", "unchecked"})
    void bindsConfigArrayInsideObject() throws Exception {
        mvc.perform(post("/platform-config/saveBatch")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"configs\":[{\"id\":null,\"type\":1,\"platformName\":\"科士达\",\"account\":\"demo\"},"
                        + "{\"id\":8,\"type\":2,\"platformName\":\"华为\"}]}"))
                .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(200));
 
        ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
        Mockito.verify(service).saveBatchConfigs(captor.capture());
        List<TPlatformConfig> configs = captor.getValue();
        assertEquals(2, configs.size());
        assertEquals("科士达", configs.get(0).getPlatformName());
        assertEquals("demo", configs.get(0).getAccount());
        assertEquals(Long.valueOf(8), configs.get(1).getId());
        assertEquals(Integer.valueOf(2), configs.get(1).getType());
    }
 
    @Test
    void rejectsMissingOrNullConfigs() throws Exception {
        for (String body : new String[]{"{}", "{\"configs\":null}"}) {
            mvc.perform(post("/platform-config/saveBatch")
                    .contentType(MediaType.APPLICATION_JSON).content(body))
                    .andExpect(status().isBadRequest());
        }
        Mockito.verifyNoInteractions(service);
    }
 
    @Test
    void rejectsEmptyConfigs() throws Exception {
        mvc.perform(post("/platform-config/saveBatch")
                .contentType(MediaType.APPLICATION_JSON).content("{\"configs\":[]}"))
                .andExpect(status().isBadRequest());
        Mockito.verifyNoInteractions(service);
    }
 
    @Test
    void rejectsBareArrayForObjectContract() throws Exception {
        mvc.perform(post("/platform-config/saveBatch")
                .contentType(MediaType.APPLICATION_JSON).content("[{\"type\":1}]"))
                .andExpect(status().isBadRequest());
        Mockito.verifyNoInteractions(service);
    }
}