Skip to content

Commit

Permalink
refact(client): remove junit4 to junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk committed May 10, 2024
1 parent 70ad2eb commit 2218811
Show file tree
Hide file tree
Showing 85 changed files with 3,233 additions and 3,013 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@
import com.alibaba.nacos.common.remote.client.RpcClientConfig;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.remote.client.ServerRequestHandler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

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

public class AbilityTest {
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class AbilityTest {

private RpcClient rpcClient;

private Connection connection;

@Test
public void testReceive() throws Exception {
void testReceive() throws Exception {
rpcClient = new RpcClient(new RpcClientConfig() {
@Override
public String name() {
Expand Down Expand Up @@ -127,7 +130,7 @@ public void close() {
};
rpcClient.start();
// test not ready
Assert.assertNull(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_1));
assertNull(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_1));

// test ready
rpcClient.serverListFactory(new ServerListFactory() {
Expand All @@ -149,23 +152,23 @@ public List<String> getServerList() {
});
rpcClient.start();
// if connect successfully
Assert.assertEquals(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_2), AbilityStatus.NOT_SUPPORTED);
assertEquals(AbilityStatus.SUPPORTED, rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_1));
assertEquals(AbilityStatus.NOT_SUPPORTED, rpcClient.getConnectionAbility(AbilityKey.SERVER_TEST_2));
}
@After
public void testServerRequestAbility() {

@AfterEach
void testServerRequestAbility() {
//test support
ServerRequestHandler serverRequestHandler = (request, connection) -> {
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.SUPPORTED);
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.SERVER_TEST_2), AbilityStatus.NOT_SUPPORTED);
assertEquals(AbilityStatus.SUPPORTED, connection.getConnectionAbility(AbilityKey.SERVER_TEST_1));
assertEquals(AbilityStatus.NOT_SUPPORTED, connection.getConnectionAbility(AbilityKey.SERVER_TEST_2));
return new Response() { };
};
serverRequestHandler.requestReply(null, connection);

// test no ability table
serverRequestHandler = (request, connection) -> {
Assert.assertEquals(connection.getConnectionAbility(AbilityKey.SERVER_TEST_1), AbilityStatus.UNKNOWN);
assertEquals(AbilityStatus.UNKNOWN, connection.getConnectionAbility(AbilityKey.SERVER_TEST_1));
return new Response() { };
};
serverRequestHandler.requestReply(null, new TestConnection(new RpcClient.ServerInfo()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@

import com.alibaba.nacos.api.ability.constant.AbilityKey;
import com.alibaba.nacos.api.ability.constant.AbilityMode;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

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;

public class ClientAbilityControlManagerTest {
class ClientAbilityControlManagerTest {

ClientAbilityControlManager clientAbilityControlManager;
@Before
public void setUp() {

@BeforeEach
void setUp() {
clientAbilityControlManager = new ClientAbilityControlManager();
}

@Test
public void testInitCurrentNodeAbilities() {
void testInitCurrentNodeAbilities() {
Map<AbilityMode, Map<AbilityKey, Boolean>> actual = clientAbilityControlManager.initCurrentNodeAbilities();
assertEquals(1, actual.size());
assertTrue(actual.containsKey(AbilityMode.SDK_CLIENT));
// Current not define sdk ability.
assertEquals(0, actual.get(AbilityMode.SDK_CLIENT).size());
}

@Test
public void testGetPriority() {
void testGetPriority() {
assertEquals(0, clientAbilityControlManager.getPriority());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class NacosClientAuthServiceImplTest {
class NacosClientAuthServiceImplTest {

@Test
public void testLoginSuccess() throws Exception {
void testLoginSuccess() throws Exception {
//given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
Expand All @@ -53,11 +56,11 @@ public void testLoginSuccess() throws Exception {
//when
boolean ret = nacosClientAuthService.login(properties);
//then
Assert.assertTrue(ret);
assertTrue(ret);
}

@Test
public void testTestLoginFailCode() throws Exception {
void testTestLoginFailCode() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setCode(400);
Expand All @@ -72,11 +75,11 @@ public void testTestLoginFailCode() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
boolean ret = nacosClientAuthService.login(properties);
Assert.assertFalse(ret);
assertFalse(ret);
}

@Test
public void testTestLoginFailHttp() throws Exception {
void testTestLoginFailHttp() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenThrow(new Exception());
Properties properties = new Properties();
Expand All @@ -89,12 +92,12 @@ public void testTestLoginFailHttp() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
boolean ret = nacosClientAuthService.login(properties);
Assert.assertFalse(ret);
assertFalse(ret);

}

@Test
public void testTestLoginServerListSuccess() throws Exception {
void testTestLoginServerListSuccess() throws Exception {
//given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
Expand All @@ -112,11 +115,11 @@ public void testTestLoginServerListSuccess() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
boolean ret = nacosClientAuthService.login(properties);
Assert.assertTrue(ret);
assertTrue(ret);
}

@Test
public void testTestLoginServerListLoginInWindow() throws Exception {
void testTestLoginServerListLoginInWindow() throws Exception {
//given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
Expand All @@ -136,12 +139,12 @@ public void testTestLoginServerListLoginInWindow() throws Exception {
nacosClientAuthService.login(properties);
//then
boolean ret = nacosClientAuthService.login(properties);
Assert.assertTrue(ret);
assertTrue(ret);

}

@Test
public void testGetAccessToken() throws Exception {
void testGetAccessToken() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"accessToken\":\"abc\",\"tokenTtl\":1000}");
Expand All @@ -158,13 +161,13 @@ public void testGetAccessToken() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
//when
Assert.assertTrue(nacosClientAuthService.login(properties));
assertTrue(nacosClientAuthService.login(properties));
//then
Assert.assertEquals("abc", nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
assertEquals("abc", nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
}

@Test
public void testGetAccessEmptyToken() throws Exception {
void testGetAccessEmptyToken() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"accessToken\":\"\",\"tokenTtl\":1000}");
Expand All @@ -181,13 +184,13 @@ public void testGetAccessEmptyToken() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
//when
Assert.assertTrue(nacosClientAuthService.login(properties));
assertTrue(nacosClientAuthService.login(properties));
//then
Assert.assertEquals("", nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
assertEquals("", nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
}

@Test
public void testGetAccessTokenWithoutToken() throws Exception {
void testGetAccessTokenWithoutToken() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"tokenTtl\":1000}");
Expand All @@ -204,13 +207,13 @@ public void testGetAccessTokenWithoutToken() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
//when
Assert.assertTrue(nacosClientAuthService.login(properties));
assertTrue(nacosClientAuthService.login(properties));
//then
Assert.assertNull(nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
assertNull(nacosClientAuthService.getLoginIdentityContext(null).getParameter(NacosAuthLoginConstant.ACCESSTOKEN));
}

@Test
public void testGetAccessTokenWithInvalidTtl() throws Exception {
void testGetAccessTokenWithInvalidTtl() throws Exception {
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> result = new HttpRestResult<>();
result.setData("{\"accessToken\":\"abc\",\"tokenTtl\":\"abc\"}");
Expand All @@ -227,6 +230,6 @@ public void testGetAccessTokenWithInvalidTtl() throws Exception {
nacosClientAuthService.setServerList(serverList);
nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
//when
Assert.assertFalse(nacosClientAuthService.login(properties));
assertFalse(nacosClientAuthService.login(properties));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
import com.alibaba.nacos.client.auth.ram.injector.AbstractResourceInjector;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.common.utils.ReflectUtils;
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 java.util.Map;
import java.util.Properties;

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;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

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

private static final String MOCK = "mock";

Expand All @@ -53,9 +53,9 @@ public class RamClientAuthServiceImplTest {
private RamContext ramContext;

private RequestResource resource;
@Before
public void setUp() throws Exception {

@BeforeEach
void setUp() throws Exception {
ramClientAuthService = new RamClientAuthServiceImpl();
Map<String, AbstractResourceInjector> resourceInjectors = (Map<String, AbstractResourceInjector>) ReflectUtils
.getFieldValue(ramClientAuthService, "resourceInjectors");
Expand All @@ -69,9 +69,9 @@ public void setUp() throws Exception {
roleProperties.setProperty(PropertyKeyConst.RAM_ROLE_NAME, PropertyKeyConst.RAM_ROLE_NAME);
resource = new RequestResource();
}

@Test
public void testLoginWithAkSk() {
void testLoginWithAkSk() {
assertTrue(ramClientAuthService.login(akSkProperties));
assertEquals(PropertyKeyConst.ACCESS_KEY, ramContext.getAccessKey());
assertEquals(PropertyKeyConst.SECRET_KEY, ramContext.getSecretKey());
Expand All @@ -81,36 +81,36 @@ public void testLoginWithAkSk() {
assertEquals(PropertyKeyConst.SECRET_KEY, ramContext.getSecretKey());
assertNull(ramContext.getRamRoleName());
}

@Test
public void testLoginWithRoleName() {
void testLoginWithRoleName() {
assertTrue(ramClientAuthService.login(roleProperties));
assertNull(PropertyKeyConst.ACCESS_KEY, ramContext.getAccessKey());
assertNull(PropertyKeyConst.SECRET_KEY, ramContext.getSecretKey());
assertNull(ramContext.getAccessKey(), PropertyKeyConst.ACCESS_KEY);
assertNull(ramContext.getSecretKey(), PropertyKeyConst.SECRET_KEY);
assertEquals(PropertyKeyConst.RAM_ROLE_NAME, ramContext.getRamRoleName());
assertTrue(ramClientAuthService.login(akSkProperties));
assertNull(PropertyKeyConst.ACCESS_KEY, ramContext.getAccessKey());
assertNull(PropertyKeyConst.SECRET_KEY, ramContext.getSecretKey());
assertNull(ramContext.getAccessKey(), PropertyKeyConst.ACCESS_KEY);
assertNull(ramContext.getSecretKey(), PropertyKeyConst.SECRET_KEY);
assertEquals(PropertyKeyConst.RAM_ROLE_NAME, ramContext.getRamRoleName());
}

@Test
public void testGetLoginIdentityContextWithoutLogin() {
void testGetLoginIdentityContextWithoutLogin() {
LoginIdentityContext actual = ramClientAuthService.getLoginIdentityContext(resource);
assertTrue(actual.getAllKey().isEmpty());
verify(mockResourceInjector, never()).doInject(resource, ramContext, actual);
}

@Test
public void testGetLoginIdentityContextWithoutInjector() {
void testGetLoginIdentityContextWithoutInjector() {
ramClientAuthService.login(akSkProperties);
LoginIdentityContext actual = ramClientAuthService.getLoginIdentityContext(resource);
assertTrue(actual.getAllKey().isEmpty());
verify(mockResourceInjector, never()).doInject(resource, ramContext, actual);
}

@Test
public void testGetLoginIdentityContextWithInjector() {
void testGetLoginIdentityContextWithInjector() {
ramClientAuthService.login(akSkProperties);
resource.setType(MOCK);
LoginIdentityContext actual = ramClientAuthService.getLoginIdentityContext(resource);
Expand Down

0 comments on commit 2218811

Please sign in to comment.