Fill UT for api module. (#11205)

* Fill UT for api module.

* For checkstyle.
This commit is contained in:
杨翊 SionYang 2023-09-28 16:09:46 +08:00 committed by GitHub
parent 1192e4c36e
commit f3fb428452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 218 additions and 7 deletions

View File

@ -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<String, AbilityKey> map = ALL_ABILITIES.getOrDefault(mode, new HashMap<>());
AbilityKey previous = map.putIfAbsent(value.getName(), value);
if (previous != null) {

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}

View File

@ -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());

View File

@ -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());
}
}

View File

@ -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<AbilityKey, Boolean> enumMap = new HashMap<>();
Map<String, Boolean> 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<AbilityKey, Boolean> actual = AbilityKey.mapEnum(AbilityMode.SERVER, Collections.emptyMap());
assertTrue(actual.isEmpty());
}
@Test
public void testMapEnum() {
Map<String, Boolean> mapStr = new HashMap<>();
mapStr.put("test-no-existed", true);
Map<AbilityKey, Boolean> 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<AbilityKey> 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<String> 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());
}
}