Add some unit test for V2Controller (#10143)

* add unit test for HealthController

* for rerun ci

* add unit test for CatalogControllerV2
This commit is contained in:
Karson 2023-03-21 15:28:32 +08:00 committed by GitHub
parent c39ba4a35c
commit fe5002f485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 179 additions and 0 deletions

View File

@ -30,6 +30,9 @@ import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import java.lang.reflect.Field;
import static org.mockito.Mockito.doReturn;
@ -76,6 +79,16 @@ public abstract class BaseTest {
ApplicationUtils.injectContext(context);
}
protected MockHttpServletRequestBuilder convert(Object simpleOb, MockHttpServletRequestBuilder builder)
throws IllegalAccessException {
Field[] declaredFields = simpleOb.getClass().getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
builder.param(declaredField.getName(), String.valueOf(declaredField.get(simpleOb)));
}
return builder;
}
protected void mockInjectPushServer() {
doReturn(pushService).when(context).getBean(UdpPushService.class);
}

View File

@ -0,0 +1,86 @@
/*
* 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.api.common.Constants;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.core.CatalogServiceV2Impl;
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.ArrayList;
import java.util.List;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class CatalogControllerV2Test extends BaseTest {
@Mock
private CatalogServiceV2Impl catalogServiceV2;
@InjectMocks
private CatalogControllerV2 catalogControllerV2;
private MockMvc mockmvc;
List instances;
@Before
public void before() {
Instance instance = new Instance();
instance.setIp("1.1.1.1");
instance.setPort(1234);
instance.setClusterName(TEST_CLUSTER_NAME);
instance.setServiceName(TEST_SERVICE_NAME);
instance.setEnabled(false);
instances = new ArrayList<>(1);
instances.add(instance);
mockmvc = MockMvcBuilders.standaloneSetup(catalogControllerV2).build();
}
@Test
public void testInstanceList() throws Exception {
String serviceNameWithoutGroup = NamingUtils.getServiceName(TEST_SERVICE_NAME);
String groupName = NamingUtils.getGroupName(TEST_SERVICE_NAME);
when(catalogServiceV2.listAllInstances(Constants.DEFAULT_NAMESPACE_ID, groupName,
serviceNameWithoutGroup)).thenReturn(instances);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(
UtilsAndCommons.DEFAULT_NACOS_NAMING_CONTEXT_V2 + UtilsAndCommons.NACOS_NAMING_CATALOG_CONTEXT
+ "/instances").param("namespaceId", Constants.DEFAULT_NAMESPACE_ID)
.param("serviceName", TEST_SERVICE_NAME).param("pageNo", "1").param("pageSize", "100");
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
JsonNode data = JacksonUtils.toObj(response.getContentAsString()).get("data").get("instances");
Assert.assertEquals(instances.size(), data.size());
}
}

View File

@ -0,0 +1,80 @@
/*
* 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.api.naming.utils.NamingUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.core.HealthOperatorV2Impl;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.model.form.UpdateHealthForm;
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.util.ReflectionTestUtils;
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 static org.mockito.Mockito.doNothing;
@RunWith(MockitoJUnitRunner.class)
public class HealthControllerV2Test extends BaseTest {
@Mock
private HealthOperatorV2Impl healthOperatorV2;
@InjectMocks
private HealthControllerV2 healthControllerV2;
private MockMvc mockmvc;
private UpdateHealthForm updateHealthForm;
@Before
public void before() {
ReflectionTestUtils.setField(healthControllerV2, "healthOperatorV2", healthOperatorV2);
mockmvc = MockMvcBuilders.standaloneSetup(healthControllerV2).build();
updateHealthForm = new UpdateHealthForm();
updateHealthForm.setHealthy(true);
updateHealthForm.setNamespaceId(TEST_NAMESPACE);
updateHealthForm.setClusterName(TEST_CLUSTER_NAME);
updateHealthForm.setGroupName(TEST_GROUP_NAME);
updateHealthForm.setServiceName(TEST_SERVICE_NAME);
updateHealthForm.setIp("123.123.123.123");
updateHealthForm.setPort(8888);
}
@Test
public void testUpdate() throws Exception {
doNothing().when(healthOperatorV2).updateHealthStatusForPersistentInstance(TEST_NAMESPACE,
NamingUtils.getGroupedName(updateHealthForm.getServiceName(), updateHealthForm.getGroupName()),
TEST_CLUSTER_NAME, "123.123.123.123", 8888, true);
MockHttpServletRequestBuilder builder = convert(updateHealthForm, MockMvcRequestBuilders.put(
UtilsAndCommons.DEFAULT_NACOS_NAMING_CONTEXT_V2 + UtilsAndCommons.NACOS_NAMING_HEALTH_CONTEXT));
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("ok", JacksonUtils.toObj(response.getContentAsString()).get("data").asText());
}
}