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

gongchao 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 7eef0c21f [Improve] add push module service unit test (#2452)
7eef0c21f is described below

commit 7eef0c21f121a33089f9a6cb157ab9d2c8abafdf
Author: YuLuo <[email protected]>
AuthorDate: Sun Aug 4 00:00:49 2024 +0800

    [Improve] add push module service unit test (#2452)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../push/service/impl/PushServiceImpl.java         |   2 +-
 .../push/service/PushGatewayServiceTest.java       | 106 ++++++++++++++++
 .../hertzbeat/push/service/PushServiceTest.java    | 133 +++++++++++++++++++++
 3 files changed, 240 insertions(+), 1 deletion(-)

diff --git 
a/push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
 
b/push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
index cbd22bdb5..d3780a399 100644
--- 
a/push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
+++ 
b/push/src/main/java/org/apache/hertzbeat/push/service/impl/PushServiceImpl.java
@@ -60,7 +60,7 @@ public class PushServiceImpl implements PushService {
 
     private static final long deleteBeforeTime = deleteMetricsPeriod / 2;
 
-    PushServiceImpl(){
+    public PushServiceImpl(){
         monitorIdCache = new HashMap<>();
         lastPushMetrics = new HashMap<>();
 
diff --git 
a/push/src/test/java/org/apache/hertzbeat/push/service/PushGatewayServiceTest.java
 
b/push/src/test/java/org/apache/hertzbeat/push/service/PushGatewayServiceTest.java
new file mode 100644
index 000000000..9cec4edc5
--- /dev/null
+++ 
b/push/src/test/java/org/apache/hertzbeat/push/service/PushGatewayServiceTest.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hertzbeat.push.service;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.hertzbeat.common.util.prometheus.Metric;
+import org.apache.hertzbeat.common.util.prometheus.PrometheusUtil;
+import org.apache.hertzbeat.push.service.impl.PushGatewayServiceImpl;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+
+/**
+ * test case for {@link PushGatewayServiceImpl}
+ */
+
+@ExtendWith(MockitoExtension.class)
+class PushGatewayServiceImplTest {
+
+       @InjectMocks
+       private PushGatewayServiceImpl pushGatewayService;
+
+       private MockedStatic<PrometheusUtil> prometheusUtilMock;
+
+       @BeforeEach
+       void setUp() {
+
+               prometheusUtilMock = mockStatic(PrometheusUtil.class);
+       }
+
+       @AfterEach
+       void tearDown() {
+
+               prometheusUtilMock.close();
+       }
+
+       @Test
+       void testPushMetricsDataSuccess() throws IOException {
+
+               String mockData = "some metric data";
+               InputStream inputStream = new 
ByteArrayInputStream(mockData.getBytes());
+               List<Metric> mockMetrics = Collections.singletonList(new 
Metric());
+
+               prometheusUtilMock.when(
+                               () -> 
PrometheusUtil.parseMetrics(any(InputStream.class))
+               ).thenReturn(mockMetrics);
+
+               boolean result = 
pushGatewayService.pushMetricsData(inputStream);
+
+               assertTrue(result);
+               prometheusUtilMock.verify(
+                               () -> 
PrometheusUtil.parseMetrics(any(InputStream.class)),
+                               times(1)
+               );
+       }
+
+       @Test
+       void testPushMetricsDataFailure() throws IOException {
+
+               String mockData = "some metric data";
+               InputStream inputStream = new 
ByteArrayInputStream(mockData.getBytes());
+
+               prometheusUtilMock.when(() -> 
PrometheusUtil.parseMetrics(any(InputStream.class))).thenReturn(null);
+
+               boolean result = 
pushGatewayService.pushMetricsData(inputStream);
+
+               assertFalse(result);
+               prometheusUtilMock.verify(
+                               () -> 
PrometheusUtil.parseMetrics(any(InputStream.class)),
+                               times(1)
+               );
+       }
+
+}
diff --git 
a/push/src/test/java/org/apache/hertzbeat/push/service/PushServiceTest.java 
b/push/src/test/java/org/apache/hertzbeat/push/service/PushServiceTest.java
new file mode 100644
index 000000000..fdf9d4578
--- /dev/null
+++ b/push/src/test/java/org/apache/hertzbeat/push/service/PushServiceTest.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hertzbeat.push.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.apache.hertzbeat.common.entity.manager.Monitor;
+import org.apache.hertzbeat.common.entity.push.PushMetrics;
+import org.apache.hertzbeat.common.entity.push.PushMetricsDto;
+import org.apache.hertzbeat.push.dao.PushMetricsDao;
+import org.apache.hertzbeat.push.dao.PushMonitorDao;
+import org.apache.hertzbeat.push.service.impl.PushServiceImpl;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * test case for {@link PushServiceImpl}
+ */
+
+@ExtendWith(MockitoExtension.class)
+class PushServiceImplTest {
+
+       @Mock
+       private PushMonitorDao monitorDao;
+
+       @Mock
+       private PushMetricsDao metricsDao;
+
+       @InjectMocks
+       private PushServiceImpl pushService;
+
+       @BeforeEach
+       void setUp() {
+
+               pushService = new PushServiceImpl();
+
+               ReflectionTestUtils.setField(pushService, "monitorDao", 
monitorDao);
+               ReflectionTestUtils.setField(pushService, "metricsDao", 
metricsDao);
+       }
+
+       @Test
+       void testPushMetricsData() {
+
+               PushMetricsDto pushMetricsDto = new PushMetricsDto();
+               List<PushMetricsDto.Metrics> metricsList = new ArrayList<>();
+               PushMetricsDto.Metrics metrics = new PushMetricsDto.Metrics();
+               metrics.setMonitorId(1L);
+               metricsList.add(metrics);
+               pushMetricsDto.setMetricsList(metricsList);
+
+               when(monitorDao.findById(anyLong())).thenReturn(Optional.of(new 
Monitor()));
+
+               pushService.pushMetricsData(pushMetricsDto);
+
+               verify(metricsDao, times(1)).saveAll(any());
+       }
+
+       @Test
+       void testGetPushMetricData() {
+
+               Long monitorId = 1L;
+               Long time = System.currentTimeMillis();
+               PushMetrics pushMetrics = PushMetrics.builder()
+                               .monitorId(monitorId)
+                               .time(time)
+                               .metrics("[{\"key\":\"value\"}]")
+                               .build();
+
+               
when(metricsDao.findFirstByMonitorIdOrderByTimeDesc(monitorId)).thenReturn(pushMetrics);
+
+               PushMetricsDto result = 
pushService.getPushMetricData(monitorId, time);
+
+               assertEquals(1, result.getMetricsList().size());
+               assertEquals(monitorId, 
result.getMetricsList().get(0).getMonitorId());
+       }
+
+       @Test
+       void testGetPushMetricDataTimeInvalid() {
+
+               Long monitorId = 1L;
+               Long time = System.currentTimeMillis() + 10000;
+               PushMetrics pushMetrics = PushMetrics.builder()
+                               .monitorId(monitorId)
+                               .time(System.currentTimeMillis())
+                               .metrics("[{\"key\":\"value\"}]")
+                               .build();
+
+               
when(metricsDao.findFirstByMonitorIdOrderByTimeDesc(monitorId)).thenReturn(pushMetrics);
+
+               PushMetricsDto result = 
pushService.getPushMetricData(monitorId, time);
+
+               assertTrue(result.getMetricsList().isEmpty());
+       }
+
+       @Test
+       void testDeletePeriodically() {
+
+               pushService.deletePeriodically();
+               verify(metricsDao, times(1)).deleteAllByTimeBefore(anyLong());
+       }
+
+}


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

Reply via email to