当天数据导入
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.rj.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
class QweiUserControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void syncCustomerShouldFetchFromQiWeiAndPrintToConsole() throws Exception {
|
||||
String tenantId = System.getProperty("tenantId", "TENANT_ID_CST_2026");
|
||||
var requestBuilder = post("/api/qweiUser/syncCustomer")
|
||||
.param("tenantId", tenantId);
|
||||
|
||||
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
|
||||
|
||||
int statusCode = mvcResult.getResponse().getStatus();
|
||||
String responseBody = mvcResult.getResponse().getContentAsString();
|
||||
System.out.println("qwei customer sync status: " + statusCode);
|
||||
System.out.println("qwei customer sync response: " + responseBody);
|
||||
|
||||
assertTrue(responseBody.contains("\"success\""), "响应缺少success字段: " + responseBody);
|
||||
if (statusCode == HttpStatus.OK.value()) {
|
||||
assertTrue(responseBody.contains("\"success\":true"), "同步失败: " + responseBody);
|
||||
return;
|
||||
}
|
||||
|
||||
assertTrue(statusCode == HttpStatus.BAD_REQUEST.value(), "非预期状态码: " + statusCode + ", body=" + responseBody);
|
||||
assertTrue(responseBody.contains("48002") || responseBody.contains("api forbidden"),
|
||||
"当前失败不是企微权限问题,请排查: " + responseBody);
|
||||
}
|
||||
}
|
||||
81
src/test/java/com/rj/controller/QweiUserControllerTest.java
Normal file
81
src/test/java/com/rj/controller/QweiUserControllerTest.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package com.rj.controller;
|
||||
|
||||
import com.rj.service.IQweiUserService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
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;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureWebMvc
|
||||
@ActiveProfiles("test")
|
||||
class QweiUserControllerTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
@MockitoBean
|
||||
private IQweiUserService qweiUserService;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void syncCustomerShouldReturnOkWhenServiceSuccess() throws Exception {
|
||||
Map<String, Object> serviceResult = new HashMap<>();
|
||||
serviceResult.put("success", true);
|
||||
serviceResult.put("message", "同步企微客户成功");
|
||||
serviceResult.put("inserted", 2);
|
||||
serviceResult.put("updated", 3);
|
||||
serviceResult.put("total", 5);
|
||||
when(qweiUserService.syncCustomerFromQiWei("TENANT_TEST")).thenReturn(serviceResult);
|
||||
|
||||
mockMvc.perform(post("/api/qweiUser/syncCustomer")
|
||||
.param("tenantId", "TENANT_TEST")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.inserted").value(2))
|
||||
.andExpect(jsonPath("$.updated").value(3))
|
||||
.andExpect(jsonPath("$.total").value(5));
|
||||
|
||||
verify(qweiUserService, times(1)).syncCustomerFromQiWei("TENANT_TEST");
|
||||
}
|
||||
|
||||
@Test
|
||||
void syncCustomerShouldReturnBadRequestWhenServiceFailed() throws Exception {
|
||||
Map<String, Object> serviceResult = new HashMap<>();
|
||||
serviceResult.put("success", false);
|
||||
serviceResult.put("message", "同步企微客户异常:测试错误");
|
||||
when(qweiUserService.syncCustomerFromQiWei("TENANT_TEST")).thenReturn(serviceResult);
|
||||
|
||||
mockMvc.perform(post("/api/qweiUser/syncCustomer")
|
||||
.param("tenantId", "TENANT_TEST")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(jsonPath("$.success").value(false));
|
||||
|
||||
verify(qweiUserService, times(1)).syncCustomerFromQiWei("TENANT_TEST");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user