This is an automated email from the ASF dual-hosted git repository.

zhengqiwei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 254421bc4 [refactor] move code from AlertReportController to 
AlertService (#2434)
254421bc4 is described below

commit 254421bc460615d9e6b4fcf3f5094c8881a40a73
Author: kangli <[email protected]>
AuthorDate: Sun Aug 4 23:37:52 2024 +0800

    [refactor] move code from AlertReportController to AlertService (#2434)
    
    Co-authored-by: Calvin <[email protected]>
---
 .../alert/controller/AlertReportController.java    | 41 +----------
 .../hertzbeat/alert/service/AlertService.java      |  7 ++
 .../alert/service/impl/AlertServiceImpl.java       | 42 ++++++++++++
 .../hertzbeat/alert/service/AlertServiceTest.java  | 80 ++++++++++++++++++++++
 4 files changed, 130 insertions(+), 40 deletions(-)

diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java
index 89b67fd18..1f803ec81 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertReportController.java
@@ -20,16 +20,10 @@ package org.apache.hertzbeat.alert.controller;
 import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
-import java.util.Date;
-import java.util.Optional;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.hertzbeat.alert.dto.CloudAlertReportAbstract;
 import org.apache.hertzbeat.alert.dto.GeneralCloudAlertReport;
-import org.apache.hertzbeat.alert.enums.CloudServiceAlarmInformationEnum;
 import org.apache.hertzbeat.alert.service.AlertService;
-import org.apache.hertzbeat.common.entity.dto.AlertReport;
 import org.apache.hertzbeat.common.entity.dto.Message;
-import org.apache.hertzbeat.common.util.JsonUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -54,40 +48,7 @@ public class AlertReportController {
     @Operation(summary = "Interface for reporting external alarm information 
of cloud service")
     public ResponseEntity<Message<Void>> 
addNewAlertReportFromCloud(@PathVariable("cloud") String cloudServiceName,
                                                                     
@RequestBody String alertReport) {
-        CloudServiceAlarmInformationEnum cloudService = 
CloudServiceAlarmInformationEnum
-                .getEnumFromCloudServiceName(cloudServiceName);
-
-        AlertReport alert = null;
-        if (cloudService != null) {
-            try {
-                CloudAlertReportAbstract cloudAlertReport = JsonUtil
-                        .fromJson(alertReport, 
cloudService.getCloudServiceAlarmInformationEntity());
-                assert cloudAlertReport != null;
-                alert = AlertReport.builder()
-                        .content(cloudAlertReport.getContent())
-                        .alertName(cloudAlertReport.getAlertName())
-                        .alertTime(cloudAlertReport.getAlertTime())
-                        .alertDuration(cloudAlertReport.getAlertDuration())
-                        .priority(cloudAlertReport.getPriority())
-                        .reportType(cloudAlertReport.getReportType())
-                        .labels(cloudAlertReport.getLabels())
-                        .annotations(cloudAlertReport.getAnnotations())
-                        .build();
-            } catch (Exception e) {
-                log.error("[alert report] parse cloud service alarm content 
failed! cloud service: {} conrent: {}",
-                        cloudService.name(), alertReport);
-            }
-        } else {
-            alert = AlertReport.builder()
-                    .content("error do not has cloud service api")
-                    .alertName("/api/alerts/report/" + cloudServiceName)
-                    .alertTime(new Date().getTime())
-                    .priority(1)
-                    .reportType(1)
-                    .build();
-        }
-        Optional.ofNullable(alert).ifPresent(alertReportPresent ->
-                alertService.addNewAlertReport(alertReportPresent));
+        alertService.addNewAlertReportFromCloud(cloudServiceName, alertReport);
         return ResponseEntity.ok(Message.success("Add report success"));
     }
     
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java
index 2bc6b182e..6ee54fac3 100644
--- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java
+++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java
@@ -76,6 +76,13 @@ public interface AlertService {
      */
     void addNewAlertReport(AlertReport alertReport);
 
+    /**
+     * Save external alarms of cloud services
+     * @param cloudServiceName cloud service name,Such as tencloud
+     * @param alertReport alert report json string
+     */
+    void addNewAlertReportFromCloud(String cloudServiceName, String 
alertReport);
+
     /**
      * Dynamic conditional query
      * @param specification Query conditions        
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java
index a817d6123..8f1374d6f 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java
@@ -22,18 +22,23 @@ import java.math.RoundingMode;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.alert.dao.AlertDao;
 import org.apache.hertzbeat.alert.dto.AlertPriorityNum;
 import org.apache.hertzbeat.alert.dto.AlertSummary;
+import org.apache.hertzbeat.alert.dto.CloudAlertReportAbstract;
+import org.apache.hertzbeat.alert.enums.CloudServiceAlarmInformationEnum;
 import org.apache.hertzbeat.alert.reduce.AlarmCommonReduce;
 import org.apache.hertzbeat.alert.service.AlertService;
 import org.apache.hertzbeat.common.constants.CommonConstants;
 import org.apache.hertzbeat.common.entity.alerter.Alert;
 import org.apache.hertzbeat.common.entity.dto.AlertReport;
+import org.apache.hertzbeat.common.util.JsonUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
@@ -121,6 +126,43 @@ public class AlertServiceImpl implements AlertService {
         alarmCommonReduce.reduceAndSendAlarm(buildAlertData(alertReport));
     }
 
+    @Override
+    public void addNewAlertReportFromCloud(String cloudServiceName, String 
alertReport) {
+        CloudServiceAlarmInformationEnum cloudService = 
CloudServiceAlarmInformationEnum
+                .getEnumFromCloudServiceName(cloudServiceName);
+
+        AlertReport alert = null;
+        if (cloudService != null) {
+            try {
+                CloudAlertReportAbstract cloudAlertReport = JsonUtil
+                        .fromJson(alertReport, 
cloudService.getCloudServiceAlarmInformationEntity());
+                assert cloudAlertReport != null;
+                alert = AlertReport.builder()
+                        .content(cloudAlertReport.getContent())
+                        .alertName(cloudAlertReport.getAlertName())
+                        .alertTime(cloudAlertReport.getAlertTime())
+                        .alertDuration(cloudAlertReport.getAlertDuration())
+                        .priority(cloudAlertReport.getPriority())
+                        .reportType(cloudAlertReport.getReportType())
+                        .labels(cloudAlertReport.getLabels())
+                        .annotations(cloudAlertReport.getAnnotations())
+                        .build();
+            } catch (Exception e) {
+                log.error("[alert report] parse cloud service alarm content 
failed! cloud service: {} conrent: {}",
+                        cloudService.name(), alertReport);
+            }
+        } else {
+            alert = AlertReport.builder()
+                    .content("error do not has cloud service api")
+                    .alertName("/api/alerts/report/" + cloudServiceName)
+                    .alertTime(new Date().getTime())
+                    .priority(1)
+                    .reportType(1)
+                    .build();
+        }
+        Optional.ofNullable(alert).ifPresent(this::addNewAlertReport);
+    }
+
     @Override
     public List<Alert> getAlerts(Specification<Alert> specification) {
 
diff --git 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertServiceTest.java
 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertServiceTest.java
index 9a7606362..229874659 100644
--- 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertServiceTest.java
+++ 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertServiceTest.java
@@ -17,13 +17,46 @@
 
 package org.apache.hertzbeat.alert.service;
 
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.hertzbeat.alert.dao.AlertDao;
+import org.apache.hertzbeat.alert.dto.AlertPriorityNum;
+import org.apache.hertzbeat.alert.dto.TenCloudAlertReport;
+import org.apache.hertzbeat.alert.reduce.AlarmCommonReduce;
+import org.apache.hertzbeat.alert.service.impl.AlertServiceImpl;
+import org.apache.hertzbeat.common.entity.alerter.Alert;
+import org.apache.hertzbeat.common.entity.dto.AlertReport;
+import org.apache.hertzbeat.common.util.JsonUtil;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 
 /**
  * Test case for {@link AlertService}
  */
+@ExtendWith(MockitoExtension.class)
 class AlertServiceTest {
+    @Mock
+    private AlertDao alertDao;
+
+    @Mock
+    private AlarmCommonReduce alarmCommonReduce;
+
+    @InjectMocks
+    private AlertServiceImpl alertService;
 
     @BeforeEach
     void setUp() {
@@ -31,29 +64,76 @@ class AlertServiceTest {
 
     @Test
     void addAlert() {
+        Alert alert = new Alert();
+        assertDoesNotThrow(() -> alertService.addAlert(alert));
+        verify(alertDao, times(1)).save(alert);
     }
 
     @Test
     void getAlerts() {
+        // todo
     }
 
     @Test
     void deleteAlerts() {
+        HashSet<Long> ids = new HashSet<>();
+        ids.add(1L);
+        ids.add(2L);
+        assertDoesNotThrow(() -> alertService.deleteAlerts(ids));
+        verify(alertDao, times(1)).deleteAlertsByIdIn(ids);
     }
 
     @Test
     void clearAlerts() {
+        assertDoesNotThrow(() -> alertService.clearAlerts());
+        verify(alertDao, times(1)).deleteAll();
     }
 
     @Test
     void editAlertStatus() {
+        Byte status = 0;
+        List<Long> ids = List.of(1L, 2L, 3L);
+        assertDoesNotThrow(() -> alertService.editAlertStatus(status, ids));
+        verify(alertDao, times(1)).updateAlertsStatus(status, ids);
     }
 
     @Test
     void getAlertsSummary() {
+        List<AlertPriorityNum> priorityNums = new ArrayList<>();
+        priorityNums.add(new AlertPriorityNum((byte) 1, 100));
+        when(alertDao.findAlertPriorityNum()).thenReturn(priorityNums);
+
+        assertDoesNotThrow(() -> alertService.getAlertsSummary());
+        verify(alertDao, times(1)).findAlertPriorityNum();
+        verify(alertDao, times(1)).count();
+
+        assertNotNull(alertService.getAlertsSummary());
     }
 
     @Test
     void addNewAlertReport() {
+        AlertReport alertReport = AlertReport.builder()
+                .annotations(new HashMap<>())
+                .priority(0)
+                .alertTime(System.currentTimeMillis())
+                .build();
+        assertDoesNotThrow(() -> alertService.addNewAlertReport(alertReport));
+        verify(alarmCommonReduce, 
times(1)).reduceAndSendAlarm(any(Alert.class));
+    }
+
+    @Test
+    void addNewAlertReportFromCloud() {
+        TenCloudAlertReport alertReport = TenCloudAlertReport.builder()
+                .firstOccurTime("2024-08-01 11:30:00")
+                .durationTime(100)
+                .build();
+        String reportJson = JsonUtil.toJson(alertReport);
+        assertDoesNotThrow(() -> 
alertService.addNewAlertReportFromCloud("tencloud", reportJson));
+        verify(alarmCommonReduce, 
times(1)).reduceAndSendAlarm(any(Alert.class));
+
+        alertService.addNewAlertReportFromCloud("alicloud", reportJson);
+        reset(alarmCommonReduce);
+        verify(alarmCommonReduce, 
times(0)).reduceAndSendAlarm(any(Alert.class));
+
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to