[Fix] fix custom monitoring bulletin NullPointerException (#3448)

Co-authored-by: Calvin <zhengqiwei@apache.org>
Co-authored-by: aias00 <liuhongyu@apache.org>
Co-authored-by: kangli <likang@apache.org>
Co-authored-by: liutianyou <tianyou.liu@qq.com>
This commit is contained in:
Duansg
2025-06-13 08:48:51 +08:00
committed by GitHub
co-authored by Calvin aias00 kangli liutianyou
parent d077c72211
commit d5d01459e7
2 changed files with 26 additions and 19 deletions
@@ -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())
@@ -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