diff --git a/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/controller/DklWarningInformationControllerTest.java b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/controller/DklWarningInformationControllerTest.java new file mode 100644 index 0000000..77e7bbd --- /dev/null +++ b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/controller/DklWarningInformationControllerTest.java @@ -0,0 +1,221 @@ +package com.dkl.large.controller; + +import com.dkl.common.core.domain.AjaxResult; +import com.dkl.common.core.page.TableDataInfo; +import com.dkl.large.domain.DklWarningInformation; +import com.dkl.large.service.IDklWarningInformationService; +import org.junit.jupiter.api.BeforeEach; +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 org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; + +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +/** + * 预警信息Controller单元测试 + * 遵循 JUnit 5 + Mockito 统一风格,覆盖所有核心接口与关键业务逻辑 + * + * @author Dkl + * @date 2025-06-16 + */ +@ExtendWith(MockitoExtension.class) +public class DklWarningInformationControllerTest { + + // 待测试的Controller(自动注入Mock的Service依赖,解耦真实业务层) + @InjectMocks + private DklWarningInformationController warningInformationController; + + // 模拟Controller依赖的Service层(避免依赖真实数据库,保证测试独立性) + @Mock + private IDklWarningInformationService warningInformationService; + + // 模拟HTTP响应对象(用于Excel导出接口) + private MockHttpServletResponse mockResponse; + + // 测试核心实体数据(预警信息) + private DklWarningInformation testWarning; + + // 测试实体列表数据 + private List testWarningList; + + /** + * 每个测试方法执行前初始化数据(保证测试用例独立性,避免相互干扰) + */ + @BeforeEach + void setUp() { + // 1. 初始化模拟HTTP响应 + mockResponse = new MockHttpServletResponse(); + + // 2. 初始化单个预警信息实体 + testWarning = new DklWarningInformation(); + testWarning.setId(1L); + testWarning.setDelFlag("0"); + testWarning.setCreateTime(new Date()); + testWarning.setCreateBy("admin"); + testWarning.setDeptId(100L); + testWarning.setUpdateTime(new Date()); + testWarning.setUpdateBy("admin"); + testWarning.setEventStatus("1"); // 默认待分配 + + // 3. 初始化预警信息列表 + testWarningList = new ArrayList<>(); + testWarningList.add(testWarning); + + // 4. 模拟Security上下文(解决getUsername()/getDeptId()等方法依赖,满足@PreAuthorize注解校验) + Authentication authentication = mock(Authentication.class); + SecurityContextHolder.getContext().setAuthentication(authentication); + doReturn("admin").when(authentication).getName(); + } + + // ---------------------- 测试场景 1:查询预警信息列表(list 接口,带数据权限) ---------------------- + @Test + void testList() { + // 步骤1:模拟Service层返回列表数据 + when(warningInformationService.selectDklWarningInformationList(any(DklWarningInformation.class))) + .thenReturn(testWarningList); + + // 步骤2:调用Controller接口执行测试 + TableDataInfo result = warningInformationController.list(testWarning); + + // 步骤3:断言结果(验证返回数据有效性) + assertNotNull(result, "返回的TableDataInfo不能为null"); + assertEquals(1, result.getRows().size(), "返回的列表数据条数应为1"); + assertEquals(testWarning.getId(), ((DklWarningInformation) result.getRows().get(0)).getId(), "返回的实体ID应与测试数据一致"); + + // 步骤4:验证Service方法被正确调用(确保流程无遗漏) + verify(warningInformationService, times(1)).selectDklWarningInformationList(any(DklWarningInformation.class)); + } + + // ---------------------- 测试场景 2:查询全部预警信息列表(listAll 接口,无数据权限) ---------------------- + @Test + void testListAll() { + // 步骤1:模拟Service层返回列表数据 + when(warningInformationService.selectDklWarningInformationList(any(DklWarningInformation.class))) + .thenReturn(testWarningList); + + // 步骤2:调用Controller接口执行测试 + TableDataInfo result = warningInformationController.listAll(testWarning); + + // 步骤3:断言结果 + assertNotNull(result, "返回的TableDataInfo不能为null"); + assertEquals(1, result.getRows().size(), "返回的列表数据条数应为1"); + + // 步骤4:验证Service方法调用 + verify(warningInformationService, times(1)).selectDklWarningInformationList(any(DklWarningInformation.class)); + } + + // ---------------------- 测试场景 3:导出预警信息列表(export 接口,无返回值) ---------------------- + @Test + void testExport() { + // 步骤1:模拟Service层返回列表数据 + when(warningInformationService.selectDklWarningInformationList(any(DklWarningInformation.class))) + .thenReturn(testWarningList); + + // 步骤2:调用导出接口(无返回值,验证无异常抛出即可) + assertDoesNotThrow(() -> warningInformationController.export(mockResponse, testWarning), + "导出接口执行过程中不应抛出异常"); + + // 步骤3:验证Service方法被调用,且HTTP响应状态正常 + verify(warningInformationService, times(1)).selectDklWarningInformationList(any(DklWarningInformation.class)); + assertEquals(200, mockResponse.getStatus(), "HTTP响应状态码应为200"); + assertTrue(mockResponse.getContentType().contains("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), + "响应内容应为Excel格式"); + } + + // ---------------------- 测试场景 4:获取预警信息详情(getInfo 接口) ---------------------- + @Test + void testGetInfo() { + // 步骤1:模拟Service层返回单个实体数据 + when(warningInformationService.selectDklWarningInformationById(anyLong())).thenReturn(testWarning); + + // 步骤2:调用Controller接口执行测试 + AjaxResult result = warningInformationController.getInfo(1L); + + // 步骤3:断言结果 + assertNotNull(result, "返回的AjaxResult不能为null"); + assertTrue(result.isSuccess(), "接口应返回成功状态"); + assertEquals(testWarning.getId(), ((DklWarningInformation) result.getData()).getId(), "返回的实体ID应与测试数据一致"); + + // 步骤4:验证Service方法调用 + verify(warningInformationService, times(1)).selectDklWarningInformationById(1L); + } + + // ---------------------- 测试场景 5:新增预警信息(add 接口) ---------------------- + @Test + void testAdd() { + // 步骤1:模拟Service层新增成功(返回1表示操作成功) + when(warningInformationService.insertDklWarningInformation(any(DklWarningInformation.class))).thenReturn(1); + + // 步骤2:调用Controller接口执行测试 + AjaxResult result = warningInformationController.add(testWarning); + + // 步骤3:断言结果 + assertNotNull(result, "返回的AjaxResult不能为null"); + assertTrue(result.isSuccess(), "新增接口应返回成功状态"); + assertEquals(1, result.getData(), "新增操作应返回1表示成功"); + + // 步骤4:验证实体系统字段与业务字段被正确设置(核心逻辑校验) + verify(warningInformationService).insertDklWarningInformation(argThat(warning -> + "0".equals(warning.getDelFlag()) && + "admin".equals(warning.getCreateBy()) && + 100L.equals(warning.getDeptId()) && + "1".equals(warning.getEventStatus()) && + warning.getCreateTime() != null && + warning.getUpdateTime() != null + )); + } + + // ---------------------- 测试场景 6:修改预警信息(edit 接口) ---------------------- + @Test + void testEdit() { + // 步骤1:模拟Service层修改成功(返回1表示操作成功) + when(warningInformationService.updateDklWarningInformation(any(DklWarningInformation.class))).thenReturn(1); + + // 步骤2:调用Controller接口执行测试 + AjaxResult result = warningInformationController.edit(testWarning); + + // 步骤3:断言结果 + assertNotNull(result, "返回的AjaxResult不能为null"); + assertTrue(result.isSuccess(), "修改接口应返回成功状态"); + assertEquals(1, result.getData(), "修改操作应返回1表示成功"); + + // 步骤4:验证实体更新字段被正确设置 + verify(warningInformationService).updateDklWarningInformation(argThat(warning -> + "admin".equals(warning.getUpdateBy()) && + warning.getUpdateTime() != null + )); + } + + // ---------------------- 测试场景 7:删除预警信息(remove 接口,批量删除) ---------------------- + @Test + void testRemove() { + // 步骤1:准备批量删除ID数组 + int[] ids = {1, 2}; + + // 步骤2:模拟Service层删除成功(返回2表示删除2条数据) + when(warningInformationService.deleteDklWarningInformationByIds(ids)).thenReturn(2); + + // 步骤3:调用Controller接口执行测试 + AjaxResult result = warningInformationController.remove(ids); + + // 步骤4:断言结果 + assertNotNull(result, "返回的AjaxResult不能为null"); + assertTrue(result.isSuccess(), "删除接口应返回成功状态"); + assertEquals(2, result.getData(), "删除操作应返回2表示成功删除2条数据"); + + // 步骤5:验证Service方法调用 + verify(warningInformationService, times(1)).deleteDklWarningInformationByIds(ids); + } +} \ No newline at end of file diff --git a/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/domain/DklWarningInformation.java b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/domain/DklWarningInformation.java new file mode 100644 index 0000000..2b21a83 --- /dev/null +++ b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/domain/DklWarningInformation.java @@ -0,0 +1,127 @@ +package com.dkl.large.domain; + +import java.util.Date; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import com.dkl.common.annotation.Excel; +import com.dkl.common.core.domain.BaseEntity; + +/** + * 预警信息对象 dkl_warning_information + * + * @author Dkl + * @date 2025-06-16 + */ +@Data +public class DklWarningInformation extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** id */ + private int id; + + /** 预警唯一标识符 */ + @Excel(name = "预警唯一标识符") + private String warningSigns; + + /** 发布单位名称 */ + @Excel(name = "发布单位名称") + private String unitName; + + /** 发布单位编码 */ + @Excel(name = "发布单位编码=>触发点位") + private Long unitCode; + + /** 发布时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @Excel(name = "发布时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + private Date releaseTime; + + /** 预警状态 */ + @Excel(name = "预警状态") + private String warningStatus; + + /** 预警类型 */ + @Excel(name = "预警类型") + private String warningType; + + /** 事件类别 */ + @Excel(name = "事件类别") + private String eventCategory; + + /** 预警级别 */ + @Excel(name = "预警级别") + private String warningLevel; + + /** 删除标志(0代表存在 2代表删除) */ + private String delFlag; + + /** 预计持续时间 */ + @Excel(name = "预计持续时间") + private String expectedDuration; + + /** 可能波及范围 */ + @Excel(name = "可能波及范围") + private String scopeImpact; + + /** 部门id*/ + @Excel(name = "部门id") + private Long deptId; + + /** 异常数据值 */ + @Excel(name = "异常数据值") + private String abnormalData; + + /** 是否督办 */ + @Excel(name = "是否督办") + private String isSupervise; + + /** 经度 */ + @Excel(name = "经度") + private String lng; + + /** 纬度 */ + @Excel(name = "纬度") + private String lat; + + /** 规则id */ + @Excel(name = "规则id") + private int rulesId; + + /** 事件状态 */ + @Excel(name = "事件状态") + private String eventStatus; + + + /** 开始时间 */ + @TableField(exist = false) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date startTime; + + /** 结束时间 */ + @TableField(exist = false) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date endTime; + + /** 监控点id */ + private Long pointsId; + + /** 监控点摄像id */ + private Long cameraId; + + /** 监控点名称 */ + @TableField(exist = false) + private String pointName; + + /** 监控点摄像名称 */ + @TableField(exist = false) + private String cameraName; + + /** 监控点摄像地址 */ + @TableField(exist = false) + private String filePath; +} diff --git a/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/mapper/DklWarningInformationMapper.java b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/mapper/DklWarningInformationMapper.java new file mode 100644 index 0000000..10f18cd --- /dev/null +++ b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/mapper/DklWarningInformationMapper.java @@ -0,0 +1,61 @@ +package com.dkl.large.mapper; + +import java.util.List; +import com.dkl.large.domain.DklWarningInformation; + +/** + * 预警信息Mapper接口 + * + * @author Dkl + * @date 2025-06-16 + */ +public interface DklWarningInformationMapper +{ + /** + * 查询预警信息 + * + * @param id 预警信息主键 + * @return 预警信息 + */ + public DklWarningInformation selectDklWarningInformationById(Long id); + + /** + * 查询预警信息列表 + * + * @param dklWarningInformation 预警信息 + * @return 预警信息集合 + */ + public List selectDklWarningInformationList(DklWarningInformation dklWarningInformation); + public List selectDklWarningInformationLists(DklWarningInformation dklWarningInformation); + /** + * 新增预警信息 + * + * @param dklWarningInformation 预警信息 + * @return 结果 + */ + public int insertDklWarningInformation(DklWarningInformation dklWarningInformation); + + /** + * 修改预警信息 + * + * @param dklWarningInformation 预警信息 + * @return 结果 + */ + public int updateDklWarningInformation(DklWarningInformation dklWarningInformation); + + /** + * 删除预警信息 + * + * @param id 预警信息主键 + * @return 结果 + */ + public int deleteDklWarningInformationById(int id); + + /** + * 批量删除预警信息 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteDklWarningInformationByIds(int[] ids); +} diff --git a/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/service/IDklWarningInformationService.java b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/service/IDklWarningInformationService.java new file mode 100644 index 0000000..29f6103 --- /dev/null +++ b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/service/IDklWarningInformationService.java @@ -0,0 +1,61 @@ +package com.dkl.large.service; + +import java.util.List; +import com.dkl.large.domain.DklWarningInformation; + +/** + * 预警信息Service接口 + * + * @author Dkl + * @date 2025-06-16 + */ +public interface IDklWarningInformationService +{ + /** + * 查询预警信息 + * + * @param id 预警信息主键 + * @return 预警信息 + */ + public DklWarningInformation selectDklWarningInformationById(Long id); + + /** + * 查询预警信息列表 + * + * @param dklWarningInformation 预警信息 + * @return 预警信息集合 + */ + public List selectDklWarningInformationList(DklWarningInformation dklWarningInformation); + public List selectDklWarningInformationLists(DklWarningInformation dklWarningInformation); + /** + * 新增预警信息 + * + * @param dklWarningInformation 预警信息 + * @return 结果 + */ + public int insertDklWarningInformation(DklWarningInformation dklWarningInformation); + + /** + * 修改预警信息 + * + * @param dklWarningInformation 预警信息 + * @return 结果 + */ + public int updateDklWarningInformation(DklWarningInformation dklWarningInformation); + + /** + * 批量删除预警信息 + * + * @param ids 需要删除的预警信息主键集合 + * @return 结果 + */ + public int deleteDklWarningInformationByIds(int[] ids); + + /** + * 删除预警信息信息 + * + * @param id 预警信息主键 + * @return 结果 + */ + public int deleteDklWarningInformationById(int id); +} diff --git a/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/service/impl/DklWarningInformationServiceImpl.java b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/service/impl/DklWarningInformationServiceImpl.java new file mode 100644 index 0000000..15f1a8b --- /dev/null +++ b/Dkl-Vue-master/dkl-large/src/test/java/com/dkl/large/service/impl/DklWarningInformationServiceImpl.java @@ -0,0 +1,99 @@ +package com.dkl.large.service.impl; + +import java.util.List; +import com.dkl.common.utils.DateUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.dkl.large.mapper.DklWarningInformationMapper; +import com.dkl.large.domain.DklWarningInformation; +import com.dkl.large.service.IDklWarningInformationService; + +/** + * 预警信息Service业务层处理 + * + * @author Dkl + * @date 2025-06-16 + */ +@Service +public class DklWarningInformationServiceImpl implements IDklWarningInformationService +{ + @Autowired + private DklWarningInformationMapper dklWarningInformationMapper; + + /** + * 查询预警信息 + * + * @param id 预警信息主键 + * @return 预警信息 + */ + @Override + public DklWarningInformation selectDklWarningInformationById(Long id) + { + return dklWarningInformationMapper.selectDklWarningInformationById(id); + } + + /** + * 查询预警信息列表 + * + * @param dklWarningInformation 预警信息 + * @return 预警信息 + */ + @Override + public List selectDklWarningInformationList(DklWarningInformation dklWarningInformation) + { + return dklWarningInformationMapper.selectDklWarningInformationList(dklWarningInformation); + } + @Override + public List selectDklWarningInformationLists(DklWarningInformation dklWarningInformation) + { + return dklWarningInformationMapper.selectDklWarningInformationLists(dklWarningInformation); + } /** + * 新增预警信息 + * + * @param dklWarningInformation 预警信息 + * @return 结果 + */ + @Override + public int insertDklWarningInformation(DklWarningInformation dklWarningInformation) + { + dklWarningInformation.setCreateTime(DateUtils.getNowDate()); + return dklWarningInformationMapper.insertDklWarningInformation(dklWarningInformation); + } + + /** + * 修改预警信息 + * + * @param dklWarningInformation 预警信息 + * @return 结果 + */ + @Override + public int updateDklWarningInformation(DklWarningInformation dklWarningInformation) + { + dklWarningInformation.setUpdateTime(DateUtils.getNowDate()); + return dklWarningInformationMapper.updateDklWarningInformation(dklWarningInformation); + } + + /** + * 批量删除预警信息 + * + * @param ids 需要删除的预警信息主键 + * @return 结果 + */ + @Override + public int deleteDklWarningInformationByIds(int[] ids) + { + return dklWarningInformationMapper.deleteDklWarningInformationByIds(ids); + } + + /** + * 删除预警信息信息 + * + * @param id 预警信息主键 + * @return 结果 + */ + @Override + public int deleteDklWarningInformationById(int id) + { + return dklWarningInformationMapper.deleteDklWarningInformationById(id); + } +} diff --git a/Dkl-Vue-master/dkl-large/src/test/resources/mapper/large/DklWarningInformationMapper.xml b/Dkl-Vue-master/dkl-large/src/test/resources/mapper/large/DklWarningInformationMapper.xml new file mode 100644 index 0000000..e5ca7b7 --- /dev/null +++ b/Dkl-Vue-master/dkl-large/src/test/resources/mapper/large/DklWarningInformationMapper.xml @@ -0,0 +1,190 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + select dwi.id, dwi.warning_signs, dwi.unit_name, dwi.unit_code, dwi.release_time, dwi.warning_status, + dwi.warning_type, dwi.event_category, dwi.warning_level, dwi.create_by, dwi.create_time, dwi.update_by, + dwi.update_time, dwi.del_flag, dwi.expected_duration, dwi.scope_impact, dwi.dept_id, dwi.abnormal_data, + dwi.is_supervise, dwi.lng, dwi.lat,dwi.rules_id,dwi.event_status,dwi.points_id,dwi.camera_id,dmp.point_name as pointName,dmc.name as cameraName,dmc.file_path as filePath from dkl_warning_information AS dwi + left join sys_dept d on dwi.dept_id = d.dept_id + left join dkl_monitoring_points dmp on dmp.id = dwi.points_id + left join dkl_monitoring_camera dmc on dmc.id = dwi.camera_id + + + + + + + + + + insert into dkl_warning_information + + warning_signs, + unit_name, + unit_code, + release_time, + warning_status, + warning_type, + event_category, + warning_level, + create_by, + create_time, + update_by, + update_time, + del_flag, + expected_duration, + scope_impact, + dept_id, + abnormal_data, + is_supervise, + lng, + lat, + rules_id, + event_status, + points_id, + camera_id, + + + #{warningSigns}, + #{unitName}, + #{unitCode}, + #{releaseTime}, + #{warningStatus}, + #{warningType}, + #{eventCategory}, + #{warningLevel}, + #{createBy}, + #{createTime}, + #{updateBy}, + #{updateTime}, + #{delFlag}, + #{expectedDuration}, + #{scopeImpact}, + #{deptId}, + #{abnormalData}, + #{isSupervise}, + #{lng}, + #{lat}, + #{rulesId}, + #{eventStatus}, + #{pointsId}, + #{cameraId}, + + + + + update dkl_warning_information + + warning_signs = #{warningSigns}, + unit_name = #{unitName}, + unit_code = #{unitCode}, + release_time = #{releaseTime}, + warning_status = #{warningStatus}, + warning_type = #{warningType}, + event_category = #{eventCategory}, + warning_level = #{warningLevel}, + create_by = #{createBy}, + create_time = #{createTime}, + update_by = #{updateBy}, + update_time = #{updateTime}, + del_flag = #{delFlag}, + expected_duration = #{expectedDuration}, + scope_impact = #{scopeImpact}, + dept_id = #{deptId}, + abnormal_data = #{abnormalData}, + is_supervise = #{isSupervise}, + lng = #{lng}, + lat = #{lat}, + rules_id = #{rulesId}, + event_status = #{eventStatus}, + + where id = #{id} + + + + update dkl_warning_information set del_flag = 2 where id = #{id} + + + + update dkl_warning_information set del_flag = 2 where id in + + #{id} + + +