|
@@ -0,0 +1,83 @@
|
|
|
|
|
+package com.ruoyi.app.order;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.ruoyi.app.utils.event.PushEventService;
|
|
|
|
|
+import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
|
+import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
|
|
+import com.ruoyi.framework.manager.AsyncManager;
|
|
|
|
|
+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.utils.JwtUtil;
|
|
|
|
|
+import com.ruoyi.system.utils.OrderLogHelper;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
+import org.redisson.api.RLock;
|
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.TimerTask;
|
|
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+class RiderAcceptanceMerchantNotificationTest {
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void committedAcceptanceSendsExactlyOneMerchantNewDeliveryOrderEvenWithoutBuyerAccount() throws Exception {
|
|
|
|
|
+ var controller = new PosOrderQsOprateController();
|
|
|
|
|
+ var orders = mock(IPosOrderService.class);
|
|
|
|
|
+ var users = mock(IInfoUserService.class);
|
|
|
|
|
+ var router = mock(MerchantNotificationRouter.class);
|
|
|
|
|
+ var lifecycle = mock(OrderLifecycleService.class);
|
|
|
|
|
+ var redisson = mock(RedissonClient.class);
|
|
|
|
|
+ var lock = mock(RLock.class);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "posOrderService", orders);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "infoUserService", users);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "merchantNotificationRouter", router);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "orderLifecycleService", lifecycle);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "redissonClient", redisson);
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "orderLogHelper", mock(OrderLogHelper.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "pushEventService", mock(PushEventService.class));
|
|
|
|
|
+ PosOrder order = new PosOrder();
|
|
|
|
|
+ order.setId(3215L); order.setDdId("ORDER-NOTIFY-1"); order.setType(0L);
|
|
|
|
|
+ order.setMdId(195L); order.setShId(801L); order.setUserId(909L);
|
|
|
|
|
+ order.setState(0L); order.setDeliveryStatus(0L);
|
|
|
|
|
+ when(orders.getOne(any())).thenReturn(order);
|
|
|
|
|
+ when(orders.getById(3215L)).thenReturn(order);
|
|
|
|
|
+ InfoUser rider = new InfoUser(); rider.setNickName("rider");
|
|
|
|
|
+ when(users.getOne(any())).thenReturn(rider);
|
|
|
|
|
+ when(redisson.getLock(anyString())).thenReturn(lock);
|
|
|
|
|
+ when(lock.tryLock(20L, 10L, TimeUnit.SECONDS)).thenReturn(true);
|
|
|
|
|
+ try (var spring = mockStatic(SpringUtils.class);
|
|
|
|
|
+ var messages = mockStatic(MessageUtils.class, call -> call.getArgument(0))) {
|
|
|
|
|
+ spring.when(() -> SpringUtils.getBean("scheduledExecutorService"))
|
|
|
|
|
+ .thenReturn(mock(ScheduledExecutorService.class));
|
|
|
|
|
+ try (var async = mockStatic(AsyncManager.class)) {
|
|
|
|
|
+ var manager = mock(AsyncManager.class);
|
|
|
|
|
+ async.when(AsyncManager::me).thenReturn(manager);
|
|
|
|
|
+ TransactionSynchronizationManager.initSynchronization();
|
|
|
|
|
+ try {
|
|
|
|
|
+ controller.acceptOrder(JwtUtil.token("831", "rider"), 3215L);
|
|
|
|
|
+ verifyNoInteractions(router, manager);
|
|
|
|
|
+ TransactionSynchronizationManager.getSynchronizations().forEach(sync -> sync.afterCommit());
|
|
|
|
|
+ var task = ArgumentCaptor.forClass(TimerTask.class);
|
|
|
|
|
+ verify(manager).execute(task.capture());
|
|
|
|
|
+ task.getValue().run();
|
|
|
|
|
+ var body = ArgumentCaptor.forClass(String.class);
|
|
|
|
|
+ verify(router).sendStoreNewOrder(eq(195L), eq(801L), eq("no.message.push.message"),
|
|
|
|
|
+ eq("no.message.push.new.order"), body.capture(), eq("ORDER-NOTIFY-1"), eq(0L));
|
|
|
|
|
+ assertEquals(0, JSON.parseObject(body.getValue()).getIntValue("type"));
|
|
|
|
|
+ assertEquals("ORDER-NOTIFY-1", JSON.parseObject(body.getValue()).getString("ddId"));
|
|
|
|
|
+ verifyNoMoreInteractions(router);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ TransactionSynchronizationManager.clearSynchronization();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|