Index: ssts-web/src/test/java/test/forgon/disinfectsystem/basedatamanager/departexpressrecyclingconfig/service/DepartExpressRecyclingConfigManagerTests.java =================================================================== diff -u --- ssts-web/src/test/java/test/forgon/disinfectsystem/basedatamanager/departexpressrecyclingconfig/service/DepartExpressRecyclingConfigManagerTests.java (revision 0) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/basedatamanager/departexpressrecyclingconfig/service/DepartExpressRecyclingConfigManagerTests.java (revision 40878) @@ -0,0 +1,172 @@ +package test.forgon.disinfectsystem.basedatamanager.departexpressrecyclingconfig.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import org.junit.Before; +import org.junit.Test; + +import com.forgon.disinfectsystem.entity.basedatamanager.supplyroomconfig.DepartExpressRecyclingConfig; + +import test.forgon.disinfectsystem.AbstractCSSDTest; +/** + * 测试快速回收科室配置 + * + */ +public class DepartExpressRecyclingConfigManagerTests extends AbstractCSSDTest{ + + private Long id1 = null; + private Long id2 = null; + @Before + public void setUp() { + // 初始化现有的配置数据 + + DepartExpressRecyclingConfig config1 = new DepartExpressRecyclingConfig(); + config1.setSequence(1); + config1.setOrgUnitId(11L); + objectDao.save(config1); + config1 = departExpressRecyclingConfigManager.getConfigById(config1.getId()); + id1 = config1.getId(); + DepartExpressRecyclingConfig config2 = new DepartExpressRecyclingConfig(); + config2.setSequence(2); + config2.setOrgUnitId(12L); + objectDao.save(config2); + config2 = departExpressRecyclingConfigManager.getConfigById(config2.getId()); + id2 = config2.getId(); + } + @Test + public void testProcessDepartExpressRecyclingConfig_EmptyConfig() { + // 测试空配置的情况 + departExpressRecyclingConfigManager.processDepartExpressRecyclingConfig("[]", true); + + // 验证执行了清空操作 + List configs = departExpressRecyclingConfigManager.getAllConfigs(null); + assertEquals("执行了清空操作,不应存在配置", 0, configs.size()); + } + + @Test + public void testProcessDepartExpressRecyclingConfig_AddNewConfigs() { + // 准备测试数据 - 新增配置 + JSONArray configArray = new JSONArray(); + + JSONObject config1 = new JSONObject(); + config1.put("sequence", 1); + config1.put("orgUnitId", 15L); + + JSONObject config2 = new JSONObject(); + config2.put("sequence", 2); + config2.put("orgUnitId", 16L); + + configArray.add(config1); + configArray.add(config2); + + // 执行测试 + departExpressRecyclingConfigManager.processDepartExpressRecyclingConfig(configArray.toString(), true); + + // 验证执行了清空操作 + List configs = departExpressRecyclingConfigManager.getAllConfigs(null); + assertEquals("执行了新增操作,存在4个配置", 2, configs.size()); + } + + @Test + public void testProcessDepartExpressRecyclingConfig_UpdateExistingConfigs() { + // 准备测试数据 - 更新现有配置 + JSONArray configArray = new JSONArray(); + + JSONObject config1 = new JSONObject(); + config1.put("id", id1); // 现有ID + config1.put("sequence", 5); // 修改序号 + config1.put("orgUnitId", 11L); // 相同的科室ID + + JSONObject config2 = new JSONObject(); + config2.put("id", id2); // 现有ID + config2.put("sequence", 2); // 相同的序号 + config2.put("orgUnitId", 99L); // 修改科室ID + + configArray.add(config1); + configArray.add(config2); + + // 执行测试 + departExpressRecyclingConfigManager.processDepartExpressRecyclingConfig(configArray.toString(), true); + List configs = departExpressRecyclingConfigManager.getAllConfigs(null); + assertEquals("执行了更新操作,存在2个配置", 2, configs.size()); + + DepartExpressRecyclingConfig departExpressRecyclingConfig1 = departExpressRecyclingConfigManager.getConfigById(id1); + //序号会被重新排序 + assertEquals("执行了序号更新操作", 2, departExpressRecyclingConfig1.getSequence().intValue()); + DepartExpressRecyclingConfig departExpressRecyclingConfig2 = departExpressRecyclingConfigManager.getConfigById(id2); + assertEquals("执行了科室更新操作", 99L, departExpressRecyclingConfig2.getOrgUnitId().longValue()); + + } + + @Test + public void testProcessDepartExpressRecyclingConfig_MixedOperations() { + // 准备测试数据 - 混合操作(新增、更新、删除) + JSONArray configArray = new JSONArray(); + + // 更新现有配置 + JSONObject config1 = new JSONObject(); + config1.put("id", id1); + config1.put("sequence", 5); + config1.put("orgUnitId", 18L); + + // 新增配置 + JSONObject config2 = new JSONObject(); + config2.put("sequence", 4); + config2.put("orgUnitId", 17L); + + configArray.add(config1); + configArray.add(config2); + // 注意:ID为2的现有配置将被删除,因为它不在传入数据中 + + // 执行测试 + departExpressRecyclingConfigManager.processDepartExpressRecyclingConfig(configArray.toString(), true); + + DepartExpressRecyclingConfig departExpressRecyclingConfig1 = departExpressRecyclingConfigManager.getConfigById(id1); + //序号会被重新排序 + assertEquals("执行了序号更新操作", 2, departExpressRecyclingConfig1.getSequence().intValue()); + assertEquals("执行了科室更新操作", 18L, departExpressRecyclingConfig1.getOrgUnitId().longValue()); + + DepartExpressRecyclingConfig departExpressRecyclingConfig2 = departExpressRecyclingConfigManager.getConfigById(id2); + assertNull("ID为2的现有配置将被删除", departExpressRecyclingConfig2); + + DepartExpressRecyclingConfig departExpressRecyclingConfig17 = departExpressRecyclingConfigManager.getConfigByOrgUnitId(17L); + assertNotNull("新增了orgUnitId 17的数据", departExpressRecyclingConfig17); + + } + + @Test + public void testAdjustConfigSequences() { + // 准备乱序的测试数据 + + DepartExpressRecyclingConfig config1 = new DepartExpressRecyclingConfig(); + config1.setSequence(5); // 乱序 + config1.setOrgUnitId(11L); + + DepartExpressRecyclingConfig config2 = new DepartExpressRecyclingConfig(); + config2.setSequence(3); // 乱序 + config2.setOrgUnitId(12L); + + + objectDao.save(config1); + objectDao.save(config2); + config1 = departExpressRecyclingConfigManager.getConfigById(config1.getId()); + config2 = departExpressRecyclingConfigManager.getConfigById(config2.getId()); + + List unorderedConfigs = departExpressRecyclingConfigManager.getAllConfigs(" order by sequence asc "); + // 执行序号调整 + departExpressRecyclingConfigManager.adjustConfigSequences(); + + // 验证序号被正确调整 + assertEquals("第一个配置序号应为1", Integer.valueOf(1), unorderedConfigs.get(0).getSequence()); + assertEquals("第二个配置序号应为2", Integer.valueOf(2), unorderedConfigs.get(1).getSequence()); + + } + +}