package zbooksoft.fams.service;
|
|
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.mockito.InjectMocks;
|
import org.mockito.Mock;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
import zbooksoft.fams.common.BusinessException;
|
import zbooksoft.fams.dao.LabelDao;
|
|
import java.math.BigDecimal;
|
import java.sql.Timestamp;
|
import java.util.Arrays;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
import static org.mockito.ArgumentMatchers.*;
|
import static org.mockito.Mockito.*;
|
|
@ExtendWith(MockitoExtension.class)
|
class LabelServiceTest {
|
|
@Mock
|
private LabelDao labelDao;
|
|
@InjectMocks
|
private LabelService service;
|
|
private static Map<String, Object> map(Object... kv) {
|
Map<String, Object> m = new HashMap<>();
|
for (int i = 0; i < kv.length; i += 2) {
|
m.put((String) kv[i], kv[i + 1]);
|
}
|
return m;
|
}
|
|
private Map<String, Object> template() {
|
return map("ID", 1L, "TEMPLATE_NAME", "办公设备标签", "LABEL_WIDTH", new BigDecimal("60"), "LABEL_HEIGHT", new BigDecimal("40"));
|
}
|
|
// ---------------- 布局校验与保存 ----------------
|
|
@Test
|
void validLayoutIsSaved() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
String layout = "{\"elements\":[{\"type\":\"qrcode\",\"x\":2,\"y\":2,\"w\":20,\"h\":20},"
|
+ "{\"type\":\"field\",\"field\":\"ASSET_NO\",\"x\":24,\"y\":2,\"w\":34,\"h\":6,\"fontSize\":9,\"bold\":true,\"showTitle\":true},"
|
+ "{\"type\":\"text\",\"text\":\"固定资产\",\"x\":24,\"y\":10,\"w\":34,\"h\":6,\"fontSize\":9}]}";
|
|
service.saveLayout(1L, layout);
|
|
verify(labelDao).updateTemplateLayout(eq(1L), argThat(json -> json.contains("ASSET_NO") && json.contains("qrcode")));
|
}
|
|
@Test
|
void layoutWithUnknownFieldIsRejected() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
String layout = "{\"elements\":[{\"type\":\"field\",\"field\":\"PASSWORD\",\"x\":0,\"y\":0,\"w\":10,\"h\":5}]}";
|
|
BusinessException e = assertThrows(BusinessException.class, () -> service.saveLayout(1L, layout));
|
assertTrue(e.getMessage().contains("PASSWORD"));
|
verify(labelDao, never()).updateTemplateLayout(anyLong(), anyString());
|
}
|
|
@Test
|
void layoutWithUnknownTypeIsRejected() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
String layout = "{\"elements\":[{\"type\":\"script\",\"x\":0,\"y\":0,\"w\":10,\"h\":5}]}";
|
|
assertThrows(BusinessException.class, () -> service.saveLayout(1L, layout));
|
}
|
|
@Test
|
void elementOutsideLabelIsRejected() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
String layout = "{\"elements\":[{\"type\":\"text\",\"text\":\"x\",\"x\":55,\"y\":0,\"w\":10,\"h\":5}]}";
|
|
BusinessException e = assertThrows(BusinessException.class, () -> service.saveLayout(1L, layout));
|
assertTrue(e.getMessage().contains("超出"));
|
}
|
|
@Test
|
void malformedJsonIsRejected() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
assertThrows(BusinessException.class, () -> service.saveLayout(1L, "{not json"));
|
}
|
|
@Test
|
void missingTemplateIsRejected() {
|
when(labelDao.findTemplate(9L)).thenReturn(null);
|
assertThrows(BusinessException.class, () -> service.saveLayout(9L, "{\"elements\":[]}"));
|
}
|
|
@Test
|
void textIsLimitedInLength() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
StringBuilder longText = new StringBuilder();
|
for (int i = 0; i < 300; i++) {
|
longText.append('字');
|
}
|
String layout = "{\"elements\":[{\"type\":\"text\",\"text\":\"" + longText + "\",\"x\":0,\"y\":0,\"w\":10,\"h\":5}]}";
|
|
assertThrows(BusinessException.class, () -> service.saveLayout(1L, layout));
|
}
|
|
// ---------------- 模板表单保存 ----------------
|
|
@Test
|
void templateSizeMustBeReasonable() {
|
assertThrows(BusinessException.class, () -> service.prepareTemplate(map("LABEL_WIDTH", "abc", "LABEL_HEIGHT", "40")));
|
assertThrows(BusinessException.class, () -> service.prepareTemplate(map("LABEL_WIDTH", "5", "LABEL_HEIGHT", "40")));
|
assertThrows(BusinessException.class, () -> service.prepareTemplate(map("LABEL_WIDTH", "60", "LABEL_HEIGHT", "500")));
|
}
|
|
@Test
|
void templateDefaultsAreFilled() {
|
Map<String, Object> form = map("LABEL_WIDTH", " 60 ", "LABEL_HEIGHT", "40.5", "IS_DEFAULT", "", "ENABLE_STATE", "");
|
service.prepareTemplate(form);
|
assertEquals("60", form.get("LABEL_WIDTH"));
|
assertEquals("40.5", form.get("LABEL_HEIGHT"));
|
assertEquals("否", form.get("IS_DEFAULT"));
|
assertEquals("启用", form.get("ENABLE_STATE"));
|
}
|
|
@Test
|
void savingDefaultTemplateClearsOtherDefaults() {
|
when(labelDao.findTemplate(1L)).thenReturn(map("ID", 1L, "IS_DEFAULT", "是"));
|
service.keepSingleDefault(1L);
|
verify(labelDao).clearDefaultExcept(1L);
|
|
when(labelDao.findTemplate(2L)).thenReturn(map("ID", 2L, "IS_DEFAULT", "否"));
|
service.keepSingleDefault(2L);
|
verify(labelDao, never()).clearDefaultExcept(2L);
|
}
|
|
// ---------------- 打印数据 ----------------
|
|
@Test
|
void printAssetsAreFormattedForLabels() {
|
when(labelDao.findCardsForLabel(Arrays.asList(10L, 11L))).thenReturn(Collections.singletonList(
|
map("ID", 10L, "ASSET_NO", "BG00001", "PURCHASE_DATE", Timestamp.valueOf("2026-01-15 00:00:00"),
|
"ORIGINAL_VALUE", new BigDecimal("5999.5"), "USER_NAME", null)));
|
|
List<Map<String, Object>> assets = service.loadPrintAssets("10,11");
|
|
assertEquals(1, assets.size());
|
assertEquals("2026-01-15", assets.get(0).get("PURCHASE_DATE"));
|
assertEquals("5999.50", assets.get(0).get("ORIGINAL_VALUE"));
|
assertEquals("", assets.get(0).get("USER_NAME"));
|
assertEquals("10", assets.get(0).get("ID"));
|
}
|
|
@Test
|
void tooManyAssetsAreRejected() {
|
StringBuilder ids = new StringBuilder();
|
for (int i = 1; i <= LabelService.MAX_PRINT_COUNT + 1; i++) {
|
ids.append(i).append(',');
|
}
|
assertThrows(BusinessException.class, () -> service.loadPrintAssets(ids.toString()));
|
}
|
|
@Test
|
void invalidIdsAreRejected() {
|
assertThrows(BusinessException.class, () -> service.loadPrintAssets("1,abc"));
|
assertThrows(BusinessException.class, () -> service.loadPrintAssets(""));
|
}
|
|
@Test
|
void printIsRecordedForEachFoundAsset() {
|
when(labelDao.findTemplate(1L)).thenReturn(template());
|
when(labelDao.findCardsForLabel(Arrays.asList(10L, 11L))).thenReturn(Arrays.asList(
|
map("ID", 10L, "ASSET_NO", "BG00001", "ASSET_NAME", "电脑"),
|
map("ID", 11L, "ASSET_NO", "BG00002", "ASSET_NAME", "打印机")));
|
|
int count = service.recordPrint("10,11", 1L, 5L, "张三");
|
|
assertEquals(2, count);
|
verify(labelDao).insertPrintLog(argThat(row -> Long.valueOf(10L).equals(row.get("ASSET_ID"))
|
&& "办公设备标签".equals(row.get("TEMPLATE_NAME")) && "张三".equals(row.get("PRINT_USER_NAME"))));
|
verify(labelDao).insertPrintLog(argThat(row -> Long.valueOf(11L).equals(row.get("ASSET_ID"))));
|
verify(labelDao).markPrinted(Arrays.asList(10L, 11L));
|
}
|
|
@Test
|
void printTimeKeepsMinutesWhileDatesKeepDays() {
|
when(labelDao.findCardsForLabel(Collections.singletonList(10L))).thenReturn(Collections.singletonList(
|
map("ID", 10L, "VERIFY_EXPIRE_DATE", Timestamp.valueOf("2026-11-01 00:00:00"),
|
"LAST_PRINT_TIME", Timestamp.valueOf("2026-10-16 10:20:30"))));
|
|
Map<String, Object> asset = service.loadPrintAssets("10").get(0);
|
|
assertEquals("2026-11-01", asset.get("VERIFY_EXPIRE_DATE"));
|
assertEquals("2026-10-16 10:20", asset.get("LAST_PRINT_TIME"));
|
}
|
}
|