Skip to content

Commit c23cf3c

Browse files
committed
Added unit test for NSServerStub to check if it properly registers and
unregisters itself upon start and stop
1 parent cf38bef commit c23cf3c

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package org.mouji.ns.test;
2+
3+
import java.io.FileNotFoundException;
4+
import java.net.Inet4Address;
5+
import java.sql.SQLException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.mouji.common.errors.ApplicationSpecificErrorException;
13+
import org.mouji.common.errors.DatabaseNotSupported;
14+
import org.mouji.common.errors.ExecuteInternalError;
15+
import org.mouji.common.errors.InvalidArgsException;
16+
import org.mouji.common.errors.ServiceNotSupportedException;
17+
import org.mouji.common.info.SerializationFormat;
18+
import org.mouji.common.info.SerializedObject;
19+
import org.mouji.common.info.ServiceInfo;
20+
import org.mouji.common.info.ServiceProviderInfo;
21+
import org.mouji.common.info.ServiceRequest;
22+
import org.mouji.common.info.ServiceSupportInfo;
23+
import org.mouji.common.info.StubEnvInfo;
24+
import org.mouji.common.info.responses.ServiceResponse;
25+
import org.mouji.common.serializer.Serializer;
26+
import org.mouji.common.services.ServiceProvider;
27+
import org.mouji.ns.core.server.NameServer;
28+
import org.mouji.stub.java.JsonSerializer;
29+
import org.mouji.stub.java.stubs.NSClient;
30+
import org.mouji.stub.java.stubs.NSServerStub;
31+
import org.mouji.stub.java.stubs.ServerStub;
32+
33+
import me.salimm.allconfig.core.errors.PrefixNotANestedConfigException;
34+
import me.salimm.allconfig.core.types.XMLConfig;
35+
36+
public class NSServerStubTest {
37+
private NameServer ns;
38+
39+
private ServiceProviderInfo nsSPInfo;
40+
private ServiceProviderInfo spInfo;
41+
42+
43+
44+
45+
@Before
46+
public void runNS() throws ClassNotFoundException, FileNotFoundException, SQLException, DatabaseNotSupported,
47+
PrefixNotANestedConfigException, Exception {
48+
ns = new NameServer(new XMLConfig("conf.xml"));
49+
ns.start();
50+
ns.reset();
51+
52+
nsSPInfo = new ServiceProviderInfo(Inet4Address.getLocalHost().getHostAddress(), 6161,
53+
StubEnvInfo.currentEnvInfo());
54+
55+
spInfo = new ServiceProviderInfo(Inet4Address.getLocalHost().getHostAddress(), 4343,
56+
StubEnvInfo.currentEnvInfo());
57+
58+
59+
}
60+
61+
@Test
62+
public void testNSServerStubStop() throws Exception {
63+
64+
ServiceInfo<Integer> service = new ServiceInfo<Integer>("add", 1);
65+
66+
List<Serializer> list = new ArrayList<Serializer>();
67+
list.add(new JsonSerializer());
68+
69+
70+
NSServerStub serverStub = new NSServerStub(spInfo.getPort(),nsSPInfo, list);
71+
72+
ServiceProvider serviceProvider = new TestServiceProvider(service,spInfo);
73+
74+
ServiceProvider provider = serviceProvider;
75+
serverStub.init(provider);
76+
77+
serverStub.start();
78+
79+
80+
81+
NSClient client = new NSClient(nsSPInfo, new ArrayList<>());
82+
83+
ServiceSupportInfo[] providers = client.getAllProviders();
84+
85+
Assert.assertEquals(1, providers.length);
86+
87+
serverStub.stop();
88+
89+
90+
providers = client.getAllProviders();
91+
92+
Assert.assertEquals(0, providers.length);
93+
94+
}
95+
}
96+
97+
class TestServiceProvider implements ServiceProvider{
98+
99+
private ServiceProviderInfo spInfo;
100+
private ServiceInfo<?> service;
101+
102+
public TestServiceProvider(ServiceInfo<?> service,ServiceProviderInfo spInfo) {
103+
this.service = service;
104+
this.spInfo = spInfo;
105+
}
106+
107+
@Override
108+
public ServiceSupportInfo service(ServiceInfo<?> info) throws ServiceNotSupportedException {
109+
return new ServiceSupportInfo(info, spInfo,
110+
new SerializationFormat[] { SerializationFormat.defaultFotmat() });
111+
}
112+
113+
@Override
114+
public boolean ping() {
115+
return true;
116+
}
117+
118+
@Override
119+
public ServiceProviderInfo info() {
120+
return spInfo;
121+
}
122+
123+
@Override
124+
public ServiceResponse<?> execute(ServiceRequest request) throws ApplicationSpecificErrorException,
125+
ExecuteInternalError, InvalidArgsException, ServiceNotSupportedException {
126+
if (request.getService().getId() == 1) {
127+
if (!(request.getArgs()[0].getContent() instanceof Integer)
128+
|| !(request.getArgs()[1].getContent() instanceof Integer)) {
129+
throw new InvalidArgsException("Both must be integers!!");
130+
}
131+
Integer num1 = (Integer) request.getArgs()[0].getContent();
132+
Integer num2 = (Integer) request.getArgs()[1].getContent();
133+
return new ServiceResponse<>(request.getService(), new SerializedObject<>(num1 + num2),
134+
request.getRequestId());
135+
} else {
136+
throw new ServiceNotSupportedException(request.getService());
137+
}
138+
}
139+
140+
@Override
141+
public List<ServiceSupportInfo> listSupport() {
142+
List<ServiceSupportInfo> list = new ArrayList<ServiceSupportInfo>();
143+
list.add(new ServiceSupportInfo(service, spInfo,
144+
new SerializationFormat[] { SerializationFormat.defaultFotmat() }));
145+
return list;
146+
}
147+
148+
}

0 commit comments

Comments
 (0)