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.OCFConstants;
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, di, userId, accessToken);
93 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
94 devicePool.addDevice(coapDevice);
98 public void testRemoveNotRegisteredDevice() throws Exception {
99 CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken);
100 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
101 devicePool.removeDevice(coapDevice);
105 public void testRemoveDevice() throws Exception {
106 CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken);
107 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
108 devicePool.addDevice(coapDevice);
109 devicePool.removeDevice(coapDevice);
113 public void testQueryDevice() throws Exception {
114 CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken);
115 CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
116 devicePool.addDevice(coapDevice);
117 devicePool.queryDevice(di);
121 public void testStopSystem() throws Exception {
122 deviceServerSystem.stopSystem();
126 public void testAddAccountResource() {
127 Account acHandler = new Account();
128 ResourceDirectory rdHandler = new ResourceDirectory();
129 ResourceFind resHandler = new ResourceFind();
130 ResourcePresence adHandler = new ResourcePresence();
131 MessageQueue mqHandler = new MessageQueue();
132 deviceServerSystem.addResource(acHandler);
133 deviceServerSystem.addResource(rdHandler);
134 deviceServerSystem.addResource(resHandler);
135 deviceServerSystem.addResource(adHandler);
136 deviceServerSystem.addResource(mqHandler);
140 public void testChannelRead() throws InterruptedException {
141 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
143 // inject mocked coapDevice into the api
144 Channel channel = mock(Channel.class);
145 Attribute<Device> attribute = mock(Attribute.class);
146 Mockito.doReturn(channel).when(ctx).channel();
147 Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
148 Mockito.doReturn(mockDevice).when(attribute).get();
150 IRequest request = MessageBuilder.createRequest(RequestMethod.GET, null,
152 Mockito.doAnswer(new Answer<Object>() {
154 public Object answer(InvocationOnMock invocation) throws Throwable {
156 Object[] args = invocation.getArguments();
157 IRequest req = (IRequest) args[0];
159 assertEquals(req, request);
165 }).when(ctx).fireChannelRead(Mockito.anyObject());
166 coapLifecycleHandler.channelRead(ctx, request);
167 assertTrue(latch.await(1L, SECONDS));
172 public void coapAuthHandlerAccountChannelReadRequest()
173 throws InterruptedException {
175 "\t--------------coapAuthHandler Account ChannelReadRequest Test------------");
176 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
177 Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
178 Channel channel = mock(Channel.class);
179 Attribute<Device> attribute = mock(Attribute.class);
180 Mockito.doReturn(channel).when(ctx).channel();
181 Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
183 Mockito.doAnswer(new Answer<Object>() {
185 public CoapRequest answer(InvocationOnMock invocation)
187 Object[] args = invocation.getArguments();
188 CoapRequest req = (CoapRequest) args[0];
189 assertEquals(req.getUriPath(),
190 "/" + OCFConstants.PREFIX_WELL_KNOWN + "/"
191 + OCFConstants.PREFIX_OCF + "/"
192 + OCFConstants.ACCOUNT_URI);
194 .parsePayloadFromCbor(req.getPayload(), HashMap.class)
199 }).when(ctx).fireChannelRead(Mockito.any());
201 HashMap<String, Object> payloadData = new HashMap<>();
202 payloadData.put("di", "sampleDevice");
204 IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
205 "/" + OCFConstants.PREFIX_WELL_KNOWN + "/"
206 + OCFConstants.PREFIX_OCF + "/"
207 + OCFConstants.ACCOUNT_URI,
208 null, ContentFormat.APPLICATION_CBOR,
209 cbor.encodingPayloadToCbor(payloadData));
210 coapAuthHandler.channelRead(ctx, request);
214 public void coapAuthHandlerPingChannelReadRequest()
215 throws InterruptedException {
217 "\t--------------coapAuthHandler Ping ChannelReadRequest Test------------");
218 ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
219 Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
220 Channel channel = mock(Channel.class);
221 Attribute<Device> attribute = mock(Attribute.class);
222 Mockito.doReturn(channel).when(ctx).channel();
223 Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
225 Mockito.doAnswer(new Answer<Object>() {
227 public CoapRequest answer(InvocationOnMock invocation)
229 Object[] args = invocation.getArguments();
230 CoapRequest req = (CoapRequest) args[0];
231 assertEquals(req.getUriPath(), "/" + OCFConstants.PREFIX_OIC
232 + "/" + OCFConstants.KEEP_ALIVE_URI);
237 }).when(ctx).fireChannelRead(Mockito.any());
239 IRequest request = MessageBuilder
240 .createRequest(RequestMethod.POST,
241 "/" + OCFConstants.PREFIX_OIC + "/"
242 + OCFConstants.KEEP_ALIVE_URI,
244 coapAuthHandler.channelRead(ctx, request);