forked from apache/hertzbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test case for the util (apache#2701)
Co-authored-by: rick <[email protected]> Co-authored-by: aias00 <[email protected]> Co-authored-by: shown <[email protected]>
- Loading branch information
1 parent
65639da
commit ff3139e
Showing
8 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
common/src/test/java/org/apache/hertzbeat/common/cache/CacheFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
common/src/test/java/org/apache/hertzbeat/common/util/JexlExpressionRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
""")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
common/src/test/java/org/apache/hertzbeat/common/util/NetworkUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
common/src/test/java/org/apache/hertzbeat/common/util/PairTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
common/src/test/java/org/apache/hertzbeat/common/util/ResponseUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |