Skip to content

Commit

Permalink
[improve] add MemoryDataStorage unit test (apache#2515)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: Calvin <[email protected]>
  • Loading branch information
yuluo-yx and Calvin979 authored Aug 12, 2024
1 parent b39602f commit 3a2350b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Map<String, String> getAnnotations() {
@Override
public String getContent() {
StringBuilder contentBuilder = new StringBuilder();
// 判断类型
// Check Type.
if (EVENT.equals(getAlarmType())) {
contentBuilder
.append("[")
Expand Down Expand Up @@ -225,5 +225,3 @@ public String getContent() {
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/

@ExtendWith(SpringExtension.class)
class WarehouseServiceImplTest {
class WarehouseServiceTest {

@Mock
private AbstractRealTimeDataStorage realTimeDataStorage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CollectRep.MetricsData> 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<CollectRep.MetricsData> 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<CollectRep.MetricsData> result = memoryDataStorage.getCurrentMetricsData(1L);
assertTrue(result.isEmpty());
}

}

0 comments on commit 3a2350b

Please sign in to comment.