Skip to content

Commit

Permalink
upgrade module naocs-plugin from junit4 to junit5 (#12229)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk committed Jun 17, 2024
1 parent ab6591a commit 21bfac7
Show file tree
Hide file tree
Showing 43 changed files with 1,208 additions and 1,224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,44 @@

package com.alibaba.nacos.plugin.auth.api;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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.junit.jupiter.api.Assertions.assertThrows;

public class IdentityContextTest {
class IdentityContextTest {

private static final String TEST = "test";

private IdentityContext identityContext;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
identityContext = new IdentityContext();
}

@Test
public void testGetParameter() {
void testGetParameter() {
assertNull(identityContext.getParameter(TEST));
identityContext.setParameter(TEST, TEST);
assertEquals(TEST, identityContext.getParameter(TEST));
}

@Test
public void testGetParameterWithDefaultValue() {
void testGetParameterWithDefaultValue() {
assertEquals(TEST, identityContext.getParameter(TEST, TEST));
identityContext.setParameter(TEST, TEST + "new");
assertEquals(TEST + "new", identityContext.getParameter(TEST, TEST));
long actual = identityContext.getParameter(TEST, 1L);
assertEquals(1L, actual);
}

@Test(expected = IllegalArgumentException.class)
public void testGetParameterWithNullDefaultValue() {
identityContext.getParameter(TEST, null);
@Test
void testGetParameterWithNullDefaultValue() {
assertThrows(IllegalArgumentException.class, () -> {
identityContext.getParameter(TEST, null);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@

package com.alibaba.nacos.plugin.auth.api;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LoginIdentityContextTest {
class LoginIdentityContextTest {

private static final String TEST = "test";

private LoginIdentityContext loginIdentityContext;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
loginIdentityContext = new LoginIdentityContext();
}

@Test
public void testSetParameter() {
void testSetParameter() {
assertNull(loginIdentityContext.getParameter(TEST));
assertTrue(loginIdentityContext.getAllKey().isEmpty());
loginIdentityContext.setParameter(TEST, TEST);
Expand All @@ -47,7 +47,7 @@ public void testSetParameter() {
}

@Test
public void testSetParameters() {
void testSetParameters() {
assertNull(loginIdentityContext.getParameter(TEST));
assertTrue(loginIdentityContext.getAllKey().isEmpty());
Map<String, String> map = new HashMap<>(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@
package com.alibaba.nacos.plugin.auth.api;

import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PermissionTest {
class PermissionTest {

private Permission permission;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
permission = new Permission(Resource.EMPTY_RESOURCE, ActionTypes.WRITE.toString());
}

@Test
public void testToString() {
assertEquals(
"Permission{resource='Resource{namespaceId='', group='', name='', type='', properties=null}', action='w'}",
void testToString() {
assertEquals("Permission{resource='Resource{namespaceId='', group='', name='', type='', properties=null}', action='w'}",
permission.toString());
}

@Test
public void testSetResource() {
void testSetResource() {
Permission permission = new Permission();
Properties properties = new Properties();
Resource resource = new Resource("NS", "G", "N", "TEST", properties);
Expand All @@ -54,7 +53,7 @@ public void testSetResource() {
}

@Test
public void testSetAction() {
void testSetAction() {
Permission permission = new Permission();
permission.setAction(ActionTypes.READ.toString());
assertEquals(ActionTypes.READ.toString(), permission.getAction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@
package com.alibaba.nacos.plugin.auth.api;

import com.alibaba.nacos.plugin.auth.constant.SignType;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class RequestResourceTest {
class RequestResourceTest {

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
}

@Test
public void testBuildNamingRequestResource() {
RequestResource actual = RequestResource.namingBuilder().setNamespace("NS").setGroup("G").setResource("Service")
.build();
void testBuildNamingRequestResource() {
RequestResource actual = RequestResource.namingBuilder().setNamespace("NS").setGroup("G").setResource("Service").build();
assertEquals(SignType.NAMING, actual.getType());
assertEquals("NS", actual.getNamespace());
assertEquals("G", actual.getGroup());
assertEquals("Service", actual.getResource());
}

@Test
public void testBuildConfigRequestResource() {
RequestResource actual = RequestResource.configBuilder().setNamespace("NS").setGroup("G").setResource("dataId")
.build();
void testBuildConfigRequestResource() {
RequestResource actual = RequestResource.configBuilder().setNamespace("NS").setGroup("G").setResource("dataId").build();
assertEquals(SignType.CONFIG, actual.getType());
assertEquals("NS", actual.getNamespace());
assertEquals("G", actual.getGroup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package com.alibaba.nacos.plugin.auth.constant;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ActionTypesTest {
class ActionTypesTest {

@Test
public void testToStringForRead() {
void testToStringForRead() {
ActionTypes actual = ActionTypes.valueOf("READ");
assertEquals("r", actual.toString());
}

@Test
public void testToStringForWrite() {
void testToStringForWrite() {
ActionTypes actual = ActionTypes.valueOf("WRITE");
assertEquals("w", actual.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,39 @@

package com.alibaba.nacos.plugin.auth.constant;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ConstantsTest {
class ConstantsTest {

@Test
public void testConstantsForAuth() {
void testConstantsForAuth() {
assertEquals("nacos.core.auth.enabled", Constants.Auth.NACOS_CORE_AUTH_ENABLED);
assertEquals("nacos.core.auth.system.type", Constants.Auth.NACOS_CORE_AUTH_SYSTEM_TYPE);
assertEquals("nacos.core.auth.caching.enabled", Constants.Auth.NACOS_CORE_AUTH_CACHING_ENABLED);
assertEquals("nacos.core.auth.server.identity.key", Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_KEY);
assertEquals("nacos.core.auth.server.identity.value", Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE);
assertEquals("nacos.core.auth.enable.userAgentAuthWhite",
Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE);
assertEquals("nacos.core.auth.enable.userAgentAuthWhite", Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE);
}

@Test
public void testConstantsForResource() {
void testConstantsForResource() {
assertEquals(":", Constants.Resource.SPLITTER);
assertEquals("*", Constants.Resource.ANY);
assertEquals("action", Constants.Resource.ACTION);
assertEquals("requestClass", Constants.Resource.REQUEST_CLASS);
}

@Test
public void testConstantsForIdentity() {
void testConstantsForIdentity() {
assertEquals("identity_id", Constants.Identity.IDENTITY_ID);
assertEquals("X-Real-IP", Constants.Identity.X_REAL_IP);
assertEquals("remote_ip", Constants.Identity.REMOTE_IP);
}

@Test
public void testConstantsForSignType() {
void testConstantsForSignType() {
assertEquals("naming", SignType.NAMING);
assertEquals("config", SignType.CONFIG);
assertEquals("console", SignType.CONSOLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@
package com.alibaba.nacos.plugin.auth.exception;

import com.alibaba.nacos.api.common.Constants;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AccessExceptionTest {
class AccessExceptionTest {

@Test
public void testNewAccessExceptionWithCode() {
void testNewAccessExceptionWithCode() {
AccessException actual = new AccessException(403);
assertEquals(403, actual.getErrCode());
assertEquals(Constants.NULL, actual.getErrMsg());
}

@Test
public void testNewAccessExceptionWithMsg() {
void testNewAccessExceptionWithMsg() {
AccessException actual = new AccessException("Test");
assertEquals("Test", actual.getErrMsg());
assertEquals(0, actual.getErrCode());
}

@Test
public void testNewAccessExceptionWithNoArgs() {
void testNewAccessExceptionWithNoArgs() {
AccessException actual = new AccessException();
assertEquals(Constants.NULL, actual.getErrMsg());
assertEquals(0, actual.getErrCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import org.junit.After;
import org.junit.Assert;
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 java.lang.reflect.Field;
import java.util.Collection;
Expand All @@ -34,15 +33,18 @@
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link ClientAuthPluginManager} unit test.
*
* @author wuyfee
* @date 2021-08-12 12:56
*/

@RunWith(MockitoJUnitRunner.class)
public class ClientAuthPluginManagerTest {
@ExtendWith(MockitoExtension.class)
class ClientAuthPluginManagerTest {

private ClientAuthPluginManager clientAuthPluginManager;

Expand All @@ -52,37 +54,36 @@ public class ClientAuthPluginManagerTest {
@Mock
private NacosRestTemplate nacosRestTemplate;

@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
@BeforeEach
void setUp() throws NoSuchFieldException, IllegalAccessException {
clientAuthPluginManager = new ClientAuthPluginManager();
}

@After
public void tearDown() throws NacosException, NoSuchFieldException, IllegalAccessException {
@AfterEach
void tearDown() throws NacosException, NoSuchFieldException, IllegalAccessException {
getServiceLoaderMap().remove(AbstractClientAuthService.class);
clientAuthPluginManager.shutdown();
}

private Map<Class<?>, Collection<Class<?>>> getServiceLoaderMap()
throws NoSuchFieldException, IllegalAccessException {
private Map<Class<?>, Collection<Class<?>>> getServiceLoaderMap() throws NoSuchFieldException, IllegalAccessException {
Field servicesField = NacosServiceLoader.class.getDeclaredField("SERVICES");
servicesField.setAccessible(true);
return (Map<Class<?>, Collection<Class<?>>>) servicesField.get(null);
}

@Test
public void testGetAuthServiceSpiImplSet() {
void testGetAuthServiceSpiImplSet() {
clientAuthPluginManager.init(serverlist, nacosRestTemplate);
Set<ClientAuthService> clientAuthServiceSet = clientAuthPluginManager.getAuthServiceSpiImplSet();
Assert.assertFalse(clientAuthServiceSet.isEmpty());
assertFalse(clientAuthServiceSet.isEmpty());
}

@Test
public void testGetAuthServiceSpiImplSetForEmpty() throws NoSuchFieldException, IllegalAccessException {
void testGetAuthServiceSpiImplSetForEmpty() throws NoSuchFieldException, IllegalAccessException {
getServiceLoaderMap().put(AbstractClientAuthService.class, Collections.emptyList());
clientAuthPluginManager.init(serverlist, nacosRestTemplate);
Set<ClientAuthService> clientAuthServiceSet = clientAuthPluginManager.getAuthServiceSpiImplSet();
Assert.assertTrue(clientAuthServiceSet.isEmpty());
assertTrue(clientAuthServiceSet.isEmpty());
}

}
Loading

0 comments on commit 21bfac7

Please sign in to comment.