Skip to content

Commit

Permalink
test: add unit test case for the util (apache#2701)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
Co-authored-by: aias00 <[email protected]>
Co-authored-by: shown <[email protected]>
  • Loading branch information
4 people authored Sep 12, 2024
1 parent 65639da commit ff3139e
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ public interface Supplier<T, E extends Exception> {
T get() throws E;
}

/**
* Runnable interface for running
*/
public interface Runnable {

/**
* Run target method.
*/
void run() throws Exception;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,4 +65,9 @@ void testCache() throws InterruptedException {
}
}

@Test
void weekCache() {
CommonCacheService<String, String> cache = new CaffeineCacheServiceImpl<>(10, 100, Duration.ofMillis(3000), true);
assertNotNull(cache);
}
}
Original file line number Diff line number Diff line change
@@ -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")
"""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +43,8 @@ void toJson() {

assertEquals("[{\"name\":\"test\",\"value\":\"pro\"},{\"name\":\"test\",\"value\":\"dev\"}]",
JsonUtil.toJson(tagList));

assertNull(JsonUtil.toJson(null));
}

@Test
Expand All @@ -49,6 +53,17 @@ void testFromJson() {
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
Expand All @@ -68,6 +83,8 @@ void testIsJsonStr() {

String jsonStringArrays = "[{\"name\":\"John\"}, {\"name\":\"Doe\"}]";
assertTrue(isJsonStr(jsonStringArrays));

assertFalse(isJsonStr("{invalid}"));
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit ff3139e

Please sign in to comment.