upgrade module naocs-auth from junit4 to junit5 (#12105)

This commit is contained in:
shalk(xiao kun) 2024-05-16 09:41:05 +08:00 committed by GitHub
parent 67b672251e
commit dc0e46e9f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 205 additions and 187 deletions

View File

@ -19,30 +19,30 @@ package com.alibaba.nacos.auth;
import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest;
import com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.api.Permission;
import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.constant.SignType;
import com.alibaba.nacos.plugin.auth.exception.AccessException;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
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;
@RunWith(MockitoJUnitRunner.class)
public class GrpcProtocolAuthServiceTest {
@ExtendWith(MockitoExtension.class)
class GrpcProtocolAuthServiceTest {
@Mock
private AuthConfigs authConfigs;
@ -53,8 +53,8 @@ public class GrpcProtocolAuthServiceTest {
private GrpcProtocolAuthService protocolAuthService;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
protocolAuthService = new GrpcProtocolAuthService(authConfigs);
protocolAuthService.initialize();
mockConfigRequest();
@ -78,7 +78,7 @@ public class GrpcProtocolAuthServiceTest {
@Test
@Secured(resource = "testResource")
public void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource");
Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals("testResource", actual.getName());
@ -90,7 +90,7 @@ public class GrpcProtocolAuthServiceTest {
@Test
@Secured(signType = "non-exist")
public void testParseResourceWithNonExistType() throws NoSuchMethodException {
void testParseResourceWithNonExistType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNonExistType");
Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals(Resource.EMPTY_RESOURCE, actual);
@ -98,7 +98,7 @@ public class GrpcProtocolAuthServiceTest {
@Test
@Secured()
public void testParseResourceWithNamingType() throws NoSuchMethodException {
void testParseResourceWithNamingType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNamingType");
Resource actual = protocolAuthService.parseResource(namingRequest, secured);
assertEquals(SignType.NAMING, actual.getType());
@ -110,7 +110,7 @@ public class GrpcProtocolAuthServiceTest {
@Test
@Secured(signType = SignType.CONFIG)
public void testParseResourceWithConfigType() throws NoSuchMethodException {
void testParseResourceWithConfigType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithConfigType");
Resource actual = protocolAuthService.parseResource(configRequest, secured);
assertEquals(SignType.CONFIG, actual.getType());
@ -121,39 +121,39 @@ public class GrpcProtocolAuthServiceTest {
}
@Test
public void testParseIdentity() {
void testParseIdentity() {
IdentityContext actual = protocolAuthService.parseIdentity(namingRequest);
assertNotNull(actual);
}
@Test
public void testValidateIdentityWithoutPlugin() throws AccessException {
void testValidateIdentityWithoutPlugin() throws AccessException {
IdentityContext identityContext = new IdentityContext();
assertTrue(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}
@Test
public void testValidateIdentityWithPlugin() throws AccessException {
void testValidateIdentityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
IdentityContext identityContext = new IdentityContext();
assertFalse(protocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}
@Test
public void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(protocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(protocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}
@Test
public void testValidateAuthorityWithPlugin() throws AccessException {
void testValidateAuthorityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
assertFalse(protocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
assertFalse(protocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}
private Secured getMethodSecure(String methodName) throws NoSuchMethodException {
Method method = GrpcProtocolAuthServiceTest.class.getMethod(methodName);
Method method = GrpcProtocolAuthServiceTest.class.getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}

View File

@ -19,32 +19,36 @@ package com.alibaba.nacos.auth;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.api.Permission;
import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.constant.SignType;
import com.alibaba.nacos.plugin.auth.exception.AccessException;
import com.alibaba.nacos.auth.mock.MockAuthPluginService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
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 static org.mockito.ArgumentMatchers.eq;
@RunWith(MockitoJUnitRunner.class)
public class HttpProtocolAuthServiceTest {
@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class HttpProtocolAuthServiceTest {
@Mock
private AuthConfigs authConfigs;
@ -54,8 +58,8 @@ public class HttpProtocolAuthServiceTest {
private HttpProtocolAuthService httpProtocolAuthService;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
httpProtocolAuthService = new HttpProtocolAuthService(authConfigs);
httpProtocolAuthService.initialize();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNNs");
@ -68,7 +72,7 @@ public class HttpProtocolAuthServiceTest {
@Test
@Secured(resource = "testResource")
public void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
void testParseResourceWithSpecifiedResource() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithSpecifiedResource");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals("testResource", actual.getName());
@ -80,7 +84,7 @@ public class HttpProtocolAuthServiceTest {
@Test
@Secured(signType = "non-exist")
public void testParseResourceWithNonExistType() throws NoSuchMethodException {
void testParseResourceWithNonExistType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNonExistType");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(Resource.EMPTY_RESOURCE, actual);
@ -88,7 +92,7 @@ public class HttpProtocolAuthServiceTest {
@Test
@Secured()
public void testParseResourceWithNamingType() throws NoSuchMethodException {
void testParseResourceWithNamingType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithNamingType");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(SignType.NAMING, actual.getType());
@ -100,7 +104,7 @@ public class HttpProtocolAuthServiceTest {
@Test
@Secured(signType = SignType.CONFIG)
public void testParseResourceWithConfigType() throws NoSuchMethodException {
void testParseResourceWithConfigType() throws NoSuchMethodException {
Secured secured = getMethodSecure("testParseResourceWithConfigType");
Resource actual = httpProtocolAuthService.parseResource(request, secured);
assertEquals(SignType.CONFIG, actual.getType());
@ -111,39 +115,39 @@ public class HttpProtocolAuthServiceTest {
}
@Test
public void testParseIdentity() {
void testParseIdentity() {
IdentityContext actual = httpProtocolAuthService.parseIdentity(request);
assertNotNull(actual);
}
@Test
public void testValidateIdentityWithoutPlugin() throws AccessException {
void testValidateIdentityWithoutPlugin() throws AccessException {
IdentityContext identityContext = new IdentityContext();
assertTrue(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}
@Test
public void testValidateIdentityWithPlugin() throws AccessException {
void testValidateIdentityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
IdentityContext identityContext = new IdentityContext();
assertFalse(httpProtocolAuthService.validateIdentity(identityContext, Resource.EMPTY_RESOURCE));
}
@Test
public void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(httpProtocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
void testValidateAuthorityWithoutPlugin() throws AccessException {
assertTrue(httpProtocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}
@Test
public void testValidateAuthorityWithPlugin() throws AccessException {
void testValidateAuthorityWithPlugin() throws AccessException {
Mockito.when(authConfigs.getNacosAuthSystemType()).thenReturn(MockAuthPluginService.TEST_PLUGIN);
assertFalse(httpProtocolAuthService
.validateAuthority(new IdentityContext(), new Permission(Resource.EMPTY_RESOURCE, "")));
assertFalse(httpProtocolAuthService.validateAuthority(new IdentityContext(),
new Permission(Resource.EMPTY_RESOURCE, "")));
}
private Secured getMethodSecure(String methodName) throws NoSuchMethodException {
Method method = HttpProtocolAuthServiceTest.class.getMethod(methodName);
Method method = HttpProtocolAuthServiceTest.class.getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}

View File

@ -18,13 +18,13 @@ package com.alibaba.nacos.auth.config;
import com.alibaba.nacos.common.event.ServerConfigChangeEvent;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AuthConfigsTest {
class AuthConfigsTest {
private static final boolean TEST_AUTH_ENABLED = true;
@ -40,8 +40,8 @@ public class AuthConfigsTest {
private MockEnvironment environment;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
environment.setProperty("nacos.core.auth.plugin.test.key", "test");
@ -49,7 +49,7 @@ public class AuthConfigsTest {
}
@Test
public void testUpgradeFromEvent() {
void testUpgradeFromEvent() {
environment.setProperty("nacos.core.auth.enabled", String.valueOf(TEST_AUTH_ENABLED));
environment.setProperty("nacos.core.auth.caching.enabled", String.valueOf(TEST_CACHING_ENABLED));
environment.setProperty("nacos.core.auth.server.identity.key", TEST_SERVER_IDENTITY_KEY);

View File

@ -18,21 +18,21 @@ package com.alibaba.nacos.auth.config;
import com.alibaba.nacos.sys.module.ModuleState;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ConfigurableApplicationContext;
import static com.alibaba.nacos.auth.config.AuthModuleStateBuilder.AUTH_ENABLED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class AuthModuleStateBuilderTest {
@ExtendWith(MockitoExtension.class)
class AuthModuleStateBuilderTest {
@Mock
private ConfigurableApplicationContext context;
@ -40,19 +40,19 @@ public class AuthModuleStateBuilderTest {
@Mock
private AuthConfigs authConfigs;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
when(context.getBean(AuthConfigs.class)).thenReturn(authConfigs);
ApplicationUtils.injectContext(context);
when(authConfigs.getNacosAuthSystemType()).thenReturn("nacos");
}
@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
}
@Test
public void testBuild() {
void testBuild() {
ModuleState actual = new AuthModuleStateBuilder().build();
assertFalse((Boolean) actual.getStates().get(AUTH_ENABLED));
assertFalse((Boolean) actual.getStates().get("login_page_enabled"));

View File

@ -17,24 +17,28 @@
package com.alibaba.nacos.auth.context;
import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.constant.Constants;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class GrpcIdentityContextBuilderTest {
@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class GrpcIdentityContextBuilderTest {
private static final String TEST_PLUGIN = "test";
@ -50,8 +54,8 @@ public class GrpcIdentityContextBuilderTest {
private GrpcIdentityContextBuilder identityContextBuilder;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
identityContextBuilder = new GrpcIdentityContextBuilder(authConfigs);
when(authConfigs.getNacosAuthSystemType()).thenReturn(TEST_PLUGIN);
Map<String, String> headers = new HashMap<>();
@ -61,14 +65,14 @@ public class GrpcIdentityContextBuilderTest {
}
@Test
public void testBuildWithoutPlugin() {
void testBuildWithoutPlugin() {
when(authConfigs.getNacosAuthSystemType()).thenReturn("non-exist");
IdentityContext actual = identityContextBuilder.build(request);
assertNull(actual.getParameter(IDENTITY_TEST_KEY));
}
@Test
public void testBuild() {
void testBuild() {
IdentityContext actual = identityContextBuilder.build(request);
assertEquals(IDENTITY_TEST_VALUE, actual.getParameter(IDENTITY_TEST_KEY));
assertEquals("1.1.1.1", actual.getParameter(Constants.Identity.REMOTE_IP));

View File

@ -16,24 +16,28 @@
package com.alibaba.nacos.auth.context;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.constant.Constants;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class HtppIdentityContextBuilderTest {
@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class HtppIdentityContextBuilderTest {
private static final String TEST_PLUGIN = "test";
@ -55,14 +59,14 @@ public class HtppIdentityContextBuilderTest {
private HttpIdentityContextBuilder identityContextBuilder;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
identityContextBuilder = new HttpIdentityContextBuilder(authConfigs);
when(authConfigs.getNacosAuthSystemType()).thenReturn(TEST_PLUGIN);
}
@Test
public void testBuildWithoutPlugin() {
void testBuildWithoutPlugin() {
mockHeader(true);
mockParameter(true);
when(authConfigs.getNacosAuthSystemType()).thenReturn("non-exist");
@ -71,7 +75,7 @@ public class HtppIdentityContextBuilderTest {
}
@Test
public void testBuildWithHeader() {
void testBuildWithHeader() {
mockHeader(true);
mockParameter(false);
IdentityContext actual = identityContextBuilder.build(request);
@ -80,7 +84,7 @@ public class HtppIdentityContextBuilderTest {
}
@Test
public void testBuildWithParameter() {
void testBuildWithParameter() {
mockHeader(false);
mockParameter(true);
IdentityContext actual = identityContextBuilder.build(request);

View File

@ -16,12 +16,12 @@
package com.alibaba.nacos.auth.mock;
import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.alibaba.nacos.plugin.auth.spi.server.AuthPluginService;
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
import com.alibaba.nacos.plugin.auth.api.Permission;
import com.alibaba.nacos.plugin.auth.api.Resource;
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.alibaba.nacos.plugin.auth.exception.AccessException;
import com.alibaba.nacos.plugin.auth.spi.server.AuthPluginService;
import java.util.Collection;
import java.util.Collections;

View File

@ -23,25 +23,25 @@ import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConfigGrpcResourceParserTest {
class ConfigGrpcResourceParserTest {
private ConfigGrpcResourceParser resourceParser;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
resourceParser = new ConfigGrpcResourceParser();
}
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithFullContext() throws NoSuchMethodException {
void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockConfigRequest("testNs", "testG", "testD");
Resource actual = resourceParser.parse(request, secured);
@ -53,7 +53,7 @@ public class ConfigGrpcResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutNamespace() throws NoSuchMethodException {
void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockConfigRequest("", "testG", "testD");
Resource actual = resourceParser.parse(request, secured);
@ -65,7 +65,7 @@ public class ConfigGrpcResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutGroup() throws NoSuchMethodException {
void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockConfigRequest("testNs", "", "testD");
Resource actual = resourceParser.parse(request, secured);
@ -77,7 +77,7 @@ public class ConfigGrpcResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutDataId() throws NoSuchMethodException {
void testParseWithoutDataId() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockConfigRequest("testNs", "testG", "");
Resource actual = resourceParser.parse(request, secured);
@ -89,7 +89,7 @@ public class ConfigGrpcResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithConfigBatchListenRequest() throws NoSuchMethodException {
void testParseWithConfigBatchListenRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
ConfigBatchListenRequest request = new ConfigBatchListenRequest();
request.addConfigListenContext("testG", "testD", "testNs", "111");
@ -112,7 +112,7 @@ public class ConfigGrpcResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName);
Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}

View File

@ -23,26 +23,26 @@ import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class NamingGrpcResourceParserTest {
class NamingGrpcResourceParserTest {
private NamingGrpcResourceParser resourceParser;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
resourceParser = new NamingGrpcResourceParser();
}
@Test
@Secured()
public void testParseWithFullContextForNamingRequest() throws NoSuchMethodException {
void testParseWithFullContextForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest("testNs", "testG", "testS");
Resource actual = resourceParser.parse(request, secured);
@ -50,13 +50,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithFullContextForOtherRequest() throws NoSuchMethodException {
void testParseWithFullContextForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockOtherRequest("testNs", "testG", "testS");
Resource actual = resourceParser.parse(request, secured);
@ -64,13 +64,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithoutNamespaceForNamingRequest() throws NoSuchMethodException {
void testParseWithoutNamespaceForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest(null, "testG", "testS");
Resource actual = resourceParser.parse(request, secured);
@ -78,13 +78,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithoutNamespaceForOtherRequest() throws NoSuchMethodException {
void testParseWithoutNamespaceForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockOtherRequest(null, "testG", "testS");
Resource actual = resourceParser.parse(request, secured);
@ -92,13 +92,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup());
assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithoutGroupForNamingRequest() throws NoSuchMethodException {
void testParseWithoutGroupForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest("testNs", null, "testS");
Resource actual = resourceParser.parse(request, secured);
@ -106,13 +106,13 @@ public class NamingGrpcResourceParserTest {
assertEquals(StringUtils.EMPTY, actual.getGroup());
assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithoutGroupForOtherRequest() throws NoSuchMethodException {
void testParseWithoutGroupForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockOtherRequest("testNs", null, "testS");
Resource actual = resourceParser.parse(request, secured);
@ -120,13 +120,13 @@ public class NamingGrpcResourceParserTest {
assertEquals(StringUtils.EMPTY, actual.getGroup());
assertEquals("testS", actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithoutDataIdForNamingRequest() throws NoSuchMethodException {
void testParseWithoutDataIdForNamingRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
AbstractNamingRequest request = mockNamingRequest("testNs", "testG", null);
Resource actual = resourceParser.parse(request, secured);
@ -134,13 +134,13 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup());
assertEquals(StringUtils.EMPTY, actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
@Test
@Secured()
public void testParseWithoutDataIdForOtherRequest() throws NoSuchMethodException {
void testParseWithoutDataIdForOtherRequest() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Request request = mockOtherRequest("testNs", "testG", null);
Resource actual = resourceParser.parse(request, secured);
@ -148,8 +148,8 @@ public class NamingGrpcResourceParserTest {
assertEquals("testG", actual.getGroup());
assertEquals(StringUtils.EMPTY, actual.getName());
assertEquals(Constants.Naming.NAMING_MODULE, actual.getType());
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty(
com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties()
.getProperty(com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS));
}
private AbstractNamingRequest mockNamingRequest(String testNs, String testG, String testS) {
@ -172,7 +172,7 @@ public class NamingGrpcResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName);
Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}

View File

@ -20,36 +20,39 @@ import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
@RunWith(MockitoJUnitRunner.class)
public class ConfigHttpResourceParserTest {
@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class ConfigHttpResourceParserTest {
@Mock
private HttpServletRequest request;
private ConfigHttpResourceParser resourceParser;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
resourceParser = new ConfigHttpResourceParser();
}
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithFullContext() throws NoSuchMethodException {
void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
@ -63,7 +66,7 @@ public class ConfigHttpResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutNamespace() throws NoSuchMethodException {
void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
@ -76,7 +79,7 @@ public class ConfigHttpResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutGroup() throws NoSuchMethodException {
void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
@ -89,7 +92,7 @@ public class ConfigHttpResourceParserTest {
@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
public void testParseWithoutDataId() throws NoSuchMethodException {
void testParseWithoutDataId() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
@ -104,7 +107,7 @@ public class ConfigHttpResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName);
Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}

View File

@ -21,37 +21,40 @@ import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.auth.api.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
@RunWith(MockitoJUnitRunner.class)
public class NamingHttpResourceParserTest {
@ExtendWith(MockitoExtension.class)
// todo remove this
@MockitoSettings(strictness = Strictness.LENIENT)
class NamingHttpResourceParserTest {
@Mock
private HttpServletRequest request;
private NamingHttpResourceParser resourceParser;
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
resourceParser = new NamingHttpResourceParser();
}
@Test
@Secured()
public void testParseWithFullContext() throws NoSuchMethodException {
void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
@ -65,7 +68,7 @@ public class NamingHttpResourceParserTest {
@Test
@Secured()
public void testParseWithoutNamespace() throws NoSuchMethodException {
void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS");
@ -78,7 +81,7 @@ public class NamingHttpResourceParserTest {
@Test
@Secured()
public void testParseWithoutGroup() throws NoSuchMethodException {
void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS");
@ -91,7 +94,7 @@ public class NamingHttpResourceParserTest {
@Test
@Secured()
public void testParseWithGroupInService() throws NoSuchMethodException {
void testParseWithGroupInService() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testG@@testS");
@ -104,7 +107,7 @@ public class NamingHttpResourceParserTest {
@Test
@Secured()
public void testParseWithoutService() throws NoSuchMethodException {
void testParseWithoutService() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
@ -117,7 +120,7 @@ public class NamingHttpResourceParserTest {
@Test
@Secured()
public void testParseWithoutGroupAndService() throws NoSuchMethodException {
void testParseWithoutGroupAndService() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Resource actual = resourceParser.parse(request, secured);
@ -129,7 +132,7 @@ public class NamingHttpResourceParserTest {
@Test
@Secured(tags = {"testTag"})
public void testParseWithTags() throws NoSuchMethodException {
void testParseWithTags() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(CommonParams.NAMESPACE_ID))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(CommonParams.GROUP_NAME))).thenReturn("testG");
@ -146,7 +149,7 @@ public class NamingHttpResourceParserTest {
StackTraceElement[] traces = new Exception().getStackTrace();
StackTraceElement callerElement = traces[1];
String methodName = callerElement.getMethodName();
Method method = this.getClass().getMethod(methodName);
Method method = this.getClass().getDeclaredMethod(methodName);
return method.getAnnotation(Secured.class);
}
}