Skip to content

Commit

Permalink
[ISSUE #6188]add ut for com.alibaba.nacos.naming.cluster (#6279)
Browse files Browse the repository at this point in the history
* add ut for  com.alibaba.nacos.naming.cluster

* fix ut
  • Loading branch information
shalk authored Jul 7, 2021
1 parent 9698bbc commit c973566
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
*
* 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.naming.cluster;

import com.alibaba.nacos.naming.consistency.ConsistencyService;
import com.alibaba.nacos.naming.misc.GlobalExecutor;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.springframework.mock.env.MockEnvironment;

import java.lang.reflect.Field;
import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

public class ServerStatusManagerTest {

@Before
public void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
}

@Test
public void testInit() {
try (MockedStatic mocked = mockStatic(GlobalExecutor.class)) {
ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class));
serverStatusManager.init();
mocked.verify(() -> GlobalExecutor.registerServerStatusUpdater(any()));
}
}

@Test
public void testGetServerStatus() {
ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class));
ServerStatus serverStatus = serverStatusManager.getServerStatus();
Assert.assertEquals(ServerStatus.STARTING, serverStatus);

}

@Test
public void testGetErrorMsg() throws NoSuchFieldException, IllegalAccessException {
ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class));
Field field = ServerStatusManager.class.getDeclaredField("consistencyService");
field.setAccessible(true);
ConsistencyService consistencyService = mock(ConsistencyService.class);
when(consistencyService.getErrorMsg()).thenReturn(Optional.empty());
field.set(serverStatusManager, consistencyService);
Optional<String> errorMsg = serverStatusManager.getErrorMsg();
Assert.assertFalse(errorMsg.isPresent());
}

@Test
public void testUpdaterFromSwitch() {
SwitchDomain switchDomain = mock(SwitchDomain.class);
String expect = ServerStatus.DOWN.toString();
when(switchDomain.getOverriddenServerStatus()).thenReturn(expect);
ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain);
ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater();
//then
updater.run();
//then
ServerStatus serverStatus = serverStatusManager.getServerStatus();
Assert.assertEquals(expect, serverStatus.toString());
}

@Test
public void testUpdaterFromConsistency1() throws NoSuchFieldException, IllegalAccessException {
SwitchDomain switchDomain = mock(SwitchDomain.class);
ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain);
Field field = ServerStatusManager.class.getDeclaredField("consistencyService");
field.setAccessible(true);
ConsistencyService consistencyService = mock(ConsistencyService.class);
when(consistencyService.isAvailable()).thenReturn(true);
field.set(serverStatusManager, consistencyService);
ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater();
//then
updater.run();
//then
Assert.assertEquals(ServerStatus.UP, serverStatusManager.getServerStatus());
}

@Test
public void testUpdaterFromConsistency2() throws NoSuchFieldException, IllegalAccessException {
SwitchDomain switchDomain = mock(SwitchDomain.class);
ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain);
Field field = ServerStatusManager.class.getDeclaredField("consistencyService");
field.setAccessible(true);
ConsistencyService consistencyService = mock(ConsistencyService.class);
when(consistencyService.isAvailable()).thenReturn(false);
field.set(serverStatusManager, consistencyService);
ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater();
//then
updater.run();
//then
Assert.assertEquals(ServerStatus.DOWN, serverStatusManager.getServerStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* 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.naming.cluster.remote.request;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class AbstractClusterRequestTest {

@Test
public void getModule() {
AbstractClusterRequest request = new AbstractClusterRequest() {
};
String actual = request.getModule();
assertEquals("cluster", actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* 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.naming.cluster.remote.request;

import com.alibaba.nacos.consistency.DataOperation;
import com.alibaba.nacos.core.distributed.distro.entity.DistroData;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

public class DistroDataRequestTest {

@Test
public void testConstructor1() {
DistroDataRequest req = new DistroDataRequest();
assertNotNull(req);
}

@Test
public void testConstructor2() {
DistroDataRequest req = new DistroDataRequest(mock(DistroData.class), mock(DataOperation.class));
assertNotNull(req);
}

@Test
public void testGetterAndSetter() {
DistroData distroData = mock(DistroData.class);
DataOperation dataOperation = mock(DataOperation.class);
DistroDataRequest req = new DistroDataRequest();
req.setDistroData(distroData);
req.setDataOperation(dataOperation);
assertEquals(distroData, req.getDistroData());
assertEquals(dataOperation, req.getDataOperation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* 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.naming.cluster.remote.response;

import com.alibaba.nacos.core.distributed.distro.entity.DistroData;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class DistroDataResponseTest {

@Test
public void test() {
DistroDataResponse distroDataResponse = new DistroDataResponse();
DistroData distroData = mock(DistroData.class);
distroDataResponse.setDistroData(distroData);
assertEquals(distroData, distroDataResponse.getDistroData());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class SerializerTest {
public class JacksonSerializerTest {

private Serializer serializer;

Expand Down
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down Expand Up @@ -1026,6 +1031,12 @@
<version>${mockito-core.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-core.version}</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
Expand Down

0 comments on commit c973566

Please sign in to comment.