From f3fb4284524222b752cefbc98e93f64101e632f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= Date: Thu, 28 Sep 2023 16:09:46 +0800 Subject: [PATCH] Fill UT for api module. (#11205) * Fill UT for api module. * For checkstyle. --- .../api/ability/constant/AbilityKey.java | 2 +- .../impl/ClusterClientAbilitiesTest.java | 30 +++++++++++ .../register/impl/SdkClientAbilitiesTest.java | 30 +++++++++++ .../register/impl/ServerAbilitiesTest.java | 30 +++++++++++ .../ConfigChangeClusterSyncRequestTest.java | 1 + .../request/ConnectResetRequestTest.java | 6 ++- .../api/remote/request/RequestMetaTest.java | 18 +++++++ .../remote/request/SetupAckRequestTest.java | 50 +++++++++++++++++++ .../response/EmptyContentResponseTest.java | 10 ++++ .../response/ServerCheckResponseTest.java | 5 +- .../nacos/api/utils/AbilityKeyTest.java | 43 ++++++++++++++-- 11 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ClusterClientAbilitiesTest.java create mode 100644 api/src/test/java/com/alibaba/nacos/api/ability/register/impl/SdkClientAbilitiesTest.java create mode 100644 api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilitiesTest.java create mode 100644 api/src/test/java/com/alibaba/nacos/api/remote/request/SetupAckRequestTest.java diff --git a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java index d77460cf3..6ce1937c4 100644 --- a/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java +++ b/api/src/main/java/com/alibaba/nacos/api/ability/constant/AbilityKey.java @@ -163,7 +163,7 @@ public enum AbilityKey { // ensure that name filed is unique under a AbilityMode try { for (AbilityKey value : AbilityKey.values()) { - AbilityMode mode = value.mode; + AbilityMode mode = value.getMode(); Map map = ALL_ABILITIES.getOrDefault(mode, new HashMap<>()); AbilityKey previous = map.putIfAbsent(value.getName(), value); if (previous != null) { diff --git a/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ClusterClientAbilitiesTest.java b/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ClusterClientAbilitiesTest.java new file mode 100644 index 000000000..b6ec8a54e --- /dev/null +++ b/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ClusterClientAbilitiesTest.java @@ -0,0 +1,30 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.api.ability.register.impl; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ClusterClientAbilitiesTest { + + @Test + public void testGetStaticAbilities() { + // TODO add the cluster client abilities. + assertTrue(ClusterClientAbilities.getStaticAbilities().isEmpty()); + } +} \ No newline at end of file diff --git a/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/SdkClientAbilitiesTest.java b/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/SdkClientAbilitiesTest.java new file mode 100644 index 000000000..ea9cabd46 --- /dev/null +++ b/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/SdkClientAbilitiesTest.java @@ -0,0 +1,30 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.api.ability.register.impl; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class SdkClientAbilitiesTest { + + @Test + public void testGetStaticAbilities() { + // TODO add the sdk client abilities. + assertTrue(SdkClientAbilities.getStaticAbilities().isEmpty()); + } +} \ No newline at end of file diff --git a/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilitiesTest.java b/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilitiesTest.java new file mode 100644 index 000000000..c8d15261c --- /dev/null +++ b/api/src/test/java/com/alibaba/nacos/api/ability/register/impl/ServerAbilitiesTest.java @@ -0,0 +1,30 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.api.ability.register.impl; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ServerAbilitiesTest { + + @Test + public void testGetStaticAbilities() { + // TODO add the server abilities. + assertTrue(ServerAbilities.getStaticAbilities().isEmpty()); + } +} \ No newline at end of file diff --git a/api/src/test/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequestTest.java b/api/src/test/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequestTest.java index 6784dc2c8..6145eec4a 100644 --- a/api/src/test/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequestTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequestTest.java @@ -40,6 +40,7 @@ public class ConfigChangeClusterSyncRequestTest extends BasedConfigRequestTest { configChangeClusterSyncRequest.setTag(TAG); configChangeClusterSyncRequest.setBeta(Boolean.TRUE); configChangeClusterSyncRequest.setLastModified(0L); + configChangeClusterSyncRequest.setBatch(false); configChangeClusterSyncRequest.putAllHeader(HEADERS); requestId = injectRequestUuId(configChangeClusterSyncRequest); } diff --git a/api/src/test/java/com/alibaba/nacos/api/remote/request/ConnectResetRequestTest.java b/api/src/test/java/com/alibaba/nacos/api/remote/request/ConnectResetRequestTest.java index 021b76457..d2a7d1192 100644 --- a/api/src/test/java/com/alibaba/nacos/api/remote/request/ConnectResetRequestTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/remote/request/ConnectResetRequestTest.java @@ -27,20 +27,24 @@ public class ConnectResetRequestTest extends BasicRequestTest { request.setServerIp("127.0.0.1"); request.setServerPort("8888"); request.setRequestId("1"); + request.setConnectionId("11111_127.0.0.1_8888"); String json = mapper.writeValueAsString(request); Assert.assertNotNull(json); Assert.assertTrue(json.contains("\"serverIp\":\"127.0.0.1\"")); Assert.assertTrue(json.contains("\"serverPort\":\"8888\"")); Assert.assertTrue(json.contains("\"module\":\"internal\"")); Assert.assertTrue(json.contains("\"requestId\":\"1\"")); + Assert.assertTrue(json.contains("\"connectionId\":\"11111_127.0.0.1_8888\"")); } @Test public void testDeserialize() throws Exception { - String json = "{\"headers\":{},\"requestId\":\"1\",\"serverIp\":\"127.0.0.1\",\"serverPort\":\"8888\",\"module\":\"internal\"}"; + String json = "{\"headers\":{},\"requestId\":\"1\",\"serverIp\":\"127.0.0.1\",\"serverPort\":\"8888\"," + + "\"module\":\"internal\",\"connectionId\":\"11111_127.0.0.1_8888\"}"; ConnectResetRequest result = mapper.readValue(json, ConnectResetRequest.class); Assert.assertNotNull(result); Assert.assertEquals("127.0.0.1", result.getServerIp()); Assert.assertEquals("8888", result.getServerPort()); + Assert.assertEquals("11111_127.0.0.1_8888", result.getConnectionId()); } } \ No newline at end of file diff --git a/api/src/test/java/com/alibaba/nacos/api/remote/request/RequestMetaTest.java b/api/src/test/java/com/alibaba/nacos/api/remote/request/RequestMetaTest.java index 89e9e0fc4..13f60b5b3 100644 --- a/api/src/test/java/com/alibaba/nacos/api/remote/request/RequestMetaTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/remote/request/RequestMetaTest.java @@ -16,9 +16,12 @@ package com.alibaba.nacos.api.remote.request; +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import com.alibaba.nacos.api.ability.constant.AbilityStatus; import org.junit.Before; import org.junit.Test; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -68,4 +71,19 @@ public class RequestMetaTest { String expected = "RequestMeta{connectionId='test-connection-id', clientIp='127.0.0.1', clientVersion='1.0.0', labels={env=dev}}"; assertEquals(expected, requestMeta.toString()); } + + @Test + public void testGetConnectionAbilityForNonExist() { + assertEquals(AbilityStatus.UNKNOWN, requestMeta.getConnectionAbility(AbilityKey.SERVER_TEST_1)); + requestMeta.setAbilityTable(Collections.emptyMap()); + assertEquals(AbilityStatus.UNKNOWN, requestMeta.getConnectionAbility(AbilityKey.SERVER_TEST_1)); + } + + @Test + public void testGetConnectionAbilityForExist() { + requestMeta.setAbilityTable(Collections.singletonMap(AbilityKey.SERVER_TEST_1.getName(), Boolean.FALSE)); + assertEquals(AbilityStatus.NOT_SUPPORTED, requestMeta.getConnectionAbility(AbilityKey.SERVER_TEST_1)); + requestMeta.setAbilityTable(Collections.singletonMap(AbilityKey.SERVER_TEST_1.getName(), Boolean.TRUE)); + assertEquals(AbilityStatus.SUPPORTED, requestMeta.getConnectionAbility(AbilityKey.SERVER_TEST_1)); + } } diff --git a/api/src/test/java/com/alibaba/nacos/api/remote/request/SetupAckRequestTest.java b/api/src/test/java/com/alibaba/nacos/api/remote/request/SetupAckRequestTest.java new file mode 100644 index 000000000..b0697db5e --- /dev/null +++ b/api/src/test/java/com/alibaba/nacos/api/remote/request/SetupAckRequestTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.api.remote.request; + +import com.alibaba.nacos.api.ability.constant.AbilityKey; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; + +public class SetupAckRequestTest extends BasicRequestTest { + + @Test + public void testSerialize() throws Exception { + SetupAckRequest request = new SetupAckRequest( + Collections.singletonMap(AbilityKey.SERVER_TEST_1.getName(), Boolean.TRUE)); + request.setRequestId("1"); + String json = mapper.writeValueAsString(request); + System.out.println(json); + Assert.assertNotNull(json); + Assert.assertTrue(json.contains("\"abilityTable\":{\"test_1\":true}")); + Assert.assertTrue(json.contains("\"module\":\"internal\"")); + Assert.assertTrue(json.contains("\"requestId\":\"1\"")); + } + + @Test + public void testDeserialize() throws Exception { + String json = + "{\"headers\":{},\"requestId\":\"1\",\"abilityTable\":{\"test_1\":true}," + "\"module\":\"internal\"}"; + SetupAckRequest result = mapper.readValue(json, SetupAckRequest.class); + Assert.assertNotNull(result); + Assert.assertTrue(result.getAbilityTable().get("test_1")); + Assert.assertEquals("1", result.getRequestId()); + Assert.assertEquals("internal", result.getModule()); + } +} \ No newline at end of file diff --git a/api/src/test/java/com/alibaba/nacos/api/remote/response/EmptyContentResponseTest.java b/api/src/test/java/com/alibaba/nacos/api/remote/response/EmptyContentResponseTest.java index c737c369b..28b23af08 100644 --- a/api/src/test/java/com/alibaba/nacos/api/remote/response/EmptyContentResponseTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/remote/response/EmptyContentResponseTest.java @@ -92,6 +92,16 @@ public class EmptyContentResponseTest { assertCommonResponse(response); } + @Test + public void testSetupAckResponse() throws JsonProcessingException { + SetupAckResponse response = new SetupAckResponse(); + response.setRequestId("1"); + String actual = mapper.writeValueAsString(response); + assertCommonResponseJson(actual); + response = mapper.readValue(COMMON_JSON, SetupAckResponse.class); + assertCommonResponse(response); + } + private void assertCommonResponse(Response response) { assertTrue(response.isSuccess()); assertNull(response.getMessage()); diff --git a/api/src/test/java/com/alibaba/nacos/api/remote/response/ServerCheckResponseTest.java b/api/src/test/java/com/alibaba/nacos/api/remote/response/ServerCheckResponseTest.java index c010f0d99..9a7603435 100644 --- a/api/src/test/java/com/alibaba/nacos/api/remote/response/ServerCheckResponseTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/remote/response/ServerCheckResponseTest.java @@ -41,12 +41,15 @@ public class ServerCheckResponseTest { ServerCheckResponse response = new ServerCheckResponse("35643245_1.1.1.1_3306", false); String actual = mapper.writeValueAsString(response); assertTrue(actual.contains("\"connectionId\":\"35643245_1.1.1.1_3306\"")); + assertTrue(actual.contains("\"supportAbilityNegotiation\":false")); } @Test public void testDeserialization() throws JsonProcessingException { - String json = "{\"resultCode\":200,\"errorCode\":0,\"connectionId\":\"35643245_1.1.1.1_3306\",\"success\":true}"; + String json = "{\"resultCode\":200,\"errorCode\":0,\"connectionId\":\"35643245_1.1.1.1_3306\",\"success\":true," + + "\"supportAbilityNegotiation\":true}"; ServerCheckResponse response = mapper.readValue(json, ServerCheckResponse.class); assertEquals("35643245_1.1.1.1_3306", response.getConnectionId()); + assertTrue(response.isSupportAbilityNegotiation()); } } \ No newline at end of file diff --git a/api/src/test/java/com/alibaba/nacos/api/utils/AbilityKeyTest.java b/api/src/test/java/com/alibaba/nacos/api/utils/AbilityKeyTest.java index 32af5ad2b..c757d5fa0 100644 --- a/api/src/test/java/com/alibaba/nacos/api/utils/AbilityKeyTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/utils/AbilityKeyTest.java @@ -21,9 +21,14 @@ import com.alibaba.nacos.api.ability.constant.AbilityMode; import org.junit.Assert; import org.junit.Test; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /**. * @author Daydreamer * @description Ability key test @@ -35,28 +40,34 @@ public class AbilityKeyTest { public void testMapStr() { Map enumMap = new HashMap<>(); Map stringBooleanMap = AbilityKey.mapStr(enumMap); - Assert.assertEquals(0, stringBooleanMap.size()); + assertEquals(0, stringBooleanMap.size()); enumMap.put(AbilityKey.SERVER_TEST_1, true); enumMap.put(AbilityKey.SERVER_TEST_2, false); stringBooleanMap = AbilityKey.mapStr(enumMap); - Assert.assertEquals(2, stringBooleanMap.size()); + assertEquals(2, stringBooleanMap.size()); Assert.assertTrue(stringBooleanMap.get(AbilityKey.SERVER_TEST_1.getName())); Assert.assertFalse(stringBooleanMap.get(AbilityKey.SERVER_TEST_2.getName())); enumMap.put(AbilityKey.SERVER_TEST_2, true); stringBooleanMap = AbilityKey.mapStr(enumMap); - Assert.assertEquals(2, stringBooleanMap.size()); + assertEquals(2, stringBooleanMap.size()); Assert.assertTrue(stringBooleanMap.get(AbilityKey.SERVER_TEST_1.getName())); Assert.assertTrue(stringBooleanMap.get(AbilityKey.SERVER_TEST_2.getName())); } + @Test + public void testMapEnumForEmpty() { + Map actual = AbilityKey.mapEnum(AbilityMode.SERVER, Collections.emptyMap()); + assertTrue(actual.isEmpty()); + } + @Test public void testMapEnum() { Map mapStr = new HashMap<>(); mapStr.put("test-no-existed", true); Map enumMap = AbilityKey.mapEnum(AbilityMode.SERVER, mapStr); - Assert.assertEquals(0, enumMap.size()); + assertEquals(0, enumMap.size()); mapStr.put(AbilityKey.SERVER_TEST_2.getName(), false); mapStr.put(AbilityKey.SERVER_TEST_1.getName(), true); @@ -73,4 +84,28 @@ public class AbilityKeyTest { } + @Test + public void testGetAllValues() { + Collection actual = AbilityKey.getAllValues(AbilityMode.SERVER); + assertEquals(2, actual.size()); + actual = AbilityKey.getAllValues(AbilityMode.SDK_CLIENT); + assertEquals(1, actual.size()); + actual = AbilityKey.getAllValues(AbilityMode.CLUSTER_CLIENT); + assertEquals(1, actual.size()); + } + + @Test + public void testGetAllNames() { + Collection actual = AbilityKey.getAllNames(AbilityMode.SERVER); + assertEquals(2, actual.size()); + actual = AbilityKey.getAllNames(AbilityMode.SDK_CLIENT); + assertEquals(1, actual.size()); + actual = AbilityKey.getAllNames(AbilityMode.CLUSTER_CLIENT); + assertEquals(1, actual.size()); + } + + @Test + public void testGetDescription() { + assertEquals("just for junit test", AbilityKey.SERVER_TEST_1.getDescription()); + } }