From ff3139e1dea02fa5e11b40164de0c47b5f30aafb Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:14:05 +0800 Subject: [PATCH] test: add unit test case for the util (#2701) Co-authored-by: rick Co-authored-by: aias00 Co-authored-by: shown --- .../hertzbeat/common/util/ResponseUtil.java | 10 +++ .../common/cache/CacheFactoryTest.java | 34 ++++++++ .../common/cache/CaffeineCacheTest.java | 7 ++ .../common/util/JexlExpressionRunnerTest.java | 69 +++++++++++++++ .../hertzbeat/common/util/JsonUtilTest.java | 17 ++++ .../common/util/NetworkUtilTest.java | 32 +++++++ .../hertzbeat/common/util/PairTest.java | 37 ++++++++ .../common/util/ResponseUtilTest.java | 86 +++++++++++++++++++ 8 files changed, 292 insertions(+) create mode 100644 common/src/test/java/org/apache/hertzbeat/common/cache/CacheFactoryTest.java create mode 100644 common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java create mode 100644 common/src/test/java/org/apache/hertzbeat/common/util/NetworkUtilTest.java create mode 100644 common/src/test/java/org/apache/hertzbeat/common/util/PairTest.java create mode 100644 common/src/test/java/org/apache/hertzbeat/common/util/ResponseUtilTest.java 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 1657c84ee50..fef61b49ae1 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 interface Supplier { 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 00000000000..9a844af6448 --- /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 8d0b11e55c2..5546329e26c 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 @@ void testCache() throws InterruptedException { } } + @Test + void weekCache() { + CommonCacheService 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 00000000000..42e06a4849d --- /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 context = new HashMap(); + 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 d4acb6089ed..a43989b2c56 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 @@ 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 @@ void toJson() { assertEquals("[{\"name\":\"test\",\"value\":\"pro\"},{\"name\":\"test\",\"value\":\"dev\"}]", JsonUtil.toJson(tagList)); + + assertNull(JsonUtil.toJson(null)); } @Test @@ -49,6 +53,17 @@ void testFromJson() { List 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 @@ void testIsJsonStr() { 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 00000000000..e358aa678e1 --- /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 00000000000..51172461e09 --- /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 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 00000000000..b81a9352574 --- /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> resp = ResponseUtil.handle(() -> { + throw new RuntimeException("test"); + }); + assertEquals(CommonConstants.FAIL_CODE, resp.getBody().getCode()); + }); + + assertDoesNotThrow(() -> { + ResponseEntity> 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> 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> resp = ResponseUtil.handle(run); + assertEquals(CommonConstants.LOGIN_FAILED_CODE, resp.getBody().getCode()); + }); + + assertDoesNotThrow(() -> { + ResponseEntity> resp = ResponseUtil.handle(() -> {}); + assertEquals(CommonConstants.SUCCESS_CODE, resp.getBody().getCode()); + }); + } + + /** + * InnerResponseUtilTest + */ + public interface InnerResponseUtilTest { + void run() throws Exception; + } +}