|
@@ -0,0 +1,102 @@
|
|
|
|
|
+package com.ruoyi.app.user;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
|
|
+import com.ruoyi.common.constant.HttpStatus;
|
|
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
+import com.ruoyi.system.domain.PosOrder;
|
|
|
|
|
+import com.ruoyi.system.service.IInfoUserService;
|
|
|
|
|
+import com.ruoyi.system.service.IPosOrderService;
|
|
|
|
|
+import com.ruoyi.system.utils.JwtUtil;
|
|
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+class InfoUserControllerTest {
|
|
|
|
|
+
|
|
|
|
|
+ private InfoUserController controller;
|
|
|
|
|
+ private IPosOrderService posOrderService;
|
|
|
|
|
+ private IInfoUserService infoUserService;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeAll
|
|
|
|
|
+ static void initializeTableMetadata() {
|
|
|
|
|
+ TableInfoHelper.initTableInfo(
|
|
|
|
|
+ new MapperBuilderAssistant(new MybatisConfiguration(), ""), PosOrder.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ controller = new TestInfoUserController();
|
|
|
|
|
+ posOrderService = mock(IPosOrderService.class);
|
|
|
|
|
+ infoUserService = mock(IInfoUserService.class);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "posOrderService", posOrderService);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "infoUserService", infoUserService);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void refusesDeletionWhenUserHasAnUnfinishedOrder() {
|
|
|
|
|
+ when(posOrderService.exists(any(Wrapper.class))).thenReturn(true);
|
|
|
|
|
+
|
|
|
|
|
+ ServiceException exception = assertThrows(ServiceException.class,
|
|
|
|
|
+ () -> controller.deleuser(tokenFor(42L)));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("抱歉,您還有未完成的訂單,無法刪除帳號", exception.getMessage());
|
|
|
|
|
+ verify(infoUserService, never()).deleteInfoUserByUserId(42L);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void checksOnlyLatestUnfinishedStatesForEveryOrderRole() {
|
|
|
|
|
+ when(posOrderService.exists(any(Wrapper.class))).thenReturn(false);
|
|
|
|
|
+ when(infoUserService.deleteInfoUserByUserId(42L)).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = controller.deleuser(tokenFor(42L));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(HttpStatus.SUCCESS, result.get(AjaxResult.CODE_TAG));
|
|
|
|
|
+ ArgumentCaptor<LambdaQueryWrapper<PosOrder>> captor =
|
|
|
|
|
+ ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
|
|
|
|
+ verify(posOrderService).exists(captor.capture());
|
|
|
|
|
+
|
|
|
|
|
+ LambdaQueryWrapper<PosOrder> query = captor.getValue();
|
|
|
|
|
+ String sql = query.getSqlSegment();
|
|
|
|
|
+ Collection<Object> parameters = new ArrayList<>(query.getParamNameValuePairs().values());
|
|
|
|
|
+ assertEquals(6, parameters.size());
|
|
|
|
|
+ assertEquals(1, Collections.frequency(parameters, 0L));
|
|
|
|
|
+ assertEquals(1, Collections.frequency(parameters, 1L));
|
|
|
|
|
+ assertEquals(1, Collections.frequency(parameters, 2L));
|
|
|
|
|
+ assertEquals(3, Collections.frequency(parameters, 42L));
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(sql.contains("user_id"));
|
|
|
|
|
+ assertTrue(sql.contains("qs_id"));
|
|
|
|
|
+ assertTrue(sql.contains("sh_id"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String tokenFor(Long userId) {
|
|
|
|
|
+ return JwtUtil.setToken(String.valueOf(userId), "test-user");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static class TestInfoUserController extends InfoUserController {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected AjaxResult toAjax(int rows) {
|
|
|
|
|
+ return new AjaxResult(HttpStatus.SUCCESS, "ok");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|