Supple unit tests for api module config package. (#10338)

* Supple unit tests for api module config.remote package.

* Supple unit tests for api module config package.
This commit is contained in:
杨翊 SionYang 2023-04-19 10:34:47 +08:00 committed by GitHub
parent 323a3c86ab
commit bbf1bd8e6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 380 additions and 10 deletions

View File

@ -0,0 +1,39 @@
/*
* 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.config;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ConfigChangeEventTest {
@Test
public void testConstructor() {
Map<String, ConfigChangeItem> mockData = new HashMap<>();
mockData.put("test", new ConfigChangeItem("testKey", null, "testValue"));
ConfigChangeEvent event = new ConfigChangeEvent(mockData);
assertEquals(1, event.getChangeItems().size());
assertEquals("testKey", event.getChangeItem("test").getKey());
assertNull(event.getChangeItem("test").getOldValue());
assertEquals("testValue", event.getChangeItem("test").getNewValue());
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.config;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ConfigChangeItemTest {
@Test
public void testSetNewValue() {
ConfigChangeItem item = new ConfigChangeItem("testKey", null, "testValue");
item.setType(PropertyChangeType.ADDED);
assertEquals("testKey", item.getKey());
assertNull(item.getOldValue());
assertEquals("testValue", item.getNewValue());
assertEquals(PropertyChangeType.ADDED, item.getType());
item.setOldValue("testValue");
item.setNewValue("testValue2");
item.setType(PropertyChangeType.MODIFIED);
assertEquals("testKey", item.getKey());
assertEquals("testValue", item.getOldValue());
assertEquals("testValue2", item.getNewValue());
assertEquals(PropertyChangeType.MODIFIED, item.getType());
item.setKey("deletedKey");
item.setType(PropertyChangeType.DELETED);
assertEquals("deletedKey", item.getKey());
assertEquals(PropertyChangeType.DELETED, item.getType());
}
@Test
public void testToString() {
ConfigChangeItem item = new ConfigChangeItem("testKey", null, "testValue");
item.setType(PropertyChangeType.ADDED);
assertEquals("ConfigChangeItem{key='testKey', oldValue='null', newValue='testValue', type=ADDED}",
item.toString());
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.config;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class ConfigTypeTest {
@Test
public void isValidType() {
assertTrue(ConfigType.isValidType("xml"));
assertTrue(ConfigType.isValidType("properties"));
assertTrue(ConfigType.isValidType("json"));
assertTrue(ConfigType.isValidType("text"));
assertTrue(ConfigType.isValidType("html"));
assertTrue(ConfigType.isValidType("yaml"));
assertTrue(ConfigType.isValidType("unset"));
assertFalse(ConfigType.isValidType(""));
assertFalse(ConfigType.isValidType("yml"));
}
@Test
public void testGetDefaultType() {
assertEquals("text", ConfigType.getDefaultType().getType());
}
}

View File

@ -0,0 +1,39 @@
/*
* 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.config.ability;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
public class ServerConfigAbilityTest {
@Test
public void testEquals() {
ServerConfigAbility ability = new ServerConfigAbility();
ability.setSupportRemoteMetrics(true);
assertEquals(ability, ability);
assertFalse(ability.equals(null));
assertFalse(ability.equals(new ClientConfigAbility()));
ServerConfigAbility newOne = new ServerConfigAbility();
assertNotEquals(ability, newOne);
newOne.setSupportRemoteMetrics(true);
assertEquals(ability, newOne);
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.config.listener;
import org.junit.Test;
import static org.junit.Assert.assertNull;
public class AbstractListenerTest {
@Test
public void testGetExecutor() {
// Default listener executor is null.
assertNull(new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
}
}.getExecutor());
}
}

View File

@ -0,0 +1,69 @@
/*
* 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.config.listener;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class AbstractSharedListenerTest {
private static final String CONFIG_CONTENT = "test";
private static Map<String, String> receivedMap;
@Before
public void setUp() {
receivedMap = new HashMap<>();
}
@Test
public void testFillContext() {
assertEquals(0, receivedMap.size());
MockShardListener listener = new MockShardListener();
listener.receiveConfigInfo(CONFIG_CONTENT);
assertEquals(2, receivedMap.size());
assertNull(receivedMap.get("group"));
assertNull(receivedMap.get("dataId"));
listener.fillContext("aaa", "ggg");
listener.receiveConfigInfo(CONFIG_CONTENT);
assertEquals(2, receivedMap.size());
assertEquals("ggg", receivedMap.get("group"));
assertEquals("aaa", receivedMap.get("dataId"));
}
@Test
public void getExecutor() {
// Default listener executor is null.
assertNull(new MockShardListener().getExecutor());
}
private static class MockShardListener extends AbstractSharedListener {
@Override
public void innerReceive(String dataId, String group, String configInfo) {
assertEquals(CONFIG_CONTENT, configInfo);
receivedMap.put("group", group);
receivedMap.put("dataId", dataId);
}
}
}

View File

@ -20,9 +20,13 @@ import com.alibaba.nacos.api.common.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
import java.util.Objects;
import static com.alibaba.nacos.api.config.remote.request.ClientConfigMetricRequest.MetricsKey.CACHE_DATA;
import static com.alibaba.nacos.api.config.remote.request.ClientConfigMetricRequest.MetricsKey.SNAPSHOT_DATA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
public class ClientConfigMetricRequestTest extends BasedConfigRequestTest {
@ -48,14 +52,40 @@ public class ClientConfigMetricRequestTest extends BasedConfigRequestTest {
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"header1\":\"test_header1\"},"
+ "\"metricsKeys\":[{\"type\":\"cacheData\",\"key\":"
+ "\"test_data+group+test_tenant\"},{\"type\":\"snapshotData\","
+ "\"key\":\"test_data+group+test_tenant\"}],\"module\":\"config\"}";
String json =
"{\"headers\":{\"header1\":\"test_header1\"}," + "\"metricsKeys\":[{\"type\":\"cacheData\",\"key\":"
+ "\"test_data+group+test_tenant\"},{\"type\":\"snapshotData\","
+ "\"key\":\"test_data+group+test_tenant\"}],\"module\":\"config\"}";
ClientConfigMetricRequest actual = mapper.readValue(json, ClientConfigMetricRequest.class);
assertEquals(actual.getMetricsKeys().size(), 2);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getHeader(HEADER_KEY), HEADER_VALUE);
}
@Test
public void testMetricsKeysEquals() {
String dataKey = String.join("+", KEY);
ClientConfigMetricRequest.MetricsKey key = ClientConfigMetricRequest.MetricsKey.build(CACHE_DATA, dataKey);
assertEquals(key, key);
assertFalse(key.equals(null));
assertFalse(key.equals(new ClientConfigMetricRequest()));
ClientConfigMetricRequest.MetricsKey newOne = ClientConfigMetricRequest.MetricsKey
.build(SNAPSHOT_DATA, dataKey);
assertNotEquals(key, newOne);
newOne.setType(CACHE_DATA);
assertEquals(key, newOne);
}
@Test
public void testMetricsHashCode() {
String dataKey = String.join("+", KEY);
ClientConfigMetricRequest.MetricsKey key = ClientConfigMetricRequest.MetricsKey.build(CACHE_DATA, dataKey);
assertEquals(Objects.hash(CACHE_DATA, dataKey), key.hashCode());
}
@Test
public void testMetricsToString() {
ClientConfigMetricRequest.MetricsKey key = ClientConfigMetricRequest.MetricsKey.build(CACHE_DATA, String.join("+", KEY));
assertEquals("MetricsKey{type='cacheData', key='test_data+group+test_tenant'}", key.toString());
}
}

View File

@ -52,4 +52,12 @@ public class ConfigBatchListenRequestTest extends BasedConfigRequestTest {
assertEquals(actual.getHeader(HEADER_KEY), HEADER_VALUE);
assertEquals(actual.getConfigListenContexts().size(), 1);
}
@Test
public void testConfigListenContextToString() {
ConfigBatchListenRequest configBatchListenRequest = new ConfigBatchListenRequest();
configBatchListenRequest.addConfigListenContext(GROUP, DATA_ID, TENANT, MD5);
assertEquals("ConfigListenContext{group='group', md5='test_MD5', dataId='test_data', tenant='test_tenant'}",
configBatchListenRequest.getConfigListenContexts().get(0).toString());
}
}

View File

@ -39,6 +39,7 @@ public class ConfigPublishRequestTest extends BasedConfigRequestTest {
configPublishRequest = new ConfigPublishRequest(DATA_ID, GROUP, TENANT, CONTENT);
configPublishRequest.putAdditionalParam(TAG_PARAM, TAG_PARAM);
configPublishRequest.putAdditionalParam(APP_NAME_PARAM, APP_NAME_PARAM);
configPublishRequest.setCasMd5(MD5);
configPublishRequest.putAllHeader(HEADERS);
requestId = injectRequestUuId(configPublishRequest);
}
@ -52,6 +53,7 @@ public class ConfigPublishRequestTest extends BasedConfigRequestTest {
assertTrue(json.contains("\"group\":\"" + GROUP));
assertTrue(json.contains("\"tenant\":\"" + TENANT));
assertTrue(json.contains("\"content\":\"" + CONTENT));
assertTrue(json.contains("\"casMd5\":\"" + MD5));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@ -59,7 +61,7 @@ public class ConfigPublishRequestTest extends BasedConfigRequestTest {
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"header1\":\"test_header1\"},\"dataId\":\"test_data\",\"group\":\"group\","
+ "\"tenant\":\"test_tenant\",\"content\":\"content\","
+ "\"tenant\":\"test_tenant\",\"content\":\"content\",\"casMd5\":\"test_MD5\","
+ "\"additionMap\":{\"appName\":\"appName\",\"tag\":\"tag\"},\"module\":\"config\"}";
ConfigPublishRequest actual = mapper.readValue(json, ConfigPublishRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
@ -67,6 +69,7 @@ public class ConfigPublishRequestTest extends BasedConfigRequestTest {
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getContent(), CONTENT);
assertEquals(actual.getCasMd5(), MD5);
assertEquals(actual.getAdditionParam(TAG_PARAM), TAG_PARAM);
assertEquals(actual.getAdditionParam(APP_NAME_PARAM), APP_NAME_PARAM);
}

View File

@ -40,6 +40,7 @@ public class ConfigQueryRequestTest extends BasedConfigRequestTest {
headers.put(Constants.Config.NOTIFY_HEADER, Boolean.TRUE.toString());
configQueryRequest = ConfigQueryRequest.build(DATA_ID, GROUP, TENANT);
configQueryRequest.putAllHeader(headers);
configQueryRequest.setTag(TAG);
requestId = injectRequestUuId(configQueryRequest);
}
@ -56,6 +57,7 @@ public class ConfigQueryRequestTest extends BasedConfigRequestTest {
assertTrue(json.contains("\"dataId\":\"" + DATA_ID));
assertTrue(json.contains("\"group\":\"" + GROUP));
assertTrue(json.contains("\"tenant\":\"" + TENANT));
assertTrue(json.contains("\"tag\":\"" + TAG));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@ -63,11 +65,12 @@ public class ConfigQueryRequestTest extends BasedConfigRequestTest {
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"notify\":\"true\"},\"dataId\":\"test_data\",\"group\":\"group\","
+ "\"tenant\":\"test_tenant\",\"notify\":true,\"module\":\"config\"}";
+ "\"tenant\":\"test_tenant\",\"notify\":true,\"module\":\"config\",\"tag\":\"tag\"}";
ConfigQueryRequest actual = mapper.readValue(json, ConfigQueryRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
assertEquals(actual.getGroup(), GROUP);
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getTag(), TAG);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
}
}

View File

@ -36,9 +36,9 @@ public class ClientConfigMetricResponseTest extends BasedConfigResponseTest {
@Before
public void before() {
metric.put("m1", "v1");
metric.put("m2", "v2");
clientConfigMetricResponse = new ClientConfigMetricResponse();
clientConfigMetricResponse.setMetrics(metric);
clientConfigMetricResponse.putMetric("m2", "v2");
requestId = injectResponseUuId(clientConfigMetricResponse);
}

View File

@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ConfigChangeBatchListenResponseTest extends BasedConfigResponseTest {
@ -42,17 +43,35 @@ public class ConfigChangeBatchListenResponseTest extends BasedConfigResponseTest
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
assertTrue(json.contains("\"changedConfigs\":[{\"group\":\"group\",\"dataId\":\"test_data\",\"tenant\":\"test_tenant\"}]"));
assertTrue(json.contains(
"\"changedConfigs\":[{\"group\":\"group\",\"dataId\":\"test_data\",\"tenant\":\"test_tenant\"}]"));
}
@Override
@Test
public void testSerializeFailResponse() throws JsonProcessingException {
ConfigChangeBatchListenResponse configChangeBatchListenResponse = ConfigChangeBatchListenResponse.buildFailResponse("Fail");
ConfigChangeBatchListenResponse configChangeBatchListenResponse = ConfigChangeBatchListenResponse
.buildFailResponse("Fail");
String json = mapper.writeValueAsString(configChangeBatchListenResponse);
assertTrue(json.contains("\"resultCode\":" + ResponseCode.FAIL.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
assertTrue(json.contains("\"message\":\"Fail\""));
assertTrue(json.contains("\"success\":false"));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"resultCode\":200,\"errorCode\":0,\"requestId\":\"061e36b0-c7bd-4fd0-950c-73b13ca1cb2f\","
+ "\"changedConfigs\":[{\"group\":\"group\",\"dataId\":\"test_data\",\"tenant\":\"test_tenant\"}],\"success\":true}";
ConfigChangeBatchListenResponse actual = mapper.readValue(json, ConfigChangeBatchListenResponse.class);
assertTrue(actual.isSuccess());
assertEquals(ResponseCode.SUCCESS.getCode(), actual.getResultCode());
assertEquals("061e36b0-c7bd-4fd0-950c-73b13ca1cb2f", actual.getRequestId());
assertEquals(TENANT, actual.getChangedConfigs().get(0).getTenant());
assertEquals(GROUP, actual.getChangedConfigs().get(0).getGroup());
assertEquals(DATA_ID, actual.getChangedConfigs().get(0).getDataId());
assertEquals("ConfigContext{group='group', dataId='test_data', tenant='test_tenant'}",
actual.getChangedConfigs().get(0).toString());
}
}

View File

@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ConfigQueryResponseTest extends BasedConfigResponseTest {
@ -30,6 +31,11 @@ public class ConfigQueryResponseTest extends BasedConfigResponseTest {
@Before
public void before() {
configQueryResponse = ConfigQueryResponse.buildSuccessResponse("success");
configQueryResponse.setContentType("text");
configQueryResponse.setEncryptedDataKey("encryptedKey");
configQueryResponse.setLastModified(1111111L);
configQueryResponse.setMd5(MD5);
configQueryResponse.setTag(TAG);
requestId = injectResponseUuId(configQueryResponse);
}
@ -40,8 +46,11 @@ public class ConfigQueryResponseTest extends BasedConfigResponseTest {
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"md5\":\"" + MD5 + "\""));
assertTrue(json.contains("\"errorCode\":0"));
assertTrue(json.contains("\"content\":\"" + "success"));
assertTrue(json.contains("\"content\":\"success\""));
assertTrue(json.contains("\"contentType\":\"text\""));
assertTrue(json.contains("\"lastModified\":1111111"));
}
@Override
@ -54,4 +63,22 @@ public class ConfigQueryResponseTest extends BasedConfigResponseTest {
assertTrue(json.contains("\"message\":\"Fail\""));
assertTrue(json.contains("\"success\":false"));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"resultCode\":200,\"errorCode\":0,\"requestId\":\"2239753e-e682-441c-83cf-fb8129ca68a4\","
+ "\"content\":\"success\",\"encryptedDataKey\":\"encryptedKey\",\"contentType\":\"text\",\"md5\":\"test_MD5\","
+ "\"lastModified\":1111111,\"tag\":\"tag\",\"beta\":false,\"success\":true}\n";
ConfigQueryResponse actual = mapper.readValue(json, ConfigQueryResponse.class);
assertTrue(actual.isSuccess());
assertEquals(ResponseCode.SUCCESS.getCode(), actual.getResultCode());
assertEquals("success", actual.getContent());
assertEquals("text", actual.getContentType());
assertEquals("2239753e-e682-441c-83cf-fb8129ca68a4", actual.getRequestId());
assertEquals(MD5, actual.getMd5());
assertEquals(TAG, actual.getTag());
assertEquals("text", actual.getContentType());
assertEquals(1111111L, actual.getLastModified());
}
}