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

shown 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 ff3139e1d test: add unit test case for the util (#2701)
ff3139e1d is described below

commit ff3139e1dea02fa5e11b40164de0c47b5f30aafb
Author: Rick <[email protected]>
AuthorDate: Thu Sep 12 09:14:05 2024 +0800

    test: add unit test case for the util (#2701)
    
    Co-authored-by: rick <[email protected]>
    Co-authored-by: aias00 <[email protected]>
    Co-authored-by: shown <[email protected]>
---
 .../apache/hertzbeat/common/util/ResponseUtil.java | 10 +++
 .../hertzbeat/common/cache/CacheFactoryTest.java   | 34 +++++++++
 .../hertzbeat/common/cache/CaffeineCacheTest.java  |  7 ++
 .../common/util/JexlExpressionRunnerTest.java      | 69 +++++++++++++++++
 .../apache/hertzbeat/common/util/JsonUtilTest.java | 17 +++++
 .../hertzbeat/common/util/NetworkUtilTest.java     | 32 ++++++++
 .../org/apache/hertzbeat/common/util/PairTest.java | 37 ++++++++++
 .../hertzbeat/common/util/ResponseUtilTest.java    | 86 ++++++++++++++++++++++
 8 files changed, 292 insertions(+)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/util/ResponseUtil.java 
b/common/src/main/java/org/apache/hertzbeat/common/util/ResponseUtil.java
index 1657c84ee..fef61b49a 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/util/ResponseUtil.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/ResponseUtil.java
@@ -66,4 +66,14 @@ public class ResponseUtil {
         T get() throws E;
     }
     
+    /**
+     * Runnable interface for running
+     */
+    public interface Runnable {
+
+        /**
+         * Run target method.
+         */
+        void run() throws Exception;
+    }
 }
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/cache/CacheFactoryTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/cache/CacheFactoryTest.java
new file mode 100644
index 000000000..9a844af64
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/cache/CacheFactoryTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.cache;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link CacheFactory}
+ */
+public class CacheFactoryTest {
+    @Test
+    void common() {
+        assertNotNull(CacheFactory.getAlertConvergeCache());
+        assertNotNull(CacheFactory.getAlertSilenceCache());
+        assertNotNull(CacheFactory.getNoticeCache());
+    }
+}
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
index 8d0b11e55..5546329e2 100644
--- 
a/common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.hertzbeat.common.cache;
 
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
 import java.time.Duration;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
@@ -63,4 +65,9 @@ class CaffeineCacheTest {
         }
     }
 
+    @Test
+    void weekCache() {
+        CommonCacheService<String, String> cache = new 
CaffeineCacheServiceImpl<>(10, 100, Duration.ofMillis(3000), true);
+        assertNotNull(cache);
+    }
 }
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
 
b/common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
new file mode 100644
index 000000000..42e06a484
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.jexl3.JexlExpression;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link JexlExpressionRunner}
+ */
+public class JexlExpressionRunnerTest {
+    @Test
+    void evaluate() {
+        Map<String, Object> context = new HashMap<String, Object>();
+        context.put("age", 2);
+
+        String expression = "1 + 2";
+        String expressionWithParam = "1 + age";
+
+        assertEquals(3, JexlExpressionRunner.evaluate(expression));
+        assertEquals(3, JexlExpressionRunner.evaluate(expression, context));
+        assertEquals(3, JexlExpressionRunner.evaluate(expressionWithParam, 
context));
+
+        assertThrows(NullPointerException.class, () -> {
+            JexlExpressionRunner.evaluate(expression, null);
+        });
+
+        JexlExpression expObj = JexlExpressionRunner.compile(expression);
+        assertNotNull(expObj);
+        assertEquals(3, JexlExpressionRunner.evaluate(expObj, context));
+    }
+
+    @Test
+    void commonFuncs() {
+        assertEquals(true, JexlExpressionRunner.evaluate("equals(1, 1)"));
+        assertEquals(true, JexlExpressionRunner.evaluate("equals(null, 
null)"));
+        assertEquals(false, JexlExpressionRunner.evaluate("equals(null, 1)")); 
+        assertEquals(true, JexlExpressionRunner.evaluate("""
+                contains("abc", "a")
+                """)); 
+        assertEquals(true, JexlExpressionRunner.evaluate("""
+                contains("Abc", "a")
+                """)); 
+    }
+}
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
index d4acb6089..a43989b2c 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
@@ -20,6 +20,8 @@ package org.apache.hertzbeat.common.util;
 import static org.apache.hertzbeat.common.util.JsonUtil.isJsonStr;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import com.fasterxml.jackson.core.type.TypeReference;
 import java.util.ArrayList;
@@ -41,6 +43,8 @@ class JsonUtilTest {
 
         
assertEquals("[{\"name\":\"test\",\"value\":\"pro\"},{\"name\":\"test\",\"value\":\"dev\"}]",
                 JsonUtil.toJson(tagList));
+
+        assertNull(JsonUtil.toJson(null));
     }
 
     @Test
@@ -49,6 +53,17 @@ class JsonUtilTest {
         List<TagItem> tagItems = JsonUtil.fromJson(jsonStr, new 
TypeReference<>() {
         });
         assertEquals("[TagItem(name=test, value=pro), TagItem(name=test, 
value=dev)]", tagItems.toString());
+        assertNull(JsonUtil.fromJson("", new TypeReference<>() {
+        }));
+        assertNull(JsonUtil.fromJson(null, new TypeReference<>() {
+        }));
+        assertNull(JsonUtil.fromJson(" ", new TypeReference<>() {
+        }));
+        assertNull(JsonUtil.fromJson(" ", String.class));
+        assertNull(JsonUtil.fromJson(" "));
+        assertNull(JsonUtil.fromJson(null));
+        assertNotNull(JsonUtil.fromJson(jsonStr));
+        assertNull(JsonUtil.fromJson("invalid"));
     }
 
     @Test
@@ -68,6 +83,8 @@ class JsonUtilTest {
 
         String jsonStringArrays = "[{\"name\":\"John\"}, {\"name\":\"Doe\"}]";
         assertTrue(isJsonStr(jsonStringArrays));
+
+        assertFalse(isJsonStr("{invalid}"));
     }
 
 }
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/NetworkUtilTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/util/NetworkUtilTest.java
new file mode 100644
index 000000000..e358aa678
--- /dev/null
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/NetworkUtilTest.java
@@ -0,0 +1,32 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for {@link NetworkUtil}
+ */
+public class NetworkUtilTest {
+    @Test
+    void common() {
+        assertFalse(NetworkUtil.isLinuxPlatform() && 
NetworkUtil.isWindowsPlatform());
+    }
+}
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/PairTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/util/PairTest.java
new file mode 100644
index 000000000..51172461e
--- /dev/null
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/PairTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test for {@link Pair}
+ */
+public class PairTest {
+    @Test
+    void common() {
+        Pair<String, String> pair = Pair.of("key", "value");
+        assertEquals("key", pair.getLeft());
+        assertEquals("value", pair.getRight());
+
+        pair.setLeft("left");
+        assertEquals("left", pair.getLeft());
+    }
+}
diff --git 
a/common/src/test/java/org/apache/hertzbeat/common/util/ResponseUtilTest.java 
b/common/src/test/java/org/apache/hertzbeat/common/util/ResponseUtilTest.java
new file mode 100644
index 000000000..b81a93525
--- /dev/null
+++ 
b/common/src/test/java/org/apache/hertzbeat/common/util/ResponseUtilTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import javax.naming.AuthenticationException;
+
+import org.apache.hertzbeat.common.constants.CommonConstants;
+import org.apache.hertzbeat.common.entity.dto.Message;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.ResponseEntity;
+
+/**
+ * Test for {@link ResponseUtil}
+ */
+public class ResponseUtilTest {
+    @Test
+    public void testHandle() {
+        assertDoesNotThrow(() -> {
+            ResponseUtil.handle(() -> "test");
+        });
+
+        assertDoesNotThrow(() -> {
+            ResponseEntity<Message<String>> resp = ResponseUtil.handle(() -> {
+                throw new RuntimeException("test");
+            });
+            assertEquals(CommonConstants.FAIL_CODE, resp.getBody().getCode());
+        });
+
+        assertDoesNotThrow(() -> {
+            ResponseEntity<Message<String>> resp = ResponseUtil.handle(() -> {
+                throw new AuthenticationException("test");
+            });
+            assertEquals(CommonConstants.LOGIN_FAILED_CODE, 
resp.getBody().getCode());
+        });
+
+        assertDoesNotThrow(() -> {
+            ResponseUtil.Runnable run = new ResponseUtil.Runnable() {
+                public void run() {
+                    throw new UnsupportedOperationException("Unimplemented 
method 'run'");
+                }
+            };
+            ResponseEntity<Message<String>> resp = ResponseUtil.handle(run);
+            assertEquals(CommonConstants.FAIL_CODE, resp.getBody().getCode());
+        });
+
+        assertDoesNotThrow(() -> {
+            ResponseUtil.Runnable run = new ResponseUtil.Runnable() {
+                public void run() throws AuthenticationException {
+                    throw new AuthenticationException("test");
+                }
+            };
+            ResponseEntity<Message<String>> resp = ResponseUtil.handle(run);
+            assertEquals(CommonConstants.LOGIN_FAILED_CODE, 
resp.getBody().getCode());
+        });
+
+        assertDoesNotThrow(() -> {
+            ResponseEntity<Message<String>> resp = ResponseUtil.handle(() -> 
{});
+            assertEquals(CommonConstants.SUCCESS_CODE, 
resp.getBody().getCode());
+        });
+    }
+
+    /**
+     * InnerResponseUtilTest
+     */
+    public interface InnerResponseUtilTest {
+        void run() throws Exception;
+    }
+}


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

Reply via email to