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 15e9bd065 [Improve] add redis data queue unit test (#2330)
15e9bd065 is described below

commit 15e9bd065656b53b585652e8560a6e4c3acd3196
Author: YuLuo <[email protected]>
AuthorDate: Tue Jul 23 23:13:03 2024 +0800

    [Improve] add redis data queue unit test (#2330)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Signed-off-by: YuLuo <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../common/queue/impl/RedisCommonDataQueue.java    |  23 ++-
 .../queue/impl/RedisCommonDataQueueTest.java       | 157 +++++++++++++++++++++
 2 files changed, 166 insertions(+), 14 deletions(-)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueue.java
 
b/common/src/main/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueue.java
index 5d32f9e8b..701b7dd35 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueue.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueue.java
@@ -17,7 +17,6 @@
 
 package org.apache.hertzbeat.common.queue.impl;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
 import io.lettuce.core.RedisClient;
 import io.lettuce.core.RedisURI;
 import io.lettuce.core.api.StatefulRedisConnection;
@@ -27,6 +26,8 @@ import org.apache.hertzbeat.common.config.CommonProperties;
 import org.apache.hertzbeat.common.entity.alerter.Alert;
 import org.apache.hertzbeat.common.entity.message.CollectRep;
 import org.apache.hertzbeat.common.queue.CommonDataQueue;
+import org.apache.hertzbeat.common.util.JsonUtil;
+import org.apache.hertzbeat.common.util.ProtoJsonUtil;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Configuration;
@@ -50,7 +51,6 @@ public class RedisCommonDataQueue implements CommonDataQueue, 
DisposableBean {
     private final String metricsDataQueueNameToPersistentStorage;
     private final String metricsDataQueueNameToRealTimeStorage;
     private final String alertsDataQueueName;
-    private final ObjectMapper objectMapper;
     private final CommonProperties.RedisProperties redisProperties;
 
     public RedisCommonDataQueue(CommonProperties properties) {
@@ -61,11 +61,7 @@ public class RedisCommonDataQueue implements 
CommonDataQueue, DisposableBean {
         }
 
         this.redisProperties = properties.getQueue().getRedis();
-        RedisURI build = RedisURI.builder()
-                .withHost(redisProperties.getRedisHost())
-                .withPort(redisProperties.getRedisPort())
-                .build();
-        System.out.println(build.toString());
+
         this.redisClient = RedisClient.create(
                 RedisURI.builder()
                         .withHost(redisProperties.getRedisHost())
@@ -78,7 +74,6 @@ public class RedisCommonDataQueue implements CommonDataQueue, 
DisposableBean {
         this.metricsDataQueueNameToPersistentStorage = 
redisProperties.getMetricsDataQueueNameToPersistentStorage();
         this.metricsDataQueueNameToRealTimeStorage = 
redisProperties.getMetricsDataQueueNameToRealTimeStorage();
         this.alertsDataQueueName = redisProperties.getAlertsDataQueueName();
-        this.objectMapper = new ObjectMapper();
     }
 
     @Override
@@ -87,7 +82,7 @@ public class RedisCommonDataQueue implements CommonDataQueue, 
DisposableBean {
         try {
             String alertJson = syncCommands.rpop(alertsDataQueueName);
             if (alertJson != null) {
-                return objectMapper.readValue(alertJson, Alert.class);
+                return JsonUtil.fromJson(alertJson, Alert.class);
             }
         } catch (Exception e) {
             log.error("please config common.queue.redis props correctly", e);
@@ -102,7 +97,7 @@ public class RedisCommonDataQueue implements 
CommonDataQueue, DisposableBean {
         try {
             String metricsDataJson = 
syncCommands.rpop(metricsDataQueueNameToAlerter);
             if (metricsDataJson != null) {
-                return objectMapper.readValue(metricsDataJson, 
CollectRep.MetricsData.class);
+                return (CollectRep.MetricsData) 
ProtoJsonUtil.toProtobuf(metricsDataJson, CollectRep.MetricsData.newBuilder());
             }
         } catch (Exception e) {
             log.error(e.getMessage());
@@ -117,7 +112,7 @@ public class RedisCommonDataQueue implements 
CommonDataQueue, DisposableBean {
         try {
             String metricsDataJson = 
syncCommands.rpop(metricsDataQueueNameToPersistentStorage);
             if (metricsDataJson != null) {
-                return objectMapper.readValue(metricsDataJson, 
CollectRep.MetricsData.class);
+                return JsonUtil.fromJson(metricsDataJson, 
CollectRep.MetricsData.class);
             }
         } catch (Exception e) {
             log.error(e.getMessage());
@@ -132,7 +127,7 @@ public class RedisCommonDataQueue implements 
CommonDataQueue, DisposableBean {
         try {
             String metricsDataJson = 
syncCommands.rpop(metricsDataQueueNameToRealTimeStorage);
             if (metricsDataJson != null) {
-                return objectMapper.readValue(metricsDataJson, 
CollectRep.MetricsData.class);
+                return JsonUtil.fromJson(metricsDataJson, 
CollectRep.MetricsData.class);
             }
         } catch (Exception e) {
             log.error(e.getMessage());
@@ -145,7 +140,7 @@ public class RedisCommonDataQueue implements 
CommonDataQueue, DisposableBean {
     public void sendAlertsData(Alert alert) {
 
         try {
-            String alertJson = objectMapper.writeValueAsString(alert);
+            String alertJson = JsonUtil.toJson(alert);
             syncCommands.lpush(alertsDataQueueName, alertJson);
         } catch (Exception e) {
             log.error(e.getMessage());
@@ -157,7 +152,7 @@ public class RedisCommonDataQueue implements 
CommonDataQueue, DisposableBean {
     public void sendMetricsData(CollectRep.MetricsData metricsData) {
 
         try {
-            String metricsDataJson = 
objectMapper.writeValueAsString(metricsData);
+            String metricsDataJson = ProtoJsonUtil.toJsonStr(metricsData);
             syncCommands.lpush(metricsDataQueueNameToAlerter, metricsDataJson);
             syncCommands.lpush(metricsDataQueueNameToPersistentStorage, 
metricsDataJson);
             syncCommands.lpush(metricsDataQueueNameToRealTimeStorage, 
metricsDataJson);
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueueTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueueTest.java
new file mode 100644
index 000000000..2344eca4f
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/queue/impl/RedisCommonDataQueueTest.java
@@ -0,0 +1,157 @@
+/*
+ * 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.common.queue.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.lettuce.core.RedisClient;
+import io.lettuce.core.RedisURI;
+import io.lettuce.core.api.StatefulRedisConnection;
+import io.lettuce.core.api.sync.RedisCommands;
+import org.apache.hertzbeat.common.config.CommonProperties;
+import org.apache.hertzbeat.common.entity.alerter.Alert;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.apache.hertzbeat.common.util.ProtoJsonUtil;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.MockitoAnnotations;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * test for {@link RedisCommonDataQueue}
+ */
+
+@ExtendWith(MockitoExtension.class)
+class RedisCommonDataQueueTest {
+
+       @Mock
+       private StatefulRedisConnection<String, String> connection;
+
+       @Mock
+       private RedisCommands<String, String> syncCommands;
+
+       @Mock
+       private ObjectMapper objectMapper;
+
+       private RedisClient redisClient;
+
+       private CommonProperties commonProperties;
+
+       private CommonProperties.RedisProperties redisProperties;
+
+       private RedisCommonDataQueue redisCommonDataQueue;
+
+       @BeforeEach
+       public void setUp() {
+
+               MockitoAnnotations.openMocks(this);
+
+               redisClient = mock(RedisClient.class);
+               commonProperties = mock(CommonProperties.class);
+               redisProperties = mock(CommonProperties.RedisProperties.class);
+               CommonProperties.DataQueueProperties dataQueueProperties = 
mock(CommonProperties.DataQueueProperties.class);
+
+               
when(commonProperties.getQueue()).thenReturn(dataQueueProperties);
+               
when(dataQueueProperties.getRedis()).thenReturn(redisProperties);
+
+               when(redisProperties.getRedisHost()).thenReturn("localhost");
+               when(redisProperties.getRedisPort()).thenReturn(6379);
+               
when(redisProperties.getMetricsDataQueueNameToAlerter()).thenReturn("metricsDataQueueToAlerter");
+               
when(redisProperties.getMetricsDataQueueNameToPersistentStorage()).thenReturn("metricsDataQueueToPersistentStorage");
+               
when(redisProperties.getMetricsDataQueueNameToRealTimeStorage()).thenReturn("metricsDataQueueToRealTimeStorage");
+               
when(redisProperties.getAlertsDataQueueName()).thenReturn("alertsDataQueue");
+
+               try (MockedStatic<RedisClient> mockedRedisClient = 
mockStatic(RedisClient.class)) {
+
+                       mockedRedisClient.when(() -> RedisClient.create(
+                                       any(RedisURI.class))
+                       ).thenReturn(redisClient);
+
+                       when(redisClient.connect()).thenReturn(connection);
+                       when(connection.sync()).thenReturn(syncCommands);
+
+                       redisCommonDataQueue = new 
RedisCommonDataQueue(commonProperties);
+               }
+       }
+
+       @Test
+       public void testPollAlertsData() throws Exception {
+
+               String alertJson = "{\"id\":\"1\",\"content\":\"Test Alert\"}";
+               Alert expectedAlert = Alert.builder().id(1L).content("Test 
Alert").build();
+
+               when(syncCommands.rpop(anyString())).thenReturn(alertJson);
+               Alert actualAlert = redisCommonDataQueue.pollAlertsData();
+               assertEquals(expectedAlert, actualAlert);
+       }
+
+       @Test
+       public void testPollMetricsDataToAlerter() throws Exception {
+
+               CollectRep.MetricsData metricsData = 
CollectRep.MetricsData.newBuilder().setMetrics("test metrics").build();
+               String metricsDataJson = "{\"metrics\":\"test metrics\"}";
+
+               
when(syncCommands.rpop("metricsDataQueueToAlerter")).thenReturn(metricsDataJson);
+
+               CollectRep.MetricsData actualMetricsData = 
redisCommonDataQueue.pollMetricsDataToAlerter();
+               assertEquals(metricsData, actualMetricsData);
+       }
+
+       @Test
+       public void testSendMetricsData() throws Exception {
+               CollectRep.MetricsData metricsData = 
CollectRep.MetricsData.newBuilder().setMetrics("test metrics").build();
+               String metricsDataJson = ProtoJsonUtil.toJsonStr(metricsData);
+
+               redisCommonDataQueue.sendMetricsData(metricsData);
+
+               verify(syncCommands).lpush("metricsDataQueueToAlerter", 
metricsDataJson);
+               
verify(syncCommands).lpush("metricsDataQueueToPersistentStorage", 
metricsDataJson);
+               verify(syncCommands).lpush("metricsDataQueueToRealTimeStorage", 
metricsDataJson);
+       }
+
+       @Test
+       public void testSendAlertsData() throws Exception {
+
+               Alert alert = Alert.builder()
+                               .content("test")
+                               .build();
+               String alertJson = 
"{\"id\":null,\"target\":null,\"alertDefineId\":null,\"priority\":0,\"content\":\"test\",\"status\":0,\"times\":null,\"firstAlarmTime\":null,\"lastAlarmTime\":null,\"triggerTimes\":null,\"tags\":null,\"creator\":null,\"modifier\":null,\"gmtCreate\":null,\"gmtUpdate\":null}";
+               redisCommonDataQueue.sendAlertsData(alert);
+
+               verify(syncCommands).lpush("alertsDataQueue", alertJson);
+       }
+
+       @Test
+       public void testDestroy() {
+               redisCommonDataQueue.destroy();
+
+               verify(connection).close();
+               verify(redisClient).shutdown();
+       }
+
+}
\ No newline at end of file


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

Reply via email to