Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] add MemoryDataStorage unit test #2515

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}
Loading