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 7672502c3 [Improve] add AlertSerializer & AlertDeserializer unit test
(#2430)
7672502c3 is described below
commit 7672502c3d6d21f10167ca228426dd0a6c76d3ec
Author: YuLuo <[email protected]>
AuthorDate: Sat Aug 3 18:19:30 2024 +0800
[Improve] add AlertSerializer & AlertDeserializer unit test (#2430)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../collector/util/TimeExpressionUtil.java | 2 +-
.../common/serialize/AlertDeserializerTest.java | 98 +++++++++++++++++++
.../common/serialize/AlertSerializerTest.java | 108 +++++++++++++++++++++
3 files changed, 207 insertions(+), 1 deletion(-)
diff --git
a/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java
b/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java
index 69f16ca3c..a34d9afa2 100644
---
a/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java
+++
b/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java
@@ -36,7 +36,7 @@ import org.apache.commons.lang3.StringUtils;
/**
* time expression deal util
*/
-public class TimeExpressionUtil {
+public final class TimeExpressionUtil {
private TimeExpressionUtil() {
}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/serialize/AlertDeserializerTest.java
b/common/src/test/java/org/apache/hertzbeat/common/serialize/AlertDeserializerTest.java
new file mode 100644
index 000000000..c90729ff5
--- /dev/null
+++
b/common/src/test/java/org/apache/hertzbeat/common/serialize/AlertDeserializerTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.alerter.Alert;
+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;
+
+/**
+ * test case for {@link AlertDeserializer}
+ */
+
+class AlertDeserializerTest {
+
+ private AlertDeserializer alertDeserializer;
+
+ @Mock
+ private Map<String, ?> configs;
+
+ @Mock
+ private Headers headers;
+
+ @BeforeEach
+ void setUp() {
+
+ MockitoAnnotations.openMocks(this);
+
+ alertDeserializer = new AlertDeserializer();
+ }
+
+ @Test
+ void testConfigure() {
+
+ alertDeserializer.configure(configs, false);
+ }
+
+ @Test
+ void testDeserializeWithBytes() {
+
+ String json = "{\"target\":\"test\",\"content\":\"test\"}";
+ byte[] bytes = json.getBytes();
+ Alert expectedAlert = Alert.builder()
+ .content("test")
+ .target("test")
+ .build();
+
+ Alert actualAlert = alertDeserializer.deserialize("", bytes);
+
+ assertEquals(expectedAlert.getContent(),
actualAlert.getContent());
+ assertEquals(expectedAlert.getTarget(),
actualAlert.getTarget());
+ }
+
+ @Test
+ void testDeserializeWithHeaders() {
+
+ String topic = "alerts";
+ byte[] data =
"{\"target\":\"test\",\"content\":\"test\"}".getBytes();
+
+ Alert expectedAlert = Alert.builder()
+ .content("test")
+ .target("test")
+ .build();
+
+ Alert actualAlert = alertDeserializer.deserialize(topic,
headers, data);
+
+ assertEquals(expectedAlert.getContent(),
actualAlert.getContent());
+ assertEquals(expectedAlert.getTarget(),
actualAlert.getTarget());
+ }
+
+ @Test
+ void testClose() {
+
+ alertDeserializer.close();
+ }
+
+}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/serialize/AlertSerializerTest.java
b/common/src/test/java/org/apache/hertzbeat/common/serialize/AlertSerializerTest.java
new file mode 100644
index 000000000..48ebb9bfb
--- /dev/null
+++
b/common/src/test/java/org/apache/hertzbeat/common/serialize/AlertSerializerTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.Arrays;
+import java.util.Map;
+
+import org.apache.hertzbeat.common.entity.alerter.Alert;
+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.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * test case for {@link AlertSerializer}
+ */
+
+class AlertSerializerTest {
+
+ private AlertSerializer alertSerializer;
+
+ @Mock
+ private Map<String, ?> configs;
+
+ @Mock
+ private Headers headers;
+
+ @BeforeEach
+ void setUp() {
+
+ MockitoAnnotations.openMocks(this);
+ alertSerializer = new AlertSerializer();
+ }
+
+ @Test
+ void testConfigure() {
+
+ alertSerializer.configure(configs, false);
+ }
+
+ @Test
+ void testSerializeWithAlert() {
+
+ Alert alert = Alert.builder()
+ .content("test")
+ .target("test")
+ .build();
+ byte[] expectedJson =
("{\"id\":null,\"target\":\"test\",\"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}").getBytes();
+
+ byte[] bytes = alertSerializer.serialize("", alert);
+
+ assertNotNull(bytes);
+ assertEquals(Arrays.toString(expectedJson),
Arrays.toString(bytes));
+ }
+
+ @Test
+ void testSerializeWithNullAlert() {
+
+ byte[] bytes = alertSerializer.serialize("", null);
+ assertNull(bytes);
+ }
+
+ @Test
+ void testSerializeWithHeaders() {
+
+ Alert alert = Alert.builder()
+ .content("test")
+ .target("test")
+ .build();
+ byte[] expectedBytes =
("{\"id\":null,\"target\":\"test\",\"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}").getBytes();
+
+ byte[] bytes = alertSerializer.serialize("alerts", headers,
alert);
+
+ assertArrayEquals(expectedBytes, bytes);
+ }
+
+ @Test
+ void testClose() {
+
+ alertSerializer.close();
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]