|
|
@@ -1,272 +0,0 @@
|
|
|
-package com.etotem.cfc.service;
|
|
|
-
|
|
|
-import com.etotem.cfc.common.Result;
|
|
|
-import com.etotem.cfc.dto.ProductDTO;
|
|
|
-import com.etotem.cfc.entity.Product;
|
|
|
-import com.etotem.cfc.mapper.ProductMapper;
|
|
|
-import com.etotem.cfc.mapper.UserMapper;
|
|
|
-import org.junit.jupiter.api.BeforeEach;
|
|
|
-import org.junit.jupiter.api.Test;
|
|
|
-import org.mockito.Mock;
|
|
|
-import org.mockito.MockitoAnnotations;
|
|
|
-
|
|
|
-import static org.junit.jupiter.api.Assertions.*;
|
|
|
-import static org.mockito.ArgumentMatchers.*;
|
|
|
-import static org.mockito.Mockito.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * ProductService 纯单元测试(无 SpringBootTest,避免 MySQL 连接)
|
|
|
- *
|
|
|
- * 测试范围:
|
|
|
- * - shelve: 上下架(on_shelf/off_shelf)
|
|
|
- * - shelve: 商品不存在/无权操作/未审核通过/操作类型错误
|
|
|
- * - review: 审核通过/拒绝/非待审核状态
|
|
|
- */
|
|
|
-public class ProductServiceTest {
|
|
|
-
|
|
|
- @Mock
|
|
|
- private ProductMapper productMapper;
|
|
|
-
|
|
|
- @Mock
|
|
|
- private UserMapper userMapper;
|
|
|
-
|
|
|
- @Mock
|
|
|
- private ProductGiftRuleService productGiftRuleService;
|
|
|
-
|
|
|
- @Mock
|
|
|
- private ProductDeliveryConfigService productDeliveryConfigService;
|
|
|
-
|
|
|
- private ProductService service;
|
|
|
-
|
|
|
- @BeforeEach
|
|
|
- public void setup() throws Exception {
|
|
|
- MockitoAnnotations.openMocks(this);
|
|
|
- service = new ProductService();
|
|
|
- setField(service, "productMapper", productMapper);
|
|
|
- setField(service, "userMapper", userMapper);
|
|
|
- setField(service, "productGiftRuleService", productGiftRuleService);
|
|
|
- setField(service, "productDeliveryConfigService", productDeliveryConfigService);
|
|
|
- }
|
|
|
-
|
|
|
- private static void setField(Object target, String fieldName, Object value) throws Exception {
|
|
|
- java.lang.reflect.Field f = target.getClass().getDeclaredField(fieldName);
|
|
|
- f.setAccessible(true);
|
|
|
- f.set(target, value);
|
|
|
- }
|
|
|
-
|
|
|
- private Product mockProduct(Long id, Long vendorId, String status) {
|
|
|
- Product p = new Product();
|
|
|
- p.setId(id);
|
|
|
- p.setVendorId(vendorId);
|
|
|
- p.setStatus(status);
|
|
|
- p.setUpdatedAt(new java.util.Date());
|
|
|
- return p;
|
|
|
- }
|
|
|
-
|
|
|
- // ==================== shelve ====================
|
|
|
-
|
|
|
- @Test
|
|
|
- public void shelve_onShelf_success() {
|
|
|
- Product product = mockProduct(1L, 100L, "approved");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.shelve(1L, 100L, "shelve");
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals("操作成功", result.getData());
|
|
|
- assertEquals("on_shelf", product.getStatus());
|
|
|
- verify(productMapper).updateById(product);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void shelve_offShelf_success() {
|
|
|
- Product product = mockProduct(1L, 100L, "on_shelf");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.shelve(1L, 100L, "unshelve");
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals("off_shelf", product.getStatus());
|
|
|
- verify(productMapper).updateById(product);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void shelve_productNotFound_returnsError() {
|
|
|
- when(productMapper.selectById(999L)).thenReturn(null);
|
|
|
-
|
|
|
- Result<String> result = service.shelve(999L, 100L, "shelve");
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("商品不存在", result.getMessage());
|
|
|
- verify(productMapper, never()).updateById(any());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void shelve_wrongVendor_returnsError() {
|
|
|
- Product product = mockProduct(1L, 100L, "approved");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.shelve(1L, 999L, "shelve");
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("无权操作", result.getMessage());
|
|
|
- verify(productMapper, never()).updateById(any());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void shelve_notApproved_returnsError() {
|
|
|
- Product product = mockProduct(1L, 100L, "pending");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.shelve(1L, 100L, "shelve");
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("商品未通过审核,无法上架", result.getMessage());
|
|
|
- verify(productMapper, never()).updateById(any());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void shelve_invalidAction_returnsError() {
|
|
|
- Product product = mockProduct(1L, 100L, "approved");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.shelve(1L, 100L, "invalid");
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("操作类型错误", result.getMessage());
|
|
|
- verify(productMapper, never()).updateById(any());
|
|
|
- }
|
|
|
-
|
|
|
- // ==================== review ====================
|
|
|
-
|
|
|
- @Test
|
|
|
- public void review_approve_success() {
|
|
|
- Product product = mockProduct(1L, 100L, "pending");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.review(1L, "approve", null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals("approved", product.getStatus());
|
|
|
- assertNull(product.getRejectReason());
|
|
|
- verify(productMapper).updateById(product);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void review_reject_setsReason() {
|
|
|
- Product product = mockProduct(1L, 100L, "pending");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.review(1L, "reject", "不符合规范");
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals("rejected", product.getStatus());
|
|
|
- assertEquals("不符合规范", product.getRejectReason());
|
|
|
- verify(productMapper).updateById(product);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void review_reject_defaultReason() {
|
|
|
- Product product = mockProduct(1L, 100L, "pending");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.review(1L, "reject", null);
|
|
|
-
|
|
|
- assertEquals("rejected", product.getStatus());
|
|
|
- assertEquals("不符合规范", product.getRejectReason());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void review_notPending_returnsError() {
|
|
|
- Product product = mockProduct(1L, 100L, "approved");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<String> result = service.review(1L, "approve", null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("仅待审核商品可审核", result.getMessage());
|
|
|
- verify(productMapper, never()).updateById(any());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void review_productNotFound_returnsError() {
|
|
|
- when(productMapper.selectById(999L)).thenReturn(null);
|
|
|
-
|
|
|
- Result<String> result = service.review(999L, "approve", null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("商品不存在", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- // ==================== create ====================
|
|
|
-
|
|
|
- @Test
|
|
|
- public void create_vendorNull_returnsError() {
|
|
|
- Product product = new Product();
|
|
|
- Result<ProductDTO> result = service.create(product, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("请先登录", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void create_vendorNotApproved_returnsError() {
|
|
|
- com.etotem.cfc.entity.User user = new com.etotem.cfc.entity.User();
|
|
|
- user.setVendorStatus("pending");
|
|
|
- when(userMapper.selectById(100L)).thenReturn(user);
|
|
|
-
|
|
|
- Product product = new Product();
|
|
|
- Result<ProductDTO> result = service.create(product, 100L);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("仅审核通过的服务商可发布商品", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- // ==================== detail ====================
|
|
|
-
|
|
|
- @Test
|
|
|
- public void detail_productNotFound_returnsError() {
|
|
|
- when(productMapper.selectById(999L)).thenReturn(null);
|
|
|
-
|
|
|
- Result<ProductDTO> result = service.detail(999L);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("商品不存在", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void detail_notOnShelf_returnsError() {
|
|
|
- Product product = mockProduct(1L, 100L, "pending");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<ProductDTO> result = service.detail(1L);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("商品未上架", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void detail_approvedStatus_returnsSuccess() {
|
|
|
- Product product = mockProduct(1L, 100L, "approved");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(product);
|
|
|
-
|
|
|
- Result<ProductDTO> result = service.detail(1L);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- }
|
|
|
-
|
|
|
- // ==================== update ====================
|
|
|
-
|
|
|
- @Test
|
|
|
- public void update_wrongVendor_returnsError() {
|
|
|
- Product existing = mockProduct(1L, 100L, "approved");
|
|
|
- when(productMapper.selectById(1L)).thenReturn(existing);
|
|
|
-
|
|
|
- Product update = new Product();
|
|
|
- update.setId(1L);
|
|
|
- Result<ProductDTO> result = service.update(update, 999L);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("无权修改此商品", result.getMessage());
|
|
|
- }
|
|
|
-}
|