|
|
|
|
@ -5,10 +5,15 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.anyList;
|
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
|
import static org.mockito.Mockito.times;
|
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import com.ruoyi.asset.constant.RfidBindStatus;
|
|
|
|
|
import com.ruoyi.asset.constant.RfidTagStatus;
|
|
|
|
|
import com.ruoyi.asset.domain.AmsRfidTag;
|
|
|
|
|
@ -17,6 +22,7 @@ import com.ruoyi.asset.mapper.AmsRfidTagMapper;
|
|
|
|
|
import com.ruoyi.asset.service.IAssetLifecycleService;
|
|
|
|
|
import com.ruoyi.asset.service.IRfidBindingService;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import com.ruoyi.system.service.ISysCodeRuleService;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
|
|
@ -36,6 +42,9 @@ class AmsRfidTagServiceImplTest
|
|
|
|
|
@Mock
|
|
|
|
|
private IRfidBindingService rfidBindingService;
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private ISysCodeRuleService sysCodeRuleService;
|
|
|
|
|
|
|
|
|
|
@InjectMocks
|
|
|
|
|
private AmsRfidTagServiceImpl service;
|
|
|
|
|
|
|
|
|
|
@ -81,6 +90,25 @@ class AmsRfidTagServiceImplTest
|
|
|
|
|
verify(amsRfidTagMapper).selectAmsRfidTagByTagIdForUpdate(10L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 新增建档必须统一使用编码规则生成标签编码,不能信任页面或接口传入的手工编码 */
|
|
|
|
|
@Test
|
|
|
|
|
void insertShouldGenerateTagCodeAndIgnoreIncomingTagCode()
|
|
|
|
|
{
|
|
|
|
|
AmsRfidTag tag = buildUnboundTag();
|
|
|
|
|
tag.setTagId(null);
|
|
|
|
|
tag.setTagCode("MANUAL-001");
|
|
|
|
|
when(sysCodeRuleService.nextCode("RFID_TAG")).thenReturn("BQ20260702000031");
|
|
|
|
|
when(amsRfidTagMapper.insertAmsRfidTag(any(AmsRfidTag.class))).thenReturn(1);
|
|
|
|
|
|
|
|
|
|
assertEquals(1, service.insertAmsRfidTag(tag));
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<AmsRfidTag> tagCaptor = ArgumentCaptor.forClass(AmsRfidTag.class);
|
|
|
|
|
verify(amsRfidTagMapper).insertAmsRfidTag(tagCaptor.capture());
|
|
|
|
|
assertEquals("BQ20260702000031", tagCaptor.getValue().getTagCode());
|
|
|
|
|
assertEquals(RfidTagStatus.NORMAL, tagCaptor.getValue().getTagStatus());
|
|
|
|
|
assertEquals(RfidBindStatus.UNBOUND, tagCaptor.getValue().getBindStatus());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 标签修改应先锁定记录并保留当前绑定状态 */
|
|
|
|
|
@Test
|
|
|
|
|
void updateShouldLockTagAndPreserveControlledFields()
|
|
|
|
|
@ -88,6 +116,7 @@ class AmsRfidTagServiceImplTest
|
|
|
|
|
AmsRfidTag current = buildUnboundTag();
|
|
|
|
|
current.setTagStatus(RfidTagStatus.VOID);
|
|
|
|
|
AmsRfidTag incoming = buildUnboundTag();
|
|
|
|
|
incoming.setTagCode("MANUAL-EDIT");
|
|
|
|
|
incoming.setTagStatus(RfidTagStatus.NORMAL);
|
|
|
|
|
incoming.setBindStatus(RfidBindStatus.BOUND);
|
|
|
|
|
incoming.setAssetId(1L);
|
|
|
|
|
@ -98,6 +127,7 @@ class AmsRfidTagServiceImplTest
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<AmsRfidTag> tagCaptor = ArgumentCaptor.forClass(AmsRfidTag.class);
|
|
|
|
|
verify(amsRfidTagMapper).updateAmsRfidTag(tagCaptor.capture());
|
|
|
|
|
assertEquals("TAG-001", tagCaptor.getValue().getTagCode());
|
|
|
|
|
assertEquals(RfidTagStatus.VOID, tagCaptor.getValue().getTagStatus());
|
|
|
|
|
assertEquals(RfidBindStatus.UNBOUND, tagCaptor.getValue().getBindStatus());
|
|
|
|
|
assertNull(tagCaptor.getValue().getAssetId());
|
|
|
|
|
@ -117,6 +147,90 @@ class AmsRfidTagServiceImplTest
|
|
|
|
|
verify(amsRfidTagMapper, never()).voidAmsRfidTagByTagId(any(), any(), any());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 空导入文件直接失败,不触发数据库查询 */
|
|
|
|
|
@Test
|
|
|
|
|
void importShouldRejectEmptyList()
|
|
|
|
|
{
|
|
|
|
|
ServiceException exception = assertThrows(ServiceException.class,
|
|
|
|
|
() -> service.importAmsRfidTag(Collections.emptyList(), "admin"));
|
|
|
|
|
|
|
|
|
|
assertEquals("导入RFID标签数据不能为空!", exception.getMessage());
|
|
|
|
|
verify(amsRfidTagMapper, never()).selectAmsRfidTagByEpcCodes(anyList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 导入允许有效行先入库,并把空EPC、文件内重复和库内重复汇总提示 */
|
|
|
|
|
@Test
|
|
|
|
|
void importShouldPersistValidRowsAndReportInvalidRows()
|
|
|
|
|
{
|
|
|
|
|
AmsRfidTag existingTag = new AmsRfidTag();
|
|
|
|
|
existingTag.setEpcCode("EPC-OLD");
|
|
|
|
|
when(amsRfidTagMapper.selectAmsRfidTagByEpcCodes(anyList())).thenReturn(List.of(existingTag));
|
|
|
|
|
when(sysCodeRuleService.nextCode("RFID_TAG")).thenReturn("BQ20260702000001");
|
|
|
|
|
when(amsRfidTagMapper.insertAmsRfidTag(any(AmsRfidTag.class))).thenReturn(1);
|
|
|
|
|
|
|
|
|
|
String message = service.importAmsRfidTag(List.of(
|
|
|
|
|
buildImportTag(" EPC-001 "),
|
|
|
|
|
buildImportTag("EPC-001"),
|
|
|
|
|
buildImportTag(" "),
|
|
|
|
|
buildImportTag("EPC-OLD")
|
|
|
|
|
), "admin");
|
|
|
|
|
|
|
|
|
|
assertTrue(message.contains("RFID标签部分导入完成"));
|
|
|
|
|
assertTrue(message.contains("成功 1 条,失败 3 条"));
|
|
|
|
|
assertTrue(message.contains("首次出现于第1条"));
|
|
|
|
|
assertTrue(message.contains("EPC编码不能为空"));
|
|
|
|
|
assertTrue(message.contains("EPC编码已存在"));
|
|
|
|
|
verify(sysCodeRuleService, times(1)).nextCode("RFID_TAG");
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<AmsRfidTag> tagCaptor = ArgumentCaptor.forClass(AmsRfidTag.class);
|
|
|
|
|
verify(amsRfidTagMapper).insertAmsRfidTag(tagCaptor.capture());
|
|
|
|
|
assertEquals("BQ20260702000001", tagCaptor.getValue().getTagCode());
|
|
|
|
|
assertEquals("EPC-001", tagCaptor.getValue().getEpcCode());
|
|
|
|
|
assertEquals(RfidTagStatus.NORMAL, tagCaptor.getValue().getTagStatus());
|
|
|
|
|
assertEquals(RfidBindStatus.UNBOUND, tagCaptor.getValue().getBindStatus());
|
|
|
|
|
assertNull(tagCaptor.getValue().getAssetId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 单次导入设置上限,避免超大Excel长时间占用请求和编码规则流水锁 */
|
|
|
|
|
@Test
|
|
|
|
|
void importShouldRejectTooManyRows()
|
|
|
|
|
{
|
|
|
|
|
List<AmsRfidTag> tagList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < 5001; i++)
|
|
|
|
|
{
|
|
|
|
|
tagList.add(buildImportTag("EPC-" + i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServiceException exception = assertThrows(ServiceException.class,
|
|
|
|
|
() -> service.importAmsRfidTag(tagList, "admin"));
|
|
|
|
|
|
|
|
|
|
assertTrue(exception.getMessage().contains("单次最多导入5000条"));
|
|
|
|
|
verify(amsRfidTagMapper, never()).selectAmsRfidTagByEpcCodes(anyList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** EPC库内预查按固定批次拆分,避免超长IN条件影响生产环境稳定性 */
|
|
|
|
|
@Test
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
void importShouldBatchExistingEpcLookup()
|
|
|
|
|
{
|
|
|
|
|
List<AmsRfidTag> tagList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < 501; i++)
|
|
|
|
|
{
|
|
|
|
|
tagList.add(buildImportTag("EPC-" + i));
|
|
|
|
|
}
|
|
|
|
|
int[] serial = {1};
|
|
|
|
|
when(amsRfidTagMapper.selectAmsRfidTagByEpcCodes(anyList())).thenReturn(Collections.emptyList());
|
|
|
|
|
when(sysCodeRuleService.nextCode("RFID_TAG")).thenAnswer(invocation -> "BQ" + serial[0]++);
|
|
|
|
|
when(amsRfidTagMapper.insertAmsRfidTag(any(AmsRfidTag.class))).thenReturn(1);
|
|
|
|
|
|
|
|
|
|
service.importAmsRfidTag(tagList, "admin");
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<List<String>> epcCodesCaptor = ArgumentCaptor.forClass(List.class);
|
|
|
|
|
verify(amsRfidTagMapper, times(2)).selectAmsRfidTagByEpcCodes(epcCodesCaptor.capture());
|
|
|
|
|
assertEquals(500, epcCodesCaptor.getAllValues().get(0).size());
|
|
|
|
|
assertEquals(1, epcCodesCaptor.getAllValues().get(1).size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AmsRfidTag buildUnboundTag()
|
|
|
|
|
{
|
|
|
|
|
AmsRfidTag tag = new AmsRfidTag();
|
|
|
|
|
@ -127,4 +241,11 @@ class AmsRfidTagServiceImplTest
|
|
|
|
|
tag.setBindStatus(RfidBindStatus.UNBOUND);
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AmsRfidTag buildImportTag(String epcCode)
|
|
|
|
|
{
|
|
|
|
|
AmsRfidTag tag = new AmsRfidTag();
|
|
|
|
|
tag.setEpcCode(epcCode);
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|