From a1938696194a5134b07f60f9965fc9a4a054d673 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Mon, 31 Oct 2022 12:26:38 +0800 Subject: [PATCH] Increase unit test code coverage --- .codecov.yml | 2 +- .../auth/filter/RewriteUserInfoApiFilter.java | 2 +- .../cn/hippo4j/auth/toolkit/AuthUtil.java | 10 +++- .../cn/hippo4j/auth/toolkit/AuthUtilTest.java | 38 ++++++++++++++ .../cn/hippo4j/auth/toolkit/ReturnTTest.java | 49 +++++++++++++++++++ 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/AuthUtilTest.java create mode 100644 hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/ReturnTTest.java diff --git a/.codecov.yml b/.codecov.yml index a6ffc2d4..ab1c78cb 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -19,7 +19,7 @@ coverage: status: patch: default: - threshold: 0.1% + threshold: 0.01% ignore: - "hippo4j-example/.*" - "docs/.*" diff --git a/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/RewriteUserInfoApiFilter.java b/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/RewriteUserInfoApiFilter.java index 8e789d30..8144f7bc 100644 --- a/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/RewriteUserInfoApiFilter.java +++ b/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/RewriteUserInfoApiFilter.java @@ -34,7 +34,7 @@ public class RewriteUserInfoApiFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - boolean enableAuthentication = AuthUtil.enableAuthentication; + boolean enableAuthentication = AuthUtil.ENABLE_AUTHENTICATION; HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; String path = httpRequest.getRequestURI(); if (!enableAuthentication && path.contains("users/info")) { diff --git a/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/toolkit/AuthUtil.java b/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/toolkit/AuthUtil.java index ab6f9b9a..62e0371f 100644 --- a/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/toolkit/AuthUtil.java +++ b/hippo4j-server/hippo4j-auth/src/main/java/cn/hippo4j/auth/toolkit/AuthUtil.java @@ -20,13 +20,19 @@ package cn.hippo4j.auth.toolkit; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +/** + * Auth util. + */ @Component public class AuthUtil { - public static boolean enableAuthentication; + /** + * Enable authentication + */ + public static boolean ENABLE_AUTHENTICATION; @Value("${hippo4j.core.auth.enabled:true}") public void setEnableAuthentication(boolean enabled) { - AuthUtil.enableAuthentication = enabled; + AuthUtil.ENABLE_AUTHENTICATION = enabled; } } \ No newline at end of file diff --git a/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/AuthUtilTest.java b/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/AuthUtilTest.java new file mode 100644 index 00000000..dd522658 --- /dev/null +++ b/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/AuthUtilTest.java @@ -0,0 +1,38 @@ +/* + * 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 cn.hippo4j.auth.toolkit; + +import cn.hippo4j.common.toolkit.Assert; +import org.junit.Before; +import org.junit.Test; + +public final class AuthUtilTest { + + private AuthUtil authUtil; + + @Before + public void beforeInit() { + authUtil = new AuthUtil(); + authUtil.setEnableAuthentication(true); + } + + @Test + public void assertGetEnableAuthentication() { + Assert.isTrue(AuthUtil.ENABLE_AUTHENTICATION); + } +} diff --git a/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/ReturnTTest.java b/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/ReturnTTest.java new file mode 100644 index 00000000..8c2ff9b2 --- /dev/null +++ b/hippo4j-server/hippo4j-auth/src/test/java/cn/hippo4j/auth/toolkit/ReturnTTest.java @@ -0,0 +1,49 @@ +/* + * 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 cn.hippo4j.auth.toolkit; + +import cn.hippo4j.common.toolkit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Objects; + +public final class ReturnTTest { + + private ReturnT returnT; + + @Before + public void beforeInit() { + returnT = new ReturnT("success"); + } + + @Test + public void assertGetCode() { + Assert.isTrue(Objects.equals(returnT.getCode(), 200)); + } + + @Test + public void assertGetMessage() { + Assert.isNull(returnT.getMsg()); + } + + @Test + public void assertGetContent() { + Assert.isTrue(Objects.equals(returnT.getContent(), "success")); + } +}