瑞利声电固定资产管理系统
ycq
2026-10-16 dbbadb021c2fc171466cc82bd9a7a82cc47165f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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"));
    }
}