diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/TenCloudAlertReport.java b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/TenCloudAlertReport.java index 6ea51c1b047..99846e6a14b 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/TenCloudAlertReport.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/TenCloudAlertReport.java @@ -174,7 +174,7 @@ public Map getAnnotations() { @Override public String getContent() { StringBuilder contentBuilder = new StringBuilder(); - // 判断类型 + // Check Type. if (EVENT.equals(getAlarmType())) { contentBuilder .append("[") @@ -225,5 +225,3 @@ public String getContent() { } } - - diff --git a/warehouse/src/test/java/org/apache/hertzbeat/warehouse/service/WarehouseServiceTest.java b/warehouse/src/test/java/org/apache/hertzbeat/warehouse/service/WarehouseServiceTest.java index ec771f121d4..43c97e4043a 100644 --- a/warehouse/src/test/java/org/apache/hertzbeat/warehouse/service/WarehouseServiceTest.java +++ b/warehouse/src/test/java/org/apache/hertzbeat/warehouse/service/WarehouseServiceTest.java @@ -43,7 +43,7 @@ */ @ExtendWith(SpringExtension.class) -class WarehouseServiceImplTest { +class WarehouseServiceTest { @Mock private AbstractRealTimeDataStorage realTimeDataStorage; diff --git a/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MemoryDataStorageTest.java b/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MemoryDataStorageTest.java index da0e1f4dfc1..d066d1b303b 100644 --- a/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MemoryDataStorageTest.java +++ b/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MemoryDataStorageTest.java @@ -17,24 +17,107 @@ package org.apache.hertzbeat.warehouse.store; +import java.util.List; +import org.apache.hertzbeat.common.entity.message.CollectRep; import org.apache.hertzbeat.warehouse.store.realtime.memory.MemoryDataStorage; +import org.apache.hertzbeat.warehouse.store.realtime.memory.MemoryProperties; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Test case for {@link MemoryDataStorage} */ + class MemoryDataStorageTest { + @Mock + private MemoryProperties memoryProperties; + + @InjectMocks + private MemoryDataStorage memoryDataStorage; + @BeforeEach void setUp() { + + MockitoAnnotations.openMocks(this); + + when(memoryProperties.initSize()).thenReturn(null); + memoryDataStorage = new MemoryDataStorage(memoryProperties); } @Test - void getCurrentMetricsData() { + void testGetCurrentMetricsDataByMetric() { + + Long monitorId = 1L; + String metric = "cpuUsage"; + CollectRep.MetricsData metricsData = mock(CollectRep.MetricsData.class); + + memoryDataStorage.saveData(metricsData); + + CollectRep.MetricsData result = memoryDataStorage.getCurrentMetricsData(monitorId, metric); + + assertNull(result); } @Test - void destroy() { + void testGetCurrentMetricsData() { + + Long monitorId = 1L; + CollectRep.MetricsData metricsData1 = mock(CollectRep.MetricsData.class); + CollectRep.MetricsData metricsData2 = mock(CollectRep.MetricsData.class); + + when(metricsData1.getId()).thenReturn(monitorId); + when(metricsData1.getMetrics()).thenReturn("cpuUsage"); + when(metricsData1.getCode()).thenReturn(CollectRep.Code.SUCCESS); + + when(metricsData2.getId()).thenReturn(monitorId); + when(metricsData2.getMetrics()).thenReturn("memoryUsage"); + when(metricsData2.getCode()).thenReturn(CollectRep.Code.SUCCESS); + + memoryDataStorage.saveData(metricsData1); + memoryDataStorage.saveData(metricsData2); + + List result = memoryDataStorage.getCurrentMetricsData(monitorId); + + assertEquals(2, result.size()); + assertTrue(result.contains(metricsData1)); + assertTrue(result.contains(metricsData2)); + } + + @Test + void testSaveDataFailure() { + + CollectRep.MetricsData metricsData = mock(CollectRep.MetricsData.class); + when(metricsData.getCode()).thenReturn(CollectRep.Code.FAIL); + + memoryDataStorage.saveData(metricsData); + + List result = memoryDataStorage.getCurrentMetricsData(metricsData.getId()); + assertTrue(result.isEmpty()); } + + @Test + void testDestroy() { + + CollectRep.MetricsData metricsData = mock(CollectRep.MetricsData.class); + when(metricsData.getId()).thenReturn(1L); + when(metricsData.getMetrics()).thenReturn("cpuUsage"); + when(metricsData.getCode()).thenReturn(CollectRep.Code.SUCCESS); + + memoryDataStorage.saveData(metricsData); + memoryDataStorage.destroy(); + + List result = memoryDataStorage.getCurrentMetricsData(1L); + assertTrue(result.isEmpty()); + } + }