Add unit test for api module (#10316)

* add some unit test for api module.

* fix code style error
This commit is contained in:
Karson 2023-04-13 19:03:55 +08:00 committed by GitHub
parent 1f28e28549
commit 916534ecfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 1083 additions and 0 deletions

View File

@ -0,0 +1,74 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.remote.request.Request;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.BeforeClass;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public abstract class BasedConfigRequestTest {
protected static ObjectMapper mapper;
protected static final String DATA_ID = "test_data";
protected static final String GROUP = "group";
protected static final String TENANT = "test_tenant";
protected static final String MD5 = "test_MD5";
protected static final String TAG = "tag";
protected static final String[] KEY = new String[] {DATA_ID, GROUP, TENANT};
protected static final Map<String, String> HEADERS = new HashMap<>();
protected static final String HEADER_KEY = "header1";
protected static final String HEADER_VALUE = "test_header1";
protected static final String CONTENT = "content";
static {
HEADERS.put(HEADER_KEY, HEADER_VALUE);
}
@BeforeClass
public static void setUp() throws Exception {
mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public abstract void testSerialize() throws JsonProcessingException;
public abstract void testDeserialize() throws JsonProcessingException;
protected String injectRequestUuId(Request request) {
String uuid = UUID.randomUUID().toString();
request.setRequestId(uuid);
return uuid;
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.common.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
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.assertTrue;
public class ClientConfigMetricRequestTest extends BasedConfigRequestTest {
@Override
@Test
public void testSerialize() throws JsonProcessingException {
ClientConfigMetricRequest clientMetrics = new ClientConfigMetricRequest();
clientMetrics.putAllHeader(HEADERS);
clientMetrics.getMetricsKeys()
.add(ClientConfigMetricRequest.MetricsKey.build(CACHE_DATA, String.join("+", KEY)));
clientMetrics.getMetricsKeys()
.add(ClientConfigMetricRequest.MetricsKey.build(SNAPSHOT_DATA, String.join("+", KEY)));
final String requestId = injectRequestUuId(clientMetrics);
String json = mapper.writeValueAsString(clientMetrics);
assertTrue(json.contains("\"type\":\"" + "cacheData" + "\""));
assertTrue(json.contains("\"type\":\"" + "snapshotData" + "\""));
assertTrue(json.contains("\"key\":\"" + String.join("+", KEY) + "\""));
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@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\"}";
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);
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.common.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ConfigBatchListenRequestTest extends BasedConfigRequestTest {
@Override
@Test
public void testSerialize() throws JsonProcessingException {
ConfigBatchListenRequest configBatchListenRequest = new ConfigBatchListenRequest();
configBatchListenRequest.putAllHeader(HEADERS);
configBatchListenRequest.addConfigListenContext(GROUP, DATA_ID, TENANT, MD5);
final String requestId = injectRequestUuId(configBatchListenRequest);
String json = mapper.writeValueAsString(configBatchListenRequest);
assertTrue(json.contains("\"listen\":" + "true"));
assertTrue(json.contains(
"\"configListenContexts\":[{\"group\":\"group\",\"md5\":\"test_MD5\",\"dataId\":\"test_data\",\"tenant\":\"test_tenant\"}]"));
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"header1\":\"test_header1\"},\"listen\":true,"
+ "\"configListenContexts\":[{\"group\":\"group\",\"md5\":\"test_MD5\","
+ "\"dataId\":\"test_data\",\"tenant\":\"test_tenant\"}],\"module\":\"config\"}";
ConfigBatchListenRequest actual = mapper.readValue(json, ConfigBatchListenRequest.class);
assertEquals(actual.isListen(), true);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getHeader(HEADER_KEY), HEADER_VALUE);
assertEquals(actual.getConfigListenContexts().size(), 1);
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.common.Constants;
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 ConfigChangeNotifyRequestTest extends BasedConfigRequestTest {
ConfigChangeNotifyRequest configChangeNotifyRequest;
String requestId;
@Before
public void before() {
configChangeNotifyRequest = ConfigChangeNotifyRequest.build(DATA_ID, GROUP, TENANT);
configChangeNotifyRequest.putAllHeader(HEADERS);
requestId = injectRequestUuId(configChangeNotifyRequest);
}
@Override
@Test
public void testSerialize() throws JsonProcessingException {
String json = mapper.writeValueAsString(configChangeNotifyRequest);
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
assertTrue(json.contains("\"dataId\":\"" + DATA_ID));
assertTrue(json.contains("\"group\":\"" + GROUP));
assertTrue(json.contains("\"tenant\":\"" + TENANT));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"header1\":\"test_header1\"},\"dataId\":\"test_data\",\"group\":"
+ "\"group\",\"tenant\":\"test_tenant\",\"module\":\"config\"}";
ConfigChangeNotifyRequest actual = mapper.readValue(json, ConfigChangeNotifyRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
assertEquals(actual.getGroup(), GROUP);
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getHeader(HEADER_KEY), HEADER_VALUE);
}
}

View File

@ -0,0 +1,73 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.common.Constants;
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 ConfigPublishRequestTest extends BasedConfigRequestTest {
ConfigPublishRequest configPublishRequest;
private static final String TAG_PARAM = "tag";
private static final String APP_NAME_PARAM = "appName";
String requestId;
@Before
public void before() {
configPublishRequest = new ConfigPublishRequest(DATA_ID, GROUP, TENANT, CONTENT);
configPublishRequest.putAdditionalParam(TAG_PARAM, TAG_PARAM);
configPublishRequest.putAdditionalParam(APP_NAME_PARAM, APP_NAME_PARAM);
configPublishRequest.putAllHeader(HEADERS);
requestId = injectRequestUuId(configPublishRequest);
}
@Override
@Test
public void testSerialize() throws JsonProcessingException {
String json = mapper.writeValueAsString(configPublishRequest);
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
assertTrue(json.contains("\"dataId\":\"" + DATA_ID));
assertTrue(json.contains("\"group\":\"" + GROUP));
assertTrue(json.contains("\"tenant\":\"" + TENANT));
assertTrue(json.contains("\"content\":\"" + CONTENT));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"header1\":\"test_header1\"},\"dataId\":\"test_data\",\"group\":\"group\","
+ "\"tenant\":\"test_tenant\",\"content\":\"content\","
+ "\"additionMap\":{\"appName\":\"appName\",\"tag\":\"tag\"},\"module\":\"config\"}";
ConfigPublishRequest actual = mapper.readValue(json, ConfigPublishRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
assertEquals(actual.getGroup(), GROUP);
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getContent(), CONTENT);
assertEquals(actual.getAdditionParam(TAG_PARAM), TAG_PARAM);
assertEquals(actual.getAdditionParam(APP_NAME_PARAM), APP_NAME_PARAM);
}
}

View File

@ -0,0 +1,73 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.common.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
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.assertTrue;
public class ConfigQueryRequestTest extends BasedConfigRequestTest {
ConfigQueryRequest configQueryRequest;
Map<String, String> headers = new HashMap<>();
String requestId;
@Before
public void before() {
headers.put(Constants.Config.NOTIFY_HEADER, Boolean.TRUE.toString());
configQueryRequest = ConfigQueryRequest.build(DATA_ID, GROUP, TENANT);
configQueryRequest.putAllHeader(headers);
requestId = injectRequestUuId(configQueryRequest);
}
@Test
public void testIsNotify() {
assertTrue(configQueryRequest.isNotify());
}
@Override
@Test
public void testSerialize() throws JsonProcessingException {
String json = mapper.writeValueAsString(configQueryRequest);
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
assertTrue(json.contains("\"dataId\":\"" + DATA_ID));
assertTrue(json.contains("\"group\":\"" + GROUP));
assertTrue(json.contains("\"tenant\":\"" + TENANT));
assertTrue(json.contains("\"requestId\":\"" + requestId));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"notify\":\"true\"},\"dataId\":\"test_data\",\"group\":\"group\","
+ "\"tenant\":\"test_tenant\",\"notify\":true,\"module\":\"config\"}";
ConfigQueryRequest actual = mapper.readValue(json, ConfigQueryRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
assertEquals(actual.getGroup(), GROUP);
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 1999-2021 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.remote.request;
import com.alibaba.nacos.api.common.Constants;
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 ConfigRemoveRequestTest extends BasedConfigRequestTest {
ConfigRemoveRequest configRemoveRequest;
String requestId;
@Before
public void before() {
configRemoveRequest = new ConfigRemoveRequest(DATA_ID, GROUP, TENANT, TAG);
requestId = injectRequestUuId(configRemoveRequest);
}
@Override
@Test
public void testSerialize() throws JsonProcessingException {
String json = mapper.writeValueAsString(configRemoveRequest);
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
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));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{},\"dataId\":\"test_data\",\"group\":\"group\",\"tenant\":\"test_tenant\""
+ ",\"tag\":\"tag\",\"module\":\"config\"}";
ConfigRemoveRequest actual = mapper.readValue(json, ConfigRemoveRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
assertEquals(actual.getGroup(), GROUP);
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getTag(), TAG);
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 1999-2021 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.remote.request.cluster;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.remote.request.BasedConfigRequestTest;
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 ConfigChangeClusterSyncRequestTest extends BasedConfigRequestTest {
ConfigChangeClusterSyncRequest configChangeClusterSyncRequest;
String requestId;
@Before
public void before() {
configChangeClusterSyncRequest = new ConfigChangeClusterSyncRequest();
configChangeClusterSyncRequest.setDataId(DATA_ID);
configChangeClusterSyncRequest.setGroup(GROUP);
configChangeClusterSyncRequest.setTenant(TENANT);
configChangeClusterSyncRequest.setTag(TAG);
configChangeClusterSyncRequest.setBeta(Boolean.TRUE);
configChangeClusterSyncRequest.setLastModified(0L);
configChangeClusterSyncRequest.putAllHeader(HEADERS);
requestId = injectRequestUuId(configChangeClusterSyncRequest);
}
@Override
@Test
public void testSerialize() throws JsonProcessingException {
String json = mapper.writeValueAsString(configChangeClusterSyncRequest);
System.out.println(json);
assertTrue(json.contains("\"module\":\"" + Constants.Config.CONFIG_MODULE));
assertTrue(json.contains("\"dataId\":\"" + DATA_ID));
assertTrue(json.contains("\"group\":\"" + GROUP));
assertTrue(json.contains("\"tenant\":\"" + TENANT));
assertTrue(json.contains("\"tag\":\"" + TAG));
assertTrue(json.contains("\"beta\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"lastModified\":" + 0));
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"headers\":{\"header1\":\"test_header1\"},\"requestId\":\"ece89111-3c42-4055-aca4-c95e16ec564b\",\"dataId\":\"test_data\","
+ "\"group\":\"group\",\"tenant\":\"test_tenant\","
+ "\"tag\":\"tag\",\"lastModified\":0,\"beta\":true,\"module\":\"config\"}";
ConfigChangeClusterSyncRequest actual = mapper.readValue(json, ConfigChangeClusterSyncRequest.class);
assertEquals(actual.getDataId(), DATA_ID);
assertEquals(actual.getGroup(), GROUP);
assertEquals(actual.getTenant(), TENANT);
assertEquals(actual.getModule(), Constants.Config.CONFIG_MODULE);
assertEquals(actual.getLastModified(), 0L);
assertTrue(actual.isBeta());
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 1999-2021 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.remote.response;
import com.alibaba.nacos.api.config.remote.request.BasedConfigRequestTest;
import com.alibaba.nacos.api.remote.response.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.UUID;
public abstract class BasedConfigResponseTest extends BasedConfigRequestTest {
protected String requestId;
@Override
public void testSerialize() throws JsonProcessingException {
}
@Override
public void testDeserialize() throws JsonProcessingException {
}
public abstract void testSerializeSuccessResponse() throws JsonProcessingException;
public abstract void testSerializeFailResponse() throws JsonProcessingException;
protected String injectResponseUuId(Response response) {
String uuid = UUID.randomUUID().toString();
response.setRequestId(uuid);
return uuid;
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright 1999-2021 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.remote.response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
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.assertTrue;
public class ClientConfigMetricResponseTest extends BasedConfigResponseTest {
ClientConfigMetricResponse clientConfigMetricResponse;
Map<String, Object> metric = new HashMap<>(16);
@Before
public void before() {
metric.put("m1", "v1");
metric.put("m2", "v2");
clientConfigMetricResponse = new ClientConfigMetricResponse();
clientConfigMetricResponse.setMetrics(metric);
requestId = injectResponseUuId(clientConfigMetricResponse);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(clientConfigMetricResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
}
@Override
public void testSerializeFailResponse() throws JsonProcessingException {
}
@Override
@Test
public void testDeserialize() throws JsonProcessingException {
String json = "{\"resultCode\":200,\"errorCode\":0,\"requestId\":\"6ef9237b-24f3-448a-87fc-713f18ee06a1\","
+ "\"metrics\":{\"m1\":\"v1\",\"m2\":\"v2\"},\"success\":true}";
ClientConfigMetricResponse actual = mapper.readValue(json, ClientConfigMetricResponse.class);
assertTrue(actual.isSuccess());
assertEquals(actual.getResultCode(), ResponseCode.SUCCESS.getCode());
assertEquals(actual.getMetrics(), metric);
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2018 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.remote.response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ConfigChangeBatchListenResponseTest extends BasedConfigResponseTest {
ConfigChangeBatchListenResponse configChangeBatchListenResponse;
@Before
public void before() {
configChangeBatchListenResponse = new ConfigChangeBatchListenResponse();
requestId = injectResponseUuId(configChangeBatchListenResponse);
configChangeBatchListenResponse.addChangeConfig(DATA_ID, GROUP, TENANT);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(configChangeBatchListenResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
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\"}]"));
}
@Override
@Test
public void testSerializeFailResponse() throws JsonProcessingException {
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"));
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 1999-2021 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.remote.response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ConfigChangeNotifyResponseTest extends BasedConfigResponseTest {
ConfigChangeNotifyResponse configChangeNotifyResponse;
@Before
public void before() {
configChangeNotifyResponse = new ConfigChangeNotifyResponse();
requestId = injectResponseUuId(configChangeNotifyResponse);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(configChangeNotifyResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
}
@Override
public void testSerializeFailResponse() throws JsonProcessingException {
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 1999-2021 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.remote.response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ConfigPublishResponseTest extends BasedConfigResponseTest {
ConfigPublishResponse configPublishResponse;
@Before
public void before() {
configPublishResponse = ConfigPublishResponse.buildSuccessResponse();
requestId = injectResponseUuId(configPublishResponse);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(configPublishResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
}
@Override
@Test
public void testSerializeFailResponse() throws JsonProcessingException {
ConfigPublishResponse configPublishResponse = ConfigPublishResponse.buildFailResponse(500, "Fail");
String json = mapper.writeValueAsString(configPublishResponse);
assertTrue(json.contains("\"resultCode\":" + ResponseCode.FAIL.getCode()));
assertTrue(json.contains("\"errorCode\":500"));
assertTrue(json.contains("\"message\":\"Fail\""));
assertTrue(json.contains("\"success\":false"));
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 1999-2021 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.remote.response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ConfigQueryResponseTest extends BasedConfigResponseTest {
ConfigQueryResponse configQueryResponse;
@Before
public void before() {
configQueryResponse = ConfigQueryResponse.buildSuccessResponse("success");
requestId = injectResponseUuId(configQueryResponse);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(configQueryResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
assertTrue(json.contains("\"content\":\"" + "success"));
}
@Override
@Test
public void testSerializeFailResponse() throws JsonProcessingException {
ConfigQueryResponse configQueryResponse = ConfigQueryResponse.buildFailResponse(500, "Fail");
String json = mapper.writeValueAsString(configQueryResponse);
assertTrue(json.contains("\"resultCode\":" + ResponseCode.FAIL.getCode()));
assertTrue(json.contains("\"errorCode\":500"));
assertTrue(json.contains("\"message\":\"Fail\""));
assertTrue(json.contains("\"success\":false"));
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 1999-2021 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.remote.response;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ConfigRemoveResponseTest extends BasedConfigResponseTest {
ConfigRemoveResponse configRemoveResponse;
@Before
public void before() {
configRemoveResponse = ConfigRemoveResponse.buildSuccessResponse();
requestId = injectResponseUuId(configRemoveResponse);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(configRemoveResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
}
@Override
@Test
public void testSerializeFailResponse() throws JsonProcessingException {
ConfigRemoveResponse configRemoveResponse = ConfigRemoveResponse.buildFailResponse("Fail");
String json = mapper.writeValueAsString(configRemoveResponse);
assertTrue(json.contains("\"resultCode\":" + ResponseCode.FAIL.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
assertTrue(json.contains("\"message\":\"Fail\""));
assertTrue(json.contains("\"success\":false"));
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 1999-2021 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.remote.response.cluster;
import com.alibaba.nacos.api.config.remote.response.BasedConfigResponseTest;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class ConfigChangeClusterSyncResponseTest extends BasedConfigResponseTest {
ConfigChangeClusterSyncResponse configChangeClusterSyncResponse;
@Before
public void before() {
configChangeClusterSyncResponse = new ConfigChangeClusterSyncResponse();
requestId = injectResponseUuId(configChangeClusterSyncResponse);
}
@Override
@Test
public void testSerializeSuccessResponse() throws JsonProcessingException {
String json = mapper.writeValueAsString(configChangeClusterSyncResponse);
assertTrue(json.contains("\"success\":" + Boolean.TRUE));
assertTrue(json.contains("\"requestId\":\"" + requestId));
assertTrue(json.contains("\"resultCode\":" + ResponseCode.SUCCESS.getCode()));
assertTrue(json.contains("\"errorCode\":0"));
}
@Override
public void testSerializeFailResponse() throws JsonProcessingException {
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 1999-2022 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.naming.controllers.v2;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.remote.ConnectionManager;
import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Arrays;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ClientInfoControllerV2Test extends BaseTest {
@InjectMocks
ClientInfoControllerV2 clientInfoControllerV2;
@Mock
private ClientManager clientManager;
@Mock
private ConnectionManager connectionManager;
@Mock
private ClientServiceIndexesManager clientServiceIndexesManager;
private MockMvc mockmvc;
private IpPortBasedClient ipPortBasedClient;
private ConnectionBasedClient connectionBasedClient;
private static final String URL =
UtilsAndCommons.DEFAULT_NACOS_NAMING_CONTEXT_V2 + UtilsAndCommons.NACOS_NAMING_CLIENT_CONTEXT;
@Before
public void before() {
when(clientManager.allClientId()).thenReturn(Arrays.asList("127.0.0.1:8080#test1", "test2#test2"));
when(clientManager.contains(anyString())).thenReturn(true);
mockmvc = MockMvcBuilders.standaloneSetup(clientInfoControllerV2).build();
ipPortBasedClient = new IpPortBasedClient("127.0.0.1:8080#test1", false);
connectionBasedClient = new ConnectionBasedClient("test2", true, 1L);
}
@Test
public void testGetClientList() throws Exception {
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/list");
MockHttpServletResponse response = mockmvc.perform(mockHttpServletRequestBuilder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
JsonNode jsonNode = JacksonUtils.toObj(response.getContentAsString()).get("data");
Assert.assertEquals(2, jsonNode.size());
}
@Test
public void testGetClientDetail() throws Exception {
when(clientManager.getClient("test1")).thenReturn(ipPortBasedClient);
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL)
.param("clientId", "test1");
MockHttpServletResponse response = mockmvc.perform(mockHttpServletRequestBuilder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
}
}