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

liutianyou 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 d5d01459e7 [Fix] fix custom monitoring bulletin `NullPointerException` 
(#3448)
d5d01459e7 is described below

commit d5d01459e7c8327342577d484a68c3f22fff37c0
Author: Duansg <[email protected]>
AuthorDate: Fri Jun 13 08:48:51 2025 +0800

    [Fix] fix custom monitoring bulletin `NullPointerException` (#3448)
    
    Co-authored-by: Calvin <[email protected]>
    Co-authored-by: aias00 <[email protected]>
    Co-authored-by: kangli <[email protected]>
    Co-authored-by: liutianyou <[email protected]>
---
 .../manager/service/impl/BulletinServiceImpl.java  |  3 ++
 .../manager/service/BulletinServiceTest.java       | 42 ++++++++++++----------
 2 files changed, 26 insertions(+), 19 deletions(-)

diff --git 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
index 6800119731..0376ea1b45 100644
--- 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
+++ 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java
@@ -120,6 +120,9 @@ public class BulletinServiceImpl implements BulletinService 
{
         List<BulletinMetricsData.Data> dataList = new ArrayList<>();
         for (Long monitorId : bulletin.getMonitorIds()) {
             Monitor monitor = monitorService.getMonitor(monitorId);
+            if (null == monitor) {
+                continue;
+            }
             BulletinMetricsData.Data.DataBuilder dataBuilder = 
BulletinMetricsData.Data.builder()
                     .monitorId(monitorId)
                     .monitorName(monitor.getName())
diff --git 
a/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java
 
b/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java
index d746292574..dd9768347b 100644
--- 
a/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java
+++ 
b/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java
@@ -17,22 +17,8 @@
 
 package org.apache.hertzbeat.manager.service;
 
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.when;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.hertzbeat.common.entity.manager.Monitor;
 import org.apache.hertzbeat.common.entity.manager.Bulletin;
-import org.apache.hertzbeat.manager.pojo.dto.BulletinMetricsData;
+import org.apache.hertzbeat.common.entity.manager.Monitor;
 import org.apache.hertzbeat.manager.dao.BulletinDao;
 import org.apache.hertzbeat.manager.service.impl.BulletinServiceImpl;
 import org.apache.hertzbeat.warehouse.store.realtime.RealTimeDataReader;
@@ -46,6 +32,21 @@ import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.jpa.domain.Specification;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
 /**
  * Test case for {@link BulletinService}
  */
@@ -145,14 +146,17 @@ public class BulletinServiceTest {
         fields.put("1", List.of("1", "2"));
         bulletin.setFields(fields);
 
-        BulletinMetricsData.BulletinMetricsDataBuilder contentBuilder = 
BulletinMetricsData.builder();
-
         Monitor monitor = new Monitor();
 
         
when(bulletinDao.findById(any(Long.class))).thenReturn(java.util.Optional.of(bulletin));
         when(realTimeDataReader.getCurrentMetricsData(any(), 
any(String.class))).thenReturn(null);
-        when(monitorService.getMonitor(any(Long.class))).thenReturn(monitor);
-        
assertNotNull(bulletinService.buildBulletinMetricsData(any(Long.class)));
+
+        when(monitorService.getMonitor(any(Long.class))).thenReturn(null);
+        
assertTrue(bulletinService.buildBulletinMetricsData(any(Long.class)).getContent().isEmpty());
+
+        when(monitorService.getMonitor(1L)).thenReturn(monitor);
+        
assertFalse(bulletinService.buildBulletinMetricsData(2L).getContent().isEmpty());
+
     }
 
     @Test


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

Reply via email to