PosStoreControllerTest.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.ruoyi.app.mendian;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.ruoyi.app.service.UserService;
  4. import com.ruoyi.common.exception.ServiceException;
  5. import com.ruoyi.common.utils.MessageUtils;
  6. import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
  7. import com.ruoyi.system.domain.InfoUser;
  8. import com.ruoyi.system.domain.PosStore;
  9. import com.ruoyi.system.service.IPosStoreService;
  10. import com.ruoyi.system.service.IInfoUserService;
  11. import com.ruoyi.system.service.MerchantStoreAccessService;
  12. import com.ruoyi.system.utils.AuthContext;
  13. import org.junit.jupiter.api.AfterEach;
  14. import org.junit.jupiter.api.BeforeEach;
  15. import org.junit.jupiter.api.Test;
  16. import org.mockito.ArgumentCaptor;
  17. import org.mockito.InjectMocks;
  18. import org.mockito.Mock;
  19. import org.mockito.MockedStatic;
  20. import org.mockito.MockitoAnnotations;
  21. import org.springframework.test.web.servlet.MockMvc;
  22. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  23. import static org.junit.jupiter.api.Assertions.assertEquals;
  24. import static org.mockito.ArgumentMatchers.any;
  25. import static org.mockito.ArgumentMatchers.anyString;
  26. import static org.mockito.Mockito.when;
  27. import static org.springframework.http.MediaType.APPLICATION_JSON;
  28. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  29. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  30. class PosStoreControllerTest {
  31. @Mock
  32. private IPosStoreService posStoreService;
  33. @Mock
  34. private IInfoUserService infoUserService;
  35. @Mock
  36. private UserService userService;
  37. @Mock
  38. private MerchantStoreAccessService merchantStoreAccessService;
  39. @InjectMocks
  40. private PosStoreController controller;
  41. private AutoCloseable mocks;
  42. private MockedStatic<MessageUtils> messages;
  43. private MockedStatic<AuthContext> authContext;
  44. private MockMvc mockMvc;
  45. private ObjectMapper objectMapper;
  46. @BeforeEach
  47. void setUp() {
  48. mocks = MockitoAnnotations.openMocks(this);
  49. messages = org.mockito.Mockito.mockStatic(MessageUtils.class);
  50. messages.when(() -> MessageUtils.message(anyString()))
  51. .thenAnswer(invocation -> invocation.getArgument(0));
  52. authContext = org.mockito.Mockito.mockStatic(AuthContext.class);
  53. authContext.when(AuthContext::requireUserId).thenReturn(101L);
  54. mockMvc = MockMvcBuilders.standaloneSetup(controller)
  55. .setControllerAdvice(new GlobalExceptionHandler())
  56. .build();
  57. objectMapper = new ObjectMapper();
  58. when(posStoreService.saveOrUpdate(any(PosStore.class))).thenReturn(true);
  59. }
  60. @AfterEach
  61. void tearDown() throws Exception {
  62. authContext.close();
  63. messages.close();
  64. mocks.close();
  65. }
  66. @Test
  67. void unauditedMerchantCannotCreateStore() throws Exception {
  68. when(userService.checkUserStatus(101L))
  69. .thenThrow(new ServiceException("账号状态未审核"));
  70. mockMvc.perform(post("/chanting/store/addmendian")
  71. .header("token", "token")
  72. .contentType(APPLICATION_JSON)
  73. .content(objectMapper.writeValueAsString(newStore(101L))))
  74. .andExpect(jsonPath("$.code").value(500))
  75. .andExpect(jsonPath("$.msg").value("账号状态未审核"));
  76. }
  77. @Test
  78. void auditedMerchantCreatesStoreOwnedByAuthenticatedOwner() throws Exception {
  79. when(userService.checkUserStatus(101L)).thenReturn(auditedMerchant(101L));
  80. PosStore saved = newStore(101L);
  81. saved.setId(9);
  82. when(posStoreService.getOne(any())).thenReturn(saved);
  83. mockMvc.perform(post("/chanting/store/addmendian")
  84. .header("token", "token")
  85. .contentType(APPLICATION_JSON)
  86. .content(objectMapper.writeValueAsString(newStore(999L))))
  87. .andExpect(jsonPath("$.code").value(200));
  88. ArgumentCaptor<PosStore> captor = ArgumentCaptor.forClass(PosStore.class);
  89. org.mockito.Mockito.verify(posStoreService).saveOrUpdate(captor.capture());
  90. assertEquals(101L, captor.getValue().getUserId());
  91. }
  92. @Test
  93. void editingExistingStoreDoesNotRequireMerchantAudit() throws Exception {
  94. when(userService.checkUserStatus(any()))
  95. .thenThrow(new ServiceException("账号状态未审核"));
  96. PosStore existing = newStore(101L);
  97. existing.setId(9);
  98. when(posStoreService.getById(9)).thenReturn(existing);
  99. mockMvc.perform(post("/chanting/store/addmendian")
  100. .header("token", "token")
  101. .contentType(APPLICATION_JSON)
  102. .content(objectMapper.writeValueAsString(existing)))
  103. .andExpect(jsonPath("$.code").value(200));
  104. }
  105. private static PosStore newStore(Long userId) {
  106. PosStore store = new PosStore();
  107. store.setPosName("测试门店");
  108. store.setUserId(userId);
  109. return store;
  110. }
  111. private static InfoUser auditedMerchant(Long userId) {
  112. InfoUser user = new InfoUser();
  113. user.setUserId(userId);
  114. user.setUserType("1");
  115. user.setAuditStatus("1");
  116. return user;
  117. }
  118. }