1 package org.iotivity.cloud.ciserver;
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.mock;
8 import java.util.HashMap;
9 import java.util.concurrent.CountDownLatch;
11 import org.iotivity.cloud.base.OICConstants;
12 import org.iotivity.cloud.base.device.CoapDevice;
13 import org.iotivity.cloud.base.device.Device;
14 import org.iotivity.cloud.base.device.IRequestChannel;
15 import org.iotivity.cloud.base.protocols.IRequest;
16 import org.iotivity.cloud.base.protocols.IResponse;
17 import org.iotivity.cloud.base.protocols.MessageBuilder;
18 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
19 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
20 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
21 import org.iotivity.cloud.base.server.CoapServer;
22 import org.iotivity.cloud.base.server.HttpServer;
23 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
24 import org.iotivity.cloud.ciserver.resources.proxy.account.Account;
25 import org.iotivity.cloud.ciserver.resources.proxy.mq.MessageQueue;
26 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceDirectory;
27 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceFind;
28 import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourcePresence;
29 import org.iotivity.cloud.util.Cbor;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
39 import io.netty.channel.Channel;
40 import io.netty.channel.ChannelHandlerContext;
41 import io.netty.util.Attribute;
43 public class DeviceServerSystemTest {
44 private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C";
45 String userId = "testuser";
46 String accessToken = "1689c70ffa245effc563017fee36d250";
47 private CoapDevice mockDevice = mock(
49 private Device device = mock(
53 DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
54 final CountDownLatch latch = new CountDownLatch(
58 IRequestChannel requestChannel;
61 DeviceServerSystem.CoapLifecycleHandler coapLifecycleHandler = deviceServerSystem.new CoapLifecycleHandler();
63 DeviceServerSystem.CoapAuthHandler coapAuthHandler = deviceServerSystem.new CoapAuthHandler();
66 public void setUp() throws Exception {
67 MockitoAnnotations.initMocks(this);
71 public void testAddHttpServer() throws Exception {
72 HttpServer httpServer = new HttpServer(null);
73 deviceServerSystem.addServer(httpServer);
77 public void testAddCoapServer() throws Exception {
78 CoapServer coapServer = new CoapServer(null);
79 deviceServerSystem.addServer(coapServer);
83 public void testGetDevicePool() throws Exception {
84 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
85 if (devicePool != null) {
86 System.out.println("devicePool returned :" + devicePool);
91 public void testAddDevice() throws Exception {
92 CoapDevice coapDevice = new CoapDevice(null);
93 coapDevice.updateDevice(di, userId, accessToken);
94 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
95 devicePool.addDevice(coapDevice);
99 public void testRemoveNotRegisteredDevice() throws Exception {
100 CoapDevice coapDevice = new CoapDevice(null);
101 coapDevice.updateDevice(di, userId, accessToken);
102 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
103 devicePool.removeDevice(coapDevice);
107 public void testRemoveDevice() throws Exception {
108 CoapDevice coapDevice = new CoapDevice(null);
109 coapDevice.updateDevice(di, userId, accessToken);
110 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
111 devicePool.addDevice(coapDevice);
112 devicePool.removeDevice(coapDevice);
116 public void testQueryDevice() throws Exception {
117 CoapDevice coapDevice = new CoapDevice(null);
118 coapDevice.updateDevice(di, userId, accessToken);
119 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
120 devicePool.addDevice(coapDevice);
121 devicePool.queryDevice(di);
125 public void testStopSystem() throws Exception {
126 deviceServerSystem.stopSystem();
130 public void testAddAccountResource() {
131 Account acHandler = new Account();
132 ResourceDirectory rdHandler = new ResourceDirectory();
133 ResourceFind resHandler = new ResourceFind();
134 ResourcePresence adHandler = new ResourcePresence();
135 MessageQueue mqHandler = new MessageQueue();
136 deviceServerSystem.addResource(acHandler);
137 deviceServerSystem.addResource(rdHandler);
138 deviceServerSystem.addResource(resHandler);
139 deviceServerSystem.addResource(adHandler);
140 deviceServerSystem.addResource(mqHandler);
144 public void testChannelRead() throws InterruptedException {
145 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
147 // inject mocked coapDevice into the api
148 Channel channel = mock(Channel.class);
149 Attribute<Device> attribute = mock(Attribute.class);
150 Mockito.doReturn(channel).when(ctx).channel();
151 Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
152 Mockito.doReturn(mockDevice).when(attribute).get();
154 IRequest request = MessageBuilder.createRequest(RequestMethod.GET, null,
156 Mockito.doAnswer(new Answer<Object>() {
158 public Object answer(InvocationOnMock invocation) throws Throwable {
160 Object[] args = invocation.getArguments();
161 IRequest req = (IRequest) args[0];
163 assertEquals(req, request);
169 }).when(ctx).fireChannelRead(Mockito.anyObject());
170 coapLifecycleHandler.channelRead(ctx, request);
171 assertTrue(latch.await(1L, SECONDS));
176 public void coapAuthHandlerAccountChannelReadRequest()
177 throws InterruptedException {
179 "\t--------------coapAuthHandler Account ChannelReadRequest Test------------");
180 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
181 Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
182 Channel channel = mock(Channel.class);
183 Attribute<Device> attribute = mock(Attribute.class);
184 Mockito.doReturn(channel).when(ctx).channel();
185 Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
187 Mockito.doAnswer(new Answer<Object>() {
189 public CoapRequest answer(InvocationOnMock invocation)
191 Object[] args = invocation.getArguments();
192 CoapRequest req = (CoapRequest) args[0];
193 assertEquals(req.getUriPath(), Constants.ACCOUNT_FULL_URI);
195 .parsePayloadFromCbor(req.getPayload(), HashMap.class)
200 }).when(ctx).fireChannelRead(Mockito.any());
202 HashMap<String, Object> payloadData = new HashMap<>();
203 payloadData.put("di", "sampleDevice");
205 IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
206 Constants.ACCOUNT_FULL_URI, null,
207 ContentFormat.APPLICATION_CBOR,
208 cbor.encodingPayloadToCbor(payloadData));
209 coapAuthHandler.channelRead(ctx, request);
213 public void coapAuthHandlerPingChannelReadRequest()
214 throws InterruptedException {
216 "\t--------------coapAuthHandler Ping ChannelReadRequest Test------------");
217 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
218 Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
219 Channel channel = mock(Channel.class);
220 Attribute<Device> attribute = mock(Attribute.class);
221 Mockito.doReturn(channel).when(ctx).channel();
222 Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
224 Mockito.doAnswer(new Answer<Object>() {
226 public CoapRequest answer(InvocationOnMock invocation)
228 Object[] args = invocation.getArguments();
229 CoapRequest req = (CoapRequest) args[0];
230 assertEquals(req.getUriPath(), "/" + OICConstants.PREFIX_OIC
231 + "/" + OICConstants.KEEP_ALIVE_URI);
236 }).when(ctx).fireChannelRead(Mockito.any());
238 IRequest request = MessageBuilder
239 .createRequest(RequestMethod.POST,
240 "/" + OICConstants.PREFIX_OIC + "/"
241 + OICConstants.KEEP_ALIVE_URI,
243 coapAuthHandler.channelRead(ctx, request);