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 b0438da53 [Improve] add KafkaMetricsDataSerializer & 
KafkaMetricsDataDeserializer unit test (#2431)
b0438da53 is described below

commit b0438da537206c862b8fbb3a12735b0ce7a756ec
Author: YuLuo <[email protected]>
AuthorDate: Fri Aug 2 18:01:53 2024 +0800

    [Improve] add KafkaMetricsDataSerializer & KafkaMetricsDataDeserializer 
unit test (#2431)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../serialize/KafkaMetricsDataSerializer.java      |   9 ++
 .../KafkaMetricsDataDeserializerTest.java          | 102 +++++++++++++++++++++
 .../serialize/KafkaMetricsDataSerializerTest.java  | 100 ++++++++++++++++++++
 3 files changed, 211 insertions(+)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializer.java
 
b/common/src/main/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializer.java
index 015dbf7ae..bcd2fe6d6 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializer.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializer.java
@@ -18,6 +18,7 @@
 package org.apache.hertzbeat.common.serialize;
 
 import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.common.entity.message.CollectRep;
 import org.apache.kafka.common.header.Headers;
 import org.apache.kafka.common.serialization.Serializer;
@@ -25,6 +26,8 @@ import org.apache.kafka.common.serialization.Serializer;
 /**
  * kafka metrics data serializer
  */
+
+@Slf4j
 public class KafkaMetricsDataSerializer implements 
Serializer<CollectRep.MetricsData> {
 
     @Override
@@ -34,6 +37,12 @@ public class KafkaMetricsDataSerializer implements 
Serializer<CollectRep.Metrics
 
     @Override
     public byte[] serialize(String s, CollectRep.MetricsData metricsData) {
+
+        if (metricsData == null) {
+            log.error("metricsData is null");
+            return null;
+        }
+
         return metricsData.toByteArray();
     }
 
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataDeserializerTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataDeserializerTest.java
new file mode 100644
index 000000000..f3da7ce17
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataDeserializerTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.serialize;
+
+import java.util.Map;
+
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.apache.kafka.common.header.Headers;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * test case for {@link KafkaMetricsDataDeserializer}
+ */
+
+class KafkaMetricsDataDeserializerTest {
+
+       private KafkaMetricsDataDeserializer deserializer;
+
+       @Mock
+       private Map<String, ?> configs;
+
+       @Mock
+       private Headers headers;
+
+       @BeforeEach
+       void setUp() {
+
+               MockitoAnnotations.openMocks(this);
+
+               deserializer = new KafkaMetricsDataDeserializer();
+       }
+
+       @Test
+       void testConfigure() {
+
+               deserializer.configure(configs, false);
+       }
+
+       @Test
+       void testDeserializeWithBytes() {
+
+               CollectRep.MetricsData expectedMetricsData = 
CollectRep.MetricsData.newBuilder()
+                               .setMetrics("someValue")
+                               .setApp("linux")
+                               .build();
+               byte[] bytes = expectedMetricsData.toByteArray();
+
+               CollectRep.MetricsData actualMetricsData = 
deserializer.deserialize("", bytes);
+
+               assertEquals(expectedMetricsData, actualMetricsData);
+       }
+
+       @Test
+       void testDeserializeWithInvalidBytes() {
+
+               byte[] invalidBytes = "invalid data".getBytes();
+
+               assertThrows(RuntimeException.class, () -> 
deserializer.deserialize("", invalidBytes));
+       }
+
+       @Test
+       void testDeserializeWithHeaders() {
+
+               CollectRep.MetricsData expectedMetricsData = 
CollectRep.MetricsData.newBuilder()
+                               .setMetrics("someValue")
+                               .setApp("linux")
+                               .build();
+               byte[] bytes = expectedMetricsData.toByteArray();
+
+               CollectRep.MetricsData actualMetricsData = 
deserializer.deserialize("topic", headers, bytes);
+
+               assertEquals(expectedMetricsData, actualMetricsData);
+       }
+
+       @Test
+       void testClose() {
+
+               deserializer.close();
+       }
+
+}
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializerTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializerTest.java
new file mode 100644
index 000000000..63d7a1dd1
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/serialize/KafkaMetricsDataSerializerTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.serialize;
+
+import java.util.Map;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.apache.kafka.common.header.Headers;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * test case for {@link KafkaMetricsDataSerializer}
+ */
+
+class KafkaMetricsDataSerializerTest {
+
+       private KafkaMetricsDataSerializer serializer;
+
+       @Mock
+       private Map<String, ?> configs;
+
+       @Mock
+       private Headers headers;
+
+       @BeforeEach
+       void setUp() {
+
+               MockitoAnnotations.openMocks(this);
+
+               serializer = new KafkaMetricsDataSerializer();
+       }
+
+       @Test
+       void testConfigure() {
+
+               serializer.configure(configs, false);
+       }
+
+       @Test
+       void testSerializeWithMetricsData() {
+
+               CollectRep.MetricsData metricsData = 
CollectRep.MetricsData.newBuilder()
+                               .setMetrics("someValue")
+                               .setApp("linux")
+                               .build();
+               byte[] bytes = serializer.serialize("", metricsData);
+
+               assertNotNull(bytes);
+               assertArrayEquals(metricsData.toByteArray(), bytes);
+       }
+
+       @Test
+       void testSerializeWithNullMetricsData() {
+
+               byte[] bytes = serializer.serialize("", null);
+
+               assertNull(bytes);
+       }
+
+       @Test
+       void testSerializeWithHeaders() {
+
+               CollectRep.MetricsData metricsData = 
CollectRep.MetricsData.newBuilder()
+                               .setMetrics("someValue")
+                               .setApp("linux")
+                               .build();
+               byte[] expectedBytes = metricsData.toByteArray();
+               byte[] bytes = serializer.serialize("topic", headers, 
metricsData);
+
+               assertArrayEquals(expectedBytes, bytes);
+       }
+
+       @Test
+       void testClose() {
+
+               serializer.close();
+       }
+
+}


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

Reply via email to