|
@@ -0,0 +1,284 @@
|
|
|
|
|
+package com.zksy.WaterSupply.MonitorAlarm;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
|
|
+import com.zksy.WaterSupply.MonitorAlarm.domain.AlarmAudit;
|
|
|
|
|
+import com.zksy.WaterSupply.MonitorAlarm.dto.in.AlarmAuditInDTO;
|
|
|
|
|
+import com.zksy.WaterSupply.MonitorAlarm.dto.in.AlarmDispatchInDTO;
|
|
|
|
|
+import com.zksy.WaterSupply.MonitorAlarm.mapper.AlarmAuditMapper;
|
|
|
|
|
+import com.zksy.WaterSupply.MonitorAlarm.service.impl.WaterSupplyAlarmServiceImpl;
|
|
|
|
|
+import com.zksy.base.alarm.domain.AlarmData;
|
|
|
|
|
+import com.zksy.base.alarm.service.AlarmDataService;
|
|
|
|
|
+import com.zksy.base.domain.EquipmentBase;
|
|
|
|
|
+import com.zksy.base.domain.EquipmentStatus;
|
|
|
|
|
+import com.zksy.base.domain.EquipmentType;
|
|
|
|
|
+import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
|
|
+import com.zksy.base.mapper.EquipmentStatusMapper;
|
|
|
|
|
+import com.zksy.base.mapper.EquipmentTypeMapper;
|
|
|
|
|
+import com.zksy.base.service.EquipmentBaseService;
|
|
|
|
|
+import com.zksy.common.exception.ServiceException;
|
|
|
|
|
+import com.zksy.common.core.domain.model.LoginUser;
|
|
|
|
|
+import com.zksy.common.core.domain.entity.SysUser;
|
|
|
|
|
+import com.zksy.ops.domain.WorkOrder;
|
|
|
|
|
+import com.zksy.ops.domain.WorkOrderLog;
|
|
|
|
|
+import com.zksy.ops.mapper.WorkOrderLogMapper;
|
|
|
|
|
+import com.zksy.ops.mapper.WorkOrderMapper;
|
|
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
+
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 报警审核转工单闭环契约测试(T03 / Issue #46)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>被测公共 seam:{@code IWaterSupplyAlarmService#auditAlarm(AlarmAuditInDTO)} 与
|
|
|
|
|
+ * {@code #dispatchToWorkOrder(AlarmDispatchInDTO)}。断言只针对外部可观察结果:
|
|
|
|
|
+ * 是否抛错、是否创建工单、工单的类型/状态/接单人。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class WaterSupplyAlarmAuditServiceTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String DEVICE_CODE = "CN-GS-202609160001";
|
|
|
|
|
+ private static final String WARNING_TYPE = "压力预警";
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeAll
|
|
|
|
|
+ static void initLambdaCache() {
|
|
|
|
|
+ MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, AlarmData.class);
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, EquipmentBase.class);
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, EquipmentStatus.class);
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, WorkOrder.class);
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, WorkOrderLog.class);
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, AlarmAudit.class);
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, EquipmentType.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private WaterSupplyAlarmServiceImpl service;
|
|
|
|
|
+ private AlarmDataService alarmDataService;
|
|
|
|
|
+ private EquipmentBaseMapper equipmentBaseMapper;
|
|
|
|
|
+ private WorkOrderMapper workOrderMapper;
|
|
|
|
|
+ private WorkOrderLogMapper workOrderLogMapper;
|
|
|
|
|
+ private AlarmAuditMapper alarmAuditMapper;
|
|
|
|
|
+ private EquipmentStatusMapper equipmentStatusMapper;
|
|
|
|
|
+ private EquipmentTypeMapper equipmentTypeMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ // 与 DeviceAbnormalControllerTest 一致:为 SecurityUtils 提供登录态
|
|
|
|
|
+ SysUser user = new SysUser();
|
|
|
|
|
+ user.setUserId(7L);
|
|
|
|
|
+ user.setUserName("operator");
|
|
|
|
|
+ user.setNickName("调度管理员");
|
|
|
|
|
+ LoginUser loginUser = new LoginUser(7L, 1L, user, Collections.emptySet());
|
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(
|
|
|
|
|
+ new UsernamePasswordAuthenticationToken(loginUser, null, Collections.emptyList()));
|
|
|
|
|
+
|
|
|
|
|
+ service = new WaterSupplyAlarmServiceImpl();
|
|
|
|
|
+ alarmDataService = mock(AlarmDataService.class);
|
|
|
|
|
+ EquipmentBaseService equipmentBaseService = mock(EquipmentBaseService.class);
|
|
|
|
|
+ equipmentBaseMapper = mock(EquipmentBaseMapper.class);
|
|
|
|
|
+ workOrderMapper = mock(WorkOrderMapper.class);
|
|
|
|
|
+ workOrderLogMapper = mock(WorkOrderLogMapper.class);
|
|
|
|
|
+ alarmAuditMapper = mock(AlarmAuditMapper.class);
|
|
|
|
|
+ equipmentStatusMapper = mock(EquipmentStatusMapper.class);
|
|
|
|
|
+ equipmentTypeMapper = mock(EquipmentTypeMapper.class);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "alarmDataService", alarmDataService);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "equipmentBaseService", equipmentBaseService);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "equipmentBaseMapper", equipmentBaseMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "workOrderMapper", workOrderMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "workOrderLogMapper", workOrderLogMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "alarmAuditMapper", alarmAuditMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "equipmentStatusMapper", equipmentStatusMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "equipmentTypeMapper", equipmentTypeMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "waterTopLevelTypeName", "供水");
|
|
|
|
|
+
|
|
|
|
|
+ when(equipmentBaseMapper.selectOne(any())).thenReturn(device());
|
|
|
|
|
+ when(equipmentBaseMapper.selectList(any())).thenReturn(Collections.singletonList(device()));
|
|
|
|
|
+ when(equipmentStatusMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(equipmentTypeMapper.selectBatchIds(any())).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(alarmAuditMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
|
|
+ when(workOrderLogMapper.insert(any(WorkOrderLog.class))).thenReturn(1);
|
|
|
|
|
+ when(workOrderMapper.insert(any(WorkOrder.class))).thenReturn(1);
|
|
|
|
|
+ when(alarmDataService.updateById(any())).thenReturn(true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static EquipmentBase device() {
|
|
|
|
|
+ EquipmentBase device = new EquipmentBase();
|
|
|
|
|
+ device.setEquipmentId("EQ-PR-1");
|
|
|
|
|
+ device.setEquipmentCode(DEVICE_CODE);
|
|
|
|
|
+ device.setEquipmentName("城南加压站压力监测点");
|
|
|
|
|
+ device.setEquipmentTypeId("TYPE-P");
|
|
|
|
|
+ return device;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static AlarmData alarm(String id, int level) {
|
|
|
|
|
+ AlarmData alarm = new AlarmData();
|
|
|
|
|
+ alarm.setId(id);
|
|
|
|
|
+ alarm.setDeviceCode(DEVICE_CODE);
|
|
|
|
|
+ alarm.setDeviceName("城南加压站压力监测点");
|
|
|
|
|
+ alarm.setDeviceType("压力监测");
|
|
|
|
|
+ alarm.setWarningType(WARNING_TYPE);
|
|
|
|
|
+ alarm.setWarningCode("T45-WARN-PRESSURE");
|
|
|
|
|
+ alarm.setActualValue(new BigDecimal("0.95"));
|
|
|
|
|
+ alarm.setMinValue(new BigDecimal("0.40"));
|
|
|
|
|
+ alarm.setMaxValue(new BigDecimal("0.80"));
|
|
|
|
|
+ alarm.setAlarmStatus(0);
|
|
|
|
|
+ alarm.setAlarmLevel(level);
|
|
|
|
|
+ alarm.setAlarmTime(LocalDateTime.now());
|
|
|
|
|
+ return alarm;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static WorkOrder order(String orderNo, String alarmId, int status) {
|
|
|
|
|
+ WorkOrder order = new WorkOrder();
|
|
|
|
|
+ order.setOrderNo(orderNo);
|
|
|
|
|
+ order.setAlarmId(alarmId);
|
|
|
|
|
+ order.setOrderStatus(status);
|
|
|
|
|
+ return order;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void transferCreatesFaultRepairDraftOrderWithoutReceiver() {
|
|
|
|
|
+ AlarmData target = alarm("a1", 1);
|
|
|
|
|
+ when(alarmDataService.getById("a1")).thenReturn(target);
|
|
|
|
|
+ // 该设备该报警类型没有未结束工单
|
|
|
|
|
+ when(alarmDataService.list(any(AbstractWrapper.class))).thenReturn(new ArrayList<>(
|
|
|
|
|
+ Collections.singletonList(target)));
|
|
|
|
|
+ when(workOrderMapper.selectCount(any())).thenReturn(0L);
|
|
|
|
|
+
|
|
|
|
|
+ AlarmAuditInDTO in = new AlarmAuditInDTO();
|
|
|
|
|
+ in.setAlarmId("a1");
|
|
|
|
|
+ in.setAuditAction(1);
|
|
|
|
|
+ in.setAuditResult(1);
|
|
|
|
|
+ in.setAuditOpinion("现场核实属实,转故障维修");
|
|
|
|
|
+
|
|
|
|
|
+ service.auditAlarm(in);
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<WorkOrder> captor = ArgumentCaptor.forClass(WorkOrder.class);
|
|
|
|
|
+ verify(workOrderMapper).insert(captor.capture());
|
|
|
|
|
+ WorkOrder created = captor.getValue();
|
|
|
|
|
+ // 工单流程重构后 work_order.order_type 已废弃:报警来源(故障维修)工单以 alarm_id 关联识别。
|
|
|
|
|
+ // 审核阶段只生成 0-待派单 工单,且不指定接单责任人。
|
|
|
|
|
+ assertEquals("a1", created.getAlarmId());
|
|
|
|
|
+ assertEquals(0, created.getOrderStatus());
|
|
|
|
|
+ assertNull(created.getDispatchTime());
|
|
|
|
|
+ assertNull(created.getReceiveUser());
|
|
|
|
|
+ assertEquals(DEVICE_CODE, created.getDeviceCode());
|
|
|
|
|
+ assertEquals("EQ-PR-1", created.getDeviceId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void transferIsRejectedWhenSameDeviceAndWarningTypeHasUnfinishedOrder() {
|
|
|
|
|
+ AlarmData target = alarm("a2", 2);
|
|
|
|
|
+ when(alarmDataService.getById("a2")).thenReturn(target);
|
|
|
|
|
+ // 同设备同报警类型的另一条报警 + 一张未结束工单(处理中)
|
|
|
|
|
+ when(alarmDataService.list(any(AbstractWrapper.class))).thenReturn(Arrays.asList(
|
|
|
|
|
+ target, alarm("a1", 1)));
|
|
|
|
|
+ when(workOrderMapper.selectCount(any())).thenReturn(1L);
|
|
|
|
|
+
|
|
|
|
|
+ AlarmAuditInDTO in = new AlarmAuditInDTO();
|
|
|
|
|
+ in.setAlarmId("a2");
|
|
|
|
|
+ in.setAuditAction(1);
|
|
|
|
|
+ in.setAuditResult(1);
|
|
|
|
|
+
|
|
|
|
|
+ ServiceException error = assertThrows(ServiceException.class, () -> service.auditAlarm(in));
|
|
|
|
|
+ assertEquals("该设备同一报警类型已存在未结束的故障维修工单,不能重复创建", error.getMessage());
|
|
|
|
|
+ verify(workOrderMapper, never()).insert(any(WorkOrder.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unfinishedCheckExcludesClosedAndCancelledOrders() {
|
|
|
|
|
+ AlarmData target = alarm("a3", 3);
|
|
|
|
|
+ when(alarmDataService.getById("a3")).thenReturn(target);
|
|
|
|
|
+ when(alarmDataService.list(any(AbstractWrapper.class))).thenReturn(
|
|
|
|
|
+ new ArrayList<>(Collections.singletonList(target)));
|
|
|
|
|
+ when(workOrderMapper.selectCount(any())).thenReturn(0L);
|
|
|
|
|
+
|
|
|
|
|
+ service.dispatchToWorkOrder(dispatch("a3"));
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<Wrapper<WorkOrder>> wrapperCaptor = wrapperCaptor();
|
|
|
|
|
+ verify(workOrderMapper).selectCount(wrapperCaptor.capture());
|
|
|
|
|
+ AbstractWrapper<?, ?, ?> wrapper = (AbstractWrapper<?, ?, ?>) wrapperCaptor.getValue();
|
|
|
|
|
+ String sql = wrapper.getSqlSegment();
|
|
|
|
|
+ // 唯一性口径必须排除终态:5-已结案、7-已取消
|
|
|
|
|
+ assertTrueContains(sql, "order_status");
|
|
|
|
|
+ assertTrueContainsValues(wrapper, 5);
|
|
|
|
|
+ assertTrueContainsValues(wrapper, 7);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void falseAlarmReleaseRecordsOpinionAndCreatesNoOrder() {
|
|
|
|
|
+ AlarmData target = alarm("a4", 2);
|
|
|
|
|
+ when(alarmDataService.getById("a4")).thenReturn(target);
|
|
|
|
|
+
|
|
|
|
|
+ AlarmAuditInDTO in = new AlarmAuditInDTO();
|
|
|
|
|
+ in.setAlarmId("a4");
|
|
|
|
|
+ in.setAuditAction(2);
|
|
|
|
|
+ in.setAuditResult(2);
|
|
|
|
|
+ in.setAuditOpinion("现场核实为误报");
|
|
|
|
|
+ in.setAttachUrl("https://example.com/evidence.jpg");
|
|
|
|
|
+
|
|
|
|
|
+ service.auditAlarm(in);
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<AlarmAudit> auditCaptor = ArgumentCaptor.forClass(AlarmAudit.class);
|
|
|
|
|
+ verify(alarmAuditMapper).insert(auditCaptor.capture());
|
|
|
|
|
+ assertEquals("现场核实为误报", auditCaptor.getValue().getAuditOpinion());
|
|
|
|
|
+ assertEquals("https://example.com/evidence.jpg", auditCaptor.getValue().getAttachUrl());
|
|
|
|
|
+ verify(workOrderMapper, never()).insert(any(WorkOrder.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void auditRejectsActionOutsideTransferAndFalseAlarmRelease() {
|
|
|
|
|
+ AlarmAuditInDTO in = new AlarmAuditInDTO();
|
|
|
|
|
+ in.setAlarmId("a5");
|
|
|
|
|
+ in.setAuditAction(9);
|
|
|
|
|
+ in.setAuditResult(1);
|
|
|
|
|
+
|
|
|
|
|
+ ServiceException error = assertThrows(ServiceException.class, () -> service.auditAlarm(in));
|
|
|
|
|
+ assertEquals("审核动作仅支持 1-转工单 或 2-误报解除", error.getMessage());
|
|
|
|
|
+ verify(alarmAuditMapper, never()).insert(any(AlarmAudit.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private AlarmDispatchInDTO dispatch(String alarmId) {
|
|
|
|
|
+ AlarmDispatchInDTO dispatch = new AlarmDispatchInDTO();
|
|
|
|
|
+ dispatch.setAlarmId(alarmId);
|
|
|
|
|
+ return dispatch;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private static ArgumentCaptor<Wrapper<WorkOrder>> wrapperCaptor() {
|
|
|
|
|
+ return (ArgumentCaptor<Wrapper<WorkOrder>>) (ArgumentCaptor<?>) ArgumentCaptor.forClass(Wrapper.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void assertTrueContains(String sql, String expected) {
|
|
|
|
|
+ if (sql == null || !sql.contains(expected)) {
|
|
|
|
|
+ throw new AssertionError("SQL 缺少期望内容:" + expected + ",实际:" + sql);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void assertTrueContainsValues(AbstractWrapper<?, ?, ?> wrapper, Object expected) {
|
|
|
|
|
+ if (!wrapper.getParamNameValuePairs().containsValue(expected)) {
|
|
|
|
|
+ throw new AssertionError("查询条件缺少排除值:" + expected
|
|
|
|
|
+ + ",实际:" + wrapper.getParamNameValuePairs());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|