|
|
@@ -0,0 +1,146 @@
|
|
|
+package com.ruoyi.app.order;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.ruoyi.app.order.dto.OrderImGroupRequest;
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.MessageUtils;
|
|
|
+import com.ruoyi.system.domain.InfoUser;
|
|
|
+import com.ruoyi.system.domain.PosOrder;
|
|
|
+import com.ruoyi.system.service.IInfoUserService;
|
|
|
+import com.ruoyi.system.service.IPosOrderService;
|
|
|
+import com.ruoyi.system.service.MerchantStoreAccessService;
|
|
|
+import com.ruoyi.system.utils.AuthContext;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+class OrderImGroupControllerTest {
|
|
|
+ private final PosOrderController controller = new PosOrderController();
|
|
|
+ private final IPosOrderService orders = mock(IPosOrderService.class);
|
|
|
+ private final IInfoUserService users = mock(IInfoUserService.class);
|
|
|
+ private final MerchantStoreAccessService merchants = mock(MerchantStoreAccessService.class);
|
|
|
+
|
|
|
+ OrderImGroupControllerTest() {
|
|
|
+ ReflectionTestUtils.setField(controller, "posOrderService", orders);
|
|
|
+ ReflectionTestUtils.setField(controller, "infoUserService", users);
|
|
|
+ ReflectionTestUtils.setField(controller, "merchantStoreAccessService", merchants);
|
|
|
+ ReflectionTestUtils.setField(controller, "linePayOrderGuard", new com.ruoyi.app.pay.LinePayOrderGuard(
|
|
|
+ mock(com.ruoyi.system.service.IPosOrderLinePaymentService.class)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void orderUserRiderAndAuthorizedMerchantCanSaveOnlyGroupField() {
|
|
|
+ try (var auth = mockStatic(AuthContext.class); var messages = mockStatic(MessageUtils.class)) {
|
|
|
+ PosOrder order = order();
|
|
|
+ when(orders.getOne(any(Wrapper.class))).thenReturn(order);
|
|
|
+ when(orders.updatePosOrder(any(PosOrder.class))).thenReturn(1);
|
|
|
+ for (String type : new String[]{"0", "2", "1", "3", "4", "5"}) {
|
|
|
+ long actorId = "0".equals(type) ? 21L : "2".equals(type) ? 33L : 11L;
|
|
|
+ auth.when(AuthContext::requireUserId).thenReturn(actorId);
|
|
|
+ InfoUser actor = new InfoUser();
|
|
|
+ actor.setUserId(actorId);
|
|
|
+ actor.setUserType(type);
|
|
|
+ actor.setStoreId(5L);
|
|
|
+ when(users.getById(actorId)).thenReturn(actor);
|
|
|
+ AjaxResult result = controller.saveImGroup("token", new OrderImGroupRequest("ORDER-1", "im-room"));
|
|
|
+ assertEquals(200, result.get("code"));
|
|
|
+ }
|
|
|
+ ArgumentCaptor<PosOrder> update = ArgumentCaptor.forClass(PosOrder.class);
|
|
|
+ verify(orders, times(6)).updatePosOrder(update.capture());
|
|
|
+ for (PosOrder patch : update.getAllValues()) {
|
|
|
+ assertEquals(7L, patch.getId());
|
|
|
+ assertEquals("im-room", patch.getImGroupId());
|
|
|
+ assertNull(patch.getState());
|
|
|
+ assertNull(patch.getQsId());
|
|
|
+ assertNull(patch.getDdId());
|
|
|
+ }
|
|
|
+ verify(merchants, times(2)).requireOrderAccess(11L, 7L);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void unrelatedUserAndRiderCannotChangeAnotherOrdersGroup() {
|
|
|
+ try (var auth = mockStatic(AuthContext.class); var messages = mockStatic(MessageUtils.class)) {
|
|
|
+ auth.when(AuthContext::requireUserId).thenReturn(99L);
|
|
|
+ when(orders.getOne(any(Wrapper.class))).thenReturn(order());
|
|
|
+ for (String type : new String[]{"0", "2"}) {
|
|
|
+ InfoUser actor = new InfoUser(); actor.setUserId(99L); actor.setUserType(type);
|
|
|
+ when(users.getById(99L)).thenReturn(actor);
|
|
|
+ assertThrows(ServiceException.class,
|
|
|
+ () -> controller.saveImGroup("token", new OrderImGroupRequest("ORDER-1", "im-room")));
|
|
|
+ }
|
|
|
+ verify(orders, never()).updatePosOrder(any());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void invalidAndOverlengthGroupAreRejectedBeforeQuery() {
|
|
|
+ try (var messages = mockStatic(MessageUtils.class)) {
|
|
|
+ for (OrderImGroupRequest input : new OrderImGroupRequest[]{null,
|
|
|
+ new OrderImGroupRequest("", "room"), new OrderImGroupRequest("ORDER-1", " "),
|
|
|
+ new OrderImGroupRequest("ORDER-1", "x".repeat(201))}) {
|
|
|
+ assertThrows(ServiceException.class, () -> controller.saveImGroup("token", input));
|
|
|
+ }
|
|
|
+ verifyNoInteractions(orders);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void acceptsTwoHundredCharactersAndRepeatedValueIsIdempotent() {
|
|
|
+ try (var auth = mockStatic(AuthContext.class); var messages = mockStatic(MessageUtils.class)) {
|
|
|
+ auth.when(AuthContext::requireUserId).thenReturn(21L);
|
|
|
+ InfoUser actor = new InfoUser(); actor.setUserId(21L); actor.setUserType("0");
|
|
|
+ when(users.getById(21L)).thenReturn(actor);
|
|
|
+ PosOrder order = order();
|
|
|
+ String groupId = "群".repeat(200);
|
|
|
+ when(orders.getOne(any(Wrapper.class))).thenReturn(order);
|
|
|
+ when(orders.updatePosOrder(any())).thenReturn(1);
|
|
|
+ assertEquals(200, controller.saveImGroup("token", new OrderImGroupRequest("ORDER-1", groupId)).get("code"));
|
|
|
+ order.setImGroupId(groupId);
|
|
|
+ assertEquals(200, controller.saveImGroup("token", new OrderImGroupRequest("ORDER-1", groupId)).get("code"));
|
|
|
+ verify(orders, times(1)).updatePosOrder(any());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private PosOrder order() {
|
|
|
+ PosOrder order = new PosOrder();
|
|
|
+ order.setId(7L); order.setDdId("ORDER-1"); order.setUserId(21L);
|
|
|
+ order.setQsId(33L); order.setShId(11L); order.setMdId(5L);
|
|
|
+ return order;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void sharedOrderDetailReturnsGroupIdentifier() {
|
|
|
+ try (var auth = mockStatic(AuthContext.class); var messages = mockStatic(MessageUtils.class)) {
|
|
|
+ auth.when(AuthContext::requireUserId).thenReturn(21L);
|
|
|
+ PosOrder order = order(); order.setImGroupId("detail-room");
|
|
|
+ order.setQsId(null); order.setFood("[]"); order.setShAddress("{}");
|
|
|
+ when(orders.getOne(any(Wrapper.class))).thenReturn(order);
|
|
|
+ InfoUser user = new InfoUser(); user.setUserId(21L); user.setUserType("0");
|
|
|
+ when(users.getById(21L)).thenReturn(user);
|
|
|
+ ReflectionTestUtils.setField(controller, "posStoreService",
|
|
|
+ mock(com.ruoyi.system.service.IPosStoreService.class));
|
|
|
+ var ratings = mock(com.ruoyi.system.service.IPosOrderRatingService.class);
|
|
|
+ var ratingMapper = mock(com.ruoyi.system.mapper.PosOrderRatingMapper.class);
|
|
|
+ when(ratings.getBaseMapper()).thenReturn(ratingMapper);
|
|
|
+ when(ratingMapper.selectCount(any(Wrapper.class))).thenReturn(0L);
|
|
|
+ ReflectionTestUtils.setField(controller, "posOrderRatingService", ratings);
|
|
|
+ var promotions = mock(com.ruoyi.system.mapper.PosOrderPromotionMapper.class);
|
|
|
+ when(promotions.selectByOrderId(7L)).thenReturn(java.util.List.of());
|
|
|
+ ReflectionTestUtils.setField(controller, "posOrderPromotionMapper", promotions);
|
|
|
+
|
|
|
+ var data = (com.alibaba.fastjson.JSONObject) controller.getorderxq("token", "ORDER-1").get("data");
|
|
|
+
|
|
|
+ assertEquals("detail-room", data.getString("imGroupId"));
|
|
|
+ order.setImGroupId(null);
|
|
|
+ var unbound = (com.alibaba.fastjson.JSONObject) controller.getorderxq("token", "ORDER-1").get("data");
|
|
|
+ assertTrue(unbound.containsKey("imGroupId"));
|
|
|
+ assertNull(unbound.get("imGroupId"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|