Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5983] Fix unit tests in nacos-config #5995

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -84,6 +92,12 @@ public Class<? extends Event> subscribeType() {
Thread.sleep(2000);
Assert.assertNotNull(reference.get());
reference.set(null);

}

@After
public void tearDown() {
EnvUtil.setIsStandalone(true);
PropertyUtil.setEmbeddedStorage(true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个修改的动机是,这个单测会通过EnvUtil.setIsStandalone(false);破坏原来的EnvUtil的字段值。
这里两行代码,将EnvUtil重置为原先的值。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在Before方法和After方法中去还原。否则异常情况下仍然会导致不会重制。

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个和@lzf核对了一下,这个是为了让DumpServiceTest中的
@Autowired DumpService service;
能够扫描到所有的依赖。如果不加就会出现依赖找不到的错误。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无法mock?

@WebAppConfiguration
public class DumpServiceTest {

Expand Down
15 changes: 15 additions & 0 deletions config/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加上这个配置文件的意义在于,没有这个配置文件,DumpServiceTest的前置@SpringBootTest就启动不了。

# 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.
#