已合并
智能运维模块告警通知插件单元测试代码迁入 #348
wuyuebin创建于 2023年7月17日
智能运维模块告警通知插件单元测试代码迁入 #348
已合并
共 33 个文件变更+4886-45
| @@ -6,6 +6,7 @@ package com.nctigba.alert.monitor.model; | |||
| 6 | 6 | ||
| 7 | import lombok.Data; | 7 | import lombok.Data; |
| 8 | import lombok.Generated; | 8 | import lombok.Generated; |
| 9 | +import lombok.experimental.Accessors; | ||
| 9 | 10 | ||
| 10 | /** | 11 | /** |
| 11 | * wuyuebin | 12 | * wuyuebin |
| @@ -13,6 +14,7 @@ import lombok.Generated; | |||
| 13 | * | 14 | * |
| 14 | */ | 15 | */ |
| 15 | 16 | ||
| 17 | + | ||
| 16 | 18 | ||
| 17 | public class AlertRecordReq { | 19 | public class AlertRecordReq { |
| 18 | private String clusterNodeId; | 20 | private String clusterNodeId; |
| @@ -6,6 +6,7 @@ package com.nctigba.alert.monitor.model; | |||
| 6 | 6 | ||
| 7 | import lombok.Data; | 7 | import lombok.Data; |
| 8 | import lombok.Generated; | 8 | import lombok.Generated; |
| 9 | +import lombok.experimental.Accessors; | ||
| 9 | 10 | ||
| 10 | /** | 11 | /** |
| 11 | * wuyuebin | 12 | * wuyuebin |
| @@ -13,6 +14,7 @@ import lombok.Generated; | |||
| 13 | * | 14 | * |
| 14 | */ | 15 | */ |
| 15 | 16 | ||
| 17 | + | ||
| 16 | 18 | ||
| 17 | public class AlertStatisticsReq { | 19 | public class AlertStatisticsReq { |
| 18 | private String clusterNodeId; | 20 | private String clusterNodeId; |
| @@ -8,14 +8,6 @@ import cn.hutool.core.collection.CollectionUtil; | |||
| 8 | import cn.hutool.core.util.StrUtil; | 8 | import cn.hutool.core.util.StrUtil; |
| 9 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | 9 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 10 | import com.gitee.starblues.bootstrap.annotation.AutowiredType; | 10 | import com.gitee.starblues.bootstrap.annotation.AutowiredType; |
| 11 | -import lombok.extern.slf4j.Slf4j; | ||
| 12 | -import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterEntity; | ||
| 13 | -import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterNodeEntity; | ||
| 14 | -import org.opengauss.admin.common.core.domain.entity.ops.OpsHostEntity; | ||
| 15 | -import org.opengauss.admin.common.exception.ServiceException; | ||
| 16 | -import org.opengauss.admin.system.plugin.facade.HostFacade; | ||
| 17 | -import org.opengauss.admin.system.service.ops.IOpsClusterNodeService; | ||
| 18 | -import org.opengauss.admin.system.service.ops.IOpsClusterService; | ||
| 19 | import com.nctigba.alert.monitor.constant.CommonConstants; | 11 | import com.nctigba.alert.monitor.constant.CommonConstants; |
| 20 | import com.nctigba.alert.monitor.dto.AlertContentParamDto; | 12 | import com.nctigba.alert.monitor.dto.AlertContentParamDto; |
| 21 | import com.nctigba.alert.monitor.entity.AlertRecord; | 13 | import com.nctigba.alert.monitor.entity.AlertRecord; |
| @@ -33,6 +25,14 @@ import com.nctigba.alert.monitor.mapper.NotifyWayMapper; | |||
| 33 | import com.nctigba.alert.monitor.model.api.AlertApiReq; | 25 | import com.nctigba.alert.monitor.model.api.AlertApiReq; |
| 34 | import com.nctigba.alert.monitor.model.api.AlertLabels; | 26 | import com.nctigba.alert.monitor.model.api.AlertLabels; |
| 35 | import com.nctigba.alert.monitor.utils.TextParser; | 27 | import com.nctigba.alert.monitor.utils.TextParser; |
| 28 | +import lombok.extern.slf4j.Slf4j; | ||
| 29 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterEntity; | ||
| 30 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterNodeEntity; | ||
| 31 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsHostEntity; | ||
| 32 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 33 | +import org.opengauss.admin.system.plugin.facade.HostFacade; | ||
| 34 | +import org.opengauss.admin.system.service.ops.IOpsClusterNodeService; | ||
| 35 | +import org.opengauss.admin.system.service.ops.IOpsClusterService; | ||
| 36 | import org.springframework.beans.factory.annotation.Autowired; | 36 | import org.springframework.beans.factory.annotation.Autowired; |
| 37 | import org.springframework.stereotype.Service; | 37 | import org.springframework.stereotype.Service; |
| 38 | import org.springframework.transaction.annotation.Transactional; | 38 | import org.springframework.transaction.annotation.Transactional; |
| @@ -97,9 +97,18 @@ public class AlertApiService { | |||
| 97 | String clusterNodeId = labels.getInstance(); | 97 | String clusterNodeId = labels.getInstance(); |
| 98 | AlertContentParamDto contentParamDto = setAndGetAlertContentParamDto(clusterNodeId, | 98 | AlertContentParamDto contentParamDto = setAndGetAlertContentParamDto(clusterNodeId, |
| 99 | alertApiReq.getStartsAt(), labels.getLevel()); | 99 | alertApiReq.getStartsAt(), labels.getLevel()); |
| 100 | - | ||
| 101 | AlertRecord alertRecord = saveAndGetRecord(alertApiReq, notifyWayNames, alertTemplateRule, contentParamDto); | 100 | AlertRecord alertRecord = saveAndGetRecord(alertApiReq, notifyWayNames, alertTemplateRule, contentParamDto); |
| 102 | contentParamDto.setContent(alertRecord.getAlertContent()); | 101 | contentParamDto.setContent(alertRecord.getAlertContent()); |
| 102 | + if (StrUtil.isBlank(alertTemplateRule.getAlertNotify())) { | ||
| 103 | + continue; | ||
| 104 | + } | ||
| 105 | + List<String> alertNotifyList = Arrays.asList(alertTemplateRule.getAlertNotify() | ||
| 106 | + .split(CommonConstants.DELIMITER)); | ||
| 107 | + String alertStatus = alertRecord.getAlertStatus().equals(CommonConstants.FIRING_STATUS) | ||
| 108 | + ? "firing" : "recover"; | ||
| 109 | + if (!alertNotifyList.contains(alertStatus)) { | ||
| 110 | + continue; | ||
| 111 | + } | ||
| 103 | 112 | ||
| 104 | Integer isRepeat = alertTemplateRule.getIsRepeat(); | 113 | Integer isRepeat = alertTemplateRule.getIsRepeat(); |
| 105 | if (isRepeat == CommonConstants.IS_NOT_REPEAT) { | 114 | if (isRepeat == CommonConstants.IS_NOT_REPEAT) { |
| @@ -109,7 +118,7 @@ public class AlertApiService { | |||
| 109 | continue; | 118 | continue; |
| 110 | } | 119 | } |
| 111 | } | 120 | } |
| 112 | - Integer isSilence = alertTemplateRule.getIsSilence(); // 是否静默 | 121 | + Integer isSilence = alertTemplateRule.getIsSilence(); |
| 113 | LocalDateTime now = LocalDateTime.now(); | 122 | LocalDateTime now = LocalDateTime.now(); |
| 114 | if (isSilence == CommonConstants.IS_SILENCE && now.isAfter(alertTemplateRule.getSilenceStartTime()) | 123 | if (isSilence == CommonConstants.IS_SILENCE && now.isAfter(alertTemplateRule.getSilenceStartTime()) |
| 115 | && now.isBefore(alertTemplateRule.getSilenceEndTime())) { | 124 | && now.isBefore(alertTemplateRule.getSilenceEndTime())) { |
| @@ -137,12 +146,12 @@ public class AlertApiService { | |||
| 137 | alertRecord = new AlertRecord(); | 146 | alertRecord = new AlertRecord(); |
| 138 | AlertTemplate alertTemplate = templateMapper.selectById(templateId); | 147 | AlertTemplate alertTemplate = templateMapper.selectById(templateId); |
| 139 | alertRecord.setClusterNodeId(clusterNodeId).setTemplateId(templateId).setTemplateRuleId( | 148 | alertRecord.setClusterNodeId(clusterNodeId).setTemplateId(templateId).setTemplateRuleId( |
| 140 | - alertTemplateRule.getId()).setStartTime(alertApiReq.getStartsAt()).setEndTime(endsAt) | 149 | + alertTemplateRule.getId()).setStartTime(alertApiReq.getStartsAt()).setEndTime(endsAt) |
| 141 | .setCreateTime(LocalDateTime.now()); | 150 | .setCreateTime(LocalDateTime.now()); |
| 142 | alertRecord.setDuration( | 151 | alertRecord.setDuration( |
| 143 | Duration.between(alertRecord.getStartTime(), alertRecord.getEndTime()).toSeconds()); | 152 | Duration.between(alertRecord.getStartTime(), alertRecord.getEndTime()).toSeconds()); |
| 144 | alertRecord.setTemplateName(alertTemplate.getTemplateName()).setTemplateRuleName( | 153 | alertRecord.setTemplateName(alertTemplate.getTemplateName()).setTemplateRuleName( |
| 145 | - alertTemplateRule.getRuleName()) | 154 | + alertTemplateRule.getRuleName()).setAlertStatus(CommonConstants.FIRING_STATUS) |
| 146 | .setTemplateRuleType(alertTemplateRule.getRuleType()).setLevel(alertTemplateRule.getLevel()) | 155 | .setTemplateRuleType(alertTemplateRule.getRuleType()).setLevel(alertTemplateRule.getLevel()) |
| 147 | .setNotifyWayIds(alertTemplateRule.getNotifyWayIds()).setNotifyWayNames(notifyWayNames); | 156 | .setNotifyWayIds(alertTemplateRule.getNotifyWayIds()).setNotifyWayNames(notifyWayNames); |
| 148 | String ruleContent = alertTemplateRule.getRuleContent(); | 157 | String ruleContent = alertTemplateRule.getRuleContent(); |
| @@ -188,7 +197,7 @@ public class AlertApiService { | |||
| 188 | opsClusterEntity.getClusterId() + "/" + opsHost.getPublicIp() + ":" + opsClusterEntity.getPort() | 197 | opsClusterEntity.getClusterId() + "/" + opsHost.getPublicIp() + ":" + opsClusterEntity.getPort() |
| 189 | + "(" + opsClusterNodeEntity.getClusterRole() + ")"; | 198 | + "(" + opsClusterNodeEntity.getClusterRole() + ")"; |
| 190 | contentParamDto.setHostname(opsHost.getHostname()).setNodeName(nodeName).setPort( | 199 | contentParamDto.setHostname(opsHost.getHostname()).setNodeName(nodeName).setPort( |
| 191 | - opsClusterEntity.getPort() != null ? opsHost.getPort().toString() : "").setHostIp( | 200 | + opsClusterEntity.getPort() != null ? opsClusterEntity.getPort().toString() : "").setHostIp( |
| 192 | opsHost.getPublicIp()).setAlertTime( | 201 | opsHost.getPublicIp()).setAlertTime( |
| 193 | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(alertTime)).setLevel( | 202 | DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(alertTime)).setLevel( |
| 194 | level); | 203 | level); |
| @@ -7,6 +7,11 @@ package com.nctigba.alert.monitor.service; | |||
| 7 | import cn.hutool.core.collection.CollectionUtil; | 7 | import cn.hutool.core.collection.CollectionUtil; |
| 8 | import cn.hutool.core.util.StrUtil; | 8 | import cn.hutool.core.util.StrUtil; |
| 9 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | 9 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 10 | +import com.nctigba.alert.monitor.constant.CommonConstants; | ||
| 11 | +import com.nctigba.alert.monitor.entity.NotifyConfig; | ||
| 12 | +import com.nctigba.alert.monitor.entity.NotifyMessage; | ||
| 13 | +import com.nctigba.alert.monitor.mapper.NotifyConfigMapper; | ||
| 14 | +import com.nctigba.alert.monitor.mapper.NotifyMessageMapper; | ||
| 10 | import jakarta.mail.Authenticator; | 15 | import jakarta.mail.Authenticator; |
| 11 | import jakarta.mail.Message; | 16 | import jakarta.mail.Message; |
| 12 | import jakarta.mail.MessagingException; | 17 | import jakarta.mail.MessagingException; |
| @@ -19,11 +24,6 @@ import jakarta.mail.internet.MimeMessage; | |||
| 19 | import jakarta.mail.internet.MimeUtility; | 24 | import jakarta.mail.internet.MimeUtility; |
| 20 | import lombok.extern.slf4j.Slf4j; | 25 | import lombok.extern.slf4j.Slf4j; |
| 21 | import org.opengauss.admin.common.exception.ServiceException; | 26 | import org.opengauss.admin.common.exception.ServiceException; |
| 22 | -import com.nctigba.alert.monitor.constant.CommonConstants; | ||
| 23 | -import com.nctigba.alert.monitor.entity.NotifyConfig; | ||
| 24 | -import com.nctigba.alert.monitor.entity.NotifyMessage; | ||
| 25 | -import com.nctigba.alert.monitor.mapper.NotifyConfigMapper; | ||
| 26 | -import com.nctigba.alert.monitor.mapper.NotifyMessageMapper; | ||
| 27 | import org.springframework.beans.factory.annotation.Autowired; | 27 | import org.springframework.beans.factory.annotation.Autowired; |
| 28 | import org.springframework.stereotype.Service; | 28 | import org.springframework.stereotype.Service; |
| 29 | 29 | ||
| @@ -69,12 +69,7 @@ public class EmailService { | |||
| 69 | NotifyConfig notifyConfig = emailConfigList.get(0); | 69 | NotifyConfig notifyConfig = emailConfigList.get(0); |
| 70 | Properties properties = this.applyProperties(notifyConfig); | 70 | Properties properties = this.applyProperties(notifyConfig); |
| 71 | try { | 71 | try { |
| 72 | - Session session = Session.getInstance(properties, new Authenticator() { | 72 | + Session session = getSession(properties, notifyConfig.getAccount(), notifyConfig.getPasswd()); |
| 73 | - | ||
| 74 | - protected PasswordAuthentication getPasswordAuthentication() { | ||
| 75 | - return new PasswordAuthentication(notifyConfig.getAccount(), notifyConfig.getPasswd()); | ||
| 76 | - } | ||
| 77 | - }); | ||
| 78 | MimeMessage mimeMessage = new MimeMessage(session); | 73 | MimeMessage mimeMessage = new MimeMessage(session); |
| 79 | String[] receiverArr = notifyMessage.getEmail().split(","); | 74 | String[] receiverArr = notifyMessage.getEmail().split(","); |
| 80 | InternetAddress[] addresses = Stream.of(receiverArr).map(item -> { | 75 | InternetAddress[] addresses = Stream.of(receiverArr).map(item -> { |
| @@ -112,12 +107,7 @@ public class EmailService { | |||
| 112 | Properties properties = this.applyProperties(notifyConfig); | 107 | Properties properties = this.applyProperties(notifyConfig); |
| 113 | for (NotifyMessage notifyMessage : notifyMessageList) { | 108 | for (NotifyMessage notifyMessage : notifyMessageList) { |
| 114 | try { | 109 | try { |
| 115 | - Session session = Session.getInstance(properties, new Authenticator() { | 110 | + Session session = getSession(properties, notifyConfig.getAccount(), notifyConfig.getPasswd()); |
| 116 | - | ||
| 117 | - protected PasswordAuthentication getPasswordAuthentication() { | ||
| 118 | - return new PasswordAuthentication(notifyConfig.getAccount(), notifyConfig.getPasswd()); | ||
| 119 | - } | ||
| 120 | - }); | ||
| 121 | MimeMessage mimeMessage = new MimeMessage(session); | 111 | MimeMessage mimeMessage = new MimeMessage(session); |
| 122 | String[] receiverArr = notifyMessage.getEmail().split(","); | 112 | String[] receiverArr = notifyMessage.getEmail().split(","); |
| 123 | InternetAddress[] addresses = Stream.of(receiverArr).map(item -> { | 113 | InternetAddress[] addresses = Stream.of(receiverArr).map(item -> { |
| @@ -146,12 +136,7 @@ public class EmailService { | |||
| 146 | public void sendTest(NotifyConfig notifyConfig, String receiver, String title, String content) { | 136 | public void sendTest(NotifyConfig notifyConfig, String receiver, String title, String content) { |
| 147 | Properties properties = this.applyProperties(notifyConfig); | 137 | Properties properties = this.applyProperties(notifyConfig); |
| 148 | try { | 138 | try { |
| 149 | - Session session = Session.getInstance(properties, new Authenticator() { | 139 | + Session session = getSession(properties, notifyConfig.getAccount(), notifyConfig.getPasswd()); |
| 150 | - | ||
| 151 | - protected PasswordAuthentication getPasswordAuthentication() { | ||
| 152 | - return new PasswordAuthentication(notifyConfig.getAccount(), notifyConfig.getPasswd()); | ||
| 153 | - } | ||
| 154 | - }); | ||
| 155 | MimeMessage mimeMessage = new MimeMessage(session); | 140 | MimeMessage mimeMessage = new MimeMessage(session); |
| 156 | String[] receiverArr = receiver.split(","); | 141 | String[] receiverArr = receiver.split(","); |
| 157 | InternetAddress[] addresses = Stream.of(receiverArr).map(item -> { | 142 | InternetAddress[] addresses = Stream.of(receiverArr).map(item -> { |
| @@ -179,4 +164,13 @@ public class EmailService { | |||
| 179 | return "<div style='white-space: pre-line;'> " + content.replaceAll(CommonConstants.LINE_SEPARATOR, "<br/>") | 164 | return "<div style='white-space: pre-line;'> " + content.replaceAll(CommonConstants.LINE_SEPARATOR, "<br/>") |
| 180 | + "</div>"; | 165 | + "</div>"; |
| 181 | } | 166 | } |
| 167 | + | ||
| 168 | + private Session getSession(Properties properties, String account, String passwd) { | ||
| 169 | + return Session.getInstance(properties, new Authenticator() { | ||
| 170 | + | ||
| 171 | + protected PasswordAuthentication getPasswordAuthentication() { | ||
| 172 | + return new PasswordAuthentication(account, passwd); | ||
| 173 | + } | ||
| 174 | + }); | ||
| 175 | + } | ||
| 182 | } | 176 | } |
| @@ -308,7 +308,7 @@ public class PrometheusService { | |||
| 308 | return new Number[0][0]; | 308 | return new Number[0][0]; |
| 309 | } | 309 | } |
| 310 | JSONObject metricInfo = result.getJSONObject(0); | 310 | JSONObject metricInfo = result.getJSONObject(0); |
| 311 | - if (metricInfo == null) { | 311 | + if (metricInfo.isEmpty()) { |
| 312 | return new Number[0][0]; | 312 | return new Number[0][0]; |
| 313 | } | 313 | } |
| 314 | JSONArray values = metricInfo.getJSONArray("values"); | 314 | JSONArray values = metricInfo.getJSONArray("values"); |
Mplugins/alert-monitor/src/main/java/com/nctigba/alert/monitor/service/impl/AlertClusterNodeConfServiceImpl.java+2-2
| @@ -114,8 +114,8 @@ public class AlertClusterNodeConfServiceImpl | |||
| 114 | // set the old rule configuration params which should be updated | 114 | // set the old rule configuration params which should be updated |
| 115 | List<AlertClusterNodeConf> oldListByNodeIds = this.baseMapper.selectList( | 115 | List<AlertClusterNodeConf> oldListByNodeIds = this.baseMapper.selectList( |
| 116 | Wrappers.<AlertClusterNodeConf>lambdaQuery().ne(AlertClusterNodeConf::getTemplateId, templateId).in( | 116 | Wrappers.<AlertClusterNodeConf>lambdaQuery().ne(AlertClusterNodeConf::getTemplateId, templateId).in( |
| 117 | - AlertClusterNodeConf::getClusterNodeId, | 117 | + AlertClusterNodeConf::getClusterNodeId, clusterNodeIdList) |
| 118 | - clusterNodeIdList).eq(AlertClusterNodeConf::getIsDeleted, CommonConstants.IS_NOT_DELETE)); | 118 | + .eq(AlertClusterNodeConf::getIsDeleted, CommonConstants.IS_NOT_DELETE)); |
| 119 | Map<Long, String> ruleConfigMap = new HashMap<>(); | 119 | Map<Long, String> ruleConfigMap = new HashMap<>(); |
| 120 | if (CollectionUtil.isNotEmpty(oldListByNodeIds)) { | 120 | if (CollectionUtil.isNotEmpty(oldListByNodeIds)) { |
| 121 | Map<Long, List<String>> listMap = oldListByNodeIds.stream().collect( | 121 | Map<Long, List<String>> listMap = oldListByNodeIds.stream().collect( |
Mplugins/alert-monitor/src/main/java/com/nctigba/alert/monitor/service/impl/AlertRecordServiceImpl.java+13-6
| @@ -97,9 +97,6 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 97 | .ge(startTime != null, AlertRecord::getStartTime, startTime) | 97 | .ge(startTime != null, AlertRecord::getStartTime, startTime) |
| 98 | .le(endTime != null, AlertRecord::getEndTime, endTime).orderByDesc(AlertRecord::getId)); | 98 | .le(endTime != null, AlertRecord::getEndTime, endTime).orderByDesc(AlertRecord::getId)); |
| 99 | Page<AlertRecordDto> recordDtoPage = new Page<>(); | 99 | Page<AlertRecordDto> recordDtoPage = new Page<>(); |
| 100 | - if (recordPage == null) { | ||
| 101 | - return recordDtoPage; | ||
| 102 | - } | ||
| 103 | List<AlertRecord> records = recordPage.getRecords(); | 100 | List<AlertRecord> records = recordPage.getRecords(); |
| 104 | recordDtoPage.setTotal(recordPage.getTotal()).setCurrent(recordPage.getCurrent()).setSize(recordPage.getSize()); | 101 | recordDtoPage.setTotal(recordPage.getTotal()).setCurrent(recordPage.getCurrent()).setSize(recordPage.getSize()); |
| 105 | if (CollectionUtil.isEmpty(records)) { | 102 | if (CollectionUtil.isEmpty(records)) { |
| @@ -187,6 +184,9 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 187 | List<Map<String, Object>> alertStatusMaps = this.listMaps(queryWrapper); | 184 | List<Map<String, Object>> alertStatusMaps = this.listMaps(queryWrapper); |
| 188 | AlertStatisticsDto alertStatisticsDto = new AlertStatisticsDto(); | 185 | AlertStatisticsDto alertStatisticsDto = new AlertStatisticsDto(); |
| 189 | for (Map<String, Object> alertStatusMap : alertStatusMaps) { | 186 | for (Map<String, Object> alertStatusMap : alertStatusMaps) { |
| 187 | + if (alertStatusMap.get("alertstatus") == null) { | ||
| 188 | + continue; | ||
| 189 | + } | ||
| 190 | int count = 0; | 190 | int count = 0; |
| 191 | if (alertStatusMap.get("count") instanceof Long) { | 191 | if (alertStatusMap.get("count") instanceof Long) { |
| 192 | count = ((Long) alertStatusMap.get("count")).intValue(); | 192 | count = ((Long) alertStatusMap.get("count")).intValue(); |
| @@ -212,6 +212,9 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 212 | List<Map<String, Object>> recordStatusMaps = this.listMaps(queryWrapper); | 212 | List<Map<String, Object>> recordStatusMaps = this.listMaps(queryWrapper); |
| 213 | AlertStatisticsDto alertStatisticsDto = new AlertStatisticsDto(); | 213 | AlertStatisticsDto alertStatisticsDto = new AlertStatisticsDto(); |
| 214 | for (Map<String, Object> recordStatusMap : recordStatusMaps) { | 214 | for (Map<String, Object> recordStatusMap : recordStatusMaps) { |
| 215 | + if (recordStatusMap.get("recordstatus") == null) { | ||
| 216 | + continue; | ||
| 217 | + } | ||
| 215 | int count = 0; | 218 | int count = 0; |
| 216 | if (recordStatusMap.get("count") instanceof Long) { | 219 | if (recordStatusMap.get("count") instanceof Long) { |
| 217 | count = ((Long) recordStatusMap.get("count")).intValue(); | 220 | count = ((Long) recordStatusMap.get("count")).intValue(); |
| @@ -236,6 +239,9 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 236 | List<Map<String, Object>> levelMaps = this.listMaps(queryWrapper); | 239 | List<Map<String, Object>> levelMaps = this.listMaps(queryWrapper); |
| 237 | AlertStatisticsDto alertStatisticsDto = new AlertStatisticsDto(); | 240 | AlertStatisticsDto alertStatisticsDto = new AlertStatisticsDto(); |
| 238 | for (Map<String, Object> levelMap : levelMaps) { | 241 | for (Map<String, Object> levelMap : levelMaps) { |
| 242 | + if (levelMap.get("level") == null) { | ||
| 243 | + continue; | ||
| 244 | + } | ||
| 239 | int count = 0; | 245 | int count = 0; |
| 240 | if (levelMap.get("count") instanceof Long) { | 246 | if (levelMap.get("count") instanceof Long) { |
| 241 | count = ((Long) levelMap.get("count")).intValue(); | 247 | count = ((Long) levelMap.get("count")).intValue(); |
| @@ -275,7 +281,7 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 275 | OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | 281 | OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); |
| 276 | OpsClusterNodeEntity opsClusterNodeEntity = clusterNodeService.getById(clusterNodeId); | 282 | OpsClusterNodeEntity opsClusterNodeEntity = clusterNodeService.getById(clusterNodeId); |
| 277 | if (opsClusterNodeEntity == null) { | 283 | if (opsClusterNodeEntity == null) { |
| 278 | - opsClusterNodeEntity = new OpsClusterNodeEntity(); | 284 | + return alertRecordDto; |
| 279 | } | 285 | } |
| 280 | if (StrUtil.isNotBlank(opsClusterNodeEntity.getHostId())) { | 286 | if (StrUtil.isNotBlank(opsClusterNodeEntity.getHostId())) { |
| 281 | opsHostEntity = hostFacade.getById(opsClusterNodeEntity.getHostId()); | 287 | opsHostEntity = hostFacade.getById(opsClusterNodeEntity.getHostId()); |
| @@ -288,7 +294,8 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 288 | + "(" + opsClusterNodeEntity.getClusterRole() + ")"; | 294 | + "(" + opsClusterNodeEntity.getClusterRole() + ")"; |
| 289 | alertRecordDto.setClusterNodeName(nodeName).setHostIpAndPort( | 295 | alertRecordDto.setClusterNodeName(nodeName).setHostIpAndPort( |
| 290 | opsHostEntity.getPublicIp() + ":" + opsClusterEntity.getPort()).setClusterId( | 296 | opsHostEntity.getPublicIp() + ":" + opsClusterEntity.getPort()).setClusterId( |
| 291 | - opsClusterEntity.getClusterId()).setNodeRole(opsClusterNodeEntity.getClusterRole().name()); | 297 | + opsClusterEntity.getClusterId()).setNodeRole(opsClusterNodeEntity.getClusterRole() != null |
| 298 | + ? opsClusterNodeEntity.getClusterRole().name() : ""); | ||
| 292 | return alertRecordDto; | 299 | return alertRecordDto; |
| 293 | } | 300 | } |
| 294 | 301 | ||
| @@ -308,7 +315,7 @@ public class AlertRecordServiceImpl extends ServiceImpl<AlertRecordMapper, Alert | |||
| 308 | relationDto.setName(MessageSourceUtil.get(templateRuleItem.getRuleExpName())).setUnit( | 315 | relationDto.setName(MessageSourceUtil.get(templateRuleItem.getRuleExpName())).setUnit( |
| 309 | templateRuleItem.getUnit()).setStartTime(alertRecord.getStartTime()).setEndTime( | 316 | templateRuleItem.getUnit()).setStartTime(alertRecord.getStartTime()).setEndTime( |
| 310 | alertRecord.getEndTime()); | 317 | alertRecord.getEndTime()); |
| 311 | - // 向前推30分钟,向后推30分钟 | 318 | + |
| 312 | LocalDateTime minTime = alertRecord.getStartTime().minusMinutes(30L); | 319 | LocalDateTime minTime = alertRecord.getStartTime().minusMinutes(30L); |
| 313 | LocalDateTime maxTime = alertRecord.getEndTime().plusMinutes(30L); | 320 | LocalDateTime maxTime = alertRecord.getEndTime().plusMinutes(30L); |
| 314 | relationDto.setMinTime(minTime).setMaxTime(maxTime); | 321 | relationDto.setMinTime(minTime).setMaxTime(maxTime); |
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/AlertApiControllerTest.java+70-0
| @@ -0,0 +1,70 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import cn.hutool.json.JSONObject; | ||
| 8 | +import com.nctigba.alert.monitor.service.AlertApiService; | ||
| 9 | +import org.junit.Before; | ||
| 10 | +import org.junit.Test; | ||
| 11 | +import org.junit.runner.RunWith; | ||
| 12 | +import org.mockito.InjectMocks; | ||
| 13 | +import org.mockito.Mock; | ||
| 14 | +import org.mockito.MockitoAnnotations; | ||
| 15 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 16 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 17 | + | ||
| 18 | +import java.util.ArrayList; | ||
| 19 | +import java.util.List; | ||
| 20 | + | ||
| 21 | +import static org.junit.Assert.assertEquals; | ||
| 22 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 23 | +import static org.mockito.Mockito.doNothing; | ||
| 24 | +import static org.mockito.Mockito.times; | ||
| 25 | +import static org.mockito.Mockito.verify; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * test AlertApiController | ||
| 29 | + * | ||
| 30 | + * 2023/7/16 02:31 | ||
| 31 | + */ | ||
| 32 | + | ||
| 33 | +public class AlertApiControllerTest { | ||
| 34 | + | ||
| 35 | + private AlertApiController alertApiController; | ||
| 36 | + | ||
| 37 | + private AlertApiService alertApiService; | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + public void setUp() { | ||
| 41 | + MockitoAnnotations.initMocks(this); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + public void testAlerts() { | ||
| 46 | + JSONObject jsonObject = new JSONObject(); | ||
| 47 | + jsonObject.put("startsAt", "2023-04-29T17:38:17.71694477Z"); | ||
| 48 | + jsonObject.put("endsAt", "2023-04-29T17:42:17.71694477Z"); | ||
| 49 | + jsonObject.put("generatorURL", "url"); | ||
| 50 | + JSONObject labels = new JSONObject(); | ||
| 51 | + labels.put("alertname", "rule_1"); | ||
| 52 | + labels.put("instance", "node1"); | ||
| 53 | + labels.put("level", "warn"); | ||
| 54 | + labels.put("templateId", 1); | ||
| 55 | + labels.put("templateRuleId", 1); | ||
| 56 | + jsonObject.put("labels", labels); | ||
| 57 | + JSONObject annotations = new JSONObject(); | ||
| 58 | + annotations.put("summary", "summary"); | ||
| 59 | + annotations.put("description", "description"); | ||
| 60 | + jsonObject.put("annotations", annotations); | ||
| 61 | + List<JSONObject> paramList = new ArrayList<>(); | ||
| 62 | + paramList.add(jsonObject); | ||
| 63 | + | ||
| 64 | + doNothing().when(alertApiService).alerts(anyList()); | ||
| 65 | + | ||
| 66 | + AjaxResult result = alertApiController.alerts(paramList); | ||
| 67 | + verify(alertApiService, times(1)).alerts(anyList()); | ||
| 68 | + assertEquals(AjaxResult.success(), result); | ||
| 69 | + } | ||
| 70 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/AlertClusterNodeConfControllerTest.java+88-0
| @@ -0,0 +1,88 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.dto.AlertClusterNodeConfDto; | ||
| 8 | +import com.nctigba.alert.monitor.entity.AlertClusterNodeConf; | ||
| 9 | +import com.nctigba.alert.monitor.model.AlertClusterNodeAndTemplateReq; | ||
| 10 | +import com.nctigba.alert.monitor.model.AlertClusterNodeConfReq; | ||
| 11 | +import com.nctigba.alert.monitor.service.AlertClusterNodeConfService; | ||
| 12 | +import org.junit.Before; | ||
| 13 | +import org.junit.Test; | ||
| 14 | +import org.junit.runner.RunWith; | ||
| 15 | +import org.mockito.InjectMocks; | ||
| 16 | +import org.mockito.Mock; | ||
| 17 | +import org.mockito.MockitoAnnotations; | ||
| 18 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 19 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 20 | + | ||
| 21 | +import java.util.ArrayList; | ||
| 22 | +import java.util.List; | ||
| 23 | + | ||
| 24 | +import static org.junit.Assert.assertEquals; | ||
| 25 | +import static org.mockito.ArgumentMatchers.any; | ||
| 26 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 27 | +import static org.mockito.Mockito.doNothing; | ||
| 28 | +import static org.mockito.Mockito.times; | ||
| 29 | +import static org.mockito.Mockito.verify; | ||
| 30 | +import static org.mockito.Mockito.when; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * test AlertClusterNodeConfController | ||
| 34 | + * | ||
| 35 | + * 2023/7/16 02:54 | ||
| 36 | + */ | ||
| 37 | + | ||
| 38 | +public class AlertClusterNodeConfControllerTest { | ||
| 39 | + | ||
| 40 | + private AlertClusterNodeConfController alertClusterNodeConfController; | ||
| 41 | + | ||
| 42 | + private AlertClusterNodeConfService alertClusterNodeConfService; | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + public void setUp() { | ||
| 46 | + MockitoAnnotations.initMocks(this); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + public void testGetList() { | ||
| 51 | + List<AlertClusterNodeConfDto> list = new ArrayList<>(); | ||
| 52 | + when(alertClusterNodeConfService.getList()).thenReturn(list); | ||
| 53 | + AjaxResult result = alertClusterNodeConfController.getList(); | ||
| 54 | + verify(alertClusterNodeConfService, times(1)).getList(); | ||
| 55 | + assertEquals(list, result.get("data")); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + | ||
| 59 | + public void testGetByClusterNodeId() { | ||
| 60 | + AlertClusterNodeConf alertClusterNodeConf = new AlertClusterNodeConf(); | ||
| 61 | + when(alertClusterNodeConfService.getByClusterNodeId(anyString())).thenReturn(alertClusterNodeConf); | ||
| 62 | + String clusterNodeId = "node1"; | ||
| 63 | + AjaxResult result = alertClusterNodeConfController.getByClusterNodeId(clusterNodeId); | ||
| 64 | + verify(alertClusterNodeConfService, times(1)).getByClusterNodeId(anyString()); | ||
| 65 | + assertEquals(alertClusterNodeConf, result.get("data")); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + public void testSaveClusterNodeConf() { | ||
| 70 | + doNothing().when(alertClusterNodeConfService).saveClusterNodeConf(any(AlertClusterNodeConfReq.class)); | ||
| 71 | + AlertClusterNodeConfReq alertClusterNodeConfReq = new AlertClusterNodeConfReq(); | ||
| 72 | + AjaxResult result = alertClusterNodeConfController.saveClusterNodeConf(alertClusterNodeConfReq); | ||
| 73 | + verify(alertClusterNodeConfService, times(1)) | ||
| 74 | + .saveClusterNodeConf(any(AlertClusterNodeConfReq.class)); | ||
| 75 | + assertEquals(AjaxResult.success(), result); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + public void testSaveAlertTemplateAndConfig() { | ||
| 80 | + doNothing().when(alertClusterNodeConfService) | ||
| 81 | + .saveAlertTemplateAndConfig(any(AlertClusterNodeAndTemplateReq.class)); | ||
| 82 | + AlertClusterNodeAndTemplateReq clusterNodeAndTemplateReq = new AlertClusterNodeAndTemplateReq(); | ||
| 83 | + AjaxResult result = alertClusterNodeConfController.saveAlertTemplateAndConfig(clusterNodeAndTemplateReq); | ||
| 84 | + verify(alertClusterNodeConfService, times(1)) | ||
| 85 | + .saveAlertTemplateAndConfig(any(AlertClusterNodeAndTemplateReq.class)); | ||
| 86 | + assertEquals(AjaxResult.success(), result); | ||
| 87 | + } | ||
| 88 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/AlertConfigControllerTest.java+64-0
| @@ -0,0 +1,64 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.entity.AlertConfig; | ||
| 8 | +import com.nctigba.alert.monitor.service.AlertConfigService; | ||
| 9 | +import org.junit.Test; | ||
| 10 | +import org.junit.runner.RunWith; | ||
| 11 | +import org.mockito.InjectMocks; | ||
| 12 | +import org.mockito.Mock; | ||
| 13 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 14 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 15 | + | ||
| 16 | +import java.util.ArrayList; | ||
| 17 | +import java.util.List; | ||
| 18 | + | ||
| 19 | +import static org.junit.Assert.assertEquals; | ||
| 20 | +import static org.mockito.ArgumentMatchers.any; | ||
| 21 | +import static org.mockito.Mockito.doNothing; | ||
| 22 | +import static org.mockito.Mockito.times; | ||
| 23 | +import static org.mockito.Mockito.verify; | ||
| 24 | +import static org.mockito.Mockito.when; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * test AlertConfigController | ||
| 28 | + * | ||
| 29 | + * 2023/7/16 03:08 | ||
| 30 | + */ | ||
| 31 | + | ||
| 32 | +public class AlertConfigControllerTest { | ||
| 33 | + | ||
| 34 | + private AlertConfigController alertConfigController; | ||
| 35 | + | ||
| 36 | + private AlertConfigService alertConfigService; | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + public void testGetAlertConfWithNull() { | ||
| 40 | + List<AlertConfig> list = new ArrayList<>(); | ||
| 41 | + when(alertConfigService.list()).thenReturn(list); | ||
| 42 | + AjaxResult result = alertConfigController.getAlertConf(); | ||
| 43 | + verify(alertConfigService, times(1)).list(); | ||
| 44 | + assertEquals(new AlertConfig(), result.get("data")); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public void testGetAlertConf() { | ||
| 48 | + List<AlertConfig> list = new ArrayList<>(); | ||
| 49 | + AlertConfig alertConfig = new AlertConfig(); | ||
| 50 | + list.add(alertConfig); | ||
| 51 | + when(alertConfigService.list()).thenReturn(list); | ||
| 52 | + AjaxResult result = alertConfigController.getAlertConf(); | ||
| 53 | + verify(alertConfigService, times(1)).list(); | ||
| 54 | + assertEquals(list.get(0), result.get("data")); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + | ||
| 58 | + public void testSaveAlertConf() { | ||
| 59 | + doNothing().when(alertConfigService).saveAlertConf(any(AlertConfig.class)); | ||
| 60 | + AlertConfig alertConfig = new AlertConfig(); | ||
| 61 | + AjaxResult result = alertConfigController.saveAlertConf(alertConfig); | ||
| 62 | + assertEquals(AjaxResult.success(), result); | ||
| 63 | + } | ||
| 64 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/AlertRecordControllerTest.java+110-0
| @@ -0,0 +1,110 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 8 | +import com.nctigba.alert.monitor.dto.AlertRecordDto; | ||
| 9 | +import com.nctigba.alert.monitor.dto.AlertRelationDto; | ||
| 10 | +import com.nctigba.alert.monitor.dto.AlertStatisticsDto; | ||
| 11 | +import com.nctigba.alert.monitor.model.AlertRecordReq; | ||
| 12 | +import com.nctigba.alert.monitor.model.AlertStatisticsReq; | ||
| 13 | +import com.nctigba.alert.monitor.service.AlertRecordService; | ||
| 14 | +import org.junit.Test; | ||
| 15 | +import org.junit.runner.RunWith; | ||
| 16 | +import org.mockito.InjectMocks; | ||
| 17 | +import org.mockito.Mock; | ||
| 18 | +import org.mockito.MockedStatic; | ||
| 19 | +import org.mockito.Spy; | ||
| 20 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 21 | +import org.opengauss.admin.common.core.page.TableDataInfo; | ||
| 22 | +import org.opengauss.admin.common.utils.ServletUtils; | ||
| 23 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 24 | + | ||
| 25 | +import java.util.ArrayList; | ||
| 26 | +import java.util.List; | ||
| 27 | + | ||
| 28 | +import static org.junit.Assert.assertEquals; | ||
| 29 | +import static org.mockito.ArgumentMatchers.any; | ||
| 30 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 31 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 32 | +import static org.mockito.Mockito.mockStatic; | ||
| 33 | +import static org.mockito.Mockito.times; | ||
| 34 | +import static org.mockito.Mockito.verify; | ||
| 35 | +import static org.mockito.Mockito.when; | ||
| 36 | + | ||
| 37 | +/** | ||
| 38 | + * test AlertRecordController | ||
| 39 | + * | ||
| 40 | + * 2023/7/16 03:19 | ||
| 41 | + */ | ||
| 42 | + | ||
| 43 | +public class AlertRecordControllerTest { | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + private AlertRecordController alertRecordController; | ||
| 47 | + | ||
| 48 | + private AlertRecordService alertRecordService; | ||
| 49 | + | ||
| 50 | + | ||
| 51 | + public void testAlertRecordListPage() { | ||
| 52 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 53 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(1); | ||
| 54 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(10); | ||
| 55 | + Page<AlertRecordDto> page = new Page<>(1, 10); | ||
| 56 | + when(alertRecordService.getListPage(any(AlertRecordReq.class), any(Page.class))).thenReturn(page); | ||
| 57 | + AlertRecordReq recordReq = new AlertRecordReq(); | ||
| 58 | + TableDataInfo result = alertRecordController.alertRecordListPage(recordReq); | ||
| 59 | + verify(alertRecordService, times(1)).getListPage(any(AlertRecordReq.class), any(Page.class)); | ||
| 60 | + assertEquals(page.getRecords(), result.getRows()); | ||
| 61 | + assertEquals(page.getTotal(), result.getTotal()); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + public void testAlertRecordStatistics() { | ||
| 67 | + AlertStatisticsDto statisticsDto = new AlertStatisticsDto(); | ||
| 68 | + when(alertRecordService.alertRecordStatistics(any(AlertStatisticsReq.class))).thenReturn(statisticsDto); | ||
| 69 | + AlertStatisticsReq alertStatisticsReq = new AlertStatisticsReq(); | ||
| 70 | + AjaxResult result = alertRecordController.alertRecordStatistics(alertStatisticsReq); | ||
| 71 | + verify(alertRecordService, times(1)) | ||
| 72 | + .alertRecordStatistics(any(AlertStatisticsReq.class)); | ||
| 73 | + assertEquals(statisticsDto, result.get("data")); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + | ||
| 77 | + public void testGetById() { | ||
| 78 | + AlertRecordDto alertRecordDto = new AlertRecordDto(); | ||
| 79 | + when(alertRecordService.getById(anyLong())).thenReturn(alertRecordDto); | ||
| 80 | + Long id = 1L; | ||
| 81 | + AjaxResult result = alertRecordController.getById(id); | ||
| 82 | + verify(alertRecordService, times(1)).getById(anyLong()); | ||
| 83 | + assertEquals(alertRecordDto, result.get("data")); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + | ||
| 87 | + public void testMarkAsUnread() { | ||
| 88 | + when(alertRecordService.markAsUnread(anyString())).thenReturn("success"); | ||
| 89 | + AjaxResult result = alertRecordController.markAsUnread("1"); | ||
| 90 | + verify(alertRecordService, times(1)).markAsUnread(anyString()); | ||
| 91 | + assertEquals("success", result.get("msg")); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + | ||
| 95 | + public void testMarkAsRead() { | ||
| 96 | + when(alertRecordService.markAsRead(anyString())).thenReturn("success"); | ||
| 97 | + AjaxResult result = alertRecordController.markAsRead("1"); | ||
| 98 | + verify(alertRecordService, times(1)).markAsRead(anyString()); | ||
| 99 | + assertEquals("success", result.get("msg")); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + | ||
| 103 | + public void testGetRelationData() { | ||
| 104 | + List<AlertRelationDto> relationData = new ArrayList<>(); | ||
| 105 | + when(alertRecordService.getRelationData(anyLong())).thenReturn(relationData); | ||
| 106 | + AjaxResult result = alertRecordController.getRelationData(1L); | ||
| 107 | + verify(alertRecordService, times(1)).getRelationData(anyLong()); | ||
| 108 | + assertEquals(relationData, result.get("data")); | ||
| 109 | + } | ||
| 110 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/AlertRuleControllerTest.java+88-0
| @@ -0,0 +1,88 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 8 | +import com.nctigba.alert.monitor.dto.AlertRuleDto; | ||
| 9 | +import com.nctigba.alert.monitor.dto.RuleItemPropertyDto; | ||
| 10 | +import com.nctigba.alert.monitor.entity.AlertRule; | ||
| 11 | +import com.nctigba.alert.monitor.model.RuleReq; | ||
| 12 | +import com.nctigba.alert.monitor.service.AlertRuleService; | ||
| 13 | +import org.junit.Test; | ||
| 14 | +import org.junit.runner.RunWith; | ||
| 15 | +import org.mockito.InjectMocks; | ||
| 16 | +import org.mockito.Mock; | ||
| 17 | +import org.mockito.MockedStatic; | ||
| 18 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 19 | +import org.opengauss.admin.common.core.page.TableDataInfo; | ||
| 20 | +import org.opengauss.admin.common.utils.ServletUtils; | ||
| 21 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 22 | + | ||
| 23 | +import java.util.ArrayList; | ||
| 24 | +import java.util.List; | ||
| 25 | + | ||
| 26 | +import static org.junit.Assert.assertEquals; | ||
| 27 | +import static org.mockito.ArgumentMatchers.any; | ||
| 28 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 29 | +import static org.mockito.Mockito.mockStatic; | ||
| 30 | +import static org.mockito.Mockito.times; | ||
| 31 | +import static org.mockito.Mockito.verify; | ||
| 32 | +import static org.mockito.Mockito.when; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * test AlertRuleController | ||
| 36 | + * | ||
| 37 | + * 2023/7/16 10:37 | ||
| 38 | + */ | ||
| 39 | + | ||
| 40 | +public class AlertRuleControllerTest { | ||
| 41 | + | ||
| 42 | + private AlertRuleController alertRuleController; | ||
| 43 | + | ||
| 44 | + private AlertRuleService alertRuleService; | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + public void testGetRuleListPage() { | ||
| 48 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 49 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(1); | ||
| 50 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(10); | ||
| 51 | + Page<AlertRuleDto> page = new Page<>(1, 10); | ||
| 52 | + when(alertRuleService.getRulePage(any(RuleReq.class), any(Page.class))).thenReturn(page); | ||
| 53 | + RuleReq ruleReq = new RuleReq(); | ||
| 54 | + TableDataInfo result = alertRuleController.getRuleListPage(ruleReq); | ||
| 55 | + verify(alertRuleService, times(1)).getRulePage(any(RuleReq.class), any(Page.class)); | ||
| 56 | + assertEquals(page.getRecords(), result.getRows()); | ||
| 57 | + assertEquals(page.getTotal(), result.getTotal()); | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + public void testGetRuleList() { | ||
| 63 | + List<AlertRuleDto> list = new ArrayList<>(); | ||
| 64 | + when(alertRuleService.getRuleList()).thenReturn(list); | ||
| 65 | + AjaxResult result = alertRuleController.getRuleList(); | ||
| 66 | + verify(alertRuleService, times(1)).getRuleList(); | ||
| 67 | + assertEquals(list, result.get("data")); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + | ||
| 71 | + public void testGetRuleById() { | ||
| 72 | + AlertRule alertRule = new AlertRule(); | ||
| 73 | + when(alertRuleService.getRuleById(anyLong())).thenReturn(alertRule); | ||
| 74 | + Long id = 1L; | ||
| 75 | + AjaxResult result = alertRuleController.getRuleById(id); | ||
| 76 | + verify(alertRuleService, times(1)).getRuleById(anyLong()); | ||
| 77 | + assertEquals(alertRule, result.get("data")); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + public void testGetRuleItemProperties() { | ||
| 82 | + List<RuleItemPropertyDto> list = new ArrayList<>(); | ||
| 83 | + when(alertRuleService.getRuleItemProperties()).thenReturn(list); | ||
| 84 | + AjaxResult result = alertRuleController.getRuleItemProperties(); | ||
| 85 | + verify(alertRuleService, times(1)).getRuleItemProperties(); | ||
| 86 | + assertEquals(list, result.get("data")); | ||
| 87 | + } | ||
| 88 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/AlertTemplateControllerTest.java+143-0
| @@ -0,0 +1,143 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 8 | +import com.nctigba.alert.monitor.dto.AlertTemplateDto; | ||
| 9 | +import com.nctigba.alert.monitor.dto.AlertTemplateRuleDto; | ||
| 10 | +import com.nctigba.alert.monitor.entity.AlertTemplate; | ||
| 11 | +import com.nctigba.alert.monitor.entity.AlertTemplateRule; | ||
| 12 | +import com.nctigba.alert.monitor.model.AlertTemplateReq; | ||
| 13 | +import com.nctigba.alert.monitor.service.AlertTemplateRuleService; | ||
| 14 | +import com.nctigba.alert.monitor.service.AlertTemplateService; | ||
| 15 | +import org.junit.Test; | ||
| 16 | +import org.junit.runner.RunWith; | ||
| 17 | +import org.mockito.InjectMocks; | ||
| 18 | +import org.mockito.Mock; | ||
| 19 | +import org.mockito.MockedStatic; | ||
| 20 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 21 | +import org.opengauss.admin.common.core.page.TableDataInfo; | ||
| 22 | +import org.opengauss.admin.common.utils.ServletUtils; | ||
| 23 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 24 | + | ||
| 25 | +import java.util.ArrayList; | ||
| 26 | +import java.util.List; | ||
| 27 | + | ||
| 28 | +import static org.junit.Assert.assertEquals; | ||
| 29 | +import static org.mockito.ArgumentMatchers.any; | ||
| 30 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 31 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 32 | +import static org.mockito.Mockito.doNothing; | ||
| 33 | +import static org.mockito.Mockito.mockStatic; | ||
| 34 | +import static org.mockito.Mockito.times; | ||
| 35 | +import static org.mockito.Mockito.verify; | ||
| 36 | +import static org.mockito.Mockito.when; | ||
| 37 | + | ||
| 38 | +/** | ||
| 39 | + * test AlertTemplateController | ||
| 40 | + * | ||
| 41 | + * 2023/7/16 11:22 | ||
| 42 | + */ | ||
| 43 | + | ||
| 44 | +public class AlertTemplateControllerTest { | ||
| 45 | + | ||
| 46 | + private AlertTemplateController alertTemplateController; | ||
| 47 | + | ||
| 48 | + private AlertTemplateService templateService; | ||
| 49 | + | ||
| 50 | + private AlertTemplateRuleService templateRuleService; | ||
| 51 | + | ||
| 52 | + | ||
| 53 | + public void testGetTemplatePage() { | ||
| 54 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 55 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(1); | ||
| 56 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(10); | ||
| 57 | + Page<AlertTemplate> page = new Page<>(1, 10); | ||
| 58 | + when(templateService.getTemplatePage(anyString(), any(Page.class))).thenReturn(page); | ||
| 59 | + TableDataInfo result = alertTemplateController.getTemplatePage(""); | ||
| 60 | + verify(templateService, times(1)).getTemplatePage(anyString(), any(Page.class)); | ||
| 61 | + assertEquals(page.getRecords(), result.getRows()); | ||
| 62 | + assertEquals(page.getTotal(), result.getTotal()); | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + public void testGetTemplateList() { | ||
| 68 | + List<AlertTemplate> list = new ArrayList<>(); | ||
| 69 | + when(templateService.getTemplateList()).thenReturn(list); | ||
| 70 | + AjaxResult result = alertTemplateController.getTemplateList(); | ||
| 71 | + verify(templateService, times(1)).getTemplateList(); | ||
| 72 | + assertEquals(list, result.get("data")); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + public void testGetTemplateDto() { | ||
| 77 | + AlertTemplateDto templateDto = new AlertTemplateDto(); | ||
| 78 | + when(templateService.getTemplate(anyLong())).thenReturn(templateDto); | ||
| 79 | + AjaxResult result = alertTemplateController.getTemplateDto(1L); | ||
| 80 | + verify(templateService, times(1)).getTemplate(anyLong()); | ||
| 81 | + assertEquals(templateDto, result.get("data")); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + | ||
| 85 | + public void testGetTemplateRulePage() { | ||
| 86 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 87 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(1); | ||
| 88 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(10); | ||
| 89 | + Page<AlertTemplateRuleDto> page = new Page<>(1, 10); | ||
| 90 | + when(templateService.getTemplateRulePage(anyLong(), anyString(), any(Page.class))).thenReturn(page); | ||
| 91 | + TableDataInfo result = alertTemplateController.getTemplateRulePage(1L, ""); | ||
| 92 | + verify(templateService, times(1)) | ||
| 93 | + .getTemplateRulePage(anyLong(), anyString(), any(Page.class)); | ||
| 94 | + assertEquals(page.getRecords(), result.getRows()); | ||
| 95 | + assertEquals(page.getTotal(), result.getTotal()); | ||
| 96 | + } | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + | ||
| 100 | + public void testGetTemplateRuleListById() { | ||
| 101 | + List<AlertTemplateRuleDto> list = new ArrayList<>(); | ||
| 102 | + when(templateService.getTemplateRuleListById(anyLong())).thenReturn(list); | ||
| 103 | + AjaxResult result = alertTemplateController.getTemplateRuleListById(1L); | ||
| 104 | + verify(templateService, times(1)).getTemplateRuleListById(anyLong()); | ||
| 105 | + assertEquals(list, result.get("data")); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + | ||
| 109 | + public void testGetTemplateRule() { | ||
| 110 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule(); | ||
| 111 | + when(templateRuleService.getTemplateRule(anyLong())).thenReturn(alertTemplateRule); | ||
| 112 | + AjaxResult result = alertTemplateController.getTemplateRule(1L); | ||
| 113 | + verify(templateRuleService, times(1)).getTemplateRule(anyLong()); | ||
| 114 | + assertEquals(alertTemplateRule, result.get("data")); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + | ||
| 118 | + public void testSaveTemplate() { | ||
| 119 | + AlertTemplate alertTemplate = new AlertTemplate(); | ||
| 120 | + when(templateService.saveTemplate(any(AlertTemplateReq.class))).thenReturn(alertTemplate); | ||
| 121 | + AlertTemplateReq templateReq = new AlertTemplateReq(); | ||
| 122 | + AjaxResult result = alertTemplateController.saveTemplate(templateReq); | ||
| 123 | + verify(templateService, times(1)).saveTemplate(any(AlertTemplateReq.class)); | ||
| 124 | + assertEquals(alertTemplate, result.get("data")); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + | ||
| 128 | + public void testDelTemplate() { | ||
| 129 | + doNothing().when(templateService).delTemplate(anyLong()); | ||
| 130 | + AjaxResult result = alertTemplateController.delTemplate(1L); | ||
| 131 | + assertEquals(AjaxResult.success(), result); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + | ||
| 135 | + public void testSaveTemplateRule() { | ||
| 136 | + AlertTemplateRuleDto templateRuleDto = new AlertTemplateRuleDto(); | ||
| 137 | + when(templateRuleService.saveTemplateRule(any(AlertTemplateRule.class))).thenReturn(templateRuleDto); | ||
| 138 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule(); | ||
| 139 | + AjaxResult result = alertTemplateController.saveTemplateRule(alertTemplateRule); | ||
| 140 | + verify(templateRuleService, times(1)).saveTemplateRule(any(AlertTemplateRule.class)); | ||
| 141 | + assertEquals(templateRuleDto, result.get("data")); | ||
| 142 | + } | ||
| 143 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/BaseControllerTest.java+69-0
| @@ -0,0 +1,69 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 8 | +import org.junit.Test; | ||
| 9 | +import org.junit.runner.RunWith; | ||
| 10 | +import org.mockito.InjectMocks; | ||
| 11 | +import org.mockito.MockedStatic; | ||
| 12 | +import org.opengauss.admin.common.core.page.TableDataInfo; | ||
| 13 | +import org.opengauss.admin.common.utils.ServletUtils; | ||
| 14 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 15 | + | ||
| 16 | +import static org.junit.Assert.assertEquals; | ||
| 17 | +import static org.mockito.ArgumentMatchers.any; | ||
| 18 | +import static org.mockito.Mockito.mockStatic; | ||
| 19 | + | ||
| 20 | +/** | ||
| 21 | + * test BaseController | ||
| 22 | + * | ||
| 23 | + * 2023/7/16 11:51 | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | +public class BaseControllerTest { | ||
| 27 | + | ||
| 28 | + private BaseController baseController; | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + public void testStartPageNull() { | ||
| 32 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 33 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(any()); | ||
| 34 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(any()); | ||
| 35 | + Page page = baseController.startPage(); | ||
| 36 | + assertEquals(1, page.getCurrent()); | ||
| 37 | + assertEquals(10, page.getSize()); | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + public void testStartPageNull2() { | ||
| 43 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 44 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(2); | ||
| 45 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(any()); | ||
| 46 | + Page page = baseController.startPage(); | ||
| 47 | + assertEquals(1, page.getCurrent()); | ||
| 48 | + assertEquals(10, page.getSize()); | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public void testStartPage() { | ||
| 53 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 54 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(2); | ||
| 55 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(100); | ||
| 56 | + Page page = baseController.startPage(); | ||
| 57 | + assertEquals(2, page.getCurrent()); | ||
| 58 | + assertEquals(100, page.getSize()); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + public void testGetDataTable() { | ||
| 64 | + Page page = new Page(); | ||
| 65 | + TableDataInfo dataTable = baseController.getDataTable(page); | ||
| 66 | + assertEquals(page.getTotal(), dataTable.getTotal()); | ||
| 67 | + assertEquals(page.getRecords(), dataTable.getRows()); | ||
| 68 | + } | ||
| 69 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/EnvironmentControllerTest.java+64-0
| @@ -0,0 +1,64 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.service.EnvironmentService; | ||
| 8 | +import org.junit.Test; | ||
| 9 | +import org.junit.runner.RunWith; | ||
| 10 | +import org.mockito.InjectMocks; | ||
| 11 | +import org.mockito.Mock; | ||
| 12 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 13 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 14 | + | ||
| 15 | +import java.util.ArrayList; | ||
| 16 | +import java.util.HashMap; | ||
| 17 | +import java.util.List; | ||
| 18 | +import java.util.Map; | ||
| 19 | + | ||
| 20 | +import static org.junit.Assert.assertEquals; | ||
| 21 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 22 | +import static org.mockito.Mockito.doNothing; | ||
| 23 | +import static org.mockito.Mockito.times; | ||
| 24 | +import static org.mockito.Mockito.verify; | ||
| 25 | +import static org.mockito.Mockito.when; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * test the function of the EnvironmentController | ||
| 29 | + * | ||
| 30 | + * 2023/7/3 23:45 | ||
| 31 | + */ | ||
| 32 | + | ||
| 33 | +public class EnvironmentControllerTest { | ||
| 34 | + | ||
| 35 | + private EnvironmentController environmentController; | ||
| 36 | + | ||
| 37 | + private EnvironmentService environmentService; | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + public void testCluster() { | ||
| 41 | + List cluster = new ArrayList<>(); | ||
| 42 | + when(environmentService.cluster()).thenReturn(cluster); | ||
| 43 | + AjaxResult result = environmentController.cluster(); | ||
| 44 | + verify(environmentService, times(1)).cluster(); | ||
| 45 | + assertEquals(cluster, result.get("data")); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + public void testCheckPrometheus() { | ||
| 50 | + doNothing().when(environmentService).checkPrometheus(); | ||
| 51 | + AjaxResult result = environmentController.checkPrometheus(); | ||
| 52 | + verify(environmentService, times(1)).checkPrometheus(); | ||
| 53 | + assertEquals(AjaxResult.success(), result); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + public void testGetAlertContentParam() { | ||
| 58 | + Map<String, Map<String, String>> map = new HashMap<>(); | ||
| 59 | + when(environmentService.getAlertContentParam(anyString())).thenReturn(map); | ||
| 60 | + AjaxResult result = environmentController.getAlertContentParam(anyString()); | ||
| 61 | + verify(environmentService, times(1)).getAlertContentParam(anyString()); | ||
| 62 | + assertEquals(map, result.get("data")); | ||
| 63 | + } | ||
| 64 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/NotifyConfigControllerTest.java+65-0
| @@ -0,0 +1,65 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.entity.NotifyConfig; | ||
| 8 | +import com.nctigba.alert.monitor.model.NotifyConfigReq; | ||
| 9 | +import com.nctigba.alert.monitor.service.NotifyConfigService; | ||
| 10 | +import org.junit.Test; | ||
| 11 | +import org.junit.runner.RunWith; | ||
| 12 | +import org.mockito.InjectMocks; | ||
| 13 | +import org.mockito.Mock; | ||
| 14 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 15 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 16 | + | ||
| 17 | +import java.util.ArrayList; | ||
| 18 | +import java.util.List; | ||
| 19 | + | ||
| 20 | +import static org.junit.Assert.assertEquals; | ||
| 21 | +import static org.mockito.ArgumentMatchers.any; | ||
| 22 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 23 | +import static org.mockito.Mockito.doNothing; | ||
| 24 | +import static org.mockito.Mockito.times; | ||
| 25 | +import static org.mockito.Mockito.verify; | ||
| 26 | +import static org.mockito.Mockito.when; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * test NotifyConfigController | ||
| 30 | + * | ||
| 31 | + * 2023/7/16 12:10 | ||
| 32 | + */ | ||
| 33 | + | ||
| 34 | +public class NotifyConfigControllerTest { | ||
| 35 | + | ||
| 36 | + private NotifyConfigController notifyConfigController; | ||
| 37 | + | ||
| 38 | + private NotifyConfigService notifyConfigService; | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + public void testGetAllList() { | ||
| 42 | + List<NotifyConfig> list = new ArrayList<>(); | ||
| 43 | + when(notifyConfigService.getAllList()).thenReturn(list); | ||
| 44 | + AjaxResult result = notifyConfigController.getAllList(); | ||
| 45 | + verify(notifyConfigService, times(1)).getAllList(); | ||
| 46 | + assertEquals(list, result.get("data")); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + public void testSaveList() { | ||
| 51 | + doNothing().when(notifyConfigService).saveList(anyList()); | ||
| 52 | + AjaxResult result = notifyConfigController.saveList(anyList()); | ||
| 53 | + verify(notifyConfigService, times(1)).saveList(anyList()); | ||
| 54 | + assertEquals(AjaxResult.success(), result); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + | ||
| 58 | + public void testTestConfig() { | ||
| 59 | + doNothing().when(notifyConfigService).testConfig(any(NotifyConfigReq.class)); | ||
| 60 | + NotifyConfigReq notifyConfigReq = new NotifyConfigReq(); | ||
| 61 | + AjaxResult result = notifyConfigController.testConfig(notifyConfigReq); | ||
| 62 | + verify(notifyConfigService, times(1)).testConfig(any(NotifyConfigReq.class)); | ||
| 63 | + assertEquals(AjaxResult.success(), result); | ||
| 64 | + } | ||
| 65 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/NotifyTemplateControllerTest.java+93-0
| @@ -0,0 +1,93 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 8 | +import com.nctigba.alert.monitor.entity.NotifyTemplate; | ||
| 9 | +import com.nctigba.alert.monitor.service.NotifyTemplateService; | ||
| 10 | +import org.junit.Test; | ||
| 11 | +import org.junit.runner.RunWith; | ||
| 12 | +import org.mockito.InjectMocks; | ||
| 13 | +import org.mockito.Mock; | ||
| 14 | +import org.mockito.MockedStatic; | ||
| 15 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 16 | +import org.opengauss.admin.common.core.page.TableDataInfo; | ||
| 17 | +import org.opengauss.admin.common.utils.ServletUtils; | ||
| 18 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 19 | + | ||
| 20 | +import java.util.ArrayList; | ||
| 21 | +import java.util.List; | ||
| 22 | + | ||
| 23 | +import static org.junit.Assert.assertEquals; | ||
| 24 | +import static org.mockito.ArgumentMatchers.any; | ||
| 25 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 26 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 27 | +import static org.mockito.Mockito.doNothing; | ||
| 28 | +import static org.mockito.Mockito.mockStatic; | ||
| 29 | +import static org.mockito.Mockito.times; | ||
| 30 | +import static org.mockito.Mockito.verify; | ||
| 31 | +import static org.mockito.Mockito.when; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * test NotifyTemplateController | ||
| 35 | + * | ||
| 36 | + * 2023/7/16 12:22 | ||
| 37 | + */ | ||
| 38 | + | ||
| 39 | +public class NotifyTemplateControllerTest { | ||
| 40 | + | ||
| 41 | + private NotifyTemplateController notifyTemplateController; | ||
| 42 | + | ||
| 43 | + private NotifyTemplateService notifyTemplateService; | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + public void testGetListPage() { | ||
| 47 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 48 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(1); | ||
| 49 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(10); | ||
| 50 | + Page page = new Page(1, 10); | ||
| 51 | + when(notifyTemplateService.getListPage(anyString(), anyString(), any(Page.class))).thenReturn(page); | ||
| 52 | + TableDataInfo result = notifyTemplateController.getListPage("", ""); | ||
| 53 | + verify(notifyTemplateService, times(1)) | ||
| 54 | + .getListPage(anyString(), anyString(), any(Page.class)); | ||
| 55 | + assertEquals(page.getRecords(), result.getRows()); | ||
| 56 | + assertEquals(page.getTotal(), result.getTotal()); | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + | ||
| 61 | + public void testGetList() { | ||
| 62 | + List<NotifyTemplate> list = new ArrayList<>(); | ||
| 63 | + when(notifyTemplateService.getList(anyString())).thenReturn(list); | ||
| 64 | + AjaxResult result = notifyTemplateController.getList(""); | ||
| 65 | + verify(notifyTemplateService, times(1)).getList(anyString()); | ||
| 66 | + assertEquals(list, result.get("data")); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + public void testGetById() { | ||
| 71 | + NotifyTemplate notifyTemplate = new NotifyTemplate(); | ||
| 72 | + when(notifyTemplateService.getById(anyLong())).thenReturn(notifyTemplate); | ||
| 73 | + AjaxResult result = notifyTemplateController.getById(1L); | ||
| 74 | + verify(notifyTemplateService, times(1)).getById(anyLong()); | ||
| 75 | + assertEquals(notifyTemplate, result.get("data")); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + public void testSaveTemplate() { | ||
| 80 | + doNothing().when(notifyTemplateService).saveTemplate(any(NotifyTemplate.class)); | ||
| 81 | + NotifyTemplate notifyTemplate = new NotifyTemplate(); | ||
| 82 | + AjaxResult result = notifyTemplateController.saveTemplate(notifyTemplate); | ||
| 83 | + verify(notifyTemplateService, times(1)).saveTemplate(any(NotifyTemplate.class)); | ||
| 84 | + assertEquals(AjaxResult.success(), result); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + public void testDelById() { | ||
| 88 | + doNothing().when(notifyTemplateService).delById(anyLong()); | ||
| 89 | + AjaxResult result = notifyTemplateController.delById(1L); | ||
| 90 | + verify(notifyTemplateService, times(1)).delById(anyLong()); | ||
| 91 | + assertEquals(AjaxResult.success(), result); | ||
| 92 | + } | ||
| 93 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/controller/NotifyWayControllerTest.java+94-0
| @@ -0,0 +1,94 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.controller; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 8 | +import com.nctigba.alert.monitor.entity.NotifyWay; | ||
| 9 | +import com.nctigba.alert.monitor.service.NotifyWayService; | ||
| 10 | +import org.junit.Test; | ||
| 11 | +import org.junit.runner.RunWith; | ||
| 12 | +import org.mockito.InjectMocks; | ||
| 13 | +import org.mockito.Mock; | ||
| 14 | +import org.mockito.MockedStatic; | ||
| 15 | +import org.opengauss.admin.common.core.domain.AjaxResult; | ||
| 16 | +import org.opengauss.admin.common.core.page.TableDataInfo; | ||
| 17 | +import org.opengauss.admin.common.utils.ServletUtils; | ||
| 18 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 19 | + | ||
| 20 | +import java.util.ArrayList; | ||
| 21 | +import java.util.List; | ||
| 22 | + | ||
| 23 | +import static org.junit.Assert.assertEquals; | ||
| 24 | +import static org.mockito.ArgumentMatchers.any; | ||
| 25 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 26 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 27 | +import static org.mockito.Mockito.doNothing; | ||
| 28 | +import static org.mockito.Mockito.mockStatic; | ||
| 29 | +import static org.mockito.Mockito.times; | ||
| 30 | +import static org.mockito.Mockito.verify; | ||
| 31 | +import static org.mockito.Mockito.when; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * test NotifyWayController | ||
| 35 | + * | ||
| 36 | + * 2023/7/16 12:41 | ||
| 37 | + */ | ||
| 38 | + | ||
| 39 | +public class NotifyWayControllerTest { | ||
| 40 | + | ||
| 41 | + private NotifyWayController notifyWayController; | ||
| 42 | + | ||
| 43 | + private NotifyWayService notifyWayService; | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + public void testGetListPage() { | ||
| 47 | + try (MockedStatic<ServletUtils> mockedStatic = mockStatic(ServletUtils.class)) { | ||
| 48 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageNum")).thenReturn(1); | ||
| 49 | + mockedStatic.when(() -> ServletUtils.getParameterToInt("pageSize")).thenReturn(10); | ||
| 50 | + Page page = new Page(1, 10); | ||
| 51 | + when(notifyWayService.getListPage(anyString(), anyString(), any(Page.class))).thenReturn(page); | ||
| 52 | + TableDataInfo result = notifyWayController.getListPage("", ""); | ||
| 53 | + verify(notifyWayService, times(1)) | ||
| 54 | + .getListPage(anyString(), anyString(), any(Page.class)); | ||
| 55 | + assertEquals(page.getRecords(), result.getRows()); | ||
| 56 | + assertEquals(page.getTotal(), result.getTotal()); | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + | ||
| 61 | + public void testGetList() { | ||
| 62 | + List<NotifyWay> list = new ArrayList<>(); | ||
| 63 | + when(notifyWayService.getList(anyString())).thenReturn(list); | ||
| 64 | + AjaxResult result = notifyWayController.getList(""); | ||
| 65 | + verify(notifyWayService, times(1)).getList(anyString()); | ||
| 66 | + assertEquals(list, result.get("data")); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + public void testGetById() { | ||
| 71 | + NotifyWay notifyWay = new NotifyWay(); | ||
| 72 | + when(notifyWayService.getById(anyLong())).thenReturn(notifyWay); | ||
| 73 | + AjaxResult result = notifyWayController.getById(1L); | ||
| 74 | + verify(notifyWayService, times(1)).getById(anyLong()); | ||
| 75 | + assertEquals(notifyWay, result.get("data")); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + public void testSaveNotifyWay() { | ||
| 80 | + doNothing().when(notifyWayService).saveNotifyWay(any(NotifyWay.class)); | ||
| 81 | + NotifyWay notifyWay = new NotifyWay(); | ||
| 82 | + AjaxResult result = notifyWayController.saveNotifyWay(notifyWay); | ||
| 83 | + verify(notifyWayService, times(1)).saveNotifyWay(any(NotifyWay.class)); | ||
| 84 | + assertEquals(AjaxResult.success(), result); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + public void testDelById() { | ||
| 89 | + doNothing().when(notifyWayService).delById(anyLong()); | ||
| 90 | + AjaxResult result = notifyWayController.delById(1L); | ||
| 91 | + verify(notifyWayService, times(1)).delById(anyLong()); | ||
| 92 | + assertEquals(AjaxResult.success(), result); | ||
| 93 | + } | ||
| 94 | +} | ||
| @@ -0,0 +1,63 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.schedule; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.entity.NotifyMessage; | ||
| 8 | +import com.nctigba.alert.monitor.mapper.NotifyMessageMapper; | ||
| 9 | +import com.nctigba.alert.monitor.service.EmailService; | ||
| 10 | +import org.junit.Test; | ||
| 11 | +import org.junit.runner.RunWith; | ||
| 12 | +import org.mockito.InjectMocks; | ||
| 13 | +import org.mockito.Mock; | ||
| 14 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 15 | + | ||
| 16 | +import java.util.ArrayList; | ||
| 17 | +import java.util.List; | ||
| 18 | + | ||
| 19 | +import static org.mockito.ArgumentMatchers.any; | ||
| 20 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 21 | +import static org.mockito.Mockito.doNothing; | ||
| 22 | +import static org.mockito.Mockito.times; | ||
| 23 | +import static org.mockito.Mockito.verify; | ||
| 24 | +import static org.mockito.Mockito.when; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * test AlertSchedule | ||
| 28 | + * | ||
| 29 | + * 2023/7/16 12:52 | ||
| 30 | + */ | ||
| 31 | + | ||
| 32 | +public class AlertScheduleTest { | ||
| 33 | + | ||
| 34 | + private AlertSchedule alertSchedule; | ||
| 35 | + | ||
| 36 | + private NotifyMessageMapper notifyMessageMapper; | ||
| 37 | + | ||
| 38 | + private EmailService emailService; | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + public void testAlertNotifyScheduleWithNull() { | ||
| 42 | + List<NotifyMessage> notifyMessages = new ArrayList<>(); | ||
| 43 | + when(notifyMessageMapper.selectList(any())).thenReturn(notifyMessages); | ||
| 44 | + alertSchedule.alertNotifySchedule(); | ||
| 45 | + verify(notifyMessageMapper, times(1)).selectList(any()); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + public void testAlertNotifySchedule() { | ||
| 50 | + List<NotifyMessage> notifyMessages = new ArrayList<>(); | ||
| 51 | + NotifyMessage notifyMessage1 = new NotifyMessage().setMessageType("email"); | ||
| 52 | + notifyMessages.add(notifyMessage1); | ||
| 53 | + NotifyMessage notifyMessage2 = new NotifyMessage().setMessageType("WeCom"); | ||
| 54 | + notifyMessages.add(notifyMessage2); | ||
| 55 | + NotifyMessage notifyMessage3 = new NotifyMessage().setMessageType("DingTalk"); | ||
| 56 | + notifyMessages.add(notifyMessage3); | ||
| 57 | + when(notifyMessageMapper.selectList(any())).thenReturn(notifyMessages); | ||
| 58 | + doNothing().when(emailService).send(anyList()); | ||
| 59 | + alertSchedule.alertNotifySchedule(); | ||
| 60 | + verify(notifyMessageMapper, times(1)).selectList(any()); | ||
| 61 | + verify(emailService, times(1)).send(anyList()); | ||
| 62 | + } | ||
| 63 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/AlertApiServiceTest.java+583-0
| @@ -0,0 +1,583 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.constant.CommonConstants; | ||
| 8 | +import com.nctigba.alert.monitor.entity.AlertRecord; | ||
| 9 | +import com.nctigba.alert.monitor.entity.AlertTemplate; | ||
| 10 | +import com.nctigba.alert.monitor.entity.AlertTemplateRule; | ||
| 11 | +import com.nctigba.alert.monitor.entity.NotifyMessage; | ||
| 12 | +import com.nctigba.alert.monitor.entity.NotifyTemplate; | ||
| 13 | +import com.nctigba.alert.monitor.entity.NotifyWay; | ||
| 14 | +import com.nctigba.alert.monitor.mapper.AlertRecordMapper; | ||
| 15 | +import com.nctigba.alert.monitor.mapper.AlertTemplateMapper; | ||
| 16 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleMapper; | ||
| 17 | +import com.nctigba.alert.monitor.mapper.NotifyMessageMapper; | ||
| 18 | +import com.nctigba.alert.monitor.mapper.NotifyTemplateMapper; | ||
| 19 | +import com.nctigba.alert.monitor.mapper.NotifyWayMapper; | ||
| 20 | +import com.nctigba.alert.monitor.model.api.AlertApiReq; | ||
| 21 | +import com.nctigba.alert.monitor.model.api.AlertLabels; | ||
| 22 | +import org.junit.Before; | ||
| 23 | +import org.junit.Test; | ||
| 24 | +import org.junit.runner.RunWith; | ||
| 25 | +import org.mockito.InjectMocks; | ||
| 26 | +import org.mockito.Mock; | ||
| 27 | +import org.mockito.MockitoAnnotations; | ||
| 28 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterEntity; | ||
| 29 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterNodeEntity; | ||
| 30 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsHostEntity; | ||
| 31 | +import org.opengauss.admin.common.enums.ops.ClusterRoleEnum; | ||
| 32 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 33 | +import org.opengauss.admin.system.plugin.facade.HostFacade; | ||
| 34 | +import org.opengauss.admin.system.service.ops.IOpsClusterNodeService; | ||
| 35 | +import org.opengauss.admin.system.service.ops.IOpsClusterService; | ||
| 36 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 37 | + | ||
| 38 | +import java.time.LocalDateTime; | ||
| 39 | +import java.util.ArrayList; | ||
| 40 | +import java.util.List; | ||
| 41 | + | ||
| 42 | +import static org.mockito.ArgumentMatchers.any; | ||
| 43 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 44 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 45 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 46 | +import static org.mockito.Mockito.times; | ||
| 47 | +import static org.mockito.Mockito.verify; | ||
| 48 | +import static org.mockito.Mockito.when; | ||
| 49 | + | ||
| 50 | +/** | ||
| 51 | + * AlertApiService function test | ||
| 52 | + * | ||
| 53 | + * 2023/7/15 18:42 | ||
| 54 | + */ | ||
| 55 | + | ||
| 56 | +public class AlertApiServiceTest { | ||
| 57 | + | ||
| 58 | + private AlertApiService alertApiService; | ||
| 59 | + | ||
| 60 | + private AlertRecordMapper alertRecordMapper; | ||
| 61 | + | ||
| 62 | + private AlertTemplateMapper templateMapper; | ||
| 63 | + | ||
| 64 | + private AlertTemplateRuleMapper templateRuleMapper; | ||
| 65 | + | ||
| 66 | + private NotifyWayMapper notifyWayMapper; | ||
| 67 | + | ||
| 68 | + private NotifyTemplateMapper notifyTemplateMapper; | ||
| 69 | + | ||
| 70 | + private NotifyMessageMapper notifyMessageMapper; | ||
| 71 | + | ||
| 72 | + private IOpsClusterService clusterService; | ||
| 73 | + | ||
| 74 | + private IOpsClusterNodeService clusterNodeService; | ||
| 75 | + | ||
| 76 | + private HostFacade hostFacade; | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + public void setUp() { | ||
| 80 | + MockitoAnnotations.initMocks(this); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + public void testAlerts1() { | ||
| 85 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 86 | + AlertApiReq alertApiReq = new AlertApiReq(); | ||
| 87 | + alertApiReqList.add(alertApiReq); | ||
| 88 | + AlertLabels labels = new AlertLabels(); | ||
| 89 | + labels.setTemplateRuleId(1L); | ||
| 90 | + alertApiReq.setLabels(labels); | ||
| 91 | + | ||
| 92 | + AlertTemplateRule alertTemplateRule = null; | ||
| 93 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 94 | + | ||
| 95 | + alertApiService.alerts(alertApiReqList); | ||
| 96 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + | ||
| 100 | + public void testAlerts2() { | ||
| 101 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 102 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 103 | + alertApiReqList.add(alertApiReq); | ||
| 104 | + | ||
| 105 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule(); | ||
| 106 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 107 | + | ||
| 108 | + OpsClusterNodeEntity opsClusterNodeEntity = null; | ||
| 109 | + when(clusterNodeService.getById(anyLong())).thenReturn(opsClusterNodeEntity); | ||
| 110 | + | ||
| 111 | + alertApiService.alerts(alertApiReqList); | ||
| 112 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 113 | + verify(clusterNodeService, times(1)).getById(anyLong()); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + | ||
| 117 | + public void testAlerts3() { | ||
| 118 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 119 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 120 | + alertApiReqList.add(alertApiReq); | ||
| 121 | + | ||
| 122 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setNotifyWayIds("1"); | ||
| 123 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 124 | + | ||
| 125 | + List<NotifyWay> notifyWays = new ArrayList<>(); | ||
| 126 | + NotifyWay notifyWay = new NotifyWay().setId(1L).setName("notifyName"); | ||
| 127 | + notifyWays.add(notifyWay); | ||
| 128 | + when(notifyWayMapper.selectBatchIds(anyList())).thenReturn(notifyWays); | ||
| 129 | + | ||
| 130 | + OpsClusterNodeEntity opsClusterNodeEntity = new OpsClusterNodeEntity(); | ||
| 131 | + opsClusterNodeEntity.setClusterNodeId("node3"); | ||
| 132 | + opsClusterNodeEntity.setHostId("1"); | ||
| 133 | + when(clusterNodeService.getById(anyString())).thenReturn(opsClusterNodeEntity); | ||
| 134 | + | ||
| 135 | + OpsHostEntity opsHost = null; | ||
| 136 | + when(hostFacade.getById(opsClusterNodeEntity.getHostId())).thenReturn(opsHost); | ||
| 137 | + | ||
| 138 | + alertApiService.alerts(alertApiReqList); | ||
| 139 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 140 | + verify(notifyWayMapper, times(1)).selectById(anyLong()); | ||
| 141 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 142 | + verify(hostFacade, times(1)).getById(opsClusterNodeEntity.getHostId()); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + | ||
| 146 | + public void testAlerts4() { | ||
| 147 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 148 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 149 | + alertApiReqList.add(alertApiReq); | ||
| 150 | + | ||
| 151 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setNotifyWayIds("1"); | ||
| 152 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 153 | + | ||
| 154 | + List<NotifyWay> notifyWays = new ArrayList<>(); | ||
| 155 | + when(notifyWayMapper.selectBatchIds(anyList())).thenReturn(notifyWays); | ||
| 156 | + | ||
| 157 | + OpsClusterNodeEntity opsClusterNodeEntity = new OpsClusterNodeEntity(); | ||
| 158 | + opsClusterNodeEntity.setClusterNodeId("node3"); | ||
| 159 | + opsClusterNodeEntity.setHostId("1"); | ||
| 160 | + opsClusterNodeEntity.setClusterId("test"); | ||
| 161 | + when(clusterNodeService.getById(anyString())).thenReturn(opsClusterNodeEntity); | ||
| 162 | + | ||
| 163 | + OpsHostEntity opsHost = new OpsHostEntity(); | ||
| 164 | + when(hostFacade.getById(opsClusterNodeEntity.getHostId())).thenReturn(opsHost); | ||
| 165 | + OpsClusterEntity opsClusterEntity = null; | ||
| 166 | + when(clusterService.getById(opsClusterNodeEntity.getClusterId())).thenReturn(opsClusterEntity); | ||
| 167 | + | ||
| 168 | + alertApiService.alerts(alertApiReqList); | ||
| 169 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 170 | + verify(notifyWayMapper, times(1)).selectById(anyLong()); | ||
| 171 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 172 | + verify(hostFacade, times(1)).getById(opsClusterNodeEntity.getHostId()); | ||
| 173 | + verify(clusterService, times(1)).getById(opsClusterNodeEntity.getClusterId()); | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + | ||
| 177 | + public void testAlerts5() { | ||
| 178 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 179 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 180 | + alertApiReqList.add(alertApiReq); | ||
| 181 | + | ||
| 182 | + AlertTemplateRule alertTemplateRule = | ||
| 183 | + new AlertTemplateRule().setNotifyWayIds("1").setRuleContent("content").setRuleName("ruleName") | ||
| 184 | + .setRuleType("index").setLevel("warn").setNotifyWayIds("1").setIsRepeat(CommonConstants.IS_NOT_REPEAT) | ||
| 185 | + .setAlertNotify("firing"); | ||
| 186 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 187 | + | ||
| 188 | + List<NotifyWay> notifyWays = new ArrayList<>(); | ||
| 189 | + NotifyWay notifyWay = new NotifyWay().setId(1L).setName("notifyName"); | ||
| 190 | + notifyWays.add(notifyWay); | ||
| 191 | + when(notifyWayMapper.selectBatchIds(anyList())).thenReturn(notifyWays); | ||
| 192 | + | ||
| 193 | + OpsClusterNodeEntity opsClusterNodeEntity = new OpsClusterNodeEntity(); | ||
| 194 | + opsClusterNodeEntity.setClusterNodeId("node3"); | ||
| 195 | + opsClusterNodeEntity.setHostId("1"); | ||
| 196 | + opsClusterNodeEntity.setClusterId("test"); | ||
| 197 | + opsClusterNodeEntity.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 198 | + when(clusterNodeService.getById(anyString())).thenReturn(opsClusterNodeEntity); | ||
| 199 | + | ||
| 200 | + OpsHostEntity opsHost = new OpsHostEntity(); | ||
| 201 | + opsHost.setPublicIp("127.0.0.1"); | ||
| 202 | + opsHost.setHostname("centos"); | ||
| 203 | + when(hostFacade.getById(opsClusterNodeEntity.getHostId())).thenReturn(opsHost); | ||
| 204 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 205 | + opsClusterEntity.setClusterId("test"); | ||
| 206 | + when(clusterService.getById(opsClusterNodeEntity.getClusterId())).thenReturn(opsClusterEntity); | ||
| 207 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 208 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 209 | + | ||
| 210 | + AlertTemplate alertTemplate = new AlertTemplate().setTemplateName("name").setId(1L); | ||
| 211 | + when(templateMapper.selectById(anyLong())).thenReturn(alertTemplate); | ||
| 212 | + when(alertRecordMapper.insert(any(AlertRecord.class))).thenReturn(1); | ||
| 213 | + when(notifyMessageMapper.selectCount(any())).thenReturn(1L); | ||
| 214 | + | ||
| 215 | + alertApiService.alerts(alertApiReqList); | ||
| 216 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 217 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 218 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 219 | + verify(hostFacade, times(1)).getById(opsClusterNodeEntity.getHostId()); | ||
| 220 | + verify(clusterService, times(1)).getById(opsClusterNodeEntity.getClusterId()); | ||
| 221 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 222 | + verify(templateMapper, times(1)).selectById(anyLong()); | ||
| 223 | + verify(alertRecordMapper, times(1)).insert(any(AlertRecord.class)); | ||
| 224 | + verify(notifyMessageMapper, times(1)).selectCount(any()); | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + | ||
| 228 | + public void testAlerts6() { | ||
| 229 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 230 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 231 | + alertApiReqList.add(alertApiReq); | ||
| 232 | + | ||
| 233 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setNotifyWayIds("1"); | ||
| 234 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 235 | + | ||
| 236 | + List<NotifyWay> notifyWays = new ArrayList<>(); | ||
| 237 | + NotifyWay notifyWay = new NotifyWay().setId(1L).setName("notifyName"); | ||
| 238 | + notifyWays.add(notifyWay); | ||
| 239 | + when(notifyWayMapper.selectBatchIds(anyList())).thenReturn(notifyWays); | ||
| 240 | + | ||
| 241 | + OpsClusterNodeEntity opsClusterNodeEntity = new OpsClusterNodeEntity(); | ||
| 242 | + opsClusterNodeEntity.setClusterNodeId("node3"); | ||
| 243 | + opsClusterNodeEntity.setHostId("1"); | ||
| 244 | + opsClusterNodeEntity.setClusterId("test"); | ||
| 245 | + opsClusterNodeEntity.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 246 | + when(clusterNodeService.getById(anyString())).thenReturn(opsClusterNodeEntity); | ||
| 247 | + | ||
| 248 | + OpsHostEntity opsHost = new OpsHostEntity(); | ||
| 249 | + opsHost.setPublicIp("127.0.0.1"); | ||
| 250 | + opsHost.setHostname("centos"); | ||
| 251 | + opsHost.setPort(22); | ||
| 252 | + when(hostFacade.getById(opsClusterNodeEntity.getHostId())).thenReturn(opsHost); | ||
| 253 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 254 | + opsClusterEntity.setClusterId("test"); | ||
| 255 | + opsClusterEntity.setPort(80); | ||
| 256 | + when(clusterService.getById(opsClusterNodeEntity.getClusterId())).thenReturn(opsClusterEntity); | ||
| 257 | + | ||
| 258 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 259 | + AlertRecord alertRecord = new AlertRecord().setAlertStatus(CommonConstants.RECOVER_STATUS); | ||
| 260 | + alertRecords.add(alertRecord); | ||
| 261 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 262 | + | ||
| 263 | + alertApiService.alerts(alertApiReqList); | ||
| 264 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 265 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 266 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 267 | + verify(hostFacade, times(1)).getById(opsClusterNodeEntity.getHostId()); | ||
| 268 | + verify(clusterService, times(1)).getById(opsClusterNodeEntity.getClusterId()); | ||
| 269 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 270 | + } | ||
| 271 | + | ||
| 272 | + | ||
| 273 | + public void testAlerts7() { | ||
| 274 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 275 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 276 | + alertApiReqList.add(alertApiReq); | ||
| 277 | + | ||
| 278 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setNotifyWayIds("1").setAlertNotify("firing"); | ||
| 279 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 280 | + | ||
| 281 | + List<NotifyWay> notifyWays = new ArrayList<>(); | ||
| 282 | + NotifyWay notifyWay = new NotifyWay().setId(1L).setName("notifyName"); | ||
| 283 | + notifyWays.add(notifyWay); | ||
| 284 | + when(notifyWayMapper.selectBatchIds(anyList())).thenReturn(notifyWays); | ||
| 285 | + | ||
| 286 | + OpsClusterNodeEntity opsClusterNodeEntity = new OpsClusterNodeEntity(); | ||
| 287 | + opsClusterNodeEntity.setClusterNodeId("node3"); | ||
| 288 | + opsClusterNodeEntity.setHostId("1"); | ||
| 289 | + opsClusterNodeEntity.setClusterId("test"); | ||
| 290 | + opsClusterNodeEntity.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 291 | + when(clusterNodeService.getById(anyString())).thenReturn(opsClusterNodeEntity); | ||
| 292 | + | ||
| 293 | + OpsHostEntity opsHost = new OpsHostEntity(); | ||
| 294 | + opsHost.setPublicIp("127.0.0.1"); | ||
| 295 | + opsHost.setHostname("centos"); | ||
| 296 | + opsHost.setPort(22); | ||
| 297 | + when(hostFacade.getById(opsClusterNodeEntity.getHostId())).thenReturn(opsHost); | ||
| 298 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 299 | + opsClusterEntity.setClusterId("test"); | ||
| 300 | + opsClusterEntity.setPort(80); | ||
| 301 | + when(clusterService.getById(opsClusterNodeEntity.getClusterId())).thenReturn(opsClusterEntity); | ||
| 302 | + | ||
| 303 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 304 | + AlertRecord alertRecord = new AlertRecord().setAlertStatus(CommonConstants.RECOVER_STATUS); | ||
| 305 | + alertRecords.add(alertRecord); | ||
| 306 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 307 | + | ||
| 308 | + alertApiService.alerts(alertApiReqList); | ||
| 309 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 310 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 311 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 312 | + verify(hostFacade, times(1)).getById(opsClusterNodeEntity.getHostId()); | ||
| 313 | + verify(clusterService, times(1)).getById(opsClusterNodeEntity.getClusterId()); | ||
| 314 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + | ||
| 318 | + public void testAlerts8() { | ||
| 319 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 320 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 321 | + alertApiReqList.add(alertApiReq); | ||
| 322 | + | ||
| 323 | + AlertTemplateRule alertTemplateRule = | ||
| 324 | + new AlertTemplateRule().setNotifyWayIds("1").setAlertNotify("firing"); | ||
| 325 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 326 | + | ||
| 327 | + setCommon(); | ||
| 328 | + | ||
| 329 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 330 | + AlertRecord alertRecord = | ||
| 331 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS).setEndTime(alertApiReq.getEndsAt()) | ||
| 332 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 333 | + alertRecords.add(alertRecord); | ||
| 334 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 335 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 336 | + | ||
| 337 | + alertApiService.alerts(alertApiReqList); | ||
| 338 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 339 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 340 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 341 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 342 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + | ||
| 346 | + public void testAlerts9() { | ||
| 347 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 348 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 349 | + alertApiReq.setEndsAt(LocalDateTime.now().plusHours(1)); | ||
| 350 | + alertApiReqList.add(alertApiReq); | ||
| 351 | + | ||
| 352 | + AlertTemplateRule alertTemplateRule = | ||
| 353 | + new AlertTemplateRule().setNotifyWayIds("1").setAlertNotify("firing"); | ||
| 354 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 355 | + | ||
| 356 | + setCommon(); | ||
| 357 | + | ||
| 358 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 359 | + AlertRecord alertRecord = | ||
| 360 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS) | ||
| 361 | + .setEndTime(alertApiReq.getEndsAt().plusHours(1)) | ||
| 362 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 363 | + alertRecords.add(alertRecord); | ||
| 364 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 365 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 366 | + | ||
| 367 | + alertApiService.alerts(alertApiReqList); | ||
| 368 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 369 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 370 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 371 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 372 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 373 | + } | ||
| 374 | + | ||
| 375 | + | ||
| 376 | + public void testAlerts10() { | ||
| 377 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 378 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 379 | + alertApiReqList.add(alertApiReq); | ||
| 380 | + | ||
| 381 | + AlertTemplateRule alertTemplateRule = | ||
| 382 | + new AlertTemplateRule().setNotifyWayIds("1").setAlertNotify("firing") | ||
| 383 | + .setIsRepeat(CommonConstants.IS_NOT_REPEAT).setIsSilence(CommonConstants.IS_SILENCE) | ||
| 384 | + .setSilenceStartTime(LocalDateTime.now().minusHours(1)) | ||
| 385 | + .setSilenceEndTime(LocalDateTime.now().plusHours(1)); | ||
| 386 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 387 | + | ||
| 388 | + setCommon(); | ||
| 389 | + | ||
| 390 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 391 | + AlertRecord alertRecord = | ||
| 392 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS) | ||
| 393 | + .setEndTime(alertApiReq.getEndsAt().minusMinutes(30)) | ||
| 394 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 395 | + alertRecords.add(alertRecord); | ||
| 396 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 397 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 398 | + when(notifyMessageMapper.selectCount(any())).thenReturn(0L); | ||
| 399 | + | ||
| 400 | + alertApiService.alerts(alertApiReqList); | ||
| 401 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 402 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 403 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 404 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 405 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 406 | + verify(notifyMessageMapper, times(1)).selectCount(any()); | ||
| 407 | + } | ||
| 408 | + | ||
| 409 | + | ||
| 410 | + public void testAlerts11() { | ||
| 411 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 412 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 413 | + alertApiReqList.add(alertApiReq); | ||
| 414 | + | ||
| 415 | + AlertTemplateRule alertTemplateRule = | ||
| 416 | + new AlertTemplateRule().setAlertNotify("firing") | ||
| 417 | + .setIsRepeat(CommonConstants.IS_REPEAT).setIsSilence(CommonConstants.IS_NOT_SILENCE); | ||
| 418 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 419 | + | ||
| 420 | + setCommon(); | ||
| 421 | + | ||
| 422 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 423 | + AlertRecord alertRecord = | ||
| 424 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS) | ||
| 425 | + .setEndTime(alertApiReq.getEndsAt().minusMinutes(30)) | ||
| 426 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 427 | + alertRecords.add(alertRecord); | ||
| 428 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 429 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 430 | + | ||
| 431 | + alertApiService.alerts(alertApiReqList); | ||
| 432 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 433 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 434 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 435 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 436 | + } | ||
| 437 | + | ||
| 438 | + | ||
| 439 | + public void testAlerts12() { | ||
| 440 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 441 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 442 | + alertApiReqList.add(alertApiReq); | ||
| 443 | + | ||
| 444 | + AlertTemplateRule alertTemplateRule = | ||
| 445 | + new AlertTemplateRule().setNotifyWayIds("1").setAlertNotify("firing") | ||
| 446 | + .setIsRepeat(CommonConstants.IS_NOT_REPEAT).setIsSilence(CommonConstants.IS_NOT_SILENCE) | ||
| 447 | + .setSilenceStartTime(LocalDateTime.now().minusHours(1)) | ||
| 448 | + .setSilenceEndTime(LocalDateTime.now().plusHours(1)); | ||
| 449 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 450 | + | ||
| 451 | + setCommon(); | ||
| 452 | + | ||
| 453 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 454 | + AlertRecord alertRecord = | ||
| 455 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS) | ||
| 456 | + .setEndTime(alertApiReq.getEndsAt().minusMinutes(30)) | ||
| 457 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 458 | + alertRecords.add(alertRecord); | ||
| 459 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 460 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 461 | + | ||
| 462 | + NotifyWay notifyWay = new NotifyWay().setNotifyTemplateId(1L); | ||
| 463 | + when(notifyWayMapper.selectById(any())).thenReturn(notifyWay); | ||
| 464 | + NotifyTemplate notifyTemplate = new NotifyTemplate(); | ||
| 465 | + notifyTemplate.setNotifyContent("notifyContent").setId(1L); | ||
| 466 | + when(notifyTemplateMapper.selectById(anyLong())).thenReturn(notifyTemplate); | ||
| 467 | + when(notifyMessageMapper.insert(any(NotifyMessage.class))).thenReturn(1); | ||
| 468 | + | ||
| 469 | + alertApiService.alerts(alertApiReqList); | ||
| 470 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 471 | + verify(notifyWayMapper, times(1)).selectBatchIds(anyList()); | ||
| 472 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 473 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 474 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 475 | + verify(notifyWayMapper, times(1)).selectById(any()); | ||
| 476 | + verify(notifyTemplateMapper, times(1)).selectById(anyLong()); | ||
| 477 | + verify(notifyMessageMapper, times(1)).insert(any(NotifyMessage.class)); | ||
| 478 | + } | ||
| 479 | + | ||
| 480 | + | ||
| 481 | + public void testAlerts13() { | ||
| 482 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 483 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 484 | + alertApiReqList.add(alertApiReq); | ||
| 485 | + | ||
| 486 | + AlertTemplateRule alertTemplateRule = | ||
| 487 | + new AlertTemplateRule().setAlertNotify("firing") | ||
| 488 | + .setIsRepeat(CommonConstants.IS_NOT_REPEAT).setIsSilence(CommonConstants.IS_SILENCE) | ||
| 489 | + .setSilenceStartTime(LocalDateTime.now().plusMinutes(30)) | ||
| 490 | + .setSilenceEndTime(LocalDateTime.now().plusHours(1)); | ||
| 491 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 492 | + | ||
| 493 | + setCommon(); | ||
| 494 | + | ||
| 495 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 496 | + AlertRecord alertRecord = | ||
| 497 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS) | ||
| 498 | + .setEndTime(alertApiReq.getEndsAt().minusMinutes(30)) | ||
| 499 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 500 | + alertRecords.add(alertRecord); | ||
| 501 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 502 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 503 | + when(notifyMessageMapper.selectCount(any())).thenReturn(0L); | ||
| 504 | + | ||
| 505 | + alertApiService.alerts(alertApiReqList); | ||
| 506 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 507 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 508 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 509 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 510 | + verify(notifyMessageMapper, times(1)).selectCount(any()); | ||
| 511 | + } | ||
| 512 | + | ||
| 513 | + | ||
| 514 | + public void testAlerts14() { | ||
| 515 | + List<AlertApiReq> alertApiReqList = new ArrayList<>(); | ||
| 516 | + AlertApiReq alertApiReq = getAlertApiReq(); | ||
| 517 | + alertApiReqList.add(alertApiReq); | ||
| 518 | + | ||
| 519 | + AlertTemplateRule alertTemplateRule = | ||
| 520 | + new AlertTemplateRule().setAlertNotify("firing") | ||
| 521 | + .setIsRepeat(CommonConstants.IS_NOT_REPEAT).setIsSilence(CommonConstants.IS_SILENCE) | ||
| 522 | + .setSilenceStartTime(LocalDateTime.now().minusHours(2)) | ||
| 523 | + .setSilenceEndTime(LocalDateTime.now().minusHours(1)); | ||
| 524 | + when(templateRuleMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 525 | + | ||
| 526 | + setCommon(); | ||
| 527 | + | ||
| 528 | + List<AlertRecord> alertRecords = new ArrayList<>(); | ||
| 529 | + AlertRecord alertRecord = | ||
| 530 | + new AlertRecord().setAlertStatus(CommonConstants.FIRING_STATUS) | ||
| 531 | + .setEndTime(alertApiReq.getEndsAt().minusMinutes(30)) | ||
| 532 | + .setStartTime(alertApiReq.getStartsAt()).setId(1L); | ||
| 533 | + alertRecords.add(alertRecord); | ||
| 534 | + when(alertRecordMapper.selectList(any())).thenReturn(alertRecords); | ||
| 535 | + when(alertRecordMapper.updateById(alertRecord)).thenReturn(1); | ||
| 536 | + when(notifyMessageMapper.selectCount(any())).thenReturn(0L); | ||
| 537 | + | ||
| 538 | + alertApiService.alerts(alertApiReqList); | ||
| 539 | + verify(templateRuleMapper, times(1)).selectById(anyLong()); | ||
| 540 | + verify(clusterNodeService, times(1)).getById(anyString()); | ||
| 541 | + verify(alertRecordMapper, times(1)).selectList(any()); | ||
| 542 | + verify(alertRecordMapper, times(1)).updateById(alertRecord); | ||
| 543 | + verify(notifyMessageMapper, times(1)).selectCount(any()); | ||
| 544 | + } | ||
| 545 | + | ||
| 546 | + private void setCommon() { | ||
| 547 | + List<NotifyWay> notifyWays = new ArrayList<>(); | ||
| 548 | + NotifyWay notifyWay = new NotifyWay().setId(1L).setName("notifyName"); | ||
| 549 | + notifyWays.add(notifyWay); | ||
| 550 | + when(notifyWayMapper.selectBatchIds(anyList())).thenReturn(notifyWays); | ||
| 551 | + | ||
| 552 | + OpsClusterNodeEntity opsClusterNodeEntity = new OpsClusterNodeEntity(); | ||
| 553 | + opsClusterNodeEntity.setClusterNodeId("node3"); | ||
| 554 | + opsClusterNodeEntity.setHostId("1"); | ||
| 555 | + opsClusterNodeEntity.setClusterId("test"); | ||
| 556 | + opsClusterNodeEntity.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 557 | + when(clusterNodeService.getById(anyString())).thenReturn(opsClusterNodeEntity); | ||
| 558 | + | ||
| 559 | + OpsHostEntity opsHost = new OpsHostEntity(); | ||
| 560 | + opsHost.setPublicIp("127.0.0.1"); | ||
| 561 | + opsHost.setHostname("centos"); | ||
| 562 | + opsHost.setPort(22); | ||
| 563 | + when(hostFacade.getById(opsClusterNodeEntity.getHostId())).thenReturn(opsHost); | ||
| 564 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 565 | + opsClusterEntity.setClusterId("test"); | ||
| 566 | + opsClusterEntity.setPort(80); | ||
| 567 | + when(clusterService.getById(opsClusterNodeEntity.getClusterId())).thenReturn(opsClusterEntity); | ||
| 568 | + } | ||
| 569 | + | ||
| 570 | + private AlertApiReq getAlertApiReq() { | ||
| 571 | + AlertApiReq alertApiReq = new AlertApiReq(); | ||
| 572 | + alertApiReq.setStartsAt(LocalDateTime.now().minusHours(1)); | ||
| 573 | + alertApiReq.setEndsAt(LocalDateTime.now()); | ||
| 574 | + AlertLabels labels = new AlertLabels(); | ||
| 575 | + labels.setLevel("warn"); | ||
| 576 | + labels.setTemplateId(1L); | ||
| 577 | + labels.setInstance("node1"); | ||
| 578 | + labels.setTemplateRuleId(1L); | ||
| 579 | + labels.setAlertname("name"); | ||
| 580 | + alertApiReq.setLabels(labels); | ||
| 581 | + return alertApiReq; | ||
| 582 | + } | ||
| 583 | +} | ||
| @@ -0,0 +1,277 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.entity.NotifyConfig; | ||
| 8 | +import com.nctigba.alert.monitor.entity.NotifyMessage; | ||
| 9 | +import com.nctigba.alert.monitor.mapper.NotifyConfigMapper; | ||
| 10 | +import com.nctigba.alert.monitor.mapper.NotifyMessageMapper; | ||
| 11 | +import jakarta.mail.Transport; | ||
| 12 | +import jakarta.mail.internet.MimeMessage; | ||
| 13 | +import org.junit.Before; | ||
| 14 | +import org.junit.Test; | ||
| 15 | +import org.junit.runner.RunWith; | ||
| 16 | +import org.mockito.InjectMocks; | ||
| 17 | +import org.mockito.Mock; | ||
| 18 | +import org.mockito.MockedStatic; | ||
| 19 | +import org.mockito.Mockito; | ||
| 20 | +import org.mockito.MockitoAnnotations; | ||
| 21 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 22 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 23 | + | ||
| 24 | +import java.util.ArrayList; | ||
| 25 | +import java.util.Collections; | ||
| 26 | +import java.util.List; | ||
| 27 | + | ||
| 28 | +import static org.mockito.ArgumentMatchers.any; | ||
| 29 | +import static org.mockito.Mockito.mockStatic; | ||
| 30 | +import static org.mockito.Mockito.times; | ||
| 31 | +import static org.mockito.Mockito.verify; | ||
| 32 | +import static org.mockito.Mockito.verifyZeroInteractions; | ||
| 33 | +import static org.mockito.Mockito.when; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * EmailService function test | ||
| 37 | + * | ||
| 38 | + * 2023/7/5 16:29 | ||
| 39 | + */ | ||
| 40 | + | ||
| 41 | +public class EmailServiceTest { | ||
| 42 | + | ||
| 43 | + private NotifyConfigMapper notifyConfigMapper; | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + private NotifyMessageMapper notifyMessageMapper; | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + private EmailService emailService; | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + public void setup() { | ||
| 53 | + MockitoAnnotations.initMocks(this); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + public void testSendWithNullNotifyMessage() { | ||
| 58 | + // Call the method to be tested with a null NotifyMessage | ||
| 59 | + NotifyMessage notifyMessage = null; | ||
| 60 | + emailService.send(notifyMessage); | ||
| 61 | + | ||
| 62 | + // Verify that the method has returned without doing anything | ||
| 63 | + verifyZeroInteractions(notifyConfigMapper, notifyMessageMapper); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + public void testSendWithNoEmailConfig() { | ||
| 68 | + // Call the method to be tested with a null NotifyMessage | ||
| 69 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 70 | + List<NotifyConfig> emailConfigList = new ArrayList<>(); | ||
| 71 | + when(notifyConfigMapper.selectList(any())).thenReturn(emailConfigList); | ||
| 72 | + emailService.send(notifyMessage); | ||
| 73 | + verify(notifyConfigMapper, times(1)).selectList(any()); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + | ||
| 77 | + public void testSendWithValidReceiver() { | ||
| 78 | + // Set up the mock NotifyConfigMapper to return a valid NotifyConfig | ||
| 79 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 80 | + notifyConfig.setSever("smtp.example.com"); | ||
| 81 | + notifyConfig.setPort(25); | ||
| 82 | + notifyConfig.setType("email"); | ||
| 83 | + notifyConfig.setSender("sender@example.com"); | ||
| 84 | + notifyConfig.setAccount("account@example.com"); | ||
| 85 | + notifyConfig.setPasswd("passwd"); | ||
| 86 | + when(notifyConfigMapper.selectList(any())).thenReturn(Collections.singletonList(notifyConfig)); | ||
| 87 | + | ||
| 88 | + // Set up the mock NotifyMessageMapper to return a valid NotifyMessage | ||
| 89 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 90 | + notifyMessage.setEmail(""); | ||
| 91 | + notifyMessage.setTitle("Test Email"); | ||
| 92 | + notifyMessage.setContent("<html><body><h1>Test Email</h1><p>This is a test email.</p></body></html>"); | ||
| 93 | + when(notifyMessageMapper.updateById(any())).thenReturn(1); | ||
| 94 | + | ||
| 95 | + // Call the method to be tested with the valid NotifyMessage | ||
| 96 | + emailService.send(notifyMessage); | ||
| 97 | + | ||
| 98 | + // Verify that the method has interacted with the mock dependencies | ||
| 99 | + Mockito.verify(notifyConfigMapper, times(1)).selectList(any()); | ||
| 100 | + Mockito.verify(notifyMessageMapper, times(1)).updateById(notifyMessage); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + | ||
| 104 | + public void testSend() { | ||
| 105 | + try (MockedStatic<Transport> mockedStatic = mockStatic(Transport.class)) { | ||
| 106 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 107 | + notifyConfig.setSever("smtp.example.com"); | ||
| 108 | + notifyConfig.setPort(25); | ||
| 109 | + notifyConfig.setType("email"); | ||
| 110 | + notifyConfig.setEmail("account@example.com"); | ||
| 111 | + notifyConfig.setSender("sender@example.com"); | ||
| 112 | + notifyConfig.setAccount("account@example.com"); | ||
| 113 | + notifyConfig.setPasswd("passwd"); | ||
| 114 | + when(notifyConfigMapper.selectList(Mockito.any())).thenReturn(Collections.singletonList(notifyConfig)); | ||
| 115 | + | ||
| 116 | + // Set up the mock NotifyMessageMapper to return a valid NotifyMessage | ||
| 117 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 118 | + notifyMessage.setEmail("test@example.com"); | ||
| 119 | + notifyMessage.setTitle("Test Email"); | ||
| 120 | + notifyMessage.setContent("<html><body><h1>Test Email</h1><p>This is a test email.</p></body></html>"); | ||
| 121 | + | ||
| 122 | + mockedStatic.when(() -> Transport.send(any())).thenAnswer(invocationOnMock -> null); | ||
| 123 | + when(notifyMessageMapper.updateById(any())).thenReturn(1); | ||
| 124 | + | ||
| 125 | + // Call the method to be tested with the valid NotifyMessage | ||
| 126 | + emailService.send(notifyMessage); | ||
| 127 | + | ||
| 128 | + // Verify that the method has interacted with the mock dependencies | ||
| 129 | + Mockito.verify(notifyConfigMapper, Mockito.times(1)).selectList(any()); | ||
| 130 | + Mockito.verify(notifyMessageMapper, Mockito.times(1)).updateById(notifyMessage); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + | ||
| 135 | + public void testSendListWithNullNotifyMessageList() { | ||
| 136 | + List<NotifyMessage> notifyMessageList = new ArrayList<>(); | ||
| 137 | + emailService.send(notifyMessageList); | ||
| 138 | + verifyZeroInteractions(notifyConfigMapper, notifyMessageMapper); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + | ||
| 142 | + public void testSendListWithNoEmailConfig() { | ||
| 143 | + List<NotifyMessage> notifyMessageList = new ArrayList<>(); | ||
| 144 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 145 | + notifyMessageList.add(notifyMessage); | ||
| 146 | + List<NotifyConfig> emailConfigList = new ArrayList<>(); | ||
| 147 | + when(notifyConfigMapper.selectList(any())).thenReturn(emailConfigList); | ||
| 148 | + emailService.send(notifyMessageList); | ||
| 149 | + verify(notifyConfigMapper, times(1)).selectList(any()); | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + | ||
| 153 | + public void testSendListWithValidReceiver() { | ||
| 154 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 155 | + notifyConfig.setSever("smtp.example.com"); | ||
| 156 | + notifyConfig.setPort(25); | ||
| 157 | + notifyConfig.setType("email"); | ||
| 158 | + notifyConfig.setSender("sender@example.com"); | ||
| 159 | + notifyConfig.setAccount("account@example.com"); | ||
| 160 | + notifyConfig.setPasswd("passwd"); | ||
| 161 | + when(notifyConfigMapper.selectList(any())).thenReturn(Collections.singletonList(notifyConfig)); | ||
| 162 | + | ||
| 163 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 164 | + notifyMessage.setEmail(""); | ||
| 165 | + notifyMessage.setTitle("Test Email"); | ||
| 166 | + notifyMessage.setContent("<html><body><h1>Test Email</h1><p>This is a test email.</p></body></html>"); | ||
| 167 | + List<NotifyMessage> notifyMessageList = new ArrayList<>(); | ||
| 168 | + notifyMessageList.add(notifyMessage); | ||
| 169 | + when(notifyMessageMapper.updateById(any())).thenReturn(1); | ||
| 170 | + | ||
| 171 | + emailService.send(notifyMessageList); | ||
| 172 | + | ||
| 173 | + Mockito.verify(notifyConfigMapper, times(1)).selectList(any()); | ||
| 174 | + Mockito.verify(notifyMessageMapper, times(1)).updateById(notifyMessage); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + | ||
| 178 | + public void testSendList() { | ||
| 179 | + try (MockedStatic<Transport> mockedStatic = mockStatic(Transport.class)) { | ||
| 180 | + // Set up the mock NotifyMessageMapper to return a valid NotifyMessage | ||
| 181 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 182 | + notifyMessage.setEmail("test@example.com"); | ||
| 183 | + notifyMessage.setTitle("Test Email"); | ||
| 184 | + notifyMessage.setContent("<html><body><h1>Test Email</h1><p>This is a test email.</p></body></html>"); | ||
| 185 | + List<NotifyMessage> notifyMessageList = new ArrayList<>(); | ||
| 186 | + notifyMessageList.add(notifyMessage); | ||
| 187 | + | ||
| 188 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 189 | + notifyConfig.setSever("smtp.example.com"); | ||
| 190 | + notifyConfig.setPort(25); | ||
| 191 | + notifyConfig.setType("email"); | ||
| 192 | + notifyConfig.setSender("sender@example.com"); | ||
| 193 | + notifyConfig.setAccount("account@example.com"); | ||
| 194 | + notifyConfig.setPasswd("passwd"); | ||
| 195 | + List<NotifyConfig> configList = new ArrayList<>(); | ||
| 196 | + configList.add(notifyConfig); | ||
| 197 | + when(notifyConfigMapper.selectList(any())).thenReturn(configList); | ||
| 198 | + | ||
| 199 | + mockedStatic.when(() -> Transport.send(any())).thenAnswer(invocationOnMock -> null); | ||
| 200 | + when(notifyMessageMapper.updateById(any())).thenReturn(1); | ||
| 201 | + | ||
| 202 | + // Call the method to be tested with the valid NotifyMessage | ||
| 203 | + emailService.send(notifyMessageList); | ||
| 204 | + | ||
| 205 | + // Verify that the method has interacted with the mock dependencies | ||
| 206 | + Mockito.verify(notifyConfigMapper, Mockito.times(1)).selectList(any()); | ||
| 207 | + Mockito.verify(notifyMessageMapper, Mockito.times(1)).updateById(notifyMessage); | ||
| 208 | + } | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + | ||
| 212 | + public void testSendList2() { | ||
| 213 | + try (MockedStatic<Transport> mockedStatic = mockStatic(Transport.class)) { | ||
| 214 | + // Set up the mock NotifyMessageMapper to return a valid NotifyMessage | ||
| 215 | + NotifyMessage notifyMessage = new NotifyMessage(); | ||
| 216 | + notifyMessage.setEmail("test@example.com"); | ||
| 217 | + notifyMessage.setTitle("Test Email"); | ||
| 218 | + notifyMessage.setContent(""); | ||
| 219 | + List<NotifyMessage> notifyMessageList = new ArrayList<>(); | ||
| 220 | + notifyMessageList.add(notifyMessage); | ||
| 221 | + | ||
| 222 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 223 | + notifyConfig.setSever("smtp.example.com"); | ||
| 224 | + notifyConfig.setPort(25); | ||
| 225 | + notifyConfig.setType("email"); | ||
| 226 | + notifyConfig.setSender("sender@example.com"); | ||
| 227 | + notifyConfig.setAccount("account@example.com"); | ||
| 228 | + notifyConfig.setPasswd("passwd"); | ||
| 229 | + List<NotifyConfig> configList = new ArrayList<>(); | ||
| 230 | + configList.add(notifyConfig); | ||
| 231 | + when(notifyConfigMapper.selectList(any())).thenReturn(configList); | ||
| 232 | + | ||
| 233 | + mockedStatic.when(() -> Transport.send(any())).thenAnswer(invocationOnMock -> null); | ||
| 234 | + when(notifyMessageMapper.updateById(any())).thenReturn(1); | ||
| 235 | + | ||
| 236 | + // Call the method to be tested with the valid NotifyMessage | ||
| 237 | + emailService.send(notifyMessageList); | ||
| 238 | + | ||
| 239 | + // Verify that the method has interacted with the mock dependencies | ||
| 240 | + Mockito.verify(notifyConfigMapper, Mockito.times(1)).selectList(any()); | ||
| 241 | + Mockito.verify(notifyMessageMapper, Mockito.times(1)).updateById(notifyMessage); | ||
| 242 | + } | ||
| 243 | + } | ||
| 244 | + | ||
| 245 | + | ||
| 246 | + public void testSendTestWithValidReceiver() { | ||
| 247 | + // Set up the mock NotifyConfigMapper to return a valid NotifyConfig | ||
| 248 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 249 | + notifyConfig.setSever("smtp.example.com"); | ||
| 250 | + notifyConfig.setPort(25); | ||
| 251 | + notifyConfig.setType("email"); | ||
| 252 | + notifyConfig.setSender("sender@example.com"); | ||
| 253 | + notifyConfig.setAccount("account@example.com"); | ||
| 254 | + notifyConfig.setPasswd("passwd"); | ||
| 255 | + | ||
| 256 | + // Call the method to be tested with valid parameters | ||
| 257 | + emailService.sendTest(notifyConfig, "", "Test Email", "<html><body><h1>Test " | ||
| 258 | + + "Email</h1><p>This is a test email.</p></body></html>"); | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + | ||
| 262 | + public void testSendTest() { | ||
| 263 | + try (MockedStatic<Transport> mockedStatic = mockStatic(Transport.class)) { | ||
| 264 | + // Call the method to be tested with an invalid receiver email address | ||
| 265 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 266 | + notifyConfig.setSever("smtp.example.com"); | ||
| 267 | + notifyConfig.setPort(25); | ||
| 268 | + notifyConfig.setType("email"); | ||
| 269 | + notifyConfig.setSender("sender@example.com"); | ||
| 270 | + notifyConfig.setAccount("account"); | ||
| 271 | + notifyConfig.setPasswd("passwd"); | ||
| 272 | + mockedStatic.when(() -> Transport.send(any(MimeMessage.class))).thenAnswer(invocationOnMock -> null); | ||
| 273 | + emailService.sendTest(notifyConfig, "test@example.com", "Test Email", | ||
| 274 | + "<html><body><h1>Test Email</h1><p>This is a test email.</p></body></html>"); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/EnvironmentServiceTest.java+96-0
| @@ -0,0 +1,96 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service; | ||
| 6 | + | ||
| 7 | +import com.nctigba.alert.monitor.entity.NctigbaEnv; | ||
| 8 | +import com.nctigba.alert.monitor.mapper.NctigbaEnvMapper; | ||
| 9 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 10 | +import org.junit.Before; | ||
| 11 | +import org.junit.Test; | ||
| 12 | +import org.junit.jupiter.api.Assertions; | ||
| 13 | +import org.junit.runner.RunWith; | ||
| 14 | +import org.mockito.InjectMocks; | ||
| 15 | +import org.mockito.Mock; | ||
| 16 | +import org.mockito.MockedStatic; | ||
| 17 | +import org.mockito.MockitoAnnotations; | ||
| 18 | +import org.opengauss.admin.common.core.domain.model.ops.OpsClusterVO; | ||
| 19 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 20 | +import org.opengauss.admin.system.plugin.facade.OpsFacade; | ||
| 21 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 22 | + | ||
| 23 | +import java.util.ArrayList; | ||
| 24 | +import java.util.List; | ||
| 25 | +import java.util.Map; | ||
| 26 | + | ||
| 27 | +import static org.junit.Assert.assertEquals; | ||
| 28 | +import static org.mockito.ArgumentMatchers.any; | ||
| 29 | +import static org.mockito.Mockito.mockStatic; | ||
| 30 | +import static org.mockito.Mockito.times; | ||
| 31 | +import static org.mockito.Mockito.verify; | ||
| 32 | +import static org.mockito.Mockito.when; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * EnvironmentService function test | ||
| 36 | + * | ||
| 37 | + * 2023/7/3 18:52 | ||
| 38 | + */ | ||
| 39 | + | ||
| 40 | +public class EnvironmentServiceTest { | ||
| 41 | + | ||
| 42 | + private EnvironmentService environmentService; | ||
| 43 | + | ||
| 44 | + private NctigbaEnvMapper envMapper; | ||
| 45 | + | ||
| 46 | + private OpsFacade opsFacade; | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + public void setUp() { | ||
| 50 | + MockitoAnnotations.initMocks(this); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + public void testCluster() { | ||
| 55 | + List<OpsClusterVO> list = new ArrayList<>(); | ||
| 56 | + when(opsFacade.listCluster()).thenReturn(list); | ||
| 57 | + List clusterList = environmentService.cluster(); | ||
| 58 | + verify(opsFacade, times(1)).listCluster(); | ||
| 59 | + assertEquals(clusterList, list); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + public void testCheckPrometheusThrowException() { | ||
| 64 | + List<NctigbaEnv> envList = new ArrayList<>(); | ||
| 65 | + when(envMapper.selectList(any())).thenReturn(envList); | ||
| 66 | + environmentService.checkPrometheus(); | ||
| 67 | + verify(envMapper, times(1)).selectList(any()); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + | ||
| 71 | + public void testCheckPrometheus() { | ||
| 72 | + List<NctigbaEnv> envList = new ArrayList<>(); | ||
| 73 | + NctigbaEnv nctigbaEnv = new NctigbaEnv(); | ||
| 74 | + nctigbaEnv.setType(NctigbaEnv.Type.PROMETHEUS); | ||
| 75 | + envList.add(nctigbaEnv); | ||
| 76 | + when(envMapper.selectList(any())).thenReturn(envList); | ||
| 77 | + environmentService.checkPrometheus(); | ||
| 78 | + verify(envMapper, times(1)).selectList(any()); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + public void testGetAlertContentParam1() { | ||
| 83 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 84 | + Map<String, Map<String, String>> map = environmentService.getAlertContentParam("alert"); | ||
| 85 | + Assertions.assertEquals(7, map.keySet().size()); | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + | ||
| 90 | + public void testGetAlertContentParam2() { | ||
| 91 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 92 | + Map<String, Map<String, String>> map = environmentService.getAlertContentParam(""); | ||
| 93 | + Assertions.assertEquals(0, map.keySet().size()); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/PrometheusServiceTest.java+750-0
| @@ -0,0 +1,750 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service; | ||
| 6 | + | ||
| 7 | +import cn.hutool.http.HttpUtil; | ||
| 8 | +import com.nctigba.alert.monitor.config.properties.AlertProperty; | ||
| 9 | +import com.nctigba.alert.monitor.config.properties.AlertmanagerProperty; | ||
| 10 | +import com.nctigba.alert.monitor.constant.CommonConstants; | ||
| 11 | +import com.nctigba.alert.monitor.entity.AlertConfig; | ||
| 12 | +import com.nctigba.alert.monitor.entity.AlertTemplateRule; | ||
| 13 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItem; | ||
| 14 | +import com.nctigba.alert.monitor.entity.NctigbaEnv; | ||
| 15 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleItemMapper; | ||
| 16 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleMapper; | ||
| 17 | +import com.nctigba.alert.monitor.mapper.NctigbaEnvMapper; | ||
| 18 | +import com.nctigba.alert.monitor.utils.SshSession; | ||
| 19 | +import org.junit.Before; | ||
| 20 | +import org.junit.Test; | ||
| 21 | +import org.junit.runner.RunWith; | ||
| 22 | +import org.mockito.InjectMocks; | ||
| 23 | +import org.mockito.Mock; | ||
| 24 | +import org.mockito.MockedStatic; | ||
| 25 | +import org.mockito.MockitoAnnotations; | ||
| 26 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsHostEntity; | ||
| 27 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsHostUserEntity; | ||
| 28 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 29 | +import org.opengauss.admin.system.plugin.facade.HostFacade; | ||
| 30 | +import org.opengauss.admin.system.plugin.facade.HostUserFacade; | ||
| 31 | +import org.opengauss.admin.system.service.ops.impl.EncryptionUtils; | ||
| 32 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 33 | + | ||
| 34 | +import java.io.IOException; | ||
| 35 | +import java.time.LocalDateTime; | ||
| 36 | +import java.util.ArrayList; | ||
| 37 | +import java.util.HashMap; | ||
| 38 | +import java.util.List; | ||
| 39 | +import java.util.Map; | ||
| 40 | + | ||
| 41 | +import static org.junit.Assert.assertEquals; | ||
| 42 | +import static org.mockito.ArgumentMatchers.any; | ||
| 43 | +import static org.mockito.ArgumentMatchers.anyInt; | ||
| 44 | +import static org.mockito.ArgumentMatchers.anyMap; | ||
| 45 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 46 | +import static org.mockito.Mockito.doNothing; | ||
| 47 | +import static org.mockito.Mockito.mock; | ||
| 48 | +import static org.mockito.Mockito.mockStatic; | ||
| 49 | +import static org.mockito.Mockito.times; | ||
| 50 | +import static org.mockito.Mockito.verify; | ||
| 51 | +import static org.mockito.Mockito.when; | ||
| 52 | + | ||
| 53 | +/** | ||
| 54 | + * test PrometheusService | ||
| 55 | + * | ||
| 56 | + * 2023/7/5 17:36 | ||
| 57 | + */ | ||
| 58 | + | ||
| 59 | +public class PrometheusServiceTest { | ||
| 60 | + | ||
| 61 | + private PrometheusService prometheusService; | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + private AlertTemplateRuleMapper alertTemplateRuleMapper; | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + private AlertTemplateRuleItemMapper alertTemplateRuleItemMapper; | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + private NctigbaEnvMapper envMapper; | ||
| 71 | + | ||
| 72 | + | ||
| 73 | + private AlertProperty alertProperty; | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + private AlertConfigService alertConfigService; | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + private EncryptionUtils encryptionUtils; | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + private HostFacade hostFacade; | ||
| 83 | + | ||
| 84 | + | ||
| 85 | + private HostUserFacade hostUserFacade; | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + public void setup() { | ||
| 89 | + MockitoAnnotations.initMocks(this); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + | ||
| 93 | + public void testPrometheusEnvDto1() { | ||
| 94 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 95 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080").setId(1L); | ||
| 96 | + alertConfigs.add(alertConfig); | ||
| 97 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 98 | + NctigbaEnv promEnv = null; | ||
| 99 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 100 | + | ||
| 101 | + prometheusService.initPrometheusConfig(); | ||
| 102 | + | ||
| 103 | + verify(alertConfigService, times(1)).list(); | ||
| 104 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + | ||
| 108 | + public void testPrometheusEnvDto2() { | ||
| 109 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 110 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080").setId(1L); | ||
| 111 | + alertConfigs.add(alertConfig); | ||
| 112 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 113 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 114 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 115 | + | ||
| 116 | + prometheusService.initPrometheusConfig(); | ||
| 117 | + | ||
| 118 | + verify(alertConfigService, times(1)).list(); | ||
| 119 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + | ||
| 123 | + public void testPrometheusEnvDto3() { | ||
| 124 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 125 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080").setId(1L); | ||
| 126 | + alertConfigs.add(alertConfig); | ||
| 127 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 128 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 129 | + promEnv.setPath("/"); | ||
| 130 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 131 | + | ||
| 132 | + prometheusService.initPrometheusConfig(); | ||
| 133 | + | ||
| 134 | + verify(alertConfigService, times(1)).list(); | ||
| 135 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + | ||
| 139 | + public void testPrometheusEnvDto4() { | ||
| 140 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 141 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080").setId(1L); | ||
| 142 | + alertConfigs.add(alertConfig); | ||
| 143 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 144 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 145 | + promEnv.setPath("/").setHostid("1"); | ||
| 146 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 147 | + OpsHostEntity promeHost = new OpsHostEntity(); | ||
| 148 | + promeHost.setHostId("1"); | ||
| 149 | + promeHost.setPublicIp("127.0.0.1"); | ||
| 150 | + promeHost.setPort(9090); | ||
| 151 | + when(hostFacade.getById(anyString())).thenReturn(promeHost); | ||
| 152 | + List<OpsHostUserEntity> hostUserList = new ArrayList<>(); | ||
| 153 | + when(hostUserFacade.listHostUserByHostId(anyString())).thenReturn(hostUserList); | ||
| 154 | + | ||
| 155 | + prometheusService.initPrometheusConfig(); | ||
| 156 | + | ||
| 157 | + verify(alertConfigService, times(1)).list(); | ||
| 158 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 159 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 160 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + | ||
| 164 | + public void testPrometheusEnvDto5() { | ||
| 165 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 166 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080").setId(1L); | ||
| 167 | + alertConfigs.add(alertConfig); | ||
| 168 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 169 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 170 | + promEnv.setPath("/").setHostid("1").setUsername("user1"); | ||
| 171 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 172 | + OpsHostEntity promeHost = new OpsHostEntity(); | ||
| 173 | + promeHost.setHostId("1"); | ||
| 174 | + promeHost.setPublicIp("127.0.0.1"); | ||
| 175 | + promeHost.setPort(9090); | ||
| 176 | + when(hostFacade.getById(anyString())).thenReturn(promeHost); | ||
| 177 | + OpsHostUserEntity hostUserEntity = new OpsHostUserEntity(); | ||
| 178 | + hostUserEntity.setUsername("user2"); | ||
| 179 | + List<OpsHostUserEntity> hostUserList = new ArrayList<>(); | ||
| 180 | + hostUserList.add(hostUserEntity); | ||
| 181 | + when(hostUserFacade.listHostUserByHostId(anyString())).thenReturn(hostUserList); | ||
| 182 | + | ||
| 183 | + prometheusService.initPrometheusConfig(); | ||
| 184 | + | ||
| 185 | + verify(alertConfigService, times(1)).list(); | ||
| 186 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 187 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 188 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + | ||
| 192 | + public void testInitPrometheusConfigWithoutAlertConfigs() { | ||
| 193 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 194 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 195 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 196 | + promEnv.setPath("/").setHostid("1").setUsername("user1").setPort(9090); | ||
| 197 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 198 | + OpsHostEntity promeHost = new OpsHostEntity(); | ||
| 199 | + promeHost.setHostId("1"); | ||
| 200 | + promeHost.setPublicIp("127.0.0.1"); | ||
| 201 | + promeHost.setPort(22); | ||
| 202 | + when(hostFacade.getById(anyString())).thenReturn(promeHost); | ||
| 203 | + OpsHostUserEntity hostUserEntity = new OpsHostUserEntity(); | ||
| 204 | + hostUserEntity.setUsername("user1"); | ||
| 205 | + hostUserEntity.setPassword("passwd"); | ||
| 206 | + List<OpsHostUserEntity> hostUserList = new ArrayList<>(); | ||
| 207 | + hostUserList.add(hostUserEntity); | ||
| 208 | + when(hostUserFacade.listHostUserByHostId(anyString())).thenReturn(hostUserList); | ||
| 209 | + when(encryptionUtils.getKey()).thenReturn(""); | ||
| 210 | + when(encryptionUtils.decrypt(anyString())).thenReturn("passwd"); | ||
| 211 | + | ||
| 212 | + prometheusService.initPrometheusConfig(); | ||
| 213 | + | ||
| 214 | + verify(alertConfigService, times(1)).list(); | ||
| 215 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 216 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 217 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 218 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 219 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + | ||
| 223 | + public void testInitPrometheusConfigDisconnectSession() { | ||
| 224 | + testPrometheusEnvDtoNormal(); | ||
| 225 | + prometheusService.initPrometheusConfig(); | ||
| 226 | + | ||
| 227 | + verify(alertConfigService, times(1)).list(); | ||
| 228 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 229 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 230 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 231 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 232 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + | ||
| 236 | + public void testInitPrometheusConfig_getPromConfigNull() { | ||
| 237 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 238 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 239 | + testPrometheusEnvDtoNormal(); | ||
| 240 | + SshSession sshSession = mock(SshSession.class); | ||
| 241 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 242 | + sshSession); | ||
| 243 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(""); | ||
| 244 | + prometheusService.initPrometheusConfig(); | ||
| 245 | + | ||
| 246 | + verify(alertConfigService, times(1)).list(); | ||
| 247 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 248 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 249 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 250 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 251 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 252 | + } | ||
| 253 | + } | ||
| 254 | + | ||
| 255 | + | ||
| 256 | + public void testInitPrometheusConfig_getPromConfigNull2() { | ||
| 257 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 258 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 259 | + testPrometheusEnvDtoNormal(); | ||
| 260 | + SshSession sshSession = mock(SshSession.class); | ||
| 261 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 262 | + sshSession); | ||
| 263 | + String result = "{\"status\":\"fail\"}"; | ||
| 264 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(result); | ||
| 265 | + prometheusService.initPrometheusConfig(); | ||
| 266 | + | ||
| 267 | + verify(alertConfigService, times(1)).list(); | ||
| 268 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 269 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 270 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 271 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 272 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 273 | + } | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + | ||
| 277 | + public void testInitPrometheusConfig_updatePromConfig1() throws IOException { | ||
| 278 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 279 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 280 | + testPrometheusEnvDtoNormal(); | ||
| 281 | + SshSession sshSession = mock(SshSession.class); | ||
| 282 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 283 | + sshSession); | ||
| 284 | + String result = "{\"status\":\"success\",\"data\":{\"yaml\":\"global:\\n scrape_interval: 5s\\n " | ||
| 285 | + + "scrape_timeout: 5s\\n evaluation_interval: 1m\\nalerting:\\n " | ||
| 286 | + + " alertmanagers:\\n - follow_redirects: true\\n enable_http2: true\\n scheme: http\\n " | ||
| 287 | + + " path_prefix: /plugins/alert-monitor\\n timeout: 10s\\n api_version: v1\\n" | ||
| 288 | + + " static_configs:\\n " | ||
| 289 | + + " - targets:\\n - 127.0.0.1:8080\\nrule_files:\\n- /data/rules/*.yml\\n" | ||
| 290 | + + "scrape_configs:\\n- job_name: prometheus\\n honor_timestamps: true\\n scrape_interval: 5s\\n " | ||
| 291 | + + "scrape_timeout: 5s\\n metrics_path: /metrics\\n scheme: http\\n follow_redirects: true\\n " | ||
| 292 | + + "enable_http2: true\\n static_configs:\\n - targets:\\n - 192.168.56.52:9100\\n " | ||
| 293 | + + "labels:\\n instance: 00336d3d-9b34-42a9-8ce8-b27048e50b8b\\n type: node\\n\"}}"; | ||
| 294 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(result); | ||
| 295 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rules/"); | ||
| 296 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 297 | + AlertmanagerProperty alertmanagerPro = new AlertmanagerProperty(); | ||
| 298 | + alertmanagerPro.setPathPrefix("/plugins/alert-monitor"); | ||
| 299 | + alertmanagerPro.setApiVersion("v1"); | ||
| 300 | + when(alertProperty.getAlertmanager()).thenReturn(alertmanagerPro); | ||
| 301 | + doNothing().when(sshSession).close(); | ||
| 302 | + | ||
| 303 | + prometheusService.initPrometheusConfig(); | ||
| 304 | + | ||
| 305 | + verify(alertConfigService, times(1)).list(); | ||
| 306 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 307 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 308 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 309 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 310 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 311 | + verify(alertProperty, times(1)).getRuleFilePrefix(); | ||
| 312 | + verify(alertProperty, times(1)).getRuleFileSuffix(); | ||
| 313 | + verify(alertProperty, times(1)).getAlertmanager(); | ||
| 314 | + verify(sshSession, times(1)).close(); | ||
| 315 | + } | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + | ||
| 319 | + public void testInitPrometheusConfig_updatePromConfig2() throws IOException { | ||
| 320 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 321 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 322 | + testPrometheusEnvDtoNormal(); | ||
| 323 | + SshSession sshSession = mock(SshSession.class); | ||
| 324 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 325 | + sshSession); | ||
| 326 | + String result = "{\"status\":\"success\",\"data\":{\"yaml\":\"global:\\n scrape_interval: 5s\\n " | ||
| 327 | + + "scrape_timeout: 5s\\n evaluation_interval: 1m\\nalerting:\\n " | ||
| 328 | + + " alertmanagers:\\n - follow_redirects: true\\n enable_http2: true\\n scheme: http\\n " | ||
| 329 | + + " path_prefix: /plugins/alert-monitor\\n timeout: 10s\\n api_version: v1\\n" | ||
| 330 | + + " static_configs:\\n" | ||
| 331 | + + "scrape_configs:\\n- job_name: prometheus\\n honor_timestamps: true\\n scrape_interval: 5s\\n " | ||
| 332 | + + "scrape_timeout: 5s\\n metrics_path: /metrics\\n scheme: http\\n follow_redirects: true\\n " | ||
| 333 | + + "enable_http2: true\\n static_configs:\\n - targets:\\n - 192.168.56.52:9100\\n " | ||
| 334 | + + "labels:\\n instance: 00336d3d-9b34-42a9-8ce8-b27048e50b8b\\n type: node\\n\"}}"; | ||
| 335 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(result); | ||
| 336 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rules/"); | ||
| 337 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 338 | + AlertmanagerProperty alertmanagerPro = new AlertmanagerProperty(); | ||
| 339 | + alertmanagerPro.setPathPrefix("/plugins/alert-monitor"); | ||
| 340 | + alertmanagerPro.setApiVersion("v1"); | ||
| 341 | + when(alertProperty.getAlertmanager()).thenReturn(alertmanagerPro); | ||
| 342 | + when(sshSession.execute(anyString())).thenReturn(""); | ||
| 343 | + doNothing().when(sshSession).upload(anyString(), anyString()); | ||
| 344 | + when(HttpUtil.post(anyString(), anyString())).thenReturn(""); | ||
| 345 | + doNothing().when(sshSession).close(); | ||
| 346 | + | ||
| 347 | + prometheusService.initPrometheusConfig(); | ||
| 348 | + | ||
| 349 | + verify(alertConfigService, times(1)).list(); | ||
| 350 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 351 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 352 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 353 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 354 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 355 | + verify(sshSession, times(1)).execute(anyString()); | ||
| 356 | + verify(sshSession, times(1)).upload(anyString(), anyString()); | ||
| 357 | + verify(sshSession, times(1)).close(); | ||
| 358 | + } | ||
| 359 | + } | ||
| 360 | + | ||
| 361 | + | ||
| 362 | + public void testInitPrometheusConfig_updatePromConfig3() throws IOException { | ||
| 363 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 364 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 365 | + testPrometheusEnvDtoNormal(); | ||
| 366 | + SshSession sshSession = mock(SshSession.class); | ||
| 367 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 368 | + sshSession); | ||
| 369 | + String result = "{\"status\":\"success\",\"data\":{\"yaml\":\"global:\\n scrape_interval: 5s\\n " | ||
| 370 | + + "scrape_timeout: 5s\\n evaluation_interval: 1m\\nalerting:\\n " | ||
| 371 | + + " alertmanagers:\\n - follow_redirects: true\\n enable_http2: true\\n scheme: http\\n " | ||
| 372 | + + " path_prefix: /alertCenter\\n timeout: 10s\\n api_version: v1\\n static_configs:\\n " | ||
| 373 | + + " - targets:\\n - 192.168.56.1:8080\\nrule_files:\\n- /data/prometheus-2.42.0/rules1/*.yml\\n" | ||
| 374 | + + "scrape_configs:\\n- job_name: prometheus\\n honor_timestamps: true\\n scrape_interval: 5s\\n " | ||
| 375 | + + "scrape_timeout: 5s\\n metrics_path: /metrics\\n scheme: http\\n follow_redirects: true\\n " | ||
| 376 | + + "enable_http2: true\\n static_configs:\\n - targets:\\n - 192.168.56.52:9100\\n " | ||
| 377 | + + "labels:\\n instance: 00336d3d-9b34-42a9-8ce8-b27048e50b8b\\n type: node\\n\"}}"; | ||
| 378 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(result); | ||
| 379 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rules/"); | ||
| 380 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 381 | + AlertmanagerProperty alertmanagerPro = new AlertmanagerProperty(); | ||
| 382 | + alertmanagerPro.setPathPrefix("/plugins/alert-monitor"); | ||
| 383 | + alertmanagerPro.setApiVersion("v1"); | ||
| 384 | + when(alertProperty.getAlertmanager()).thenReturn(alertmanagerPro); | ||
| 385 | + when(sshSession.execute(anyString())).thenReturn(""); | ||
| 386 | + doNothing().when(sshSession).upload(anyString(), anyString()); | ||
| 387 | + when(HttpUtil.post(anyString(), anyString())).thenReturn(""); | ||
| 388 | + doNothing().when(sshSession).close(); | ||
| 389 | + prometheusService.initPrometheusConfig(); | ||
| 390 | + | ||
| 391 | + verify(alertConfigService, times(1)).list(); | ||
| 392 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 393 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 394 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 395 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 396 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 397 | + verify(sshSession, times(1)).execute(anyString()); | ||
| 398 | + verify(sshSession, times(1)).upload(anyString(), anyString()); | ||
| 399 | + verify(sshSession, times(1)).close(); | ||
| 400 | + } | ||
| 401 | + } | ||
| 402 | + | ||
| 403 | + | ||
| 404 | + public void testInitPrometheusConfig_updatePromConfig4() throws IOException { | ||
| 405 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 406 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 407 | + testPrometheusEnvDtoNormal(); | ||
| 408 | + SshSession sshSession = mock(SshSession.class); | ||
| 409 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 410 | + sshSession); | ||
| 411 | + String result = "{\"status\":\"success\",\"data\":{\"yaml\":\"global:\\n scrape_interval: 5s\\n " | ||
| 412 | + + "scrape_timeout: 5s\\n evaluation_interval: 1m\\nalerting:\\n" | ||
| 413 | + + "scrape_configs:\\n- job_name: prometheus\\n honor_timestamps: true\\n scrape_interval: 5s\\n " | ||
| 414 | + + "scrape_timeout: 5s\\n metrics_path: /metrics\\n scheme: http\\n follow_redirects: true\\n " | ||
| 415 | + + "enable_http2: true\\n static_configs:\\n - targets:\\n - 192.168.56.52:9100\\n " | ||
| 416 | + + "labels:\\n instance: 00336d3d-9b34-42a9-8ce8-b27048e50b8b\\n type: node\\n\"}}"; | ||
| 417 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(result); | ||
| 418 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rules"); | ||
| 419 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 420 | + AlertmanagerProperty alertmanagerPro = new AlertmanagerProperty(); | ||
| 421 | + alertmanagerPro.setPathPrefix("/plugins/alert-monitor"); | ||
| 422 | + alertmanagerPro.setApiVersion("v1"); | ||
| 423 | + when(alertProperty.getAlertmanager()).thenReturn(alertmanagerPro); | ||
| 424 | + when(sshSession.execute(anyString())).thenReturn(""); | ||
| 425 | + doNothing().when(sshSession).upload(anyString(), anyString()); | ||
| 426 | + when(HttpUtil.post(anyString(), anyString())).thenReturn(""); | ||
| 427 | + doNothing().when(sshSession).close(); | ||
| 428 | + | ||
| 429 | + prometheusService.initPrometheusConfig(); | ||
| 430 | + | ||
| 431 | + verify(alertConfigService, times(1)).list(); | ||
| 432 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 433 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 434 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 435 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 436 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 437 | + verify(sshSession, times(1)).execute(anyString()); | ||
| 438 | + verify(sshSession, times(1)).upload(anyString(), anyString()); | ||
| 439 | + verify(sshSession, times(1)).close(); | ||
| 440 | + } | ||
| 441 | + } | ||
| 442 | + | ||
| 443 | + | ||
| 444 | + public void testQueryRange1() { | ||
| 445 | + try (MockedStatic<HttpUtil> mockedStatic = mockStatic(HttpUtil.class)) { | ||
| 446 | + mockedStatic.when(() -> HttpUtil.get(anyString(), anyMap())).thenReturn(""); | ||
| 447 | + Number[][] result = prometheusService.queryRange("127.0.0.1", "8080", "query", | ||
| 448 | + LocalDateTime.now().minusHours(1), LocalDateTime.now()); | ||
| 449 | + assertEquals(0, result.length); | ||
| 450 | + } | ||
| 451 | + } | ||
| 452 | + | ||
| 453 | + | ||
| 454 | + public void testQueryRange2() { | ||
| 455 | + try (MockedStatic<HttpUtil> mockedStatic = mockStatic(HttpUtil.class)) { | ||
| 456 | + String res = "{\"status\":\"fail\"}"; | ||
| 457 | + mockedStatic.when(() -> HttpUtil.get(anyString(), anyMap())).thenReturn(res); | ||
| 458 | + Number[][] result = prometheusService.queryRange("127.0.0.1", "8080", "query", | ||
| 459 | + LocalDateTime.now().minusHours(1), LocalDateTime.now()); | ||
| 460 | + assertEquals(0, result.length); | ||
| 461 | + } | ||
| 462 | + } | ||
| 463 | + | ||
| 464 | + | ||
| 465 | + public void testQueryRange3() { | ||
| 466 | + try (MockedStatic<HttpUtil> mockedStatic = mockStatic(HttpUtil.class)) { | ||
| 467 | + String res = "{\"status\":\"success\"}"; | ||
| 468 | + mockedStatic.when(() -> HttpUtil.get(anyString(), anyMap())).thenReturn(res); | ||
| 469 | + Number[][] result = prometheusService.queryRange("127.0.0.1", "8080", "query", | ||
| 470 | + LocalDateTime.now().minusHours(1), LocalDateTime.now()); | ||
| 471 | + assertEquals(0, result.length); | ||
| 472 | + } | ||
| 473 | + } | ||
| 474 | + | ||
| 475 | + | ||
| 476 | + public void testQueryRange4() { | ||
| 477 | + try (MockedStatic<HttpUtil> mockedStatic = mockStatic(HttpUtil.class)) { | ||
| 478 | + String res = "{\"status\":\"success\",\"data\":{\"result\":[]}}"; | ||
| 479 | + mockedStatic.when(() -> HttpUtil.get(anyString(), anyMap())).thenReturn(res); | ||
| 480 | + Number[][] result = prometheusService.queryRange("127.0.0.1", "8080", "query", | ||
| 481 | + LocalDateTime.now().minusHours(1), LocalDateTime.now()); | ||
| 482 | + assertEquals(0, result.length); | ||
| 483 | + } | ||
| 484 | + } | ||
| 485 | + | ||
| 486 | + | ||
| 487 | + public void testQueryRange5() { | ||
| 488 | + try (MockedStatic<HttpUtil> mockedStatic = mockStatic(HttpUtil.class)) { | ||
| 489 | + String res = "{\"status\":\"success\",\"data\":{\"result\":[{}]}}"; | ||
| 490 | + mockedStatic.when(() -> HttpUtil.get(anyString(), anyMap())).thenReturn(res); | ||
| 491 | + Number[][] result = prometheusService.queryRange("127.0.0.1", "8080", "query", | ||
| 492 | + LocalDateTime.now().minusHours(1), LocalDateTime.now()); | ||
| 493 | + assertEquals(0, result.length); | ||
| 494 | + } | ||
| 495 | + } | ||
| 496 | + | ||
| 497 | + | ||
| 498 | + public void testQueryRange6() { | ||
| 499 | + try (MockedStatic<HttpUtil> mockedStatic = mockStatic(HttpUtil.class)) { | ||
| 500 | + String res = "{\"status\":\"success\",\"data\":{\"result\":[{\"values\":[[1683472020," | ||
| 501 | + + "\"1.5090909090907871\"]]}]}}"; | ||
| 502 | + mockedStatic.when(() -> HttpUtil.get(anyString(), anyMap())).thenReturn(res); | ||
| 503 | + Number[][] result = prometheusService.queryRange("127.0.0.1", "8080", "query", | ||
| 504 | + LocalDateTime.now().minusHours(1), LocalDateTime.now()); | ||
| 505 | + assertEquals(1, result.length); | ||
| 506 | + } | ||
| 507 | + } | ||
| 508 | + | ||
| 509 | + | ||
| 510 | + public void testUpdateAlertConfigThrowException() { | ||
| 511 | + AlertConfig alertConfig = new AlertConfig(); | ||
| 512 | + List<AlertConfig> delAlertConfigList = new ArrayList<>(); | ||
| 513 | + prometheusService.updateAlertConfig(alertConfig, delAlertConfigList); | ||
| 514 | + } | ||
| 515 | + | ||
| 516 | + | ||
| 517 | + public void testUpdateAlertConfigThrowException2() { | ||
| 518 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1"); | ||
| 519 | + List<AlertConfig> delAlertConfigList = new ArrayList<>(); | ||
| 520 | + prometheusService.updateAlertConfig(alertConfig, delAlertConfigList); | ||
| 521 | + } | ||
| 522 | + | ||
| 523 | + | ||
| 524 | + public void testUpdateAlertConfigThrowException3() { | ||
| 525 | + testPrometheusEnvDtoNormal(); | ||
| 526 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080"); | ||
| 527 | + List<AlertConfig> delAlertConfigList = new ArrayList<>(); | ||
| 528 | + prometheusService.updateAlertConfig(alertConfig, delAlertConfigList); | ||
| 529 | + } | ||
| 530 | + | ||
| 531 | + | ||
| 532 | + public void testUpdateAlertConfig() throws IOException { | ||
| 533 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 534 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 535 | + testPrometheusEnvDtoNormal(); | ||
| 536 | + SshSession sshSession = mock(SshSession.class); | ||
| 537 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 538 | + sshSession); | ||
| 539 | + String result = "{\"status\":\"success\",\"data\":{\"yaml\":\"global:\\n scrape_interval: 5s\\n " | ||
| 540 | + + "scrape_timeout: 5s\\n evaluation_interval: 1m\\nalerting:\\n " | ||
| 541 | + + " alertmanagers:\\n - follow_redirects: true\\n enable_http2: true\\n scheme: http\\n " | ||
| 542 | + + " path_prefix: /alertCenter\\n timeout: 10s\\n api_version: v1\\n static_configs:\\n " | ||
| 543 | + + " - targets:\\n - 192.168.56.1:8080\\nrule_files:\\n- /data/prometheus-2.42.0/rules1/*.yml\\n" | ||
| 544 | + + "scrape_configs:\\n- job_name: prometheus\\n honor_timestamps: true\\n scrape_interval: 5s\\n " | ||
| 545 | + + "scrape_timeout: 5s\\n metrics_path: /metrics\\n scheme: http\\n follow_redirects: true\\n " | ||
| 546 | + + "enable_http2: true\\n static_configs:\\n - targets:\\n - 192.168.56.52:9100\\n " | ||
| 547 | + + "labels:\\n instance: 00336d3d-9b34-42a9-8ce8-b27048e50b8b\\n type: node\\n\"}}"; | ||
| 548 | + mockedStatic2.when(() -> HttpUtil.get(anyString())).thenReturn(result); | ||
| 549 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rules/"); | ||
| 550 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 551 | + AlertmanagerProperty alertmanagerPro = new AlertmanagerProperty(); | ||
| 552 | + alertmanagerPro.setPathPrefix("/plugins/alert-monitor"); | ||
| 553 | + alertmanagerPro.setApiVersion("v1"); | ||
| 554 | + when(alertProperty.getAlertmanager()).thenReturn(alertmanagerPro); | ||
| 555 | + when(sshSession.execute(anyString())).thenReturn(""); | ||
| 556 | + doNothing().when(sshSession).upload(anyString(), anyString()); | ||
| 557 | + when(HttpUtil.post(anyString(), anyString())).thenReturn(""); | ||
| 558 | + doNothing().when(sshSession).close(); | ||
| 559 | + | ||
| 560 | + AlertConfig alertConfig = new AlertConfig().setId(2L).setAlertPort("127.0.0.1").setAlertPort("8080"); | ||
| 561 | + List<AlertConfig> delAlertConfigList = new ArrayList<>(); | ||
| 562 | + AlertConfig delAlertConfig = new AlertConfig().setId(3L).setAlertIp("192.168.56.1").setAlertPort("8080"); | ||
| 563 | + delAlertConfigList.add(delAlertConfig); | ||
| 564 | + prometheusService.updateAlertConfig(alertConfig, delAlertConfigList); | ||
| 565 | + | ||
| 566 | + verify(alertConfigService, times(1)).list(); | ||
| 567 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 568 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 569 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 570 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 571 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 572 | + verify(sshSession, times(1)).execute(anyString()); | ||
| 573 | + verify(sshSession, times(1)).upload(anyString(), anyString()); | ||
| 574 | + verify(sshSession, times(1)).close(); | ||
| 575 | + } | ||
| 576 | + } | ||
| 577 | + | ||
| 578 | + | ||
| 579 | + public void testUpdateRuleConfigNull() { | ||
| 580 | + Map<Long, String> ruleConfigMap = new HashMap<>(); | ||
| 581 | + prometheusService.updateRuleConfig(ruleConfigMap); | ||
| 582 | + } | ||
| 583 | + | ||
| 584 | + | ||
| 585 | + public void testUpdateRuleConfigWithNullClusterNode() throws IOException { | ||
| 586 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class)) { | ||
| 587 | + testPrometheusEnvDtoNormal(); | ||
| 588 | + SshSession sshSession = mock(SshSession.class); | ||
| 589 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 590 | + sshSession); | ||
| 591 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rule/"); | ||
| 592 | + when(sshSession.execute("ls /data")).thenReturn(""); | ||
| 593 | + doNothing().when(sshSession).close(); | ||
| 594 | + | ||
| 595 | + Map<Long, String> ruleConfigMap = new HashMap<>(); | ||
| 596 | + ruleConfigMap.put(1L, ""); | ||
| 597 | + prometheusService.updateRuleConfig(ruleConfigMap); | ||
| 598 | + | ||
| 599 | + verify(alertConfigService, times(1)).list(); | ||
| 600 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 601 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 602 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 603 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 604 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 605 | + verify(alertProperty, times(4)).getRuleFilePrefix(); | ||
| 606 | + verify(sshSession, times(1)).execute("ls /data"); | ||
| 607 | + verify(sshSession, times(1)).close(); | ||
| 608 | + } | ||
| 609 | + } | ||
| 610 | + | ||
| 611 | + | ||
| 612 | + public void testUpdateRuleConfig1() throws IOException { | ||
| 613 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 614 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 615 | + testPrometheusEnvDtoNormal(); | ||
| 616 | + SshSession sshSession = mock(SshSession.class); | ||
| 617 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 618 | + sshSession); | ||
| 619 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rule/"); | ||
| 620 | + when(sshSession.execute("ls /data")).thenReturn(""); | ||
| 621 | + when(sshSession.execute("mkdir /data/rule/")).thenReturn(""); | ||
| 622 | + when(sshSession.execute("ls /data/rule/")).thenReturn(""); | ||
| 623 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 624 | + when(sshSession.execute("touch /data/rule/rule_template_1.yml")).thenReturn(""); | ||
| 625 | + when(sshSession.execute("cat /data/rule/rule_template_1.yml")).thenReturn(""); | ||
| 626 | + | ||
| 627 | + List<AlertTemplateRule> alertTemplateRules = new ArrayList<>(); | ||
| 628 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule(); | ||
| 629 | + alertTemplateRule.setId(1L).setTemplateId(1L).setRuleId(1L).setRuleName("CPU使用率过高").setLevel("warn") | ||
| 630 | + .setRuleType("index").setRuleExpComb("A").setRuleContent("${nodeName}的CPU使用率超过90%") | ||
| 631 | + .setNotifyDuration(2).setNotifyDurationUnit("m").setIsRepeat(CommonConstants.IS_REPEAT).setIsSilence(0) | ||
| 632 | + .setAlertNotify("firing").setNotifyWayIds("1"); | ||
| 633 | + alertTemplateRules.add(alertTemplateRule); | ||
| 634 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(alertTemplateRules); | ||
| 635 | + List<AlertTemplateRuleItem> alertTemplateRuleItems = new ArrayList<>(); | ||
| 636 | + AlertTemplateRuleItem alertTemplateRuleItem = new AlertTemplateRuleItem(); | ||
| 637 | + alertTemplateRuleItem.setId(1L).setTemplateRuleId(1L).setRuleItemId(1L).setRuleMark("A") | ||
| 638 | + .setRuleExpName("cpuUsage").setOperate(">=").setLimitValue("90").setUnit("%").setRuleExp("100 - avg" | ||
| 639 | + + "(rate(agent_cpu_seconds_total{mode=\"idle\",instance=~\"${instances}\"}[5m])) by(instance) " | ||
| 640 | + + " * 100").setAction("normal"); | ||
| 641 | + alertTemplateRuleItems.add(alertTemplateRuleItem); | ||
| 642 | + when(alertTemplateRuleItemMapper.selectList(any())).thenReturn(alertTemplateRuleItems); | ||
| 643 | + | ||
| 644 | + when(sshSession.execute("rm /data/rule/rule_template_1.yml")).thenReturn(""); | ||
| 645 | + doNothing().when(sshSession).upload(anyString(), anyString()); | ||
| 646 | + mockedStatic2.when(() -> HttpUtil.post(anyString(), anyString())).thenReturn(""); | ||
| 647 | + doNothing().when(sshSession).close(); | ||
| 648 | + | ||
| 649 | + Map<Long, String> ruleConfigMap = new HashMap<>(); | ||
| 650 | + ruleConfigMap.put(1L, "node1"); | ||
| 651 | + prometheusService.updateRuleConfig(ruleConfigMap); | ||
| 652 | + | ||
| 653 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 654 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 655 | + verify(alertProperty, times(5)).getRuleFilePrefix(); | ||
| 656 | + verify(alertProperty, times(2)).getRuleFileSuffix(); | ||
| 657 | + verify(sshSession, times(6)).execute(anyString()); | ||
| 658 | + verify(sshSession, times(1)).upload(anyString(), anyString()); | ||
| 659 | + verify(sshSession, times(1)).close(); | ||
| 660 | + } | ||
| 661 | + } | ||
| 662 | + | ||
| 663 | + | ||
| 664 | + public void testUpdateRuleConfigWithNullClusterNode2() throws IOException { | ||
| 665 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class)) { | ||
| 666 | + testPrometheusEnvDtoNormal(); | ||
| 667 | + SshSession sshSession = mock(SshSession.class); | ||
| 668 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 669 | + sshSession); | ||
| 670 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rule/"); | ||
| 671 | + when(sshSession.execute("ls /data")).thenReturn("rule"); | ||
| 672 | + when(sshSession.execute("ls /data/rule/")).thenReturn(""); | ||
| 673 | + doNothing().when(sshSession).close(); | ||
| 674 | + | ||
| 675 | + Map<Long, String> ruleConfigMap = new HashMap<>(); | ||
| 676 | + ruleConfigMap.put(1L, ""); | ||
| 677 | + prometheusService.updateRuleConfig(ruleConfigMap); | ||
| 678 | + | ||
| 679 | + verify(alertConfigService, times(1)).list(); | ||
| 680 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 681 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 682 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 683 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 684 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 685 | + verify(alertProperty, times(4)).getRuleFilePrefix(); | ||
| 686 | + verify(sshSession, times(1)).execute("ls /data"); | ||
| 687 | + verify(sshSession, times(1)).execute("ls /data/rule/"); | ||
| 688 | + verify(sshSession, times(1)).close(); | ||
| 689 | + } | ||
| 690 | + } | ||
| 691 | + | ||
| 692 | + | ||
| 693 | + public void testUpdateRuleConfigWithNullClusterNode3() throws IOException { | ||
| 694 | + try (MockedStatic<SshSession> mockedStatic = mockStatic(SshSession.class); | ||
| 695 | + MockedStatic<HttpUtil> mockedStatic2 = mockStatic(HttpUtil.class)) { | ||
| 696 | + testPrometheusEnvDtoNormal(); | ||
| 697 | + SshSession sshSession = mock(SshSession.class); | ||
| 698 | + mockedStatic.when(() -> SshSession.connect(anyString(), anyInt(), anyString(), anyString())).thenReturn( | ||
| 699 | + sshSession); | ||
| 700 | + when(alertProperty.getRuleFilePrefix()).thenReturn("rule/"); | ||
| 701 | + when(sshSession.execute("ls /data")).thenReturn("rule"); | ||
| 702 | + when(sshSession.execute("ls /data/rule/")).thenReturn("rule_template_1.yml"); | ||
| 703 | + when(alertProperty.getRuleFileSuffix()).thenReturn(".yml"); | ||
| 704 | + when(sshSession.execute("rm -f /data/rule/rule_template_1.yml")).thenReturn(""); | ||
| 705 | + mockedStatic2.when(() -> HttpUtil.post(anyString(), anyString())).thenReturn(""); | ||
| 706 | + doNothing().when(sshSession).close(); | ||
| 707 | + | ||
| 708 | + Map<Long, String> ruleConfigMap = new HashMap<>(); | ||
| 709 | + ruleConfigMap.put(1L, ""); | ||
| 710 | + prometheusService.updateRuleConfig(ruleConfigMap); | ||
| 711 | + | ||
| 712 | + verify(alertConfigService, times(1)).list(); | ||
| 713 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 714 | + verify(hostFacade, times(1)).getById(anyString()); | ||
| 715 | + verify(hostUserFacade, times(1)).listHostUserByHostId(anyString()); | ||
| 716 | + verify(encryptionUtils, times(1)).getKey(); | ||
| 717 | + verify(encryptionUtils, times(1)).decrypt(anyString()); | ||
| 718 | + verify(alertProperty, times(4)).getRuleFilePrefix(); | ||
| 719 | + verify(sshSession, times(1)).execute("ls /data"); | ||
| 720 | + verify(sshSession, times(1)).execute("ls /data/rule/"); | ||
| 721 | + verify(alertProperty, times(1)).getRuleFileSuffix(); | ||
| 722 | + verify(sshSession, times(1)) | ||
| 723 | + .execute("rm -f /data/rule/rule_template_1.yml"); | ||
| 724 | + verify(sshSession, times(1)).close(); | ||
| 725 | + } | ||
| 726 | + } | ||
| 727 | + | ||
| 728 | + private void testPrometheusEnvDtoNormal() { | ||
| 729 | + List<AlertConfig> alertConfigs = new ArrayList<>(); | ||
| 730 | + AlertConfig alertConfig = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080").setId(1L); | ||
| 731 | + alertConfigs.add(alertConfig); | ||
| 732 | + when(alertConfigService.list()).thenReturn(alertConfigs); | ||
| 733 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 734 | + promEnv.setPath("/data").setHostid("1").setUsername("user1").setPort(9090); | ||
| 735 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 736 | + OpsHostEntity promeHost = new OpsHostEntity(); | ||
| 737 | + promeHost.setHostId("1"); | ||
| 738 | + promeHost.setPublicIp("127.0.0.1"); | ||
| 739 | + promeHost.setPort(22); | ||
| 740 | + when(hostFacade.getById(anyString())).thenReturn(promeHost); | ||
| 741 | + OpsHostUserEntity hostUserEntity = new OpsHostUserEntity(); | ||
| 742 | + hostUserEntity.setUsername("user1"); | ||
| 743 | + hostUserEntity.setPassword("passwd"); | ||
| 744 | + List<OpsHostUserEntity> hostUserList = new ArrayList<>(); | ||
| 745 | + hostUserList.add(hostUserEntity); | ||
| 746 | + when(hostUserFacade.listHostUserByHostId(anyString())).thenReturn(hostUserList); | ||
| 747 | + when(encryptionUtils.getKey()).thenReturn(""); | ||
| 748 | + when(encryptionUtils.decrypt(anyString())).thenReturn("passwd"); | ||
| 749 | + } | ||
| 750 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertClusterNodeConfServiceImplTest.java+273-0
| @@ -0,0 +1,273 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import cn.hutool.core.bean.BeanUtil; | ||
| 8 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 9 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | ||
| 10 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 11 | +import com.nctigba.alert.monitor.dto.AlertClusterNodeConfDto; | ||
| 12 | +import com.nctigba.alert.monitor.entity.AlertClusterNodeConf; | ||
| 13 | +import com.nctigba.alert.monitor.entity.AlertTemplate; | ||
| 14 | +import com.nctigba.alert.monitor.mapper.AlertClusterNodeConfMapper; | ||
| 15 | +import com.nctigba.alert.monitor.mapper.AlertTemplateMapper; | ||
| 16 | +import com.nctigba.alert.monitor.model.AlertClusterNodeAndTemplateReq; | ||
| 17 | +import com.nctigba.alert.monitor.model.AlertClusterNodeConfReq; | ||
| 18 | +import com.nctigba.alert.monitor.model.AlertTemplateReq; | ||
| 19 | +import com.nctigba.alert.monitor.service.AlertTemplateService; | ||
| 20 | +import com.nctigba.alert.monitor.service.PrometheusService; | ||
| 21 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 22 | +import org.junit.Before; | ||
| 23 | +import org.junit.Test; | ||
| 24 | +import org.junit.runner.RunWith; | ||
| 25 | +import org.mockito.InjectMocks; | ||
| 26 | +import org.mockito.Mock; | ||
| 27 | +import org.mockito.MockitoAnnotations; | ||
| 28 | +import org.mockito.Spy; | ||
| 29 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterEntity; | ||
| 30 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterNodeEntity; | ||
| 31 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsHostEntity; | ||
| 32 | +import org.opengauss.admin.common.enums.ops.ClusterRoleEnum; | ||
| 33 | +import org.opengauss.admin.system.plugin.facade.HostFacade; | ||
| 34 | +import org.opengauss.admin.system.service.ops.IOpsClusterNodeService; | ||
| 35 | +import org.opengauss.admin.system.service.ops.IOpsClusterService; | ||
| 36 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 37 | + | ||
| 38 | +import java.util.ArrayList; | ||
| 39 | +import java.util.List; | ||
| 40 | +import java.util.Set; | ||
| 41 | +import java.util.stream.Collectors; | ||
| 42 | + | ||
| 43 | +import static org.junit.Assert.assertEquals; | ||
| 44 | +import static org.junit.Assert.assertNotNull; | ||
| 45 | +import static org.junit.Assert.assertNull; | ||
| 46 | +import static org.mockito.ArgumentMatchers.any; | ||
| 47 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 48 | +import static org.mockito.ArgumentMatchers.anyMap; | ||
| 49 | +import static org.mockito.Mockito.doNothing; | ||
| 50 | +import static org.mockito.Mockito.times; | ||
| 51 | +import static org.mockito.Mockito.verify; | ||
| 52 | +import static org.mockito.Mockito.when; | ||
| 53 | + | ||
| 54 | +/** | ||
| 55 | + * test the function of the AlertClusterNodeConfServiceImpl | ||
| 56 | + * | ||
| 57 | + * 2023/7/11 22:57 | ||
| 58 | + */ | ||
| 59 | + | ||
| 60 | +public class AlertClusterNodeConfServiceImplTest { | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + private AlertClusterNodeConfServiceImpl alertClusterNodeConfService; | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + private AlertClusterNodeConfMapper baseMapper; | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + private PrometheusService prometheusService; | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + private AlertTemplateService templateService; | ||
| 73 | + | ||
| 74 | + | ||
| 75 | + private AlertTemplateMapper templateMapper; | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + private IOpsClusterService clusterService; | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + private IOpsClusterNodeService clusterNodeService; | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + private HostFacade hostFacade; | ||
| 85 | + | ||
| 86 | + | ||
| 87 | + public void before() { | ||
| 88 | + MockitoAnnotations.initMocks(this); | ||
| 89 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 90 | + AlertClusterNodeConf.class); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + | ||
| 94 | + public void testSaveClusterNodeConfNull() { | ||
| 95 | + AlertClusterNodeConfReq alertClusterNodeConfReq = new AlertClusterNodeConfReq(); | ||
| 96 | + alertClusterNodeConfReq.setClusterNodeIds("node1"); | ||
| 97 | + alertClusterNodeConfReq.setTemplateId(1L); | ||
| 98 | + AlertClusterNodeConf clusterNodeConf = new AlertClusterNodeConf(); | ||
| 99 | + clusterNodeConf.setId(1L).setTemplateId(1L).setClusterNodeId("node1"); | ||
| 100 | + List<AlertClusterNodeConf> oldList = List.of(clusterNodeConf); | ||
| 101 | + when(baseMapper.selectList(any())).thenReturn(oldList); | ||
| 102 | + alertClusterNodeConfService.saveClusterNodeConf(alertClusterNodeConfReq); | ||
| 103 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + | ||
| 107 | + public void testSaveClusterNodeConf1() { | ||
| 108 | + AlertClusterNodeConfReq alertClusterNodeConfReq = new AlertClusterNodeConfReq(); | ||
| 109 | + alertClusterNodeConfReq.setClusterNodeIds("node1,node2"); | ||
| 110 | + alertClusterNodeConfReq.setTemplateId(1L); | ||
| 111 | + AlertClusterNodeConf alertClusterNodeConf = new AlertClusterNodeConf(); | ||
| 112 | + alertClusterNodeConf.setClusterNodeId("node2").setTemplateId(2L); | ||
| 113 | + List<AlertClusterNodeConf> oldList = new ArrayList<>(); | ||
| 114 | + oldList.add(alertClusterNodeConf); | ||
| 115 | + when(baseMapper.selectList(any())).thenReturn(oldList); | ||
| 116 | + List list = anyList(); | ||
| 117 | + when(alertClusterNodeConfService.saveBatch(list)).thenReturn(true); | ||
| 118 | + doNothing().when(prometheusService).updateRuleConfig(anyMap()); | ||
| 119 | + | ||
| 120 | + alertClusterNodeConfService.saveClusterNodeConf(alertClusterNodeConfReq); | ||
| 121 | + | ||
| 122 | + verify(baseMapper, times(3)).selectList(any()); | ||
| 123 | + verify(alertClusterNodeConfService, times(1)).saveBatch(list); | ||
| 124 | + verify(alertClusterNodeConfService, times(1)).update(any(), any(LambdaUpdateWrapper.class)); | ||
| 125 | + verify(prometheusService, times(1)).updateRuleConfig(anyMap()); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + | ||
| 129 | + public void testSaveClusterNodeConf2() { | ||
| 130 | + AlertClusterNodeConfReq alertClusterNodeConfReq = new AlertClusterNodeConfReq(); | ||
| 131 | + alertClusterNodeConfReq.setClusterNodeIds("node1,node2"); | ||
| 132 | + alertClusterNodeConfReq.setTemplateId(1L); | ||
| 133 | + List<AlertClusterNodeConf> oldList = new ArrayList<>(); | ||
| 134 | + when(baseMapper.selectList(any())).thenReturn(oldList); | ||
| 135 | + List list = anyList(); | ||
| 136 | + when(alertClusterNodeConfService.saveBatch(list)).thenReturn(true); | ||
| 137 | + doNothing().when(prometheusService).updateRuleConfig(anyMap()); | ||
| 138 | + | ||
| 139 | + alertClusterNodeConfService.saveClusterNodeConf(alertClusterNodeConfReq); | ||
| 140 | + | ||
| 141 | + verify(baseMapper, times(2)).selectList(any()); | ||
| 142 | + verify(alertClusterNodeConfService, times(1)).saveBatch(list); | ||
| 143 | + verify(alertClusterNodeConfService, times(1)).update(any(), any(LambdaUpdateWrapper.class)); | ||
| 144 | + verify(prometheusService, times(1)).updateRuleConfig(anyMap()); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + | ||
| 148 | + public void testGetByClusterNodeIdNull() { | ||
| 149 | + String clusterNodeId = "node1"; | ||
| 150 | + when(baseMapper.selectList(any())).thenReturn(anyList()); | ||
| 151 | + AlertClusterNodeConf alertClusterNodeConf = alertClusterNodeConfService.getByClusterNodeId(clusterNodeId); | ||
| 152 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 153 | + assertNotNull(alertClusterNodeConf); | ||
| 154 | + assertNull(alertClusterNodeConf.getClusterNodeId()); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + | ||
| 158 | + public void testGetByClusterNodeId() { | ||
| 159 | + String clusterNodeId = "node1"; | ||
| 160 | + List<AlertClusterNodeConf> list = List.of(new AlertClusterNodeConf().setClusterNodeId("node1")); | ||
| 161 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 162 | + AlertClusterNodeConf alertClusterNodeConf1 = alertClusterNodeConfService.getByClusterNodeId(clusterNodeId); | ||
| 163 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 164 | + assertEquals("node1", alertClusterNodeConf1.getClusterNodeId()); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + | ||
| 168 | + public void testSaveAlertTemplateAndConfig() { | ||
| 169 | + AlertClusterNodeAndTemplateReq clusterNodeAndTemplateReq = | ||
| 170 | + new AlertClusterNodeAndTemplateReq().setClusterNodeIds("node1,node2").setTemplateName("templateName") | ||
| 171 | + .setTemplateRuleReqList(new ArrayList<>()); | ||
| 172 | + AlertTemplateReq templateReq = new AlertTemplateReq(); | ||
| 173 | + BeanUtil.copyProperties(clusterNodeAndTemplateReq, templateReq); | ||
| 174 | + AlertTemplate alertTemplate = new AlertTemplate().setTemplateName(templateReq.getTemplateName()).setId(1L); | ||
| 175 | + when(templateService.saveTemplate(any())).thenReturn(alertTemplate); | ||
| 176 | + doNothing().when(alertClusterNodeConfService).saveClusterNodeConf(any()); | ||
| 177 | + | ||
| 178 | + alertClusterNodeConfService.saveAlertTemplateAndConfig(clusterNodeAndTemplateReq); | ||
| 179 | + | ||
| 180 | + verify(templateService, times(1)).saveTemplate(any()); | ||
| 181 | + verify(alertClusterNodeConfService, times(1)).saveClusterNodeConf(any()); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + | ||
| 185 | + public void testGetList1() { | ||
| 186 | + List<OpsClusterNodeEntity> list = new ArrayList<>(); | ||
| 187 | + OpsClusterNodeEntity opsClusterNode = new OpsClusterNodeEntity(); | ||
| 188 | + opsClusterNode.setClusterNodeId("node123"); | ||
| 189 | + opsClusterNode.setClusterId("test"); | ||
| 190 | + opsClusterNode.setHostId("1"); | ||
| 191 | + opsClusterNode.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 192 | + list.add(opsClusterNode); | ||
| 193 | + when(clusterNodeService.list()).thenReturn(list); | ||
| 194 | + | ||
| 195 | + Set<String> set = list.stream().map(item -> item.getHostId()).collect(Collectors.toSet()); | ||
| 196 | + List<OpsHostEntity> opsHostEntities = new ArrayList<>(); | ||
| 197 | + OpsHostEntity opsHostEntity = new OpsHostEntity(); | ||
| 198 | + opsHostEntity.setHostId("1"); | ||
| 199 | + opsHostEntity.setPublicIp("127.0.0.1"); | ||
| 200 | + opsHostEntities.add(opsHostEntity); | ||
| 201 | + when(hostFacade.listByIds(set)).thenReturn(opsHostEntities); | ||
| 202 | + | ||
| 203 | + List<OpsClusterEntity> opsClusterEntities = new ArrayList<>(); | ||
| 204 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 205 | + opsClusterEntity.setClusterId("test"); | ||
| 206 | + opsClusterEntity.setPort(80); | ||
| 207 | + opsClusterEntities.add(opsClusterEntity); | ||
| 208 | + when(clusterService.listByIds(any())).thenReturn(opsClusterEntities); | ||
| 209 | + | ||
| 210 | + List<AlertClusterNodeConf> alertClusterNodeConfs = new ArrayList<>(); | ||
| 211 | + AlertClusterNodeConf alertClusterNodeConf = | ||
| 212 | + new AlertClusterNodeConf().setClusterNodeId("node123").setTemplateId(1L); | ||
| 213 | + alertClusterNodeConfs.add(alertClusterNodeConf); | ||
| 214 | + when(baseMapper.selectList(any())).thenReturn(alertClusterNodeConfs); | ||
| 215 | + | ||
| 216 | + AlertTemplate alertTemplate = new AlertTemplate().setId(1L).setTemplateName("name"); | ||
| 217 | + when(templateMapper.selectById(any())).thenReturn(alertTemplate); | ||
| 218 | + | ||
| 219 | + List<AlertClusterNodeConfDto> resultList = alertClusterNodeConfService.getList(); | ||
| 220 | + | ||
| 221 | + verify(clusterNodeService, times(1)).list(); | ||
| 222 | + verify(hostFacade, times(1)).listByIds(set); | ||
| 223 | + verify(clusterService, times(1)).listByIds(anyList()); | ||
| 224 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 225 | + verify(templateMapper, times(1)).selectById(any()); | ||
| 226 | + assertEquals(1, resultList.size()); | ||
| 227 | + AlertClusterNodeConfDto alertClusterNodeConfDto = new AlertClusterNodeConfDto(); | ||
| 228 | + alertClusterNodeConfDto.setClusterNodeId("node123").setNodeName("test/127.0.0.1:80(MASTER)") | ||
| 229 | + .setTemplateId(1L).setTemplateName("name"); | ||
| 230 | + assertEquals(alertClusterNodeConfDto, resultList.get(0)); | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + | ||
| 234 | + public void testGetList2() { | ||
| 235 | + List<OpsClusterNodeEntity> list = new ArrayList<>(); | ||
| 236 | + OpsClusterNodeEntity opsClusterNode = new OpsClusterNodeEntity(); | ||
| 237 | + opsClusterNode.setClusterNodeId("node123"); | ||
| 238 | + opsClusterNode.setClusterId("test"); | ||
| 239 | + opsClusterNode.setHostId("1"); | ||
| 240 | + opsClusterNode.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 241 | + list.add(opsClusterNode); | ||
| 242 | + when(clusterNodeService.list()).thenReturn(list); | ||
| 243 | + | ||
| 244 | + Set<String> set = list.stream().map(item -> item.getHostId()).collect(Collectors.toSet()); | ||
| 245 | + List<OpsHostEntity> opsHostEntities = new ArrayList<>(); | ||
| 246 | + OpsHostEntity opsHostEntity = new OpsHostEntity(); | ||
| 247 | + opsHostEntity.setHostId("1"); | ||
| 248 | + opsHostEntity.setPublicIp("127.0.0.1"); | ||
| 249 | + opsHostEntities.add(opsHostEntity); | ||
| 250 | + when(hostFacade.listByIds(set)).thenReturn(opsHostEntities); | ||
| 251 | + | ||
| 252 | + List<OpsClusterEntity> opsClusterEntities = new ArrayList<>(); | ||
| 253 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 254 | + opsClusterEntity.setClusterId("test"); | ||
| 255 | + opsClusterEntity.setPort(80); | ||
| 256 | + opsClusterEntities.add(opsClusterEntity); | ||
| 257 | + when(clusterService.listByIds(any())).thenReturn(opsClusterEntities); | ||
| 258 | + | ||
| 259 | + List<AlertClusterNodeConf> alertClusterNodeConfs = new ArrayList<>(); | ||
| 260 | + when(baseMapper.selectList(any())).thenReturn(alertClusterNodeConfs); | ||
| 261 | + | ||
| 262 | + List<AlertClusterNodeConfDto> resultList = alertClusterNodeConfService.getList(); | ||
| 263 | + | ||
| 264 | + verify(clusterNodeService, times(1)).list(); | ||
| 265 | + verify(hostFacade, times(1)).listByIds(set); | ||
| 266 | + verify(clusterService, times(1)).listByIds(anyList()); | ||
| 267 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 268 | + assertEquals(1, resultList.size()); | ||
| 269 | + AlertClusterNodeConfDto alertClusterNodeConfDto = new AlertClusterNodeConfDto(); | ||
| 270 | + alertClusterNodeConfDto.setClusterNodeId("node123").setNodeName("test/127.0.0.1:80(MASTER)"); | ||
| 271 | + assertEquals(alertClusterNodeConfDto, resultList.get(0)); | ||
| 272 | + } | ||
| 273 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertConfigServiceImplTest.java+91-0
| @@ -0,0 +1,91 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.nctigba.alert.monitor.entity.AlertConfig; | ||
| 10 | +import com.nctigba.alert.monitor.mapper.AlertConfigMapper; | ||
| 11 | +import com.nctigba.alert.monitor.service.PrometheusService; | ||
| 12 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 13 | +import org.junit.Before; | ||
| 14 | +import org.junit.Test; | ||
| 15 | +import org.junit.runner.RunWith; | ||
| 16 | +import org.mockito.InjectMocks; | ||
| 17 | +import org.mockito.Mock; | ||
| 18 | +import org.mockito.MockitoAnnotations; | ||
| 19 | +import org.mockito.Spy; | ||
| 20 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 21 | + | ||
| 22 | +import java.util.ArrayList; | ||
| 23 | +import java.util.List; | ||
| 24 | + | ||
| 25 | +import static org.mockito.ArgumentMatchers.any; | ||
| 26 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 27 | +import static org.mockito.Mockito.doNothing; | ||
| 28 | +import static org.mockito.Mockito.times; | ||
| 29 | +import static org.mockito.Mockito.verify; | ||
| 30 | +import static org.mockito.Mockito.when; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * test the function of the AlertConfigServiceImpl | ||
| 34 | + * | ||
| 35 | + * 2023/7/12 23:24 | ||
| 36 | + */ | ||
| 37 | + | ||
| 38 | +public class AlertConfigServiceImplTest { | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + private AlertConfigServiceImpl alertConfigService; | ||
| 42 | + | ||
| 43 | + private PrometheusService prometheusService; | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + private AlertConfigMapper baseMapper; | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + public void before() { | ||
| 51 | + MockitoAnnotations.initMocks(this); | ||
| 52 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 53 | + AlertConfig.class); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + public void testSaveAlertConf1() { | ||
| 58 | + AlertConfig alertConfig = new AlertConfig().setId(1L).setAlertIp("127.0.0.1").setAlertPort("80"); | ||
| 59 | + List<AlertConfig> list = new ArrayList<>(); | ||
| 60 | + list.add(alertConfig); | ||
| 61 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 62 | + | ||
| 63 | + List<Long> ids = anyList(); | ||
| 64 | + when(alertConfigService.removeBatchByIds(ids)).thenReturn(true); | ||
| 65 | + | ||
| 66 | + AlertConfig param = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080"); | ||
| 67 | + doNothing().when(prometheusService).updateAlertConfig(param, list); | ||
| 68 | + | ||
| 69 | + alertConfigService.saveAlertConf(param); | ||
| 70 | + | ||
| 71 | + verify(alertConfigService, times(1)).list(); | ||
| 72 | + verify(alertConfigService, times(1)).removeBatchByIds(ids); | ||
| 73 | + verify(alertConfigService, times(1)).saveOrUpdate(any()); | ||
| 74 | + verify(prometheusService, times(1)).updateAlertConfig(param, list); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + public void testSaveAlertConf2() { | ||
| 79 | + List<AlertConfig> list = new ArrayList<>(); | ||
| 80 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 81 | + | ||
| 82 | + AlertConfig param = new AlertConfig().setAlertIp("127.0.0.1").setAlertPort("8080"); | ||
| 83 | + doNothing().when(prometheusService).updateAlertConfig(param, list); | ||
| 84 | + | ||
| 85 | + alertConfigService.saveAlertConf(param); | ||
| 86 | + | ||
| 87 | + verify(alertConfigService, times(1)).list(); | ||
| 88 | + verify(alertConfigService, times(1)).saveOrUpdate(any()); | ||
| 89 | + verify(prometheusService, times(1)).updateAlertConfig(param, list); | ||
| 90 | + } | ||
| 91 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertRecordServiceImplTest.java+402-0
| @@ -0,0 +1,402 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 10 | +import com.nctigba.alert.monitor.constant.CommonConstants; | ||
| 11 | +import com.nctigba.alert.monitor.dto.AlertRecordDto; | ||
| 12 | +import com.nctigba.alert.monitor.dto.AlertRelationDto; | ||
| 13 | +import com.nctigba.alert.monitor.dto.AlertStatisticsDto; | ||
| 14 | +import com.nctigba.alert.monitor.entity.AlertRecord; | ||
| 15 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItem; | ||
| 16 | +import com.nctigba.alert.monitor.entity.NctigbaEnv; | ||
| 17 | +import com.nctigba.alert.monitor.mapper.AlertRecordMapper; | ||
| 18 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleItemMapper; | ||
| 19 | +import com.nctigba.alert.monitor.mapper.NctigbaEnvMapper; | ||
| 20 | +import com.nctigba.alert.monitor.model.AlertRecordReq; | ||
| 21 | +import com.nctigba.alert.monitor.model.AlertStatisticsReq; | ||
| 22 | +import com.nctigba.alert.monitor.service.PrometheusService; | ||
| 23 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 24 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 25 | +import org.junit.Before; | ||
| 26 | +import org.junit.Test; | ||
| 27 | +import org.junit.runner.RunWith; | ||
| 28 | +import org.mockito.InjectMocks; | ||
| 29 | +import org.mockito.Mock; | ||
| 30 | +import org.mockito.MockedStatic; | ||
| 31 | +import org.mockito.MockitoAnnotations; | ||
| 32 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterEntity; | ||
| 33 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsClusterNodeEntity; | ||
| 34 | +import org.opengauss.admin.common.core.domain.entity.ops.OpsHostEntity; | ||
| 35 | +import org.opengauss.admin.common.enums.ops.ClusterRoleEnum; | ||
| 36 | +import org.opengauss.admin.system.plugin.facade.HostFacade; | ||
| 37 | +import org.opengauss.admin.system.service.ops.IOpsClusterNodeService; | ||
| 38 | +import org.opengauss.admin.system.service.ops.IOpsClusterService; | ||
| 39 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 40 | + | ||
| 41 | +import java.time.LocalDateTime; | ||
| 42 | +import java.time.format.DateTimeFormatter; | ||
| 43 | +import java.util.ArrayList; | ||
| 44 | +import java.util.HashMap; | ||
| 45 | +import java.util.List; | ||
| 46 | +import java.util.Map; | ||
| 47 | + | ||
| 48 | +import static org.junit.Assert.assertEquals; | ||
| 49 | +import static org.junit.Assert.assertNull; | ||
| 50 | +import static org.mockito.ArgumentMatchers.any; | ||
| 51 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 52 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 53 | +import static org.mockito.Mockito.mockStatic; | ||
| 54 | +import static org.mockito.Mockito.times; | ||
| 55 | +import static org.mockito.Mockito.verify; | ||
| 56 | +import static org.mockito.Mockito.when; | ||
| 57 | + | ||
| 58 | +/** | ||
| 59 | + * test the AlertRecordService | ||
| 60 | + * | ||
| 61 | + * 2023/7/10 08:41 | ||
| 62 | + */ | ||
| 63 | + | ||
| 64 | +public class AlertRecordServiceImplTest { | ||
| 65 | + | ||
| 66 | + private IOpsClusterService clusterService; | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + private IOpsClusterNodeService clusterNodeService; | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + private HostFacade hostFacade; | ||
| 73 | + | ||
| 74 | + | ||
| 75 | + private AlertTemplateRuleItemMapper templateRuleItemMapper; | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + private PrometheusService prometheusService; | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + private NctigbaEnvMapper envMapper; | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + private AlertRecordServiceImpl alertRecordService; | ||
| 85 | + | ||
| 86 | + | ||
| 87 | + private AlertRecordMapper baseMapper; | ||
| 88 | + | ||
| 89 | + | ||
| 90 | + | ||
| 91 | + public void before() { | ||
| 92 | + MockitoAnnotations.initMocks(this); | ||
| 93 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), AlertRecord.class); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + | ||
| 97 | + public void testGetListPageNull() { | ||
| 98 | + AlertRecordReq alertRecordReq = new AlertRecordReq(); | ||
| 99 | + alertRecordReq.setClusterNodeId("node123"); | ||
| 100 | + Page<AlertRecord> page = new Page<>(1, 10); | ||
| 101 | + when(baseMapper.selectPage(any(Page.class), any())).thenReturn(page); | ||
| 102 | + Page<AlertRecordDto> listPage = alertRecordService.getListPage(alertRecordReq, page); | ||
| 103 | + assertEquals(0, listPage.getTotal()); | ||
| 104 | + assertEquals(page.getRecords(), listPage.getRecords()); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + | ||
| 108 | + public void testGetListPage() { | ||
| 109 | + OpsClusterNodeEntity opsClusterNode = new OpsClusterNodeEntity(); | ||
| 110 | + opsClusterNode.setClusterNodeId("node123"); | ||
| 111 | + opsClusterNode.setClusterId("test"); | ||
| 112 | + opsClusterNode.setHostId("1"); | ||
| 113 | + opsClusterNode.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 114 | + List<OpsClusterNodeEntity> opsClusterNodeEntities = List.of(opsClusterNode); | ||
| 115 | + OpsHostEntity opsHostEntity = new OpsHostEntity(); | ||
| 116 | + opsHostEntity.setHostId("1"); | ||
| 117 | + opsHostEntity.setPublicIp("127.0.0.1"); | ||
| 118 | + List<OpsHostEntity> opsHostEntities = List.of(opsHostEntity); | ||
| 119 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 120 | + opsClusterEntity.setClusterId("test"); | ||
| 121 | + opsClusterEntity.setPort(80); | ||
| 122 | + List<OpsClusterEntity> opsClusterEntities = List.of(opsClusterEntity); | ||
| 123 | + // Mock dependencies | ||
| 124 | + when(clusterNodeService.listByIds(anyList())).thenReturn(opsClusterNodeEntities); | ||
| 125 | + when(hostFacade.listByIds(anyList())).thenReturn(opsHostEntities); | ||
| 126 | + when(clusterService.listByIds(anyList())).thenReturn(opsClusterEntities); | ||
| 127 | + // Mock database query result | ||
| 128 | + List<AlertRecord> records = new ArrayList<>(); | ||
| 129 | + AlertRecord alertRecord = new AlertRecord(); | ||
| 130 | + alertRecord.setId(1L).setClusterNodeId("node123").setAlertStatus(0).setRecordStatus(0).setLevel("warn") | ||
| 131 | + .setStartTime(LocalDateTime.parse("2022-01-01 01:00:00", | ||
| 132 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) | ||
| 133 | + .setEndTime(LocalDateTime.parse("2022-01-01 02:00:00", | ||
| 134 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
| 135 | + records.add(alertRecord); | ||
| 136 | + Page<AlertRecord> page = new Page<>(1, 10); | ||
| 137 | + page.setRecords(records); | ||
| 138 | + page.setTotal(1); | ||
| 139 | + when(baseMapper.selectPage(any(Page.class), any())).thenReturn(page); | ||
| 140 | + // Act | ||
| 141 | + AlertRecordReq alertRecordReq = new AlertRecordReq(); | ||
| 142 | + Page pageReq = new Page(1, 10); | ||
| 143 | + Page<AlertRecordDto> result = alertRecordService.getListPage(alertRecordReq, pageReq); | ||
| 144 | + // Assert | ||
| 145 | + assertEquals(1, result.getTotal()); | ||
| 146 | + AlertRecordDto dto = result.getRecords().get(0); | ||
| 147 | + assertEquals(alertRecord.getId(), dto.getId()); | ||
| 148 | + assertEquals("node123", dto.getClusterNodeId()); | ||
| 149 | + assertEquals("test", dto.getClusterId()); | ||
| 150 | + assertEquals("127.0.0.1:80", dto.getHostIpAndPort()); | ||
| 151 | + assertEquals("MASTER", dto.getNodeRole()); | ||
| 152 | + assertEquals("test/127.0.0.1:80(MASTER)", dto.getClusterNodeName()); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + | ||
| 156 | + public void testGetListPage2() { | ||
| 157 | + OpsClusterNodeEntity opsClusterNode = new OpsClusterNodeEntity(); | ||
| 158 | + opsClusterNode.setClusterNodeId("node123"); | ||
| 159 | + opsClusterNode.setClusterId("test"); | ||
| 160 | + opsClusterNode.setHostId("1"); | ||
| 161 | + opsClusterNode.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 162 | + List<OpsClusterNodeEntity> opsClusterNodeEntities = List.of(opsClusterNode); | ||
| 163 | + OpsHostEntity opsHostEntity = new OpsHostEntity(); | ||
| 164 | + opsHostEntity.setHostId("1"); | ||
| 165 | + opsHostEntity.setPublicIp("127.0.0.1"); | ||
| 166 | + List<OpsHostEntity> opsHostEntities = List.of(opsHostEntity); | ||
| 167 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 168 | + opsClusterEntity.setClusterId("test"); | ||
| 169 | + opsClusterEntity.setPort(80); | ||
| 170 | + List<OpsClusterEntity> opsClusterEntities = List.of(opsClusterEntity); | ||
| 171 | + // Mock dependencies | ||
| 172 | + when(clusterNodeService.listByIds(anyList())).thenReturn(opsClusterNodeEntities); | ||
| 173 | + when(hostFacade.listByIds(anyList())).thenReturn(opsHostEntities); | ||
| 174 | + when(clusterService.listByIds(anyList())).thenReturn(opsClusterEntities); | ||
| 175 | + // Mock database query result | ||
| 176 | + List<AlertRecord> records = new ArrayList<>(); | ||
| 177 | + AlertRecord alertRecord = new AlertRecord(); | ||
| 178 | + alertRecord.setId(1L).setClusterNodeId("node123").setAlertStatus(0).setRecordStatus(0).setLevel("warn") | ||
| 179 | + .setStartTime(LocalDateTime.parse("2022-01-01 01:00:00", | ||
| 180 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) | ||
| 181 | + .setEndTime(LocalDateTime.parse("2022-01-01 02:00:00", | ||
| 182 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
| 183 | + records.add(alertRecord); | ||
| 184 | + Page<AlertRecord> page = new Page<>(1, 10); | ||
| 185 | + page.setRecords(records); | ||
| 186 | + page.setTotal(1); | ||
| 187 | + when(baseMapper.selectPage(any(Page.class), any())).thenReturn(page); | ||
| 188 | + // Act | ||
| 189 | + AlertRecordReq alertRecordReq = new AlertRecordReq(); | ||
| 190 | + alertRecordReq.setStartTime("2022-01-01 01:00:00").setClusterNodeId("node123") | ||
| 191 | + .setEndTime("2022-01-01 02:00:00").setAlertLevel("warn").setAlertStatus("0").setRecordStatus("0"); | ||
| 192 | + Page pageReq = new Page(1, 10); | ||
| 193 | + Page<AlertRecordDto> result = alertRecordService.getListPage(alertRecordReq, pageReq); | ||
| 194 | + // Assert | ||
| 195 | + assertEquals(1, result.getTotal()); | ||
| 196 | + AlertRecordDto dto = result.getRecords().get(0); | ||
| 197 | + assertEquals(alertRecord.getId(), dto.getId()); | ||
| 198 | + assertEquals("test/127.0.0.1:80(MASTER)", dto.getClusterNodeName()); | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + | ||
| 202 | + public void testAlertRecordStatistics() { | ||
| 203 | + Long total = 3L; | ||
| 204 | + when(baseMapper.selectCount(any())).thenReturn(total); | ||
| 205 | + AlertStatisticsReq alertStatisticsReq = new AlertStatisticsReq(); | ||
| 206 | + AlertStatisticsDto alertStatisticsDto = alertRecordService.alertRecordStatistics(alertStatisticsReq); | ||
| 207 | + | ||
| 208 | + Integer totalNum = total.intValue(); | ||
| 209 | + assertEquals(totalNum, alertStatisticsDto.getTotalNum()); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + | ||
| 213 | + public void testAlertRecordStatistics2() { | ||
| 214 | + Long total = 3L; | ||
| 215 | + when(baseMapper.selectCount(any())).thenReturn(total); | ||
| 216 | + | ||
| 217 | + List<Map<String, Object>> mapList = new ArrayList<>(); | ||
| 218 | + Map<String, Object> map1 = new HashMap<>(); | ||
| 219 | + map1.put("alertstatus", CommonConstants.FIRING_STATUS); | ||
| 220 | + map1.put("count", 1L); | ||
| 221 | + mapList.add(map1); | ||
| 222 | + Map<String, Object> map2 = new HashMap<>(); | ||
| 223 | + map2.put("alertstatus", CommonConstants.RECOVER_STATUS); | ||
| 224 | + map2.put("count", 1L); | ||
| 225 | + mapList.add(map2); | ||
| 226 | + Map<String, Object> map3 = new HashMap<>(); | ||
| 227 | + map3.put("recordstatus", CommonConstants.UNREAD_STATUS); | ||
| 228 | + map3.put("count", 1L); | ||
| 229 | + mapList.add(map3); | ||
| 230 | + Map<String, Object> map4 = new HashMap<>(); | ||
| 231 | + map4.put("recordstatus", CommonConstants.READ_STATUS); | ||
| 232 | + map4.put("count", 1L); | ||
| 233 | + mapList.add(map4); | ||
| 234 | + Map<String, Object> map5 = new HashMap<>(); | ||
| 235 | + map5.put("level", CommonConstants.SERIOUS); | ||
| 236 | + map5.put("count", 1L); | ||
| 237 | + mapList.add(map5); | ||
| 238 | + Map<String, Object> map6 = new HashMap<>(); | ||
| 239 | + map6.put("level", CommonConstants.WARN); | ||
| 240 | + map6.put("count", 1L); | ||
| 241 | + mapList.add(map6); | ||
| 242 | + Map<String, Object> map7 = new HashMap<>(); | ||
| 243 | + map7.put("level", CommonConstants.INFO); | ||
| 244 | + map7.put("count", 1L); | ||
| 245 | + mapList.add(map7); | ||
| 246 | + | ||
| 247 | + when(baseMapper.selectMaps(any())).thenReturn(mapList); | ||
| 248 | + | ||
| 249 | + AlertStatisticsReq alertStatisticsReq = new AlertStatisticsReq(); | ||
| 250 | + alertStatisticsReq.setStartTime("2022-01-01 01:00:00").setClusterNodeId("node123") | ||
| 251 | + .setEndTime("2022-01-01 02:00:00"); | ||
| 252 | + AlertStatisticsDto alertStatisticsDto = alertRecordService.alertRecordStatistics(alertStatisticsReq); | ||
| 253 | + | ||
| 254 | + Integer totalNum = total.intValue(); | ||
| 255 | + assertEquals(totalNum, alertStatisticsDto.getTotalNum()); | ||
| 256 | + verify(baseMapper, times(1)).selectCount(any()); | ||
| 257 | + verify(baseMapper, times(3)).selectMaps(any()); | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + | ||
| 261 | + public void testMarkAsRead() { | ||
| 262 | + String ids = "1,2,3"; | ||
| 263 | + String result = alertRecordService.markAsRead(ids); | ||
| 264 | + assertEquals("success", result); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + | ||
| 268 | + public void testMarkAsUnRead() { | ||
| 269 | + String ids = "1,2,3"; | ||
| 270 | + String result = alertRecordService.markAsUnread(ids); | ||
| 271 | + assertEquals("success", result); | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + | ||
| 275 | + public void testGetByIdNull() { | ||
| 276 | + AlertRecord alertRecord = null; | ||
| 277 | + when(baseMapper.selectById(any())).thenReturn(alertRecord); | ||
| 278 | + AlertRecordDto alertRecordDto = alertRecordService.getById(any()); | ||
| 279 | + verify(baseMapper, times(1)).selectById(any()); | ||
| 280 | + assertNull(alertRecordDto.getId()); | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + | ||
| 284 | + public void testGetById() { | ||
| 285 | + AlertRecord alertRecord = new AlertRecord(); | ||
| 286 | + alertRecord.setId(1L).setClusterNodeId("node123").setAlertStatus(0).setRecordStatus(0).setLevel("warn") | ||
| 287 | + .setStartTime(LocalDateTime.parse("2022-01-01 01:00:00", | ||
| 288 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) | ||
| 289 | + .setEndTime(LocalDateTime.parse("2022-01-01 02:00:00", | ||
| 290 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
| 291 | + when(baseMapper.selectById(any())).thenReturn(alertRecord); | ||
| 292 | + | ||
| 293 | + OpsClusterNodeEntity opsClusterNode = new OpsClusterNodeEntity(); | ||
| 294 | + opsClusterNode.setClusterNodeId("node123"); | ||
| 295 | + opsClusterNode.setClusterId("test"); | ||
| 296 | + opsClusterNode.setHostId("1"); | ||
| 297 | + opsClusterNode.setClusterRole(ClusterRoleEnum.MASTER); | ||
| 298 | + when(clusterNodeService.getById(alertRecord.getClusterNodeId())).thenReturn(opsClusterNode); | ||
| 299 | + | ||
| 300 | + OpsHostEntity opsHostEntity = new OpsHostEntity(); | ||
| 301 | + opsHostEntity.setHostId("1"); | ||
| 302 | + opsHostEntity.setPublicIp("127.0.0.1"); | ||
| 303 | + when(hostFacade.getById(opsClusterNode.getHostId())).thenReturn(opsHostEntity); | ||
| 304 | + | ||
| 305 | + OpsClusterEntity opsClusterEntity = new OpsClusterEntity(); | ||
| 306 | + opsClusterEntity.setClusterId("test"); | ||
| 307 | + opsClusterEntity.setPort(80); | ||
| 308 | + when(clusterService.getById(opsClusterNode.getClusterId())).thenReturn(opsClusterEntity); | ||
| 309 | + | ||
| 310 | + AlertRecordDto dto = alertRecordService.getById(any()); | ||
| 311 | + | ||
| 312 | + verify(baseMapper, times(1)).selectById(any()); | ||
| 313 | + verify(clusterNodeService, times(1)).getById(alertRecord.getClusterNodeId()); | ||
| 314 | + verify(hostFacade, times(1)).getById(opsClusterNode.getHostId()); | ||
| 315 | + verify(clusterService, times(1)).getById(opsClusterNode.getClusterId()); | ||
| 316 | + assertEquals(alertRecord.getId(), dto.getId()); | ||
| 317 | + assertEquals("node123", dto.getClusterNodeId()); | ||
| 318 | + assertEquals("test", dto.getClusterId()); | ||
| 319 | + assertEquals("127.0.0.1:80", dto.getHostIpAndPort()); | ||
| 320 | + assertEquals("MASTER", dto.getNodeRole()); | ||
| 321 | + assertEquals("test/127.0.0.1:80(MASTER)", dto.getClusterNodeName()); | ||
| 322 | + } | ||
| 323 | + | ||
| 324 | + | ||
| 325 | + public void testGetById2() { | ||
| 326 | + AlertRecord alertRecord = new AlertRecord(); | ||
| 327 | + alertRecord.setId(1L).setClusterNodeId("node123").setAlertStatus(0).setRecordStatus(0).setLevel("warn") | ||
| 328 | + .setStartTime(LocalDateTime.parse("2022-01-01 01:00:00", | ||
| 329 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) | ||
| 330 | + .setEndTime(LocalDateTime.parse("2022-01-01 02:00:00", | ||
| 331 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
| 332 | + when(baseMapper.selectById(any())).thenReturn(alertRecord); | ||
| 333 | + | ||
| 334 | + OpsClusterNodeEntity opsClusterNode = new OpsClusterNodeEntity(); | ||
| 335 | + when(clusterNodeService.getById(alertRecord.getClusterNodeId())).thenReturn(opsClusterNode); | ||
| 336 | + | ||
| 337 | + AlertRecordDto dto = alertRecordService.getById(any()); | ||
| 338 | + | ||
| 339 | + verify(baseMapper, times(1)).selectById(any()); | ||
| 340 | + verify(clusterNodeService, times(1)).getById(alertRecord.getClusterNodeId()); | ||
| 341 | + assertEquals(alertRecord.getId(), dto.getId()); | ||
| 342 | + assertEquals("node123", dto.getClusterNodeId()); | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + | ||
| 346 | + public void testGetById3() { | ||
| 347 | + AlertRecord alertRecord = new AlertRecord(); | ||
| 348 | + alertRecord.setId(1L).setClusterNodeId("node123").setAlertStatus(0).setRecordStatus(0).setLevel("warn") | ||
| 349 | + .setStartTime(LocalDateTime.parse("2022-01-01 01:00:00", | ||
| 350 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) | ||
| 351 | + .setEndTime(LocalDateTime.parse("2022-01-01 02:00:00", | ||
| 352 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
| 353 | + when(baseMapper.selectById(any())).thenReturn(alertRecord); | ||
| 354 | + | ||
| 355 | + OpsClusterNodeEntity opsClusterNode = null; | ||
| 356 | + when(clusterNodeService.getById(alertRecord.getClusterNodeId())).thenReturn(opsClusterNode); | ||
| 357 | + | ||
| 358 | + AlertRecordDto dto = alertRecordService.getById(any()); | ||
| 359 | + | ||
| 360 | + verify(baseMapper, times(1)).selectById(any()); | ||
| 361 | + verify(clusterNodeService, times(1)).getById(alertRecord.getClusterNodeId()); | ||
| 362 | + assertEquals(alertRecord.getId(), dto.getId()); | ||
| 363 | + assertEquals("node123", dto.getClusterNodeId()); | ||
| 364 | + } | ||
| 365 | + | ||
| 366 | + | ||
| 367 | + public void testGetRelationData() { | ||
| 368 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 369 | + AlertRecord alertRecord = new AlertRecord(); | ||
| 370 | + alertRecord.setId(1L).setClusterNodeId("node123").setAlertStatus(0).setRecordStatus(0).setLevel("warn") | ||
| 371 | + .setStartTime(LocalDateTime.parse("2022-01-01 01:00:00", | ||
| 372 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) | ||
| 373 | + .setEndTime(LocalDateTime.parse("2022-01-01 02:00:00", | ||
| 374 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).setTemplateId(1L); | ||
| 375 | + when(baseMapper.selectById(any())).thenReturn(alertRecord); | ||
| 376 | + AlertTemplateRuleItem ruleItem = new AlertTemplateRuleItem(); | ||
| 377 | + ruleItem.setRuleExpName("ruleExpName").setUnit("unit").setLimitValue("10").setRuleExp("ruleExp"); | ||
| 378 | + List<AlertTemplateRuleItem> ruleItemList = List.of(ruleItem); | ||
| 379 | + when(templateRuleItemMapper.selectList(any())).thenReturn(ruleItemList); | ||
| 380 | + NctigbaEnv promEnv = new NctigbaEnv(); | ||
| 381 | + promEnv.setHostid("1").setPort(9090); | ||
| 382 | + when(envMapper.selectOne(any())).thenReturn(promEnv); | ||
| 383 | + OpsHostEntity opsHostEntity = new OpsHostEntity(); | ||
| 384 | + opsHostEntity.setHostId("1"); | ||
| 385 | + opsHostEntity.setPublicIp("127.0.0.1"); | ||
| 386 | + when(hostFacade.getById(promEnv.getHostid())).thenReturn(opsHostEntity); | ||
| 387 | + | ||
| 388 | + String name = "ruleExpName"; | ||
| 389 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn(name); | ||
| 390 | + | ||
| 391 | + Number[][] datas = new Number[0][0]; | ||
| 392 | + when(prometheusService.queryRange(any(), any(), any(), any(), any())).thenReturn(datas); | ||
| 393 | + List<AlertRelationDto> relationDataList = alertRecordService.getRelationData(any()); | ||
| 394 | + verify(baseMapper, times(1)).selectById(any()); | ||
| 395 | + verify(templateRuleItemMapper, times(1)).selectList(any()); | ||
| 396 | + verify(envMapper, times(1)).selectOne(any()); | ||
| 397 | + verify(hostFacade, times(1)).getById(any()); | ||
| 398 | + verify(prometheusService, times(1)).queryRange(any(), any(), any(), any(), any()); | ||
| 399 | + assertEquals(1, relationDataList.size()); | ||
| 400 | + } | ||
| 401 | + } | ||
| 402 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertRuleServiceImplTest.java+226-0
| @@ -0,0 +1,226 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 10 | +import com.nctigba.alert.monitor.config.properties.AlertProperty; | ||
| 11 | +import com.nctigba.alert.monitor.config.properties.RuleItemProperty; | ||
| 12 | +import com.nctigba.alert.monitor.dto.AlertRuleDto; | ||
| 13 | +import com.nctigba.alert.monitor.dto.RuleItemPropertyDto; | ||
| 14 | +import com.nctigba.alert.monitor.entity.AlertRule; | ||
| 15 | +import com.nctigba.alert.monitor.entity.AlertRuleItem; | ||
| 16 | +import com.nctigba.alert.monitor.entity.AlertRuleItemParam; | ||
| 17 | +import com.nctigba.alert.monitor.mapper.AlertRuleItemMapper; | ||
| 18 | +import com.nctigba.alert.monitor.mapper.AlertRuleItemParamMapper; | ||
| 19 | +import com.nctigba.alert.monitor.mapper.AlertRuleMapper; | ||
| 20 | +import com.nctigba.alert.monitor.model.RuleReq; | ||
| 21 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 22 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 23 | +import org.junit.Before; | ||
| 24 | +import org.junit.Test; | ||
| 25 | +import org.junit.runner.RunWith; | ||
| 26 | +import org.mockito.InjectMocks; | ||
| 27 | +import org.mockito.Mock; | ||
| 28 | +import org.mockito.MockedStatic; | ||
| 29 | +import org.mockito.MockitoAnnotations; | ||
| 30 | +import org.mockito.Spy; | ||
| 31 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 32 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 33 | + | ||
| 34 | +import java.util.ArrayList; | ||
| 35 | +import java.util.List; | ||
| 36 | + | ||
| 37 | +import static org.junit.Assert.assertEquals; | ||
| 38 | +import static org.mockito.ArgumentMatchers.any; | ||
| 39 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 40 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 41 | +import static org.mockito.Mockito.mockStatic; | ||
| 42 | +import static org.mockito.Mockito.times; | ||
| 43 | +import static org.mockito.Mockito.verify; | ||
| 44 | +import static org.mockito.Mockito.when; | ||
| 45 | + | ||
| 46 | +/** | ||
| 47 | + * test the function of the AlertRuleServiceImpl | ||
| 48 | + * | ||
| 49 | + * 2023/7/13 09:21 | ||
| 50 | + */ | ||
| 51 | + | ||
| 52 | +public class AlertRuleServiceImplTest { | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + private AlertRuleServiceImpl alertRuleService; | ||
| 56 | + | ||
| 57 | + private AlertProperty alertProperty; | ||
| 58 | + | ||
| 59 | + private AlertRuleItemMapper alertRuleItemMapper; | ||
| 60 | + | ||
| 61 | + private AlertRuleItemParamMapper ruleItemParamMapper; | ||
| 62 | + | ||
| 63 | + private AlertRuleMapper baseMapper; | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + public void before() { | ||
| 68 | + MockitoAnnotations.initMocks(this); | ||
| 69 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 70 | + AlertRule.class); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + | ||
| 74 | + public void testGetRulePageNull() { | ||
| 75 | + Page page = new Page(1, 10); | ||
| 76 | + Page<AlertRule> alertRulePage = new Page<>(1, 10); | ||
| 77 | + when(baseMapper.selectPage(eq(page), any())).thenReturn(alertRulePage); | ||
| 78 | + | ||
| 79 | + RuleReq ruleReq = new RuleReq(); | ||
| 80 | + Page<AlertRuleDto> ruleDtoPage = alertRuleService.getRulePage(ruleReq, page); | ||
| 81 | + verify(baseMapper, times(1)).selectPage(eq(page), any()); | ||
| 82 | + assertEquals(0L, ruleDtoPage.getTotal()); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + | ||
| 86 | + public void testGetRulePage() { | ||
| 87 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 88 | + Page page = new Page(1, 10); | ||
| 89 | + AlertRule alertRule = new AlertRule().setId(1L); | ||
| 90 | + List<AlertRule> alertRuleList = new ArrayList<>(); | ||
| 91 | + alertRuleList.add(alertRule); | ||
| 92 | + Page<AlertRule> alertRulePage = new Page<>(1, 10); | ||
| 93 | + alertRulePage.setRecords(alertRuleList).setTotal(1); | ||
| 94 | + when(baseMapper.selectPage(eq(page), any())).thenReturn(alertRulePage); | ||
| 95 | + | ||
| 96 | + AlertRuleItem ruleItem1 = new AlertRuleItem().setId(1L).setRuleId(1L).setAction("normal") | ||
| 97 | + .setOperate(">=").setLimitValue("50").setRuleMark("A").setRuleExpName("cpu").setUnit("%"); | ||
| 98 | + AlertRuleItem ruleItem2 = new AlertRuleItem().setId(2L).setRuleId(1L).setAction( | ||
| 99 | + "increase").setOperate(">=").setLimitValue("50").setRuleMark("B").setRuleExpName("cpu").setUnit(""); | ||
| 100 | + AlertRuleItem ruleItem3 = new AlertRuleItem().setId(3L).setRuleId(1L).setAction( | ||
| 101 | + "decrease").setOperate(">=").setLimitValue("50").setRuleMark("C").setRuleExpName("cpu").setUnit(""); | ||
| 102 | + AlertRuleItem ruleItem4 = new AlertRuleItem().setId(4L).setRuleId(1L).setAction( | ||
| 103 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("D").setRuleExpName("cpu").setUnit(""); | ||
| 104 | + List<AlertRuleItem> alertRuleItems = new ArrayList<>(); | ||
| 105 | + alertRuleItems.add(ruleItem1); | ||
| 106 | + alertRuleItems.add(ruleItem2); | ||
| 107 | + alertRuleItems.add(ruleItem3); | ||
| 108 | + alertRuleItems.add(ruleItem4); | ||
| 109 | + when(alertRuleItemMapper.selectList(any())).thenReturn(alertRuleItems); | ||
| 110 | + | ||
| 111 | + List<AlertRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 112 | + AlertRuleItemParam ruleItemParam1 = | ||
| 113 | + new AlertRuleItemParam().setId(1L).setItemId(1L).setParamValue("val1"); | ||
| 114 | + AlertRuleItemParam ruleItemParam2 = | ||
| 115 | + new AlertRuleItemParam().setId(2L).setItemId(2L).setParamValue("val2"); | ||
| 116 | + itemParamList.add(ruleItemParam1); | ||
| 117 | + itemParamList.add(ruleItemParam2); | ||
| 118 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 119 | + | ||
| 120 | + RuleReq ruleReq = new RuleReq(); | ||
| 121 | + Page<AlertRuleDto> ruleDtoPage = alertRuleService.getRulePage(ruleReq, page); | ||
| 122 | + verify(baseMapper, times(1)).selectPage(eq(page), any()); | ||
| 123 | + verify(alertRuleItemMapper, times(1)).selectList(any()); | ||
| 124 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 125 | + assertEquals(1L, ruleDtoPage.getTotal()); | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + | ||
| 130 | + public void testGetRuleByIdThrowException() { | ||
| 131 | + AlertRule alertRule = null; | ||
| 132 | + when(baseMapper.selectById(anyLong())).thenReturn(alertRule); | ||
| 133 | + alertRuleService.getRuleById(anyLong()); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + | ||
| 137 | + public void testGetRuleById() { | ||
| 138 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 139 | + AlertRule alertRule = new AlertRule().setId(1L).setRuleName("ruleName"); | ||
| 140 | + when(baseMapper.selectById(anyLong())).thenReturn(alertRule); | ||
| 141 | + | ||
| 142 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn("ruleName"); | ||
| 143 | + | ||
| 144 | + List<AlertRuleItem> alertRuleItems = new ArrayList<>(); | ||
| 145 | + AlertRuleItem alertRuleItem = new AlertRuleItem().setId(1L).setRuleId(1L); | ||
| 146 | + alertRuleItems.add(alertRuleItem); | ||
| 147 | + when(alertRuleItemMapper.selectList(any())).thenReturn(alertRuleItems); | ||
| 148 | + | ||
| 149 | + List<AlertRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 150 | + AlertRuleItemParam ruleItemParam1 = | ||
| 151 | + new AlertRuleItemParam().setId(1L).setItemId(1L).setParamValue("val1"); | ||
| 152 | + itemParamList.add(ruleItemParam1); | ||
| 153 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 154 | + | ||
| 155 | + AlertRule result = alertRuleService.getRuleById(anyLong()); | ||
| 156 | + | ||
| 157 | + verify(baseMapper, times(1)).selectById(anyLong()); | ||
| 158 | + verify(alertRuleItemMapper, times(1)).selectList(any()); | ||
| 159 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 160 | + assertEquals(alertRule.getId(), result.getId()); | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + | ||
| 165 | + public void testGetRuleItemProperties() { | ||
| 166 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 167 | + List<RuleItemProperty> ruleItems = new ArrayList<>(); | ||
| 168 | + RuleItemProperty ruleItemProperty = new RuleItemProperty(); | ||
| 169 | + ruleItemProperty.setName("name"); | ||
| 170 | + ruleItems.add(ruleItemProperty); | ||
| 171 | + when(alertProperty.getRuleItems()).thenReturn(ruleItems); | ||
| 172 | + | ||
| 173 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn("name"); | ||
| 174 | + | ||
| 175 | + List<RuleItemPropertyDto> ruleItemProperties = alertRuleService.getRuleItemProperties(); | ||
| 176 | + | ||
| 177 | + verify(alertProperty).getRuleItems(); | ||
| 178 | + assertEquals(ruleItems.size(), ruleItemProperties.size()); | ||
| 179 | + assertEquals("name", ruleItemProperties.get(0).getI18nName()); | ||
| 180 | + } | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + | ||
| 184 | + public void testGetRuleList() { | ||
| 185 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 186 | + List<AlertRule> alertRules = new ArrayList<>(); | ||
| 187 | + AlertRule alertRule = new AlertRule().setId(1L); | ||
| 188 | + alertRules.add(alertRule); | ||
| 189 | + when(baseMapper.selectList(any())).thenReturn(alertRules); | ||
| 190 | + | ||
| 191 | + AlertRuleItem ruleItem1 = new AlertRuleItem().setId(1L).setRuleId(1L).setAction("normal") | ||
| 192 | + .setOperate(">=").setLimitValue("50").setRuleMark("A").setRuleExpName("cpu").setUnit("%"); | ||
| 193 | + AlertRuleItem ruleItem2 = new AlertRuleItem().setId(2L).setRuleId(1L).setAction( | ||
| 194 | + "increase").setOperate(">=").setLimitValue("50").setRuleMark("B").setRuleExpName("cpu").setUnit(""); | ||
| 195 | + AlertRuleItem ruleItem3 = new AlertRuleItem().setId(3L).setRuleId(1L).setAction( | ||
| 196 | + "decrease").setOperate(">=").setLimitValue("50").setRuleMark("C").setRuleExpName("cpu").setUnit(""); | ||
| 197 | + AlertRuleItem ruleItem4 = new AlertRuleItem().setId(4L).setRuleId(1L).setAction( | ||
| 198 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("D").setRuleExpName("cpu").setUnit(""); | ||
| 199 | + List<AlertRuleItem> alertRuleItems = new ArrayList<>(); | ||
| 200 | + alertRuleItems.add(ruleItem1); | ||
| 201 | + alertRuleItems.add(ruleItem2); | ||
| 202 | + alertRuleItems.add(ruleItem3); | ||
| 203 | + alertRuleItems.add(ruleItem4); | ||
| 204 | + when(alertRuleItemMapper.selectList(any())).thenReturn(alertRuleItems); | ||
| 205 | + | ||
| 206 | + List<AlertRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 207 | + AlertRuleItemParam ruleItemParam1 = | ||
| 208 | + new AlertRuleItemParam().setId(1L).setItemId(1L).setParamValue("val1"); | ||
| 209 | + AlertRuleItemParam ruleItemParam2 = | ||
| 210 | + new AlertRuleItemParam().setId(2L).setItemId(2L).setParamValue("val2"); | ||
| 211 | + itemParamList.add(ruleItemParam1); | ||
| 212 | + itemParamList.add(ruleItemParam2); | ||
| 213 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 214 | + | ||
| 215 | + String name = "ruleExpName"; | ||
| 216 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn(name); | ||
| 217 | + | ||
| 218 | + List<AlertRuleDto> ruleList = alertRuleService.getRuleList(); | ||
| 219 | + | ||
| 220 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 221 | + verify(alertRuleItemMapper, times(1)).selectList(any()); | ||
| 222 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 223 | + assertEquals(alertRules.size(), ruleList.size()); | ||
| 224 | + } | ||
| 225 | + } | ||
| 226 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertTemplateRuleItemServiceImplTest.java+46-0
| @@ -0,0 +1,46 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItem; | ||
| 10 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 11 | +import org.junit.Before; | ||
| 12 | +import org.junit.Test; | ||
| 13 | +import org.junit.runner.RunWith; | ||
| 14 | +import org.mockito.InjectMocks; | ||
| 15 | +import org.mockito.MockitoAnnotations; | ||
| 16 | +import org.mockito.Spy; | ||
| 17 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 18 | + | ||
| 19 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 20 | +import static org.mockito.Mockito.times; | ||
| 21 | +import static org.mockito.Mockito.verify; | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * test the function of the AlertTemplateRuleItemServiceImpl | ||
| 25 | + * | ||
| 26 | + * 2023/7/13 15:03 | ||
| 27 | + */ | ||
| 28 | + | ||
| 29 | +public class AlertTemplateRuleItemServiceImplTest { | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + private AlertTemplateRuleItemServiceImpl alertTemplateRuleItemService; | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + public void before() { | ||
| 36 | + MockitoAnnotations.initMocks(this); | ||
| 37 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 38 | + AlertTemplateRuleItem.class); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + public void testSaveOrUpdateList() { | ||
| 43 | + alertTemplateRuleItemService.saveOrUpdateList(anyList()); | ||
| 44 | + verify(alertTemplateRuleItemService, times(1)).saveOrUpdateBatch(anyList()); | ||
| 45 | + } | ||
| 46 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertTemplateRuleServiceImplTest.java+190-0
| @@ -0,0 +1,190 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.nctigba.alert.monitor.dto.AlertTemplateRuleDto; | ||
| 10 | +import com.nctigba.alert.monitor.entity.AlertTemplateRule; | ||
| 11 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItem; | ||
| 12 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItemParam; | ||
| 13 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleItemMapper; | ||
| 14 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleItemParamMapper; | ||
| 15 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleMapper; | ||
| 16 | +import com.nctigba.alert.monitor.service.AlertTemplateRuleItemParamService; | ||
| 17 | +import com.nctigba.alert.monitor.service.AlertTemplateRuleItemService; | ||
| 18 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 19 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 20 | +import org.junit.Before; | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.junit.runner.RunWith; | ||
| 23 | +import org.mockito.InjectMocks; | ||
| 24 | +import org.mockito.Mock; | ||
| 25 | +import org.mockito.MockedStatic; | ||
| 26 | +import org.mockito.MockitoAnnotations; | ||
| 27 | +import org.mockito.Spy; | ||
| 28 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 29 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 30 | + | ||
| 31 | +import java.util.ArrayList; | ||
| 32 | +import java.util.List; | ||
| 33 | + | ||
| 34 | +import static org.junit.Assert.assertEquals; | ||
| 35 | +import static org.junit.Assert.assertNull; | ||
| 36 | +import static org.mockito.ArgumentMatchers.any; | ||
| 37 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 38 | +import static org.mockito.Mockito.mockStatic; | ||
| 39 | +import static org.mockito.Mockito.times; | ||
| 40 | +import static org.mockito.Mockito.verify; | ||
| 41 | +import static org.mockito.Mockito.when; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * test the function of the AlertTemplateRuleServiceImpl | ||
| 45 | + * | ||
| 46 | + * 2023/7/13 16:40 | ||
| 47 | + */ | ||
| 48 | + | ||
| 49 | +public class AlertTemplateRuleServiceImplTest { | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + private AlertTemplateRuleServiceImpl alertTemplateRuleService; | ||
| 53 | + | ||
| 54 | + private AlertTemplateRuleItemMapper alertTemplateRuleItemMapper; | ||
| 55 | + | ||
| 56 | + private AlertTemplateRuleItemService templateRuleItemService; | ||
| 57 | + | ||
| 58 | + private AlertTemplateRuleItemParamMapper ruleItemParamMapper; | ||
| 59 | + | ||
| 60 | + private AlertTemplateRuleItemParamService ruleItemParamService; | ||
| 61 | + | ||
| 62 | + private AlertTemplateRuleMapper baseMapper; | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + public void before() { | ||
| 66 | + MockitoAnnotations.initMocks(this); | ||
| 67 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 68 | + AlertTemplateRule.class); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + public void testGetTemplateRuleThrowException() { | ||
| 73 | + AlertTemplateRule alertTemplateRule = null; | ||
| 74 | + when(baseMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 75 | + alertTemplateRuleService.getTemplateRule(anyLong()); | ||
| 76 | + verify(baseMapper, times(1)).selectById(anyLong()); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + | ||
| 80 | + public void testGetTemplateRule() { | ||
| 81 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setTemplateId(1L).setId(1L).setRuleId(1L); | ||
| 82 | + when(baseMapper.selectById(anyLong())).thenReturn(alertTemplateRule); | ||
| 83 | + | ||
| 84 | + List<AlertTemplateRuleItem> alertTemplateRuleItems = new ArrayList<>(); | ||
| 85 | + AlertTemplateRuleItem ruleItem = new AlertTemplateRuleItem().setId(1L).setTemplateRuleId(1L); | ||
| 86 | + alertTemplateRuleItems.add(ruleItem); | ||
| 87 | + when(alertTemplateRuleItemMapper.selectList(any())).thenReturn(alertTemplateRuleItems); | ||
| 88 | + | ||
| 89 | + List<AlertTemplateRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 90 | + AlertTemplateRuleItemParam ruleItemParam = new AlertTemplateRuleItemParam(); | ||
| 91 | + ruleItemParam.setId(1L).setItemId(1L); | ||
| 92 | + itemParamList.add(ruleItemParam); | ||
| 93 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 94 | + | ||
| 95 | + AlertTemplateRule templateRule = alertTemplateRuleService.getTemplateRule(anyLong()); | ||
| 96 | + | ||
| 97 | + verify(baseMapper, times(1)).selectById(anyLong()); | ||
| 98 | + verify(alertTemplateRuleItemMapper, times(1)).selectList(any()); | ||
| 99 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 100 | + assertEquals(alertTemplateRule.getId(), templateRule.getId()); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + | ||
| 104 | + public void testSaveTemplateRuleWithoutRuleItem() { | ||
| 105 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule(); | ||
| 106 | + AlertTemplateRuleDto alertTemplateRuleDto = alertTemplateRuleService.saveTemplateRule(alertTemplateRule); | ||
| 107 | + verify(alertTemplateRuleService, times(1)).saveOrUpdate(alertTemplateRule); | ||
| 108 | + assertNull(alertTemplateRuleDto.getAlertRuleItemList()); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + | ||
| 112 | + public void testSaveTemplateRule() { | ||
| 113 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 114 | + AlertTemplateRuleItem ruleItem = new AlertTemplateRuleItem().setId(1L).setTemplateRuleId(1L) | ||
| 115 | + .setAction("normal").setOperate(">=").setLimitValue("50").setRuleMark("A").setRuleExpName("cpu") | ||
| 116 | + .setUnit("%"); | ||
| 117 | + AlertTemplateRuleItem ruleItem2 = new AlertTemplateRuleItem().setTemplateRuleId(1L).setAction( | ||
| 118 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("B").setRuleExpName("cpu").setUnit(""); | ||
| 119 | + AlertTemplateRuleItem ruleItem3 = new AlertTemplateRuleItem().setId(3L).setTemplateRuleId(1L).setAction( | ||
| 120 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("C").setRuleExpName("cpu").setUnit(""); | ||
| 121 | + List<AlertTemplateRuleItem> templateRuleItemList = new ArrayList<>(); | ||
| 122 | + templateRuleItemList.add(ruleItem); | ||
| 123 | + templateRuleItemList.add(ruleItem2); | ||
| 124 | + templateRuleItemList.add(ruleItem3); | ||
| 125 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setId(1L); | ||
| 126 | + alertTemplateRule.setAlertRuleItemList(templateRuleItemList); | ||
| 127 | + List<AlertTemplateRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 128 | + AlertTemplateRuleItemParam ruleItemParam = new AlertTemplateRuleItemParam().setItemId(1L).setId(1L); | ||
| 129 | + itemParamList.add(ruleItemParam); | ||
| 130 | + ruleItem.setItemParamList(itemParamList); | ||
| 131 | + ruleItem2.setItemParamList(new ArrayList<>()); | ||
| 132 | + AlertTemplateRuleItemParam ruleItemParam3 = new AlertTemplateRuleItemParam().setItemId(3L); | ||
| 133 | + List<AlertTemplateRuleItemParam> itemParamList3 = new ArrayList<>(); | ||
| 134 | + itemParamList3.add(ruleItemParam3); | ||
| 135 | + ruleItem3.setItemParamList(itemParamList3); | ||
| 136 | + | ||
| 137 | + when(templateRuleItemService.saveOrUpdate(ruleItem)).thenReturn(true); | ||
| 138 | + when(templateRuleItemService.saveOrUpdate(ruleItem2)).thenReturn(true); | ||
| 139 | + | ||
| 140 | + when(ruleItemParamService.saveOrUpdateBatch(itemParamList)).thenReturn(true); | ||
| 141 | + | ||
| 142 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn("info"); | ||
| 143 | + | ||
| 144 | + AlertTemplateRuleDto alertTemplateRuleDto = alertTemplateRuleService.saveTemplateRule(alertTemplateRule); | ||
| 145 | + | ||
| 146 | + verify(alertTemplateRuleService, times(1)).saveOrUpdate(alertTemplateRule); | ||
| 147 | + verify(templateRuleItemService, times(3)).saveOrUpdate(any()); | ||
| 148 | + verify(ruleItemParamService, times(1)).saveOrUpdateBatch(itemParamList); | ||
| 149 | + assertEquals(3, alertTemplateRuleDto.getAlertRuleItemList().size()); | ||
| 150 | + assertEquals(1, alertTemplateRuleDto.getAlertRuleItemList().get(0).getItemParamList().size()); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + | ||
| 155 | + public void testGetDtoListByTemplateId() { | ||
| 156 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 157 | + List<AlertTemplateRule> alertRules = new ArrayList<>(); | ||
| 158 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setId(1L); | ||
| 159 | + alertRules.add(alertTemplateRule); | ||
| 160 | + when(baseMapper.selectList(any())).thenReturn(alertRules); | ||
| 161 | + | ||
| 162 | + List<AlertTemplateRuleItem> alertRuleItems = new ArrayList<>(); | ||
| 163 | + AlertTemplateRuleItem ruleItem1 = new AlertTemplateRuleItem().setId(1L).setTemplateRuleId(1L).setAction( | ||
| 164 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("A").setRuleExpName("cpu").setUnit("%"); | ||
| 165 | + AlertTemplateRuleItem ruleItem2 = new AlertTemplateRuleItem().setId(2L).setTemplateRuleId(1L).setAction( | ||
| 166 | + "increase").setOperate(">=").setLimitValue("50").setRuleMark("B").setRuleExpName("cpu").setUnit(""); | ||
| 167 | + AlertTemplateRuleItem ruleItem3 = new AlertTemplateRuleItem().setId(3L).setTemplateRuleId(1L).setAction( | ||
| 168 | + "decrease").setOperate(">=").setLimitValue("50").setRuleMark("C").setRuleExpName("cpu").setUnit(""); | ||
| 169 | + AlertTemplateRuleItem ruleItem4 = new AlertTemplateRuleItem().setId(4L).setTemplateRuleId(1L).setAction( | ||
| 170 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("D").setRuleExpName("cpu").setUnit(""); | ||
| 171 | + alertRuleItems.add(ruleItem1); | ||
| 172 | + alertRuleItems.add(ruleItem2); | ||
| 173 | + alertRuleItems.add(ruleItem3); | ||
| 174 | + alertRuleItems.add(ruleItem4); | ||
| 175 | + when(alertTemplateRuleItemMapper.selectList(any())).thenReturn(alertRuleItems); | ||
| 176 | + | ||
| 177 | + List<AlertTemplateRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 178 | + AlertTemplateRuleItemParam ruleItemParam = new AlertTemplateRuleItemParam().setItemId(1L).setId(1L); | ||
| 179 | + itemParamList.add(ruleItemParam); | ||
| 180 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 181 | + | ||
| 182 | + List<AlertTemplateRuleDto> dtoList = alertTemplateRuleService.getDtoListByTemplateId(anyLong()); | ||
| 183 | + | ||
| 184 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 185 | + verify(alertTemplateRuleItemMapper, times(1)).selectList(any()); | ||
| 186 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 187 | + assertEquals(1, dtoList.size()); | ||
| 188 | + } | ||
| 189 | + } | ||
| 190 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/AlertTemplateServiceImplTest.java+424-0
| @@ -0,0 +1,424 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 10 | +import com.nctigba.alert.monitor.dto.AlertTemplateDto; | ||
| 11 | +import com.nctigba.alert.monitor.dto.AlertTemplateRuleDto; | ||
| 12 | +import com.nctigba.alert.monitor.entity.AlertClusterNodeConf; | ||
| 13 | +import com.nctigba.alert.monitor.entity.AlertRule; | ||
| 14 | +import com.nctigba.alert.monitor.entity.AlertRuleItem; | ||
| 15 | +import com.nctigba.alert.monitor.entity.AlertRuleItemParam; | ||
| 16 | +import com.nctigba.alert.monitor.entity.AlertTemplate; | ||
| 17 | +import com.nctigba.alert.monitor.entity.AlertTemplateRule; | ||
| 18 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItem; | ||
| 19 | +import com.nctigba.alert.monitor.entity.AlertTemplateRuleItemParam; | ||
| 20 | +import com.nctigba.alert.monitor.mapper.AlertRuleItemMapper; | ||
| 21 | +import com.nctigba.alert.monitor.mapper.AlertRuleItemParamMapper; | ||
| 22 | +import com.nctigba.alert.monitor.mapper.AlertRuleMapper; | ||
| 23 | +import com.nctigba.alert.monitor.mapper.AlertTemplateMapper; | ||
| 24 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleItemMapper; | ||
| 25 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleItemParamMapper; | ||
| 26 | +import com.nctigba.alert.monitor.mapper.AlertTemplateRuleMapper; | ||
| 27 | +import com.nctigba.alert.monitor.model.AlertTemplateReq; | ||
| 28 | +import com.nctigba.alert.monitor.model.AlertTemplateRuleReq; | ||
| 29 | +import com.nctigba.alert.monitor.service.AlertClusterNodeConfService; | ||
| 30 | +import com.nctigba.alert.monitor.service.AlertTemplateRuleItemParamService; | ||
| 31 | +import com.nctigba.alert.monitor.service.AlertTemplateRuleService; | ||
| 32 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 33 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 34 | +import org.junit.Before; | ||
| 35 | +import org.junit.Test; | ||
| 36 | +import org.junit.runner.RunWith; | ||
| 37 | +import org.mockito.InjectMocks; | ||
| 38 | +import org.mockito.Mock; | ||
| 39 | +import org.mockito.MockedStatic; | ||
| 40 | +import org.mockito.MockitoAnnotations; | ||
| 41 | +import org.mockito.Spy; | ||
| 42 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 43 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 44 | + | ||
| 45 | +import java.util.ArrayList; | ||
| 46 | +import java.util.List; | ||
| 47 | + | ||
| 48 | +import static org.junit.Assert.assertEquals; | ||
| 49 | +import static org.mockito.ArgumentMatchers.any; | ||
| 50 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 51 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 52 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 53 | +import static org.mockito.Mockito.mockStatic; | ||
| 54 | +import static org.mockito.Mockito.times; | ||
| 55 | +import static org.mockito.Mockito.verify; | ||
| 56 | +import static org.mockito.Mockito.when; | ||
| 57 | + | ||
| 58 | +/** | ||
| 59 | + * test the function of the AlertTemplateServiceImpl | ||
| 60 | + * | ||
| 61 | + * 2023/7/13 17:53 | ||
| 62 | + */ | ||
| 63 | + | ||
| 64 | +public class AlertTemplateServiceImplTest { | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + private AlertTemplateServiceImpl alertTemplateService; | ||
| 68 | + | ||
| 69 | + private AlertTemplateMapper baseMapper; | ||
| 70 | + | ||
| 71 | + private AlertRuleMapper alertRuleMapper; | ||
| 72 | + | ||
| 73 | + private AlertRuleItemMapper alertRuleItemMapper; | ||
| 74 | + | ||
| 75 | + private AlertRuleItemParamMapper ruleItemParamMapper; | ||
| 76 | + | ||
| 77 | + private AlertTemplateRuleMapper alertTemplateRuleMapper; | ||
| 78 | + | ||
| 79 | + private AlertTemplateRuleItemMapper alertTemplateRuleItemMapper; | ||
| 80 | + | ||
| 81 | + private AlertTemplateRuleService templateRuleService; | ||
| 82 | + | ||
| 83 | + private AlertTemplateRuleItemParamMapper tRuleItemParamMapper; | ||
| 84 | + | ||
| 85 | + private AlertTemplateRuleItemParamService templateRuleItemParamService; | ||
| 86 | + | ||
| 87 | + private AlertClusterNodeConfService nodeConfService; | ||
| 88 | + | ||
| 89 | + | ||
| 90 | + public void before() { | ||
| 91 | + MockitoAnnotations.initMocks(this); | ||
| 92 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 93 | + AlertTemplate.class); | ||
| 94 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 95 | + AlertTemplateRuleItem.class); | ||
| 96 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 97 | + AlertTemplateRule.class); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + | ||
| 101 | + public void testGetTemplatePage() { | ||
| 102 | + Page page = new Page(1, 10); | ||
| 103 | + Page<AlertTemplate> templatePage = new Page<>(1, 10); | ||
| 104 | + when(baseMapper.selectPage(eq(page), any())).thenReturn(templatePage); | ||
| 105 | + | ||
| 106 | + Page<AlertTemplate> result = alertTemplateService.getTemplatePage("", page); | ||
| 107 | + | ||
| 108 | + verify(baseMapper, times(1)).selectPage(eq(page), any()); | ||
| 109 | + assertEquals(0, result.getTotal()); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + | ||
| 113 | + public void testGetTemplateRulePageNull() { | ||
| 114 | + Page page = new Page(1, 10); | ||
| 115 | + Page<AlertTemplateRule> rulePage = new Page<>(1, 10); | ||
| 116 | + when(alertTemplateRuleMapper.selectPage(eq(page), any())).thenReturn(rulePage); | ||
| 117 | + | ||
| 118 | + Page<AlertTemplateRuleDto> result = alertTemplateService.getTemplateRulePage(1L, "", page); | ||
| 119 | + | ||
| 120 | + verify(alertTemplateRuleMapper, times(1)).selectPage(eq(page), any()); | ||
| 121 | + assertEquals(0, result.getTotal()); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + | ||
| 125 | + public void testGetTemplateRulePage() { | ||
| 126 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 127 | + Page page = new Page(1, 10); | ||
| 128 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 129 | + AlertTemplateRule templateRule = new AlertTemplateRule().setId(1L); | ||
| 130 | + templateRuleList.add(templateRule); | ||
| 131 | + Page<AlertTemplateRule> rulePage = new Page<>(1, 10); | ||
| 132 | + rulePage.setTotal(1L).setRecords(templateRuleList); | ||
| 133 | + when(alertTemplateRuleMapper.selectPage(eq(page), any())).thenReturn(rulePage); | ||
| 134 | + | ||
| 135 | + AlertTemplateRuleItem ruleItem1 = new AlertTemplateRuleItem().setId(1L).setTemplateRuleId(1L).setAction( | ||
| 136 | + "normal") | ||
| 137 | + .setOperate(">=").setLimitValue("50").setRuleMark("A").setRuleExpName("cpu").setUnit("%"); | ||
| 138 | + AlertTemplateRuleItem ruleItem2 = new AlertTemplateRuleItem().setId(2L).setTemplateRuleId(1L).setAction( | ||
| 139 | + "increase").setOperate(">=").setLimitValue("50").setRuleMark("B").setRuleExpName("cpu").setUnit(""); | ||
| 140 | + AlertTemplateRuleItem ruleItem3 = new AlertTemplateRuleItem().setId(3L).setTemplateRuleId(1L).setAction( | ||
| 141 | + "decrease").setOperate(">=").setLimitValue("50").setRuleMark("C").setRuleExpName("cpu").setUnit(""); | ||
| 142 | + AlertTemplateRuleItem ruleItem4 = new AlertTemplateRuleItem().setId(4L).setTemplateRuleId(1L).setAction( | ||
| 143 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("D").setRuleExpName("cpu").setUnit(""); | ||
| 144 | + List<AlertTemplateRuleItem> alertTemplateRuleItems = new ArrayList<>(); | ||
| 145 | + alertTemplateRuleItems.add(ruleItem1); | ||
| 146 | + alertTemplateRuleItems.add(ruleItem2); | ||
| 147 | + alertTemplateRuleItems.add(ruleItem3); | ||
| 148 | + alertTemplateRuleItems.add(ruleItem4); | ||
| 149 | + when(alertTemplateRuleItemMapper.selectList(any())).thenReturn(alertTemplateRuleItems); | ||
| 150 | + | ||
| 151 | + AlertTemplateRuleItemParam ruleItemParam1 = | ||
| 152 | + new AlertTemplateRuleItemParam().setId(1L).setItemId(1L).setParamValue("val1"); | ||
| 153 | + AlertTemplateRuleItemParam ruleItemParam2 = | ||
| 154 | + new AlertTemplateRuleItemParam().setId(2L).setItemId(2L).setParamValue("val2"); | ||
| 155 | + List<AlertTemplateRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 156 | + itemParamList.add(ruleItemParam1); | ||
| 157 | + itemParamList.add(ruleItemParam2); | ||
| 158 | + when(tRuleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 159 | + | ||
| 160 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn("name"); | ||
| 161 | + | ||
| 162 | + Page<AlertTemplateRuleDto> result = alertTemplateService.getTemplateRulePage(1L, "", page); | ||
| 163 | + | ||
| 164 | + verify(alertTemplateRuleMapper, times(1)).selectPage(eq(page), any()); | ||
| 165 | + verify(alertTemplateRuleItemMapper, times(1)).selectList(any()); | ||
| 166 | + verify(tRuleItemParamMapper, times(1)).selectList(any()); | ||
| 167 | + assertEquals(1, result.getTotal()); | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + | ||
| 172 | + public void testGetTemplate() { | ||
| 173 | + AlertTemplate alertTemplate = new AlertTemplate().setId(1L); | ||
| 174 | + when(baseMapper.selectById(anyLong())).thenReturn(alertTemplate); | ||
| 175 | + | ||
| 176 | + List<AlertTemplateRuleDto> ruleDtoList = new ArrayList<>(); | ||
| 177 | + when(templateRuleService.getDtoListByTemplateId(anyLong())).thenReturn(ruleDtoList); | ||
| 178 | + | ||
| 179 | + AlertTemplateDto template = alertTemplateService.getTemplate(anyLong()); | ||
| 180 | + | ||
| 181 | + verify(baseMapper, times(1)).selectById(anyLong()); | ||
| 182 | + verify(templateRuleService, times(1)).getDtoListByTemplateId(anyLong()); | ||
| 183 | + assertEquals(alertTemplate.getId(), template.getId()); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + | ||
| 187 | + public void testGetTemplateList() { | ||
| 188 | + List<AlertTemplate> list = new ArrayList<>(); | ||
| 189 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 190 | + | ||
| 191 | + List<AlertTemplate> templateList = alertTemplateService.getTemplateList(); | ||
| 192 | + | ||
| 193 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 194 | + assertEquals(list, templateList); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + | ||
| 198 | + public void testSaveTemplate1() { | ||
| 199 | + AlertTemplateReq templateReq = new AlertTemplateReq(); | ||
| 200 | + templateReq.setId(1L); | ||
| 201 | + templateReq.setTemplateName("name"); | ||
| 202 | + List<AlertTemplateRuleReq> templateRuleReqList = new ArrayList<>(); | ||
| 203 | + AlertTemplateRuleReq ruleReq1 = new AlertTemplateRuleReq(); | ||
| 204 | + ruleReq1.setRuleId(2L); | ||
| 205 | + templateRuleReqList.add(ruleReq1); | ||
| 206 | + templateReq.setTemplateRuleReqList(templateRuleReqList); | ||
| 207 | + AlertTemplate alertTemplate = new AlertTemplate(); | ||
| 208 | + when(baseMapper.selectById(templateReq.getId())).thenReturn(alertTemplate); | ||
| 209 | + | ||
| 210 | + List<AlertTemplateRule> alertTemplateRules = new ArrayList<>(); | ||
| 211 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(alertTemplateRules); | ||
| 212 | + | ||
| 213 | + for (AlertTemplateRuleReq alertTemplateRuleReq : templateRuleReqList) { | ||
| 214 | + AlertRule alertRule = new AlertRule().setId(1L); | ||
| 215 | + when(alertRuleMapper.selectById(alertTemplateRuleReq.getRuleId())).thenReturn(alertRule); | ||
| 216 | + when(alertTemplateRuleMapper.insert(any(AlertTemplateRule.class))).thenReturn(1); | ||
| 217 | + AlertRuleItem alertRuleItem1 = new AlertRuleItem().setRuleId(1L).setId(1L); | ||
| 218 | + List<AlertRuleItem> alertRuleItems = new ArrayList<>(); | ||
| 219 | + alertRuleItems.add(alertRuleItem1); | ||
| 220 | + when(alertRuleItemMapper.selectList(any())).thenReturn(alertRuleItems); | ||
| 221 | + when(alertTemplateRuleItemMapper.insert(any(AlertTemplateRuleItem.class))).thenReturn(1); | ||
| 222 | + AlertRuleItemParam ruleItemParam = new AlertRuleItemParam().setItemId(1L); | ||
| 223 | + List<AlertRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 224 | + itemParamList.add(ruleItemParam); | ||
| 225 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 226 | + when(templateRuleItemParamService.saveBatch(anyList())).thenReturn(true); | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + AlertTemplate result = alertTemplateService.saveTemplate(templateReq); | ||
| 230 | + | ||
| 231 | + verify(baseMapper, times(1)).selectById(anyLong()); | ||
| 232 | + verify(alertTemplateService, times(1)).saveOrUpdate(any()); | ||
| 233 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 234 | + verify(alertRuleMapper, times(1)).selectById(anyLong()); | ||
| 235 | + verify(alertTemplateRuleMapper, times(1)).insert(any(AlertTemplateRule.class)); | ||
| 236 | + verify(alertRuleItemMapper, times(1)).selectList(any()); | ||
| 237 | + verify(alertTemplateRuleItemMapper, times(1)).insert(any(AlertTemplateRuleItem.class)); | ||
| 238 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 239 | + verify(templateRuleItemParamService, times(1)).saveBatch(anyList()); | ||
| 240 | + assertEquals(templateReq.getTemplateName(), result.getTemplateName()); | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + | ||
| 244 | + public void testSaveTemplate2() { | ||
| 245 | + AlertTemplateReq templateReq = new AlertTemplateReq(); | ||
| 246 | + templateReq.setTemplateName("name"); | ||
| 247 | + List<AlertTemplateRuleReq> templateRuleReqList = new ArrayList<>(); | ||
| 248 | + AlertTemplateRuleReq ruleReq2 = new AlertTemplateRuleReq(); | ||
| 249 | + ruleReq2.setRuleId(2L); | ||
| 250 | + templateRuleReqList.add(ruleReq2); | ||
| 251 | + templateReq.setTemplateRuleReqList(templateRuleReqList); | ||
| 252 | + | ||
| 253 | + List<AlertTemplateRule> alertTemplateRules = new ArrayList<>(); | ||
| 254 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(alertTemplateRules); | ||
| 255 | + | ||
| 256 | + for (AlertTemplateRuleReq alertTemplateRuleReq : templateRuleReqList) { | ||
| 257 | + AlertRule alertRule = new AlertRule().setId(1L); | ||
| 258 | + when(alertRuleMapper.selectById(alertTemplateRuleReq.getRuleId())).thenReturn(alertRule); | ||
| 259 | + when(alertTemplateRuleMapper.insert(any(AlertTemplateRule.class))).thenReturn(1); | ||
| 260 | + | ||
| 261 | + AlertRuleItem alertRuleItem1 = new AlertRuleItem().setRuleId(1L).setId(1L); | ||
| 262 | + List<AlertRuleItem> alertRuleItems = new ArrayList<>(); | ||
| 263 | + alertRuleItems.add(alertRuleItem1); | ||
| 264 | + when(alertRuleItemMapper.selectList(any())).thenReturn(alertRuleItems); | ||
| 265 | + when(alertTemplateRuleItemMapper.insert(any(AlertTemplateRuleItem.class))).thenReturn(1); | ||
| 266 | + | ||
| 267 | + List<AlertRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 268 | + when(ruleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + AlertTemplate result = alertTemplateService.saveTemplate(templateReq); | ||
| 272 | + | ||
| 273 | + verify(alertTemplateService, times(1)).saveOrUpdate(any()); | ||
| 274 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 275 | + verify(alertRuleMapper, times(1)).selectById(anyLong()); | ||
| 276 | + verify(alertTemplateRuleMapper, times(1)).insert(any(AlertTemplateRule.class)); | ||
| 277 | + verify(alertRuleItemMapper, times(1)).selectList(any()); | ||
| 278 | + verify(alertTemplateRuleItemMapper, times(1)).insert(any(AlertTemplateRuleItem.class)); | ||
| 279 | + verify(ruleItemParamMapper, times(1)).selectList(any()); | ||
| 280 | + assertEquals(templateReq.getTemplateName(), result.getTemplateName()); | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + | ||
| 284 | + public void testSaveTemplate3() { | ||
| 285 | + AlertTemplateReq templateReq = new AlertTemplateReq(); | ||
| 286 | + templateReq.setId(1L); | ||
| 287 | + templateReq.setTemplateName("name"); | ||
| 288 | + List<AlertTemplateRuleReq> templateRuleReqList = new ArrayList<>(); | ||
| 289 | + AlertTemplateRuleReq ruleReq1 = new AlertTemplateRuleReq(); | ||
| 290 | + ruleReq1.setRuleId(1L); | ||
| 291 | + ruleReq1.setTemplateRuleId(1L); | ||
| 292 | + templateRuleReqList.add(ruleReq1); | ||
| 293 | + templateReq.setTemplateRuleReqList(templateRuleReqList); | ||
| 294 | + | ||
| 295 | + AlertTemplate alertTemplate = new AlertTemplate(); | ||
| 296 | + when(baseMapper.selectById(templateReq.getId())).thenReturn(alertTemplate); | ||
| 297 | + | ||
| 298 | + AlertTemplateRule templateRule = new AlertTemplateRule().setId(3L); | ||
| 299 | + List<AlertTemplateRule> alertTemplateRules = new ArrayList<>(); | ||
| 300 | + alertTemplateRules.add(templateRule); | ||
| 301 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(alertTemplateRules); | ||
| 302 | + when(alertTemplateRuleMapper.updateById(any(AlertTemplateRule.class))).thenReturn(1); | ||
| 303 | + when(alertTemplateRuleItemMapper.update(any(), any())).thenReturn(1); | ||
| 304 | + | ||
| 305 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule().setTemplateId(1L).setId(1L); | ||
| 306 | + when(alertTemplateRuleMapper.selectById(alertTemplateRule.getTemplateId())) | ||
| 307 | + .thenReturn(alertTemplateRule); | ||
| 308 | + | ||
| 309 | + AlertTemplate result = alertTemplateService.saveTemplate(templateReq); | ||
| 310 | + | ||
| 311 | + verify(baseMapper, times(1)).selectById(anyLong()); | ||
| 312 | + verify(alertTemplateService, times(1)).saveOrUpdate(any()); | ||
| 313 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 314 | + verify(alertTemplateRuleMapper, times(2)).updateById(any(AlertTemplateRule.class)); | ||
| 315 | + verify(alertTemplateRuleItemMapper, times(1)).update(any(), any()); | ||
| 316 | + verify(alertTemplateRuleMapper, times(1)).selectById(anyLong()); | ||
| 317 | + assertEquals(templateReq.getTemplateName(), result.getTemplateName()); | ||
| 318 | + } | ||
| 319 | + | ||
| 320 | + | ||
| 321 | + public void testGetTemplateRuleListByIdNull() { | ||
| 322 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 323 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(templateRuleList); | ||
| 324 | + List<AlertTemplateRuleDto> templateRuleDtoList = alertTemplateService.getTemplateRuleListById(anyLong()); | ||
| 325 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 326 | + assertEquals(0, templateRuleDtoList.size()); | ||
| 327 | + } | ||
| 328 | + | ||
| 329 | + | ||
| 330 | + public void testGetTemplateRuleListById() { | ||
| 331 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 332 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 333 | + AlertTemplateRule templateRule = new AlertTemplateRule().setId(1L); | ||
| 334 | + templateRuleList.add(templateRule); | ||
| 335 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(templateRuleList); | ||
| 336 | + | ||
| 337 | + AlertTemplateRuleItem ruleItem1 = new AlertTemplateRuleItem().setId(1L).setTemplateRuleId(1L).setAction( | ||
| 338 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("A").setRuleExpName("cpu").setUnit("%"); | ||
| 339 | + AlertTemplateRuleItem ruleItem2 = new AlertTemplateRuleItem().setId(2L).setTemplateRuleId(1L).setAction( | ||
| 340 | + "increase").setOperate(">=").setLimitValue("50").setRuleMark("B").setRuleExpName("cpu").setUnit(""); | ||
| 341 | + AlertTemplateRuleItem ruleItem3 = new AlertTemplateRuleItem().setId(3L).setTemplateRuleId(1L).setAction( | ||
| 342 | + "decrease").setOperate(">=").setLimitValue("50").setRuleMark("C").setRuleExpName("cpu").setUnit(""); | ||
| 343 | + AlertTemplateRuleItem ruleItem4 = new AlertTemplateRuleItem().setId(4L).setTemplateRuleId(1L).setAction( | ||
| 344 | + "normal").setOperate(">=").setLimitValue("50").setRuleMark("D").setRuleExpName("cpu").setUnit(""); | ||
| 345 | + List<AlertTemplateRuleItem> alertTemplateRuleItems = new ArrayList<>(); | ||
| 346 | + alertTemplateRuleItems.add(ruleItem1); | ||
| 347 | + alertTemplateRuleItems.add(ruleItem2); | ||
| 348 | + alertTemplateRuleItems.add(ruleItem3); | ||
| 349 | + alertTemplateRuleItems.add(ruleItem4); | ||
| 350 | + when(alertTemplateRuleItemMapper.selectList(any())).thenReturn(alertTemplateRuleItems); | ||
| 351 | + | ||
| 352 | + AlertTemplateRuleItemParam ruleItemParam1 = | ||
| 353 | + new AlertTemplateRuleItemParam().setId(1L).setItemId(1L).setParamValue("val1"); | ||
| 354 | + AlertTemplateRuleItemParam ruleItemParam2 = | ||
| 355 | + new AlertTemplateRuleItemParam().setId(2L).setItemId(2L).setParamValue("val2"); | ||
| 356 | + List<AlertTemplateRuleItemParam> itemParamList = new ArrayList<>(); | ||
| 357 | + itemParamList.add(ruleItemParam1); | ||
| 358 | + itemParamList.add(ruleItemParam2); | ||
| 359 | + when(tRuleItemParamMapper.selectList(any())).thenReturn(itemParamList); | ||
| 360 | + | ||
| 361 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn("name"); | ||
| 362 | + | ||
| 363 | + List<AlertTemplateRuleDto> templateRuleDtoList = alertTemplateService.getTemplateRuleListById(anyLong()); | ||
| 364 | + | ||
| 365 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 366 | + verify(alertTemplateRuleItemMapper, times(1)).selectList(any()); | ||
| 367 | + verify(tRuleItemParamMapper, times(1)).selectList(any()); | ||
| 368 | + assertEquals(1, templateRuleDtoList.size()); | ||
| 369 | + } | ||
| 370 | + } | ||
| 371 | + | ||
| 372 | + | ||
| 373 | + public void testDelTemplateThrowException() { | ||
| 374 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 375 | + AlertClusterNodeConf nodeConf = new AlertClusterNodeConf(); | ||
| 376 | + List<AlertClusterNodeConf> nodeConfList = new ArrayList<>(); | ||
| 377 | + nodeConfList.add(nodeConf); | ||
| 378 | + when(nodeConfService.list(any())).thenReturn(nodeConfList); | ||
| 379 | + mockedStatic.when(() -> MessageSourceUtil.get(any())).thenReturn("templateIsUsed"); | ||
| 380 | + alertTemplateService.delTemplate(anyLong()); | ||
| 381 | + verify(nodeConfService, times(1)).list(any()); | ||
| 382 | + } | ||
| 383 | + } | ||
| 384 | + | ||
| 385 | + | ||
| 386 | + public void testDelTemplate1() { | ||
| 387 | + List<AlertClusterNodeConf> nodeConfList = new ArrayList<>(); | ||
| 388 | + when(nodeConfService.list(any())).thenReturn(nodeConfList); | ||
| 389 | + | ||
| 390 | + when(baseMapper.update(any(), any())).thenReturn(1); | ||
| 391 | + | ||
| 392 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 393 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(templateRuleList); | ||
| 394 | + when(alertTemplateRuleMapper.update(any(), any())).thenReturn(1); | ||
| 395 | + | ||
| 396 | + alertTemplateService.delTemplate(anyLong()); | ||
| 397 | + verify(nodeConfService, times(1)).list(any()); | ||
| 398 | + verify(baseMapper, times(1)).update(any(), any()); | ||
| 399 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 400 | + verify(alertTemplateRuleMapper, times(1)).update(any(), any()); | ||
| 401 | + } | ||
| 402 | + | ||
| 403 | + | ||
| 404 | + public void testDelTemplate2() { | ||
| 405 | + List<AlertClusterNodeConf> nodeConfList = new ArrayList<>(); | ||
| 406 | + when(nodeConfService.list(any())).thenReturn(nodeConfList); | ||
| 407 | + | ||
| 408 | + when(baseMapper.update(any(), any())).thenReturn(1); | ||
| 409 | + | ||
| 410 | + AlertTemplateRule templateRule = new AlertTemplateRule().setId(1L); | ||
| 411 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 412 | + templateRuleList.add(templateRule); | ||
| 413 | + when(alertTemplateRuleMapper.selectList(any())).thenReturn(templateRuleList); | ||
| 414 | + when(alertTemplateRuleMapper.update(any(), any())).thenReturn(1); | ||
| 415 | + when(alertTemplateRuleItemMapper.update(any(), any())).thenReturn(1); | ||
| 416 | + | ||
| 417 | + alertTemplateService.delTemplate(anyLong()); | ||
| 418 | + verify(nodeConfService, times(1)).list(any()); | ||
| 419 | + verify(baseMapper, times(1)).update(any(), any()); | ||
| 420 | + verify(alertTemplateRuleMapper, times(1)).selectList(any()); | ||
| 421 | + verify(alertTemplateRuleMapper, times(1)).update(any(), any()); | ||
| 422 | + verify(alertTemplateRuleMapper, times(1)).update(any(), any()); | ||
| 423 | + } | ||
| 424 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/NotifyConfigServiceImplTest.java+188-0
| @@ -0,0 +1,188 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.nctigba.alert.monitor.constant.CommonConstants; | ||
| 10 | +import com.nctigba.alert.monitor.entity.NotifyConfig; | ||
| 11 | +import com.nctigba.alert.monitor.entity.NotifyTemplate; | ||
| 12 | +import com.nctigba.alert.monitor.entity.NotifyWay; | ||
| 13 | +import com.nctigba.alert.monitor.mapper.NotifyConfigMapper; | ||
| 14 | +import com.nctigba.alert.monitor.mapper.NotifyTemplateMapper; | ||
| 15 | +import com.nctigba.alert.monitor.mapper.NotifyWayMapper; | ||
| 16 | +import com.nctigba.alert.monitor.model.NotifyConfigReq; | ||
| 17 | +import com.nctigba.alert.monitor.service.EmailService; | ||
| 18 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 19 | +import org.junit.Before; | ||
| 20 | +import org.junit.Test; | ||
| 21 | +import org.junit.runner.RunWith; | ||
| 22 | +import org.mockito.InjectMocks; | ||
| 23 | +import org.mockito.Mock; | ||
| 24 | +import org.mockito.MockitoAnnotations; | ||
| 25 | +import org.mockito.Spy; | ||
| 26 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 27 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 28 | +import org.springframework.validation.SmartValidator; | ||
| 29 | + | ||
| 30 | +import java.util.ArrayList; | ||
| 31 | +import java.util.List; | ||
| 32 | + | ||
| 33 | +import static org.junit.Assert.assertEquals; | ||
| 34 | +import static org.mockito.ArgumentMatchers.any; | ||
| 35 | +import static org.mockito.ArgumentMatchers.anyList; | ||
| 36 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 37 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 38 | +import static org.mockito.Mockito.doNothing; | ||
| 39 | +import static org.mockito.Mockito.times; | ||
| 40 | +import static org.mockito.Mockito.verify; | ||
| 41 | +import static org.mockito.Mockito.when; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * test the function of the NotifyConfigServiceImpl | ||
| 45 | + * | ||
| 46 | + * 2023/7/14 14:13 | ||
| 47 | + */ | ||
| 48 | + | ||
| 49 | +public class NotifyConfigServiceImplTest { | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + private NotifyConfigServiceImpl notifyConfigService; | ||
| 53 | + | ||
| 54 | + private NotifyConfigMapper baseMapper; | ||
| 55 | + | ||
| 56 | + private EmailService emailService; | ||
| 57 | + | ||
| 58 | + private NotifyWayMapper notifyWayMapper; | ||
| 59 | + | ||
| 60 | + private NotifyTemplateMapper notifyTemplateMapper; | ||
| 61 | + | ||
| 62 | + private SmartValidator smartValidator; | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + public void before() { | ||
| 66 | + MockitoAnnotations.initMocks(this); | ||
| 67 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 68 | + NotifyConfig.class); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + public void testGetAllListNull() { | ||
| 73 | + List<NotifyConfig> list = notifyConfigService.getAllList(); | ||
| 74 | + verify(notifyConfigService, times(1)).list(any()); | ||
| 75 | + assertEquals(0, list.size()); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + public void testGetAllList() { | ||
| 80 | + NotifyConfig notifyConfig = new NotifyConfig(); | ||
| 81 | + notifyConfig.setType("email"); | ||
| 82 | + List<NotifyConfig> list = new ArrayList<>(); | ||
| 83 | + list.add(notifyConfig); | ||
| 84 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 85 | + | ||
| 86 | + List<NotifyConfig> notifyConfigList = notifyConfigService.getAllList(); | ||
| 87 | + | ||
| 88 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 89 | + assertEquals(list, notifyConfigList); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + | ||
| 93 | + public void testSaveListThrowException() { | ||
| 94 | + List<NotifyConfig> list = new ArrayList<>(); | ||
| 95 | + notifyConfigService.saveList(list); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + | ||
| 99 | + public void testSaveList1() { | ||
| 100 | + List<NotifyConfig> list = new ArrayList<>(); | ||
| 101 | + NotifyConfig notifyConfig1 = new NotifyConfig(); | ||
| 102 | + notifyConfig1.setType(CommonConstants.EMAIL).setId(1L).setEnable(CommonConstants.ENABLE); | ||
| 103 | + list.add(notifyConfig1); | ||
| 104 | + NotifyConfig notifyConfig2 = new NotifyConfig(); | ||
| 105 | + notifyConfig2.setType(CommonConstants.WE_COM).setId(2L).setEnable(CommonConstants.ENABLE); | ||
| 106 | + list.add(notifyConfig2); | ||
| 107 | + NotifyConfig notifyConfig3 = new NotifyConfig(); | ||
| 108 | + notifyConfig3.setType(CommonConstants.DING_TALK).setId(3L).setEnable(CommonConstants.ENABLE); | ||
| 109 | + list.add(notifyConfig3); | ||
| 110 | + | ||
| 111 | + doNothing().when(smartValidator).validate(any(), any(), eq(NotifyConfig.DefaultGroup.class)); | ||
| 112 | + | ||
| 113 | + List<NotifyConfig> oldList = new ArrayList<>(); | ||
| 114 | + NotifyConfig notifyConfig = new NotifyConfig().setType(CommonConstants.EMAIL).setId(1L) | ||
| 115 | + .setEnable(CommonConstants.ENABLE); | ||
| 116 | + oldList.add(notifyConfig); | ||
| 117 | + when(baseMapper.selectList(any())).thenReturn(oldList); | ||
| 118 | + when(notifyConfigService.saveOrUpdateBatch(anyList())).thenReturn(true); | ||
| 119 | + | ||
| 120 | + notifyConfigService.saveList(list); | ||
| 121 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 122 | + verify(notifyConfigService, times(2)).saveOrUpdateBatch(anyList()); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + | ||
| 126 | + public void testSaveList2() { | ||
| 127 | + List<NotifyConfig> list = new ArrayList<>(); | ||
| 128 | + NotifyConfig notifyConfig1 = new NotifyConfig(); | ||
| 129 | + notifyConfig1.setType(CommonConstants.EMAIL).setId(1L).setEnable(CommonConstants.DISABLE); | ||
| 130 | + list.add(notifyConfig1); | ||
| 131 | + NotifyConfig notifyConfig2 = new NotifyConfig(); | ||
| 132 | + notifyConfig2.setType(CommonConstants.WE_COM).setId(2L).setEnable(CommonConstants.DISABLE); | ||
| 133 | + list.add(notifyConfig2); | ||
| 134 | + NotifyConfig notifyConfig3 = new NotifyConfig(); | ||
| 135 | + notifyConfig3.setType(CommonConstants.DING_TALK).setId(3L).setEnable(CommonConstants.DISABLE); | ||
| 136 | + list.add(notifyConfig3); | ||
| 137 | + | ||
| 138 | + doNothing().when(smartValidator).validate(any(), any(), eq(NotifyConfig.DefaultGroup.class)); | ||
| 139 | + | ||
| 140 | + List<NotifyConfig> oldList = new ArrayList<>(); | ||
| 141 | + NotifyConfig notifyConfig = new NotifyConfig().setType(CommonConstants.EMAIL).setId(1L) | ||
| 142 | + .setEnable(CommonConstants.ENABLE); | ||
| 143 | + oldList.add(notifyConfig); | ||
| 144 | + when(baseMapper.selectList(any())).thenReturn(oldList); | ||
| 145 | + when(notifyConfigService.saveOrUpdateBatch(anyList())).thenReturn(true); | ||
| 146 | + | ||
| 147 | + notifyConfigService.saveList(list); | ||
| 148 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 149 | + verify(notifyConfigService, times(2)).saveOrUpdateBatch(anyList()); | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + | ||
| 153 | + public void testTestConfig() { | ||
| 154 | + NotifyConfigReq notifyConfigReq = new NotifyConfigReq(); | ||
| 155 | + notifyConfigReq.setType(CommonConstants.EMAIL); | ||
| 156 | + notifyConfigReq.setNotifyWayId(1L); | ||
| 157 | + NotifyWay notifyWay = new NotifyWay().setNotifyTemplateId(1L); | ||
| 158 | + when(notifyWayMapper.selectById(anyLong())).thenReturn(notifyWay); | ||
| 159 | + NotifyTemplate notifyTemplate = new NotifyTemplate().setId(1L); | ||
| 160 | + when(notifyTemplateMapper.selectById(anyLong())).thenReturn(notifyTemplate); | ||
| 161 | + doNothing().when(emailService).sendTest(notifyConfigReq, notifyWay.getEmail(), notifyTemplate.getNotifyTitle(), | ||
| 162 | + notifyTemplate.getNotifyContent()); | ||
| 163 | + | ||
| 164 | + notifyConfigService.testConfig(notifyConfigReq); | ||
| 165 | + | ||
| 166 | + verify(notifyWayMapper, times(1)).selectById(anyLong()); | ||
| 167 | + verify(notifyTemplateMapper, times(1)).selectById(anyLong()); | ||
| 168 | + verify(emailService, times(1)).sendTest(notifyConfigReq, notifyWay.getEmail(), notifyTemplate.getNotifyTitle(), | ||
| 169 | + notifyTemplate.getNotifyContent()); | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + | ||
| 173 | + public void testTestConfigThrowException() { | ||
| 174 | + NotifyConfigReq notifyConfigReq = new NotifyConfigReq(); | ||
| 175 | + notifyConfigReq.setNotifyWayId(1L); | ||
| 176 | + notifyConfigReq.setType(CommonConstants.WE_COM); | ||
| 177 | + NotifyWay notifyWay = new NotifyWay().setNotifyTemplateId(1L); | ||
| 178 | + when(notifyWayMapper.selectById(anyLong())).thenReturn(notifyWay); | ||
| 179 | + | ||
| 180 | + NotifyTemplate notifyTemplate = new NotifyTemplate().setId(1L); | ||
| 181 | + when(notifyTemplateMapper.selectById(anyLong())).thenReturn(notifyTemplate); | ||
| 182 | + | ||
| 183 | + notifyConfigService.testConfig(notifyConfigReq); | ||
| 184 | + | ||
| 185 | + verify(notifyWayMapper, times(1)).selectById(anyLong()); | ||
| 186 | + verify(notifyTemplateMapper, times(1)).selectById(anyLong()); | ||
| 187 | + } | ||
| 188 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/NotifyTemplateServiceImplTest.java+123-0
| @@ -0,0 +1,123 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 10 | +import com.nctigba.alert.monitor.entity.NotifyTemplate; | ||
| 11 | +import com.nctigba.alert.monitor.entity.NotifyWay; | ||
| 12 | +import com.nctigba.alert.monitor.mapper.NotifyTemplateMapper; | ||
| 13 | +import com.nctigba.alert.monitor.service.NotifyWayService; | ||
| 14 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 15 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 16 | +import org.junit.Before; | ||
| 17 | +import org.junit.Test; | ||
| 18 | +import org.junit.runner.RunWith; | ||
| 19 | +import org.mockito.InjectMocks; | ||
| 20 | +import org.mockito.Mock; | ||
| 21 | +import org.mockito.MockedStatic; | ||
| 22 | +import org.mockito.MockitoAnnotations; | ||
| 23 | +import org.mockito.Spy; | ||
| 24 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 25 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 26 | + | ||
| 27 | +import java.util.ArrayList; | ||
| 28 | +import java.util.List; | ||
| 29 | + | ||
| 30 | +import static org.junit.Assert.assertEquals; | ||
| 31 | +import static org.mockito.ArgumentMatchers.any; | ||
| 32 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 33 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 34 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 35 | +import static org.mockito.Mockito.mockStatic; | ||
| 36 | +import static org.mockito.Mockito.times; | ||
| 37 | +import static org.mockito.Mockito.verify; | ||
| 38 | +import static org.mockito.Mockito.when; | ||
| 39 | + | ||
| 40 | +/** | ||
| 41 | + * test the function of the NotifyTemplateServiceImpl | ||
| 42 | + * | ||
| 43 | + * 2023/7/14 18:54 | ||
| 44 | + */ | ||
| 45 | + | ||
| 46 | +public class NotifyTemplateServiceImplTest { | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + private NotifyTemplateServiceImpl notifyTemplateService; | ||
| 50 | + | ||
| 51 | + private NotifyTemplateMapper baseMapper; | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + private NotifyWayService notifyWayService; | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + public void before() { | ||
| 58 | + MockitoAnnotations.initMocks(this); | ||
| 59 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 60 | + NotifyTemplate.class); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + public void testGetListPage() { | ||
| 65 | + Page page = new Page(1, 10); | ||
| 66 | + Page<NotifyTemplate> templatePage = new Page<>(1, 10); | ||
| 67 | + when(baseMapper.selectPage(eq(page), any())).thenReturn(templatePage); | ||
| 68 | + | ||
| 69 | + Page result = notifyTemplateService.getListPage("", "", page); | ||
| 70 | + | ||
| 71 | + verify(baseMapper, times(1)).selectPage(eq(page), any()); | ||
| 72 | + assertEquals(templatePage, result); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + public void testSaveTemplate1() { | ||
| 77 | + NotifyTemplate notifyTemplate = new NotifyTemplate(); | ||
| 78 | + when(baseMapper.insert(any(NotifyTemplate.class))).thenReturn(1); | ||
| 79 | + notifyTemplateService.saveTemplate(notifyTemplate); | ||
| 80 | + verify(baseMapper, times(1)).insert(any(NotifyTemplate.class)); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + public void testSaveTemplate2() { | ||
| 85 | + NotifyTemplate notifyTemplate = new NotifyTemplate().setId(1L); | ||
| 86 | + when(baseMapper.updateById(any(NotifyTemplate.class))).thenReturn(1); | ||
| 87 | + notifyTemplateService.saveTemplate(notifyTemplate); | ||
| 88 | + verify(baseMapper, times(1)).insert(any(NotifyTemplate.class)); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + | ||
| 92 | + public void testDelByIdThrowException() { | ||
| 93 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 94 | + List<NotifyWay> list = new ArrayList<>(); | ||
| 95 | + NotifyWay notifyWay = new NotifyWay(); | ||
| 96 | + list.add(notifyWay); | ||
| 97 | + when(notifyWayService.list(any())).thenReturn(list); | ||
| 98 | + notifyTemplateService.delById(anyLong()); | ||
| 99 | + verify(notifyWayService, times(1)).list(any()); | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + | ||
| 104 | + public void testDelById() { | ||
| 105 | + List<NotifyWay> list = new ArrayList<>(); | ||
| 106 | + when(notifyWayService.list(any())).thenReturn(list); | ||
| 107 | + when(baseMapper.update(any(), any())).thenReturn(1); | ||
| 108 | + | ||
| 109 | + notifyTemplateService.delById(anyLong()); | ||
| 110 | + | ||
| 111 | + verify(notifyWayService, times(1)).list(any()); | ||
| 112 | + verify(baseMapper, times(1)).update(any(), any()); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + | ||
| 116 | + public void testGetList() { | ||
| 117 | + List<NotifyTemplate> list = new ArrayList<>(); | ||
| 118 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 119 | + List<NotifyTemplate> resultList = notifyTemplateService.getList(anyString()); | ||
| 120 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 121 | + assertEquals(list, resultList); | ||
| 122 | + } | ||
| 123 | +} | ||
Aplugins/alert-monitor/src/test/java/com/nctigba/alert/monitor/service/impl/NotifyWayServiceImplTest.java+147-0
| @@ -0,0 +1,147 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) GBA-NCTI-ISDC. 2022-2023. All rights reserved. | ||
| 3 | + */ | ||
| 4 | + | ||
| 5 | +package com.nctigba.alert.monitor.service.impl; | ||
| 6 | + | ||
| 7 | +import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
| 8 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
| 9 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 10 | +import com.nctigba.alert.monitor.dto.NotifyWayDto; | ||
| 11 | +import com.nctigba.alert.monitor.entity.AlertRule; | ||
| 12 | +import com.nctigba.alert.monitor.entity.AlertTemplateRule; | ||
| 13 | +import com.nctigba.alert.monitor.entity.NotifyWay; | ||
| 14 | +import com.nctigba.alert.monitor.mapper.NotifyWayMapper; | ||
| 15 | +import com.nctigba.alert.monitor.service.AlertRuleService; | ||
| 16 | +import com.nctigba.alert.monitor.service.AlertTemplateRuleService; | ||
| 17 | +import com.nctigba.alert.monitor.utils.MessageSourceUtil; | ||
| 18 | +import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
| 19 | +import org.junit.Before; | ||
| 20 | +import org.junit.Test; | ||
| 21 | +import org.junit.runner.RunWith; | ||
| 22 | +import org.mockito.InjectMocks; | ||
| 23 | +import org.mockito.Mock; | ||
| 24 | +import org.mockito.MockedStatic; | ||
| 25 | +import org.mockito.MockitoAnnotations; | ||
| 26 | +import org.mockito.Spy; | ||
| 27 | +import org.opengauss.admin.common.exception.ServiceException; | ||
| 28 | +import org.springframework.test.context.junit4.SpringRunner; | ||
| 29 | + | ||
| 30 | +import java.util.ArrayList; | ||
| 31 | +import java.util.List; | ||
| 32 | + | ||
| 33 | +import static org.junit.Assert.assertEquals; | ||
| 34 | +import static org.mockito.ArgumentMatchers.any; | ||
| 35 | +import static org.mockito.ArgumentMatchers.anyLong; | ||
| 36 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 37 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 38 | +import static org.mockito.Mockito.mockStatic; | ||
| 39 | +import static org.mockito.Mockito.times; | ||
| 40 | +import static org.mockito.Mockito.verify; | ||
| 41 | +import static org.mockito.Mockito.when; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * test the function of the NotifyWayServiceImpl | ||
| 45 | + * | ||
| 46 | + * 2023/7/14 19:20 | ||
| 47 | + */ | ||
| 48 | + | ||
| 49 | +public class NotifyWayServiceImplTest { | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + private NotifyWayServiceImpl notifyWayService; | ||
| 53 | + | ||
| 54 | + private AlertRuleService ruleService; | ||
| 55 | + | ||
| 56 | + private AlertTemplateRuleService templateRuleService; | ||
| 57 | + | ||
| 58 | + private NotifyWayMapper baseMapper; | ||
| 59 | + | ||
| 60 | + | ||
| 61 | + public void before() { | ||
| 62 | + MockitoAnnotations.initMocks(this); | ||
| 63 | + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), | ||
| 64 | + NotifyWay.class); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + | ||
| 68 | + public void testGetListPage() { | ||
| 69 | + Page page = new Page(1, 10); | ||
| 70 | + Page<NotifyWayDto> notifyWayDtoPage = new Page<>(1, 10); | ||
| 71 | + when(baseMapper.selectDtoPage(eq(page), any())).thenReturn(notifyWayDtoPage); | ||
| 72 | + | ||
| 73 | + Page result = notifyWayService.getListPage("", "", page); | ||
| 74 | + | ||
| 75 | + verify(baseMapper, times(1)).selectDtoPage(eq(page), any()); | ||
| 76 | + assertEquals(notifyWayDtoPage, result); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + | ||
| 80 | + public void testGetList() { | ||
| 81 | + List<NotifyWay> list = new ArrayList<>(); | ||
| 82 | + when(baseMapper.selectList(any())).thenReturn(list); | ||
| 83 | + List<NotifyWay> resultList = notifyWayService.getList(anyString()); | ||
| 84 | + verify(baseMapper, times(1)).selectList(any()); | ||
| 85 | + assertEquals(list, resultList); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + | ||
| 89 | + public void testSaveNotifyWay1() { | ||
| 90 | + NotifyWay notifyWay = new NotifyWay(); | ||
| 91 | + when(baseMapper.insert(any(NotifyWay.class))).thenReturn(1); | ||
| 92 | + notifyWayService.saveNotifyWay(notifyWay); | ||
| 93 | + verify(baseMapper, times(1)).insert(any(NotifyWay.class)); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + | ||
| 97 | + public void testSaveNotifyWay2() { | ||
| 98 | + NotifyWay notifyWay = new NotifyWay().setId(1L); | ||
| 99 | + when(baseMapper.updateById(any(NotifyWay.class))).thenReturn(1); | ||
| 100 | + notifyWayService.saveNotifyWay(notifyWay); | ||
| 101 | + verify(baseMapper, times(1)).insert(any(NotifyWay.class)); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + | ||
| 105 | + public void testDelByIdThrowException1() { | ||
| 106 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 107 | + List<AlertRule> ruleList = new ArrayList<>(); | ||
| 108 | + AlertRule alertRule = new AlertRule(); | ||
| 109 | + ruleList.add(alertRule); | ||
| 110 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 111 | + when(ruleService.list(any())).thenReturn(ruleList); | ||
| 112 | + when(templateRuleService.list(any())).thenReturn(templateRuleList); | ||
| 113 | + notifyWayService.delById(anyLong()); | ||
| 114 | + verify(ruleService, times(1)).list(any()); | ||
| 115 | + verify(templateRuleService, times(1)).list(any()); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + | ||
| 120 | + public void testDelByIdThrowException2() { | ||
| 121 | + try (MockedStatic<MessageSourceUtil> mockedStatic = mockStatic(MessageSourceUtil.class)) { | ||
| 122 | + List<AlertRule> ruleList = new ArrayList<>(); | ||
| 123 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 124 | + AlertTemplateRule alertTemplateRule = new AlertTemplateRule(); | ||
| 125 | + templateRuleList.add(alertTemplateRule); | ||
| 126 | + when(ruleService.list(any())).thenReturn(ruleList); | ||
| 127 | + when(templateRuleService.list(any())).thenReturn(templateRuleList); | ||
| 128 | + notifyWayService.delById(anyLong()); | ||
| 129 | + verify(ruleService, times(1)).list(any()); | ||
| 130 | + verify(templateRuleService, times(1)).list(any()); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + | ||
| 135 | + public void testDelById() { | ||
| 136 | + List<AlertRule> ruleList = new ArrayList<>(); | ||
| 137 | + List<AlertTemplateRule> templateRuleList = new ArrayList<>(); | ||
| 138 | + when(ruleService.list(any())).thenReturn(ruleList); | ||
| 139 | + when(templateRuleService.list(any())).thenReturn(templateRuleList); | ||
| 140 | + when(baseMapper.update(any(), any())).thenReturn(1); | ||
| 141 | + | ||
| 142 | + notifyWayService.delById(anyLong()); | ||
| 143 | + verify(ruleService, times(1)).list(any()); | ||
| 144 | + verify(templateRuleService, times(1)).list(any()); | ||
| 145 | + verify(baseMapper, times(1)).update(any(), any()); | ||
| 146 | + } | ||
| 147 | +} | ||