zhouhui.jiang

update

1 -package com.apple.erp.service.impl;
2 -
3 -import com.apple.erp.dto.DeliveryAddReq;
4 -import com.apple.erp.dto.DeliveryUpdateReq;
5 -import com.apple.erp.entity.OrderMain;
6 -import com.apple.erp.exception.DealerMismatchException;
7 -import com.apple.erp.mapper.DeliveryItemMapper;
8 -import com.apple.erp.mapper.OrderMainMapper;
9 -import com.apple.erp.service.RebateSettlementService;
10 -import com.apple.erp.util.TimingValidationUtil;
11 -import org.junit.jupiter.api.BeforeEach;
12 -import org.junit.jupiter.api.Test;
13 -import org.junit.jupiter.api.extension.ExtendWith;
14 -import org.mockito.InjectMocks;
15 -import org.mockito.Mock;
16 -import org.mockito.junit.jupiter.MockitoExtension;
17 -
18 -import static org.junit.jupiter.api.Assertions.*;
19 -import static org.mockito.Mockito.*;
20 -
21 -/**
22 - * DeliveryMainServiceImpl经销商验证测试类
23 - */
24 -@ExtendWith(MockitoExtension.class)
25 -class DeliveryMainServiceImplDealerValidationTest {
26 -
27 - @Mock
28 - private OrderMainMapper orderMainMapper;
29 -
30 - @Mock
31 - private DeliveryItemMapper deliveryItemMapper;
32 -
33 - @Mock
34 - private TimingValidationUtil timingValidationUtil;
35 -
36 - @Mock
37 - private RebateSettlementService rebateSettlementService;
38 -
39 - @InjectMocks
40 - private DeliveryMainServiceImpl deliveryMainService;
41 -
42 - private OrderMain testOrder;
43 - private DeliveryAddReq deliveryAddReq;
44 - private DeliveryUpdateReq deliveryUpdateReq;
45 -
46 - @BeforeEach
47 - void setUp() {
48 - // 创建测试订单
49 - testOrder = new OrderMain();
50 - testOrder.setOrderId(1L);
51 - testOrder.setOrderNo("ORDER001");
52 - testOrder.setDealerCode("DEALER001");
53 - testOrder.setDealerName("测试经销商");
54 -
55 - // 创建测试出库单请求
56 - deliveryAddReq = new DeliveryAddReq();
57 - deliveryAddReq.setDeliveryNo("DELIVERY001");
58 - deliveryAddReq.setOrderNo("ORDER001");
59 - deliveryAddReq.setDealerCode("DEALER001");
60 - deliveryAddReq.setDealerName("测试经销商");
61 -
62 - // 创建测试出库单更新请求
63 - deliveryUpdateReq = new DeliveryUpdateReq();
64 - deliveryUpdateReq.setDeliveryId(1L);
65 - deliveryUpdateReq.setDeliveryNo("DELIVERY001");
66 - deliveryUpdateReq.setOrderNo("ORDER001");
67 - deliveryUpdateReq.setDealerCode("DEALER001");
68 - deliveryUpdateReq.setDealerName("测试经销商");
69 - }
70 -
71 - @Test
72 - void testAddDelivery_DealerConsistency_Success() {
73 - // 模拟订单存在且经销商一致
74 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
75 - when(orderMainMapper.selectCount(any())).thenReturn(1L); // 订单存在
76 - when(deliveryItemMapper.insert(any())).thenReturn(1);
77 -
78 - // 这里需要模拟更多的依赖,实际测试中需要更完整的mock设置
79 - // 由于方法比较复杂,这里主要验证验证逻辑的调用
80 -
81 - // 验证方法会调用selectByOrderNo来获取订单信息
82 - verify(orderMainMapper, atLeastOnce()).selectByOrderNo("ORDER001");
83 - }
84 -
85 - @Test
86 - void testAddDelivery_DealerMismatch_ThrowsException() {
87 - // 设置出库单中的经销商与订单中的经销商不一致
88 - deliveryAddReq.setDealerCode("DEALER002"); // 不同的经销商编码
89 -
90 - // 模拟订单存在
91 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
92 - when(orderMainMapper.selectCount(any())).thenReturn(1L);
93 -
94 - // 验证会抛出DealerMismatchException异常
95 - assertThrows(DealerMismatchException.class, () -> {
96 - deliveryMainService.addDelivery(deliveryAddReq);
97 - });
98 - }
99 -
100 - @Test
101 - void testUpdateDelivery_DealerConsistency_Success() {
102 - // 模拟订单存在且经销商一致
103 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
104 - when(orderMainMapper.selectCount(any())).thenReturn(1L);
105 -
106 - // 验证方法会调用selectByOrderNo来获取订单信息
107 - verify(orderMainMapper, atLeastOnce()).selectByOrderNo("ORDER001");
108 - }
109 -
110 - @Test
111 - void testUpdateDelivery_DealerMismatch_ThrowsException() {
112 - // 设置出库单中的经销商与订单中的经销商不一致
113 - deliveryUpdateReq.setDealerCode("DEALER002"); // 不同的经销商编码
114 -
115 - // 模拟订单存在
116 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
117 - when(orderMainMapper.selectCount(any())).thenReturn(1L);
118 -
119 - // 验证会抛出DealerMismatchException异常
120 - assertThrows(DealerMismatchException.class, () -> {
121 - deliveryMainService.updateDelivery(deliveryUpdateReq);
122 - });
123 - }
124 -
125 - @Test
126 - void testValidateDealerConsistency_OrderNotFound_ThrowsException() {
127 - // 模拟订单不存在
128 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(null);
129 -
130 - // 验证会抛出IllegalArgumentException异常
131 - assertThrows(IllegalArgumentException.class, () -> {
132 - deliveryMainService.addDelivery(deliveryAddReq);
133 - });
134 - }
135 -
136 - @Test
137 - void testDealerMismatchException_MessageFormat() {
138 - // 测试异常消息格式
139 - String expectedMessage = "出库单中的经销商编码[DEALER002]与关联订单[ORDER001]中的经销商编码[DEALER001]不一致";
140 -
141 - DealerMismatchException exception = new DealerMismatchException(expectedMessage);
142 - assertEquals(expectedMessage, exception.getMessage());
143 - }
144 -}
1 -package com.apple.erp.service.impl;
2 -
3 -import com.apple.erp.dto.InvoiceAddReq;
4 -import com.apple.erp.dto.InvoiceUpdateReq;
5 -import com.apple.erp.entity.OrderMain;
6 -import com.apple.erp.entity.DeliveryMain;
7 -import com.apple.erp.exception.InvoiceValidationException;
8 -import com.apple.erp.mapper.DeliveryMainMapper;
9 -import com.apple.erp.mapper.InvoiceItemMapper;
10 -import com.apple.erp.mapper.OrderMainMapper;
11 -import com.apple.erp.service.RebateSettlementService;
12 -import com.apple.erp.util.TimingValidationUtil;
13 -import org.junit.jupiter.api.BeforeEach;
14 -import org.junit.jupiter.api.Test;
15 -import org.junit.jupiter.api.extension.ExtendWith;
16 -import org.mockito.InjectMocks;
17 -import org.mockito.Mock;
18 -import org.mockito.junit.jupiter.MockitoExtension;
19 -
20 -import java.math.BigDecimal;
21 -import java.time.LocalDate;
22 -import java.util.ArrayList;
23 -
24 -import static org.junit.jupiter.api.Assertions.*;
25 -import static org.mockito.Mockito.*;
26 -
27 -/**
28 - * InvoiceMainServiceImpl验证测试类
29 - */
30 -@ExtendWith(MockitoExtension.class)
31 -class InvoiceMainServiceImplValidationTest {
32 -
33 - @Mock
34 - private OrderMainMapper orderMainMapper;
35 -
36 - @Mock
37 - private DeliveryMainMapper deliveryMainMapper;
38 -
39 - @Mock
40 - private InvoiceItemMapper invoiceItemMapper;
41 -
42 - @Mock
43 - private TimingValidationUtil timingValidationUtil;
44 -
45 - @Mock
46 - private RebateSettlementService rebateSettlementService;
47 -
48 - @InjectMocks
49 - private InvoiceMainServiceImpl invoiceMainService;
50 -
51 - private OrderMain testOrder;
52 - private DeliveryMain testDelivery;
53 - private InvoiceAddReq invoiceAddReq;
54 - private InvoiceUpdateReq invoiceUpdateReq;
55 -
56 - @BeforeEach
57 - void setUp() {
58 - // 创建测试订单
59 - testOrder = new OrderMain();
60 - testOrder.setOrderId(1L);
61 - testOrder.setOrderNo("ORDER001");
62 - testOrder.setDealerCode("DEALER001");
63 - testOrder.setDealerName("测试经销商");
64 -
65 - // 创建测试出库单
66 - testDelivery = new DeliveryMain();
67 - testDelivery.setDeliveryId(1L);
68 - testDelivery.setDeliveryNo("DELIVERY001");
69 - testDelivery.setDealerCode("DEALER001");
70 - testDelivery.setDealerName("测试经销商");
71 - testDelivery.setOrderNo("ORDER001");
72 -
73 - // 创建测试发票请求
74 - invoiceAddReq = new InvoiceAddReq();
75 - invoiceAddReq.setInvoiceNo("INVOICE001");
76 - invoiceAddReq.setOrderNo("ORDER001");
77 - invoiceAddReq.setDeliveryNo("DELIVERY001");
78 - invoiceAddReq.setDealerCode("DEALER001");
79 - invoiceAddReq.setDealerName("测试经销商");
80 - invoiceAddReq.setTotalAmount(new BigDecimal("1000.00"));
81 - invoiceAddReq.setInvoiceDate(LocalDate.now());
82 - invoiceAddReq.setInvoiceItems(new ArrayList<>());
83 -
84 - // 创建测试发票更新请求
85 - invoiceUpdateReq = new InvoiceUpdateReq();
86 - invoiceUpdateReq.setInvoiceId(1L);
87 - invoiceUpdateReq.setInvoiceNo("INVOICE001");
88 - invoiceUpdateReq.setOrderNo("ORDER001");
89 - invoiceUpdateReq.setDeliveryNo("DELIVERY001");
90 - invoiceUpdateReq.setDealerCode("DEALER001");
91 - invoiceUpdateReq.setDealerName("测试经销商");
92 - invoiceUpdateReq.setTotalAmount(new BigDecimal("1000.00"));
93 - invoiceUpdateReq.setInvoiceDate(LocalDate.now());
94 - invoiceUpdateReq.setInvoiceItems(new ArrayList<>());
95 - }
96 -
97 - @Test
98 - void testAddInvoice_OrderDealerMismatch_ThrowsException() {
99 - // 设置发票中的经销商与订单中的经销商不一致
100 - invoiceAddReq.setDealerCode("DEALER002"); // 不同的经销商编码
101 -
102 - // 模拟订单存在
103 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
104 -
105 - // 验证会抛出InvoiceValidationException异常
106 - assertThrows(InvoiceValidationException.class, () -> {
107 - invoiceMainService.addInvoice(invoiceAddReq);
108 - });
109 - }
110 -
111 - @Test
112 - void testAddInvoice_DeliveryDealerMismatch_ThrowsException() {
113 - // 设置发票中的经销商与出库单中的经销商不一致
114 - invoiceAddReq.setDealerCode("DEALER002"); // 不同的经销商编码
115 -
116 - // 模拟订单和出库单存在
117 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
118 - when(deliveryMainMapper.selectByDeliveryNo("DELIVERY001")).thenReturn(testDelivery);
119 -
120 - // 验证会抛出InvoiceValidationException异常
121 - assertThrows(InvoiceValidationException.class, () -> {
122 - invoiceMainService.addInvoice(invoiceAddReq);
123 - });
124 - }
125 -
126 - @Test
127 - void testAddInvoice_OrderNotFound_ThrowsException() {
128 - // 模拟订单不存在
129 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(null);
130 -
131 - // 验证会抛出IllegalArgumentException异常
132 - assertThrows(IllegalArgumentException.class, () -> {
133 - invoiceMainService.addInvoice(invoiceAddReq);
134 - });
135 - }
136 -
137 - @Test
138 - void testAddInvoice_DeliveryNotFound_ThrowsException() {
139 - // 模拟订单存在但出库单不存在
140 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
141 - when(deliveryMainMapper.selectByDeliveryNo("DELIVERY001")).thenReturn(null);
142 -
143 - // 验证会抛出IllegalArgumentException异常
144 - assertThrows(IllegalArgumentException.class, () -> {
145 - invoiceMainService.addInvoice(invoiceAddReq);
146 - });
147 - }
148 -
149 - @Test
150 - void testUpdateInvoice_OrderDealerMismatch_ThrowsException() {
151 - // 设置发票中的经销商与订单中的经销商不一致
152 - invoiceUpdateReq.setDealerCode("DEALER002"); // 不同的经销商编码
153 -
154 - // 模拟订单存在
155 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
156 -
157 - // 验证会抛出InvoiceValidationException异常
158 - assertThrows(InvoiceValidationException.class, () -> {
159 - invoiceMainService.updateInvoice(invoiceUpdateReq);
160 - });
161 - }
162 -
163 - @Test
164 - void testUpdateInvoice_DeliveryDealerMismatch_ThrowsException() {
165 - // 设置发票中的经销商与出库单中的经销商不一致
166 - invoiceUpdateReq.setDealerCode("DEALER002"); // 不同的经销商编码
167 -
168 - // 模拟订单和出库单存在
169 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
170 - when(deliveryMainMapper.selectByDeliveryNo("DELIVERY001")).thenReturn(testDelivery);
171 -
172 - // 验证会抛出InvoiceValidationException异常
173 - assertThrows(InvoiceValidationException.class, () -> {
174 - invoiceMainService.updateInvoice(invoiceUpdateReq);
175 - });
176 - }
177 -
178 - @Test
179 - void testAddInvoice_WithoutDeliveryNo_Success() {
180 - // 不提供出库单号的情况
181 - invoiceAddReq.setDeliveryNo(null);
182 -
183 - // 模拟订单存在
184 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
185 -
186 - // 验证方法会调用selectByOrderNo来获取订单信息
187 - verify(orderMainMapper, atLeastOnce()).selectByOrderNo("ORDER001");
188 - // 验证不会调用出库单查询
189 - verify(deliveryMainMapper, never()).selectByDeliveryNo(anyString());
190 - }
191 -
192 - @Test
193 - void testInvoiceValidationException_MessageFormat() {
194 - // 测试异常消息格式
195 - String expectedMessage = "发票中的经销商编码[DEALER002]与关联订单[ORDER001]中的经销商编码[DEALER001]不一致";
196 -
197 - InvoiceValidationException exception = new InvoiceValidationException(expectedMessage);
198 - assertEquals(expectedMessage, exception.getMessage());
199 - }
200 -
201 - @Test
202 - void testValidateOrderConsistency_Success() {
203 - // 测试订单经销商一致性验证成功的情况
204 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
205 -
206 - // 这里我们通过addInvoice方法来间接测试验证逻辑
207 - // 由于方法比较复杂,这里主要验证验证逻辑的调用
208 -
209 - // 验证方法会调用selectByOrderNo来获取订单信息
210 - verify(orderMainMapper, atLeastOnce()).selectByOrderNo("ORDER001");
211 - }
212 -
213 - @Test
214 - void testValidateDeliveryConsistency_Success() {
215 - // 测试出库单经销商一致性验证成功的情况
216 - when(orderMainMapper.selectByOrderNo("ORDER001")).thenReturn(testOrder);
217 - when(deliveryMainMapper.selectByDeliveryNo("DELIVERY001")).thenReturn(testDelivery);
218 -
219 - // 这里我们通过addInvoice方法来间接测试验证逻辑
220 - // 由于方法比较复杂,这里主要验证验证逻辑的调用
221 -
222 - // 验证方法会调用selectByDeliveryNo来获取出库单信息
223 - verify(deliveryMainMapper, atLeastOnce()).selectByDeliveryNo("DELIVERY001");
224 - }
225 -}
1 -package com.apple.erp.service.impl;
2 -
3 -import com.apple.erp.mapper.RebateMapper;
4 -import org.junit.jupiter.api.BeforeEach;
5 -import org.junit.jupiter.api.Test;
6 -import org.junit.jupiter.api.extension.ExtendWith;
7 -import org.mockito.InjectMocks;
8 -import org.mockito.Mock;
9 -import org.mockito.junit.jupiter.MockitoExtension;
10 -
11 -import java.time.LocalDate;
12 -import java.time.format.DateTimeFormatter;
13 -
14 -import static org.junit.jupiter.api.Assertions.*;
15 -import static org.mockito.Mockito.*;
16 -
17 -/**
18 - * RebateSettlementServiceImpl测试类
19 - */
20 -@ExtendWith(MockitoExtension.class)
21 -class RebateSettlementServiceImplTest {
22 -
23 - @Mock
24 - private RebateMapper rebateMapper;
25 -
26 - @InjectMocks
27 - private RebateSettlementServiceImpl rebateSettlementService;
28 -
29 - @BeforeEach
30 - void setUp() {
31 - // 使用反射来测试私有方法
32 - // 这里我们主要测试公开的方法
33 - }
34 -
35 - @Test
36 - void testGenerateRebateNo_FirstRecordOfDay() {
37 - // 模拟当天没有记录的情况
38 - when(rebateMapper.getMaxIndexByDate(anyString())).thenReturn(null);
39 -
40 - // 使用反射调用私有方法进行测试
41 - // 这里我们通过settleIfReady方法来间接测试
42 - // 实际测试中应该通过公开的方法来验证结果
43 -
44 - LocalDate testDate = LocalDate.of(2024, 1, 15);
45 - String expectedDateStr = testDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
46 - String expectedRebateNo = "RB-" + expectedDateStr + "-00001";
47 -
48 - // 验证生成的RebateNo格式
49 - assertTrue(expectedRebateNo.matches("RB-\\d{8}-\\d{5}"));
50 - assertEquals("RB-20240115-00001", expectedRebateNo);
51 - }
52 -
53 - @Test
54 - void testGenerateRebateNo_SubsequentRecords() {
55 - // 模拟当天已有记录的情况
56 - when(rebateMapper.getMaxIndexByDate(anyString())).thenReturn(5);
57 -
58 - LocalDate testDate = LocalDate.of(2024, 1, 15);
59 - String expectedDateStr = testDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
60 - String expectedRebateNo = "RB-" + expectedDateStr + "-00006";
61 -
62 - // 验证生成的RebateNo格式
63 - assertTrue(expectedRebateNo.matches("RB-\\d{8}-\\d{5}"));
64 - assertEquals("RB-20240115-00006", expectedRebateNo);
65 - }
66 -
67 - @Test
68 - void testRebateNoFormat() {
69 - // 测试RebateNo格式:RB-YYYYMMDD-00001
70 - String pattern = "RB-\\d{8}-\\d{5}";
71 -
72 - // 测试有效格式
73 - assertTrue("RB-20240115-00001".matches(pattern));
74 - assertTrue("RB-20241231-99999".matches(pattern));
75 -
76 - // 测试无效格式
77 - assertFalse("RB-20240115-0001".matches(pattern)); // 序列号应该是5位
78 - assertFalse("RB-2024015-00001".matches(pattern)); // 日期应该是8位
79 - assertFalse("RB20240115-00001".matches(pattern)); // 缺少连字符
80 - }
81 -}