diff --git a/config/src/test/java/com/alibaba/nacos/config/server/auth/MockAuthManager.java b/config/src/test/java/com/alibaba/nacos/config/server/auth/MockAuthManager.java new file mode 100644 index 00000000000..b89123b7464 --- /dev/null +++ b/config/src/test/java/com/alibaba/nacos/config/server/auth/MockAuthManager.java @@ -0,0 +1,42 @@ +/* + * 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.config.server.auth; + +import com.alibaba.nacos.auth.AuthManager; +import com.alibaba.nacos.auth.exception.AccessException; +import com.alibaba.nacos.auth.model.Permission; +import com.alibaba.nacos.auth.model.User; +import org.springframework.stereotype.Component; + +@Component +public class MockAuthManager implements AuthManager { + + @Override + public User login(Object request) throws AccessException { + return null; + } + + @Override + public User loginRemote(Object request) throws AccessException { + return null; + } + + @Override + public void auth(Permission permission, User user) throws AccessException { + + } +} diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerUnitTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerUnitTest.java index b85d8162837..ccd89bb14b8 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerUnitTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerUnitTest.java @@ -18,22 +18,32 @@ import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.service.datasource.DataSourceService; +import com.alibaba.nacos.core.cluster.MemberLookup; +import com.alibaba.nacos.core.cluster.ServerMemberManager; +import com.alibaba.nacos.sys.env.EnvUtil; 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.Mockito; +import org.springframework.core.env.StandardEnvironment; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; +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 javax.servlet.ServletContext; +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.Mockito.when; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MockServletContext.class) @WebAppConfiguration @@ -47,15 +57,31 @@ public class HealthControllerUnitTest { private MockMvc mockmvc; + @Mock + private ServerMemberManager memberManager; + + @Mock + private ServletContext servletContext; + + @Mock + private MemberLookup memberLookup; + @Before - public void setUp() throws Exception { + public void setUp() { + EnvUtil.setEnvironment(new StandardEnvironment()); + Map infos = new HashMap<>(); + infos.put("addressServerHealth", true); + when(memberLookup.info()).thenReturn(infos); + when(memberManager.getLookup()).thenReturn(memberLookup); + when(servletContext.getContextPath()).thenReturn("/nacos"); + ReflectionTestUtils.setField(healthController, "memberManager", memberManager); mockmvc = MockMvcBuilders.standaloneSetup(healthController).build(); } @Test public void testGetHealth() throws Exception { - Mockito.when(dataSourceService.getHealth()).thenReturn("UP"); + when(dataSourceService.getHealth()).thenReturn("UP"); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH); String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString(); Assert.assertEquals("UP", actualValue); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java index af76c8676f7..8a5a76a328d 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java @@ -22,13 +22,21 @@ import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; import com.alibaba.nacos.config.server.utils.PropertyUtil; import com.alibaba.nacos.sys.env.EnvUtil; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.util.concurrent.atomic.AtomicReference; public class ConfigChangePublisherTest { + @Before + public void startUP() { + EnvUtil.setIsStandalone(true); + PropertyUtil.setEmbeddedStorage(true); + } + @Test public void testConfigChangeNotify() throws InterruptedException { @@ -84,6 +92,12 @@ public Class subscribeType() { Thread.sleep(2000); Assert.assertNotNull(reference.get()); reference.set(null); + } + @After + public void tearDown() { + EnvUtil.setIsStandalone(true); + PropertyUtil.setEmbeddedStorage(true); + } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java index 1ca0d209667..fb4756b579a 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java @@ -19,12 +19,14 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest +@SpringBootApplication(scanBasePackages = "com.alibaba.nacos") @WebAppConfiguration public class DumpServiceTest { diff --git a/config/src/test/resources/application.properties b/config/src/test/resources/application.properties new file mode 100644 index 00000000000..d65a9b08308 --- /dev/null +++ b/config/src/test/resources/application.properties @@ -0,0 +1,15 @@ +# +# 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. +#