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.AssetBaseDao; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; 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 AssetBaseServiceTest { @Mock private AssetBaseDao assetBaseDao; @InjectMocks private AssetBaseService service; private static Map map(Object... kv) { Map m = new HashMap<>(); for (int i = 0; i < kv.length; i += 2) { m.put((String) kv[i], kv[i + 1]); } return m; } // ---------------- 资产类别 ---------------- @Test void topCategoryRequiresCode() { Map form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", ""); BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCategory(0L, form)); assertTrue(e.getMessage().contains("类别码")); } @Test void topCategoryCodeIsUpperCasedAndLevelIsOne() { Map form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "bg"); when(assetBaseDao.existsTopCategoryCode("BG", 0L)).thenReturn(false); service.prepareCategory(0L, form); assertEquals("BG", form.get("CATEGORY_CODE")); assertEquals(1, form.get("CATEGORY_LEVEL")); assertEquals("BG", form.get("TOP_CATEGORY_CODE")); assertEquals("", form.get("PARENT_NAME")); assertEquals("启用", form.get("ENABLE_STATE")); } @Test void duplicateTopCategoryCodeIsRejected() { Map form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "BG"); when(assetBaseDao.existsTopCategoryCode("BG", 5L)).thenReturn(true); assertThrows(BusinessException.class, () -> service.prepareCategory(5L, form)); } @Test void invalidCategoryCodeIsRejected() { Map form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "办公"); assertThrows(BusinessException.class, () -> service.prepareCategory(0L, form)); } @Test void subCategoryInheritsTopFromLevelOneParent() { Map form = map("PARENT_ID", "10", "CATEGORY_NAME", "电脑", "DEVICE_TYPE", ""); when(assetBaseDao.findCategory(10L)).thenReturn(map("ID", 10L, "CATEGORY_LEVEL", 1, "CATEGORY_CODE", "BG", "CATEGORY_NAME", "办公设备", "DEVICE_TYPE", "普通设备")); service.prepareCategory(0L, form); assertEquals(2, form.get("CATEGORY_LEVEL")); assertEquals(10L, form.get("TOP_CATEGORY_ID")); assertEquals("BG", form.get("TOP_CATEGORY_CODE")); assertEquals("办公设备", form.get("PARENT_NAME")); assertEquals("普通设备", form.get("DEVICE_TYPE")); } @Test void thirdLevelInheritsTopFromParentsTopFields() { Map form = map("PARENT_ID", "20", "CATEGORY_NAME", "笔记本"); when(assetBaseDao.findCategory(20L)).thenReturn(map("ID", 20L, "CATEGORY_LEVEL", 2, "CATEGORY_NAME", "电脑", "TOP_CATEGORY_ID", 10L, "TOP_CATEGORY_CODE", "BG")); service.prepareCategory(0L, form); assertEquals(3, form.get("CATEGORY_LEVEL")); assertEquals(10L, form.get("TOP_CATEGORY_ID")); assertEquals("BG", form.get("TOP_CATEGORY_CODE")); } @Test void fourthLevelIsRejected() { Map form = map("PARENT_ID", "30", "CATEGORY_NAME", "x"); when(assetBaseDao.findCategory(30L)).thenReturn(map("ID", 30L, "CATEGORY_LEVEL", 3, "CATEGORY_NAME", "笔记本")); BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCategory(0L, form)); assertTrue(e.getMessage().contains("三级")); } @Test void categoryCannotBeItsOwnParent() { Map form = map("PARENT_ID", "7", "CATEGORY_NAME", "x"); assertThrows(BusinessException.class, () -> service.prepareCategory(7L, form)); } @Test void categoryWithChildrenCannotChangeParent() { Map form = map("PARENT_ID", "10", "CATEGORY_NAME", "电脑"); when(assetBaseDao.findCategory(20L)).thenReturn(map("ID", 20L, "PARENT_ID", 11L, "CATEGORY_LEVEL", 2)); when(assetBaseDao.hasChildCategory(20L)).thenReturn(true); assertThrows(BusinessException.class, () -> service.prepareCategory(20L, form)); } @Test void syncTopCategoryOnlyForLevelOne() { when(assetBaseDao.findCategory(10L)).thenReturn(map("ID", 10L, "CATEGORY_LEVEL", 1, "CATEGORY_CODE", "BG")); service.syncCategoryTop(10L); verify(assetBaseDao).syncTopCategory(10L, "BG"); when(assetBaseDao.findCategory(20L)).thenReturn(map("ID", 20L, "CATEGORY_LEVEL", 2)); service.syncCategoryTop(20L); verify(assetBaseDao, never()).syncTopCategory(eq(20L), anyString()); } // ---------------- 存放位置 ---------------- @Test void rootLocationFullNameIsItsName() { Map form = map("PARENT_ID", "", "LOCATION_NAME", "1号楼"); service.prepareLocation(0L, form); assertEquals("1号楼", form.get("FULL_NAME")); assertEquals("", form.get("PARENT_NAME")); } @Test void childLocationFullNameJoinsParent() { Map form = map("PARENT_ID", "1", "LOCATION_NAME", "3层"); when(assetBaseDao.findLocation(1L)).thenReturn(map("ID", 1L, "LOCATION_NAME", "1号楼", "FULL_NAME", "1号楼")); service.prepareLocation(0L, form); assertEquals("1号楼/3层", form.get("FULL_NAME")); assertEquals("1号楼", form.get("PARENT_NAME")); } @Test void locationCannotBeItsOwnParent() { Map form = map("PARENT_ID", "1", "LOCATION_NAME", "x"); assertThrows(BusinessException.class, () -> service.prepareLocation(1L, form)); } @Test void refreshLocationDescendantsRewritesSubtreeAndCards() { when(assetBaseDao.findLocation(1L)).thenReturn(map("ID", 1L, "FULL_NAME", "A楼")); when(assetBaseDao.findChildLocations(1L)).thenReturn(Collections.singletonList(map("ID", 2L, "LOCATION_NAME", "3层"))); when(assetBaseDao.findChildLocations(2L)).thenReturn(Collections.singletonList(map("ID", 3L, "LOCATION_NAME", "301"))); when(assetBaseDao.findChildLocations(3L)).thenReturn(Collections.>emptyList()); service.refreshLocationDescendants(1L); verify(assetBaseDao).updateCardLocationName(1L, "A楼"); verify(assetBaseDao).updateLocationFullName(2L, "A楼", "A楼/3层"); verify(assetBaseDao).updateCardLocationName(2L, "A楼/3层"); verify(assetBaseDao).updateLocationFullName(3L, "A楼/3层", "A楼/3层/301"); verify(assetBaseDao).updateCardLocationName(3L, "A楼/3层/301"); } // ---------------- 资产卡片 ---------------- private Map leafCategory() { return map("ID", 30L, "CATEGORY_LEVEL", 3, "CATEGORY_NAME", "笔记本", "TOP_CATEGORY_CODE", "BG", "DEVICE_TYPE", "普通设备", "ENABLE_STATE", "启用", "WARRANTY_MONTHS", 36, "MAINTAIN_MONTHS", 12); } @Test void newCardGetsAssetNoAndDefaults() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "联想笔记本", "ASSET_NO", "", "ASSET_STATE", "", "DEVICE_TYPE", "", "PURCHASE_DATE", "2026-01-15", "ORIGINAL_VALUE", "5999.00"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); when(assetBaseDao.nextSerialNo("BG")).thenReturn(7L); service.prepareCard(0L, form); assertEquals("BG00007", form.get("ASSET_NO")); assertEquals("笔记本", form.get("CATEGORY_NAME")); assertEquals("BG", form.get("TOP_CATEGORY_CODE")); assertEquals("普通设备", form.get("DEVICE_TYPE")); assertEquals("在用", form.get("ASSET_STATE")); assertEquals("手工新增", form.get("SOURCE_TYPE")); assertEquals(0, form.get("LOCK_STATE")); assertEquals(0, form.get("PRINT_STATE")); assertEquals("2029-01-15", form.get("WARRANTY_EXPIRE_DATE")); assertEquals("2027-01-15", form.get("NEXT_MAINTAIN_DATE")); } @Test void existingAssetNoIsKept() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "BG00001", "ASSET_STATE", "闲置", "WARRANTY_EXPIRE_DATE", "2030-01-01"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); service.prepareCard(9L, form); assertEquals("BG00001", form.get("ASSET_NO")); assertEquals("闲置", form.get("ASSET_STATE")); assertEquals("2030-01-01", form.get("WARRANTY_EXPIRE_DATE")); assertFalse(form.containsKey("LOCK_STATE")); verify(assetBaseDao, never()).nextSerialNo(anyString()); } @Test void cardWithoutCategoryIsRejected() { Map form = map("CATEGORY_ID", "", "ASSET_NAME", "x"); assertThrows(BusinessException.class, () -> service.prepareCard(0L, form)); } @Test void cardCategoryWithoutTopCodeIsRejected() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x"); Map category = leafCategory(); category.put("TOP_CATEGORY_CODE", null); when(assetBaseDao.findCategory(30L)).thenReturn(category); BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCard(0L, form)); assertTrue(e.getMessage().contains("类别码")); verify(assetBaseDao, never()).nextSerialNo(anyString()); } @Test void disabledCategoryIsRejected() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x"); Map category = leafCategory(); category.put("ENABLE_STATE", "停用"); when(assetBaseDao.findCategory(30L)).thenReturn(category); assertThrows(BusinessException.class, () -> service.prepareCard(0L, form)); } @Test void invalidOriginalValueIsRejectedBeforeNumbering() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ORIGINAL_VALUE", "abc"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); assertThrows(BusinessException.class, () -> service.prepareCard(0L, form)); verify(assetBaseDao, never()).nextSerialNo(anyString()); } @Test void cardLocationNameComesFromLocationFullName() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "BG00001", "LOCATION_ID", "3"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); when(assetBaseDao.findLocation(3L)).thenReturn(map("ID", 3L, "FULL_NAME", "1号楼/3层/301")); service.prepareCard(9L, form); assertEquals("1号楼/3层/301", form.get("LOCATION_NAME")); } @Test void inboundLogIsWrittenOnce() { Map form = map("ASSET_NO", "BG00007", "ASSET_NAME", "联想笔记本", "ASSET_STATE", "在用", "SOURCE_TYPE", "手工新增", "USE_DEPT_NAME", "信息中心", "USER_NAME", "张三", "LOCATION_NAME", "1号楼/3层/301"); when(assetBaseDao.existsChangeLog(100L, "入库")).thenReturn(false); service.recordCardInbound(100L, form, 1L, "管理员"); verify(assetBaseDao).insertChangeLog(argThat(row -> Long.valueOf(100L).equals(row.get("ASSET_ID")) && "入库".equals(row.get("CHANGE_TYPE")) && "在用".equals(row.get("AFTER_STATE")) && "手工新增".equals(row.get("BILL_TYPE")) && String.valueOf(row.get("AFTER_CONTENT")).contains("信息中心") && "管理员".equals(row.get("OPERATE_USER_NAME")))); when(assetBaseDao.existsChangeLog(100L, "入库")).thenReturn(true); service.recordCardInbound(100L, form, 1L, "管理员"); verify(assetBaseDao, times(1)).insertChangeLog(anyMap()); } @Test void assetNoFormatPadsToFiveDigits() { assertEquals("JL00128", AssetBaseService.formatAssetNo("JL", 128)); assertEquals("BG123456", AssetBaseService.formatAssetNo("BG", 123456)); } @Test void generatedAssetNoSkipsNumbersAlreadyUsedByImportedAssets() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", ""); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); when(assetBaseDao.nextSerialNo("BG")).thenReturn(5L, 6L); when(assetBaseDao.existsAssetNo("BG00005")).thenReturn(true); when(assetBaseDao.existsAssetNo("BG00006")).thenReturn(false); service.prepareCard(0L, form); assertEquals("BG00006", form.get("ASSET_NO")); } @Test void invalidAssetStateIsRejected() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_STATE", "正常"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCard(0L, form)); assertTrue(e.getMessage().contains("资产状态")); } @Test void invalidDeviceTypeIsRejected() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "DEVICE_TYPE", "电梯"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); assertThrows(BusinessException.class, () -> service.prepareCard(0L, form)); } @Test void formSaveClearsImportPendingNote() { Map form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "BG00001", "REMARK", AssetBaseService.IMPORT_PENDING_PREFIX + "类别「台式机」不存在】原备注"); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); service.prepareCard(9L, form); assertEquals("原备注", form.get("REMARK")); } @Test void importedCardIsResolvedNumberedAndLogged() { Map row = map("ID", 100L, "ASSET_NAME", "联想笔记本", "CATEGORY_NAME", "笔记本", "USE_DEPT_NAME", "信息中心", "USER_NAME", "张三", "LOCATION_NAME", "1号楼/3层", "SAFETY_USER_NAME", "", "SOURCE_TYPE", "Excel导入", "PURCHASE_DATE", "2026-01-15 00:00:00.0", "REMARK", "旧备注"); when(assetBaseDao.findCardsWithoutAssetNo(anyInt())).thenReturn(Collections.singletonList(row)); when(assetBaseDao.findCategoriesByName("笔记本")).thenReturn(Collections.singletonList(leafCategory())); when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory()); when(assetBaseDao.findDeptIdsByName("信息中心")).thenReturn(Collections.singletonList(200L)); when(assetBaseDao.findUserIdsByTrueName("张三")).thenReturn(Collections.singletonList(300L)); when(assetBaseDao.findLocationsByName("1号楼/3层")).thenReturn(Collections.singletonList( map("ID", 3L, "LOCATION_NAME", "3层", "FULL_NAME", "1号楼/3层"))); when(assetBaseDao.findLocation(3L)).thenReturn(map("ID", 3L, "FULL_NAME", "1号楼/3层")); when(assetBaseDao.nextSerialNo("BG")).thenReturn(1L); int[] result = service.completeImportedCards(1L, "管理员"); assertArrayEquals(new int[]{1, 0}, result); verify(assetBaseDao).updateCard(eq(100L), argThat(values -> "BG00001".equals(values.get("ASSET_NO")) && Long.valueOf(30L).equals(values.get("CATEGORY_ID")) && Long.valueOf(200L).equals(values.get("USE_DEPT_ID")) && Long.valueOf(300L).equals(values.get("USER_ID")) && Long.valueOf(3L).equals(values.get("LOCATION_ID")) && "在用".equals(values.get("ASSET_STATE")) && "2029-01-15".equals(values.get("WARRANTY_EXPIRE_DATE")) && "旧备注".equals(values.get("REMARK")))); verify(assetBaseDao).insertChangeLog(argThat(log -> "BG00001".equals(log.get("ASSET_NO")))); } @Test void importedCardWithUnknownCategoryIsMarkedPending() { Map row = map("ID", 101L, "ASSET_NAME", "x", "CATEGORY_NAME", "台式机", "REMARK", ""); when(assetBaseDao.findCardsWithoutAssetNo(anyInt())).thenReturn(Collections.singletonList(row)); when(assetBaseDao.findCategoriesByName("台式机")).thenReturn(Collections.>emptyList()); int[] result = service.completeImportedCards(1L, "管理员"); assertArrayEquals(new int[]{0, 1}, result); verify(assetBaseDao).updateCard(eq(101L), argThat(values -> String.valueOf(values.get("REMARK")).startsWith(AssetBaseService.IMPORT_PENDING_PREFIX) && String.valueOf(values.get("REMARK")).contains("台式机") && !values.containsKey("ASSET_NO"))); verify(assetBaseDao, never()).nextSerialNo(anyString()); verify(assetBaseDao, never()).insertChangeLog(anyMap()); } @Test void importedCardWithDuplicateUserNameIsMarkedPending() { Map row = map("ID", 102L, "ASSET_NAME", "x", "CATEGORY_NAME", "笔记本", "USER_NAME", "李明"); when(assetBaseDao.findCardsWithoutAssetNo(anyInt())).thenReturn(Collections.singletonList(row)); when(assetBaseDao.findCategoriesByName("笔记本")).thenReturn(Collections.singletonList(leafCategory())); when(assetBaseDao.findUserIdsByTrueName("李明")).thenReturn(Arrays.asList(1L, 2L)); int[] result = service.completeImportedCards(1L, "管理员"); assertArrayEquals(new int[]{0, 1}, result); verify(assetBaseDao).updateCard(eq(102L), argThat(values -> String.valueOf(values.get("REMARK")).contains("李明"))); } @Test void lowerCaseFormKeysAreReadToo() { Map form = map("parent_id", "", "category_name", "办公设备", "category_code", "DZ"); service.prepareCategory(0L, form); assertEquals(Arrays.asList(1, "DZ"), Arrays.asList(form.get("CATEGORY_LEVEL"), form.get("TOP_CATEGORY_CODE"))); } }