[ISSUE #6643] Add unit tests for package com.alibaba.nacos.naming.core.v2.service. (#6644)

This commit is contained in:
brotherlu-xcq 2021-08-13 09:47:00 +08:00 committed by GitHub
parent 5388290e3f
commit 27b0a0cd8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,22 +17,30 @@
package com.alibaba.nacos.naming.core.v2.service.impl;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.consistency.DataOperation;
import com.alibaba.nacos.consistency.Serializer;
import com.alibaba.nacos.consistency.cp.CPProtocol;
import com.alibaba.nacos.consistency.entity.ReadRequest;
import com.alibaba.nacos.consistency.entity.Response;
import com.alibaba.nacos.consistency.entity.WriteRequest;
import com.alibaba.nacos.core.distributed.ProtocolManager;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.PersistentIpPortClientManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ConfigurableApplicationContext;
import java.lang.reflect.Field;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ -78,7 +86,6 @@ public class PersistentClientOperationServiceImplTest {
ApplicationUtils.injectContext(applicationContext);
Field serializerField = PersistentClientOperationServiceImpl.class.getDeclaredField("serializer");
serializerField.setAccessible(true);
clientManager = new PersistentIpPortClientManager();
persistentClientOperationServiceImpl = new PersistentClientOperationServiceImpl(clientManager);
serializerField.set(persistentClientOperationServiceImpl, serializer);
}
@ -104,4 +111,44 @@ public class PersistentClientOperationServiceImplTest {
public void testUnsubscribeService() {
persistentClientOperationServiceImpl.unsubscribeService(service, subscriber, clientId);
}
@Test(expected = UnsupportedOperationException.class)
public void testOnRequest() {
persistentClientOperationServiceImpl.onRequest(ReadRequest.newBuilder().build());
}
@Test
public void testOnApply() {
PersistentClientOperationServiceImpl.InstanceStoreRequest request = new PersistentClientOperationServiceImpl.InstanceStoreRequest();
Service service1 = Service.newService("A", "B", "C");
request.setService(service1);
request.setClientId("xxxx");
request.setInstance(new Instance());
Mockito.when(serializer.deserialize(Mockito.any())).thenReturn(request);
Mockito.when(clientManager.contains(Mockito.anyString())).thenReturn(true);
IpPortBasedClient ipPortBasedClient = Mockito.mock(IpPortBasedClient.class);
Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient);
WriteRequest writeRequest = WriteRequest.newBuilder()
.setOperation(DataOperation.ADD.name())
.build();
Response response = persistentClientOperationServiceImpl.onApply(writeRequest);
Assert.assertTrue(response.getSuccess());
writeRequest = WriteRequest.newBuilder()
.setOperation(DataOperation.DELETE.name())
.build();
response = persistentClientOperationServiceImpl.onApply(writeRequest);
Assert.assertTrue(response.getSuccess());
ServiceManager.getInstance().removeSingleton(service1);
writeRequest = WriteRequest.newBuilder()
.setOperation(DataOperation.VERIFY.name())
.build();
response = persistentClientOperationServiceImpl.onApply(writeRequest);
Assert.assertFalse(response.getSuccess());
}
}