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);
|
}
|
}
|