|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+package com.ruoyi.web.modules.industryservice.controller;
|
|
|
2
|
+
|
|
|
3
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
4
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
5
|
+import static org.mockito.Mockito.when;
|
|
|
6
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
|
|
7
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
8
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
9
|
+
|
|
|
10
|
+import java.util.HashSet;
|
|
|
11
|
+import java.util.Set;
|
|
|
12
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
13
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
14
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
15
|
+import org.junit.jupiter.api.Test;
|
|
|
16
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
17
|
+import org.mockito.InjectMocks;
|
|
|
18
|
+import org.mockito.Mock;
|
|
|
19
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
20
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
21
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
22
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
23
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
24
|
+import com.ruoyi.common.constant.Constants;
|
|
|
25
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
26
|
+import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
27
|
+import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
|
|
|
28
|
+import com.ruoyi.web.modules.industryservice.service.IBizCooperativeDevelopmentService;
|
|
|
29
|
+
|
|
|
30
|
+@ExtendWith(MockitoExtension.class)
|
|
|
31
|
+@DisplayName("合作社发展数据填报 HTTP 接口 MockMvc")
|
|
|
32
|
+class BizCooperativeDevelopmentControllerApiTest
|
|
|
33
|
+{
|
|
|
34
|
+ @Mock
|
|
|
35
|
+ private IBizCooperativeDevelopmentService bizCooperativeDevelopmentService;
|
|
|
36
|
+
|
|
|
37
|
+ @InjectMocks
|
|
|
38
|
+ private BizCooperativeDevelopmentController controller;
|
|
|
39
|
+
|
|
|
40
|
+ private MockMvc mockMvc;
|
|
|
41
|
+
|
|
|
42
|
+ @BeforeEach
|
|
|
43
|
+ void setUp()
|
|
|
44
|
+ {
|
|
|
45
|
+ mockMvc = MockMvcBuilders.standaloneSetup(controller)
|
|
|
46
|
+ .setControllerAdvice(new GlobalExceptionHandler())
|
|
|
47
|
+ .build();
|
|
|
48
|
+ loginAsAllPermissions();
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ @AfterEach
|
|
|
52
|
+ void tearDown()
|
|
|
53
|
+ {
|
|
|
54
|
+ SecurityContextHolder.clearContext();
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ @Test
|
|
|
58
|
+ @DisplayName("DELETE 单条删除")
|
|
|
59
|
+ void deleteSingle() throws Exception
|
|
|
60
|
+ {
|
|
|
61
|
+ when(bizCooperativeDevelopmentService.deleteBizCooperativeDevelopmentByIds(any(), eq("tester"))).thenReturn(1);
|
|
|
62
|
+ mockMvc.perform(delete("/dataModel/cooperativeDevelopment/7"))
|
|
|
63
|
+ .andExpect(status().isOk())
|
|
|
64
|
+ .andExpect(jsonPath("$.code").value(200));
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ @Test
|
|
|
68
|
+ @DisplayName("DELETE 批量删除 ids 逗号分隔")
|
|
|
69
|
+ void deleteBatch() throws Exception
|
|
|
70
|
+ {
|
|
|
71
|
+ when(bizCooperativeDevelopmentService.deleteBizCooperativeDevelopmentByIds(any(), eq("tester"))).thenReturn(2);
|
|
|
72
|
+ mockMvc.perform(delete("/dataModel/cooperativeDevelopment/7,8"))
|
|
|
73
|
+ .andExpect(status().isOk())
|
|
|
74
|
+ .andExpect(jsonPath("$.code").value(200));
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ private static void loginAsAllPermissions()
|
|
|
78
|
+ {
|
|
|
79
|
+ SysUser u = new SysUser();
|
|
|
80
|
+ u.setUserId(1L);
|
|
|
81
|
+ u.setUserName("tester");
|
|
|
82
|
+ u.setPassword("x");
|
|
|
83
|
+ Set<String> perms = new HashSet<>();
|
|
|
84
|
+ perms.add(Constants.ALL_PERMISSION);
|
|
|
85
|
+ LoginUser lu = new LoginUser(1L, 100L, u, perms);
|
|
|
86
|
+ UsernamePasswordAuthenticationToken auth =
|
|
|
87
|
+ new UsernamePasswordAuthenticationToken(lu, "", lu.getAuthorities());
|
|
|
88
|
+ SecurityContextHolder.getContext().setAuthentication(auth);
|
|
|
89
|
+ }
|
|
|
90
|
+}
|