Replace fastjson with jackson for Service
This commit is contained in:
parent
4607bb7c00
commit
d34bef36a9
@ -15,8 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||
import com.alibaba.nacos.common.utils.MD5Utils;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
@ -32,6 +31,10 @@ import com.alibaba.nacos.naming.pojo.Record;
|
||||
import com.alibaba.nacos.naming.push.PushService;
|
||||
import com.alibaba.nacos.naming.selector.NoneSelector;
|
||||
import com.alibaba.nacos.naming.selector.Selector;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.collections.ListUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -48,11 +51,12 @@ import java.util.*;
|
||||
*
|
||||
* @author nkorange
|
||||
*/
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
public class Service extends com.alibaba.nacos.api.naming.pojo.Service implements Record, RecordListener<Instances> {
|
||||
|
||||
private static final String SERVICE_NAME_SYNTAX = "[0-9a-zA-Z@\\.:_-]+";
|
||||
|
||||
@JSONField(serialize = false)
|
||||
@JsonIgnore
|
||||
private ClientBeatCheckTask clientBeatCheckTask = new ClientBeatCheckTask(this);
|
||||
|
||||
/**
|
||||
@ -90,7 +94,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
|
||||
super(name);
|
||||
}
|
||||
|
||||
@JSONField(serialize = false)
|
||||
@JsonIgnore
|
||||
public PushService getPushService() {
|
||||
return ApplicationUtils.getBean(PushService.class);
|
||||
}
|
||||
@ -322,10 +326,14 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
|
||||
}
|
||||
|
||||
public String toJSON() {
|
||||
return JSON.toJSONString(this);
|
||||
try {
|
||||
return JacksonUtils.toJson(this);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Service toJson failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@JSONField(serialize = false)
|
||||
@JsonIgnore
|
||||
public String getServiceString() {
|
||||
Map<Object, Object> serviceObject = new HashMap<Object, Object>(10);
|
||||
Service service = this;
|
||||
@ -369,7 +377,11 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
|
||||
|
||||
serviceObject.put("clusters", clustersList);
|
||||
|
||||
return JSON.toJSONString(serviceObject);
|
||||
try {
|
||||
return JacksonUtils.toJson(serviceObject);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Service toJson failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming;
|
||||
|
||||
import com.alibaba.nacos.core.utils.ApplicationUtils;
|
||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
|
||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
|
||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet;
|
||||
@ -27,19 +28,30 @@ import com.alibaba.nacos.naming.push.PushService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* @author nkorange
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BaseTest {
|
||||
|
||||
protected static final String TEST_CLUSTER_NAME = "test-cluster";
|
||||
|
||||
protected static final String TEST_SERVICE_NAME = "test-service";
|
||||
|
||||
protected static final String TEST_GROUP_NAME = "test-group-name";
|
||||
|
||||
protected static final String TEST_NAMESPACE = "test-namespace";
|
||||
|
||||
@Mock
|
||||
public ServiceManager serviceManager;
|
||||
|
||||
@ -52,36 +64,52 @@ public class BaseTest {
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Spy
|
||||
protected ConfigurableApplicationContext context;
|
||||
|
||||
@Mock
|
||||
protected DistroMapper distroMapper;
|
||||
|
||||
@Spy
|
||||
protected SwitchDomain switchDomain;
|
||||
|
||||
@Mock
|
||||
protected HealthCheckProcessorDelegate delegate;
|
||||
|
||||
@Mock
|
||||
protected PushService pushService;
|
||||
|
||||
@Spy
|
||||
private MockEnvironment environment;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ApplicationUtils.injectEnvironment(environment);
|
||||
ApplicationUtils.injectContext(context);
|
||||
}
|
||||
|
||||
protected void mockRaft() {
|
||||
RaftPeer peer = new RaftPeer();
|
||||
peer.ip = NetUtils.localServer();
|
||||
raftCore.setPeerSet(peerSet);
|
||||
Mockito.when(peerSet.local()).thenReturn(peer);
|
||||
Mockito.when(peerSet.getLeader()).thenReturn(peer);
|
||||
Mockito.when(peerSet.isLeader(NetUtils.localServer())).thenReturn(true);
|
||||
}
|
||||
|
||||
doReturn(distroMapper).when(context).getBean(DistroMapper.class);
|
||||
doReturn(switchDomain).when(context).getBean(SwitchDomain.class);
|
||||
doReturn(delegate).when(context).getBean(HealthCheckProcessorDelegate.class);
|
||||
protected void mockInjectPushServer() {
|
||||
doReturn(pushService).when(context).getBean(PushService.class);
|
||||
}
|
||||
|
||||
protected static final String TEST_CLUSTER_NAME = "test-cluster";
|
||||
protected static final String TEST_SERVICE_NAME = "test-service";
|
||||
protected static final String TEST_GROUP_NAME = "test-group-name";
|
||||
protected static final String TEST_NAMESPACE = "test-namespace";
|
||||
protected void mockInjectHealthCheckProcessor() {
|
||||
doReturn(delegate).when(context).getBean(HealthCheckProcessorDelegate.class);
|
||||
}
|
||||
|
||||
@Spy
|
||||
protected ApplicationContext context;
|
||||
@Mock
|
||||
protected DistroMapper distroMapper;
|
||||
@Spy
|
||||
protected SwitchDomain switchDomain;
|
||||
@Mock
|
||||
protected HealthCheckProcessorDelegate delegate;
|
||||
@Mock
|
||||
protected PushService pushService;
|
||||
protected void mockInjectSwitchDomain() {
|
||||
doReturn(switchDomain).when(context).getBean(SwitchDomain.class);
|
||||
}
|
||||
|
||||
protected void mockInjectDistroMapper() {
|
||||
doReturn(distroMapper).when(context).getBean(DistroMapper.class);
|
||||
}
|
||||
}
|
||||
|
@ -15,41 +15,31 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.nacos.naming.BaseTest;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import com.alibaba.nacos.naming.push.PushService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* @author nkorange
|
||||
*/
|
||||
public class DomainTest {
|
||||
public class DomainTest extends BaseTest {
|
||||
|
||||
private Service service;
|
||||
@Spy
|
||||
protected ApplicationContext context;
|
||||
@Mock
|
||||
protected PushService pushService;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
super.before();
|
||||
service = new Service();
|
||||
service.setName("nacos.service.1");
|
||||
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, service);
|
||||
service.addCluster(cluster);
|
||||
doReturn(pushService).when(context).getBean(PushService.class);
|
||||
mockInjectPushServer();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -49,6 +49,9 @@ public class ServiceManagerTest extends BaseTest {
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
mockInjectHealthCheckProcessor();
|
||||
mockInjectDistroMapper();
|
||||
mockInjectSwitchDomain();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -15,30 +15,44 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.nacos.api.selector.SelectorType;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.alibaba.nacos.naming.BaseTest;
|
||||
import com.alibaba.nacos.naming.selector.NoneSelector;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Spy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author jifengnan 2019-04-28
|
||||
*/
|
||||
public class ServiceTest extends BaseTest {
|
||||
@Spy
|
||||
|
||||
private Service service;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
service = new Service("test-service");
|
||||
mockInjectPushServer();
|
||||
mockInjectHealthCheckProcessor();
|
||||
mockInjectDistroMapper();
|
||||
mockInjectSwitchDomain();
|
||||
JacksonUtils.registerSubtype(NoneSelector.class, SelectorType.none.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateIPs() {
|
||||
service.setName("test-service");
|
||||
List<Instance> instances = new ArrayList<>();
|
||||
Instance instance = new Instance("1.1.1.1", 1, "test-instance1");
|
||||
instances.add(instance);
|
||||
@ -55,4 +69,56 @@ public class ServiceTest extends BaseTest {
|
||||
instances.remove(null);
|
||||
Assert.assertEquals(instances, service.allIPs(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize() throws Exception {
|
||||
String actual = new Service("test-service").toJSON();
|
||||
assertTrue(actual.contains("\"checksum\":\"cb129dc536a387e791ee6f63251c816b\""));
|
||||
assertTrue(actual.contains("\"clusterMap\":{}"));
|
||||
assertTrue(actual.contains("\"empty\":true"));
|
||||
assertTrue(actual.contains("\"enabled\":true"));
|
||||
assertTrue(actual.contains("\"finalizeCount\":0"));
|
||||
assertTrue(actual.contains("\"ipDeleteTimeout\":30000"));
|
||||
assertTrue(actual.contains("\"lastModifiedMillis\":0"));
|
||||
assertTrue(actual.contains("\"metadata\":{}"));
|
||||
assertTrue(actual.contains("\"name\":\"test-service\""));
|
||||
assertTrue(actual.contains("\"owners\":[]"));
|
||||
assertTrue(actual.contains("\"protectThreshold\":0.0"));
|
||||
assertTrue(actual.contains("\"resetWeight\":false"));
|
||||
assertTrue(actual.contains("\"selector\":{\"type\":\"none\"}"));
|
||||
assertFalse(actual.contains("clientBeatCheckTask"));
|
||||
assertFalse(actual.contains("serviceString"));
|
||||
assertFalse(actual.contains("pushService"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize() throws Exception {
|
||||
String example = "{\"checksum\":\"cb129dc536a387e791ee6f63251c816b\",\"clusterMap\":{},\"empty\":true,\"enabled\":true,\"finalizeCount\":0,\"ipDeleteTimeout\":30000,\"lastModifiedMillis\":0,\"metadata\":{},\"name\":\"test-service\",\"owners\":[],\"protectThreshold\":0.0,\"resetWeight\":false,\"selector\":{\"type\":\"none\"}}";
|
||||
Service actual = JacksonUtils.toObj(example, Service.class);
|
||||
assertEquals("cb129dc536a387e791ee6f63251c816b", actual.getChecksum());
|
||||
assertEquals("test-service", actual.getName());
|
||||
assertTrue(actual.getClusterMap().isEmpty());
|
||||
assertTrue(actual.isEmpty());
|
||||
assertTrue(actual.getEnabled());
|
||||
assertTrue(actual.getMetadata().isEmpty());
|
||||
assertTrue(actual.getOwners().isEmpty());
|
||||
assertEquals(0, actual.getFinalizeCount());
|
||||
assertEquals(30000, actual.getIpDeleteTimeout());
|
||||
assertEquals(0, actual.getLastModifiedMillis());
|
||||
assertEquals(0, actual.getLastModifiedMillis());
|
||||
assertEquals(0.0, actual.getProtectThreshold(), 0);
|
||||
assertFalse(actual.getResetWeight());
|
||||
assertThat(actual.getSelector(), instanceOf(NoneSelector.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServiceString() {
|
||||
String actual = service.getServiceString();
|
||||
assertTrue(actual.contains("\"invalidIPCount\":0"));
|
||||
assertTrue(actual.contains("\"name\":\"test-service\""));
|
||||
assertTrue(actual.contains("\"ipCount\":0"));
|
||||
assertTrue(actual.contains("\"owners\":[]"));
|
||||
assertTrue(actual.contains("\"protectThreshold\":0.0"));
|
||||
assertTrue(actual.contains("\"clusters\":[]"));
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,9 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -23,7 +25,8 @@ import java.util.List;
|
||||
* @author Nicholas
|
||||
*/
|
||||
@SpringBootTest
|
||||
@RunWith(SpringRunner.class)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MockServletContext.class)
|
||||
public class SubscribeManagerTest extends BaseTest {
|
||||
|
||||
@Mock
|
||||
|
Loading…
Reference in New Issue
Block a user