21cf5ea33f64469390324bcc1bc2eb889ee5170b
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / DeviceServerSystemTest.java
1 package org.iotivity.cloud.ciserver;
2
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;
7
8 import java.util.HashMap;
9 import java.util.concurrent.CountDownLatch;
10
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;
38
39 import io.netty.channel.Channel;
40 import io.netty.channel.ChannelHandlerContext;
41 import io.netty.util.Attribute;
42
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(
48             CoapDevice.class);
49     private Device                          device               = mock(
50             Device.class);
51     IResponse                               res                  = null;
52     IRequest                                req                  = null;
53     DeviceServerSystem                      deviceServerSystem   = new DeviceServerSystem();
54     final CountDownLatch                    latch                = new CountDownLatch(
55             1);
56
57     @Mock
58     IRequestChannel                         requestChannel;
59
60     @InjectMocks
61     DeviceServerSystem.CoapLifecycleHandler coapLifecycleHandler = deviceServerSystem.new CoapLifecycleHandler();
62     @InjectMocks
63     DeviceServerSystem.CoapAuthHandler      coapAuthHandler      = deviceServerSystem.new CoapAuthHandler();
64
65     @Before
66     public void setUp() throws Exception {
67         MockitoAnnotations.initMocks(this);
68     }
69
70     @Before
71     public void testAddHttpServer() throws Exception {
72         HttpServer httpServer = new HttpServer(null);
73         deviceServerSystem.addServer(httpServer);
74     }
75
76     @Before
77     public void testAddCoapServer() throws Exception {
78         CoapServer coapServer = new CoapServer(null);
79         deviceServerSystem.addServer(coapServer);
80     }
81
82     @Test
83     public void testGetDevicePool() throws Exception {
84         CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
85         if (devicePool != null) {
86             System.out.println("devicePool returned :" + devicePool);
87         }
88     }
89
90     @Test
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);
96     }
97
98     @Test
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);
104     }
105
106     @Test
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);
113     }
114
115     @Test
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);
122     }
123
124     @Test
125     public void testStopSystem() throws Exception {
126         deviceServerSystem.stopSystem();
127     }
128
129     @Test
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);
141     }
142
143     @Test
144     public void testChannelRead() throws InterruptedException {
145         ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
146
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();
153
154         IRequest request = MessageBuilder.createRequest(RequestMethod.GET, null,
155                 null);
156         Mockito.doAnswer(new Answer<Object>() {
157             @Override
158             public Object answer(InvocationOnMock invocation) throws Throwable {
159
160                 Object[] args = invocation.getArguments();
161                 IRequest req = (IRequest) args[0];
162
163                 assertEquals(req, request);
164
165                 latch.countDown();
166                 return null;
167             }
168
169         }).when(ctx).fireChannelRead(Mockito.anyObject());
170         coapLifecycleHandler.channelRead(ctx, request);
171         assertTrue(latch.await(1L, SECONDS));
172
173     }
174
175     @Test
176     public void coapAuthHandlerAccountChannelReadRequest()
177             throws InterruptedException {
178         System.out.println(
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());
186
187         Mockito.doAnswer(new Answer<Object>() {
188             @Override
189             public CoapRequest answer(InvocationOnMock invocation)
190                     throws Throwable {
191                 Object[] args = invocation.getArguments();
192                 CoapRequest req = (CoapRequest) args[0];
193                 assertEquals(req.getUriPath(),
194                         "/" + OCFConstants.PREFIX_WELL_KNOWN + "/"
195                                 + OCFConstants.PREFIX_OCF + "/"
196                                 + OCFConstants.ACCOUNT_URI);
197                 assertTrue(cbor
198                         .parsePayloadFromCbor(req.getPayload(), HashMap.class)
199                         .containsKey("di"));
200                 latch.countDown();
201                 return null;
202             }
203         }).when(ctx).fireChannelRead(Mockito.any());
204
205         HashMap<String, Object> payloadData = new HashMap<>();
206         payloadData.put("di", "sampleDevice");
207
208         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
209                 "/" + OCFConstants.PREFIX_WELL_KNOWN + "/"
210                         + OCFConstants.PREFIX_OCF + "/"
211                         + OCFConstants.ACCOUNT_URI,
212                 null, ContentFormat.APPLICATION_CBOR,
213                 cbor.encodingPayloadToCbor(payloadData));
214         coapAuthHandler.channelRead(ctx, request);
215     }
216
217     @Test
218     public void coapAuthHandlerPingChannelReadRequest()
219             throws InterruptedException {
220         System.out.println(
221                 "\t--------------coapAuthHandler Ping ChannelReadRequest Test------------");
222         ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
223         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
224         Channel channel = mock(Channel.class);
225         Attribute<Device> attribute = mock(Attribute.class);
226         Mockito.doReturn(channel).when(ctx).channel();
227         Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
228
229         Mockito.doAnswer(new Answer<Object>() {
230             @Override
231             public CoapRequest answer(InvocationOnMock invocation)
232                     throws Throwable {
233                 Object[] args = invocation.getArguments();
234                 CoapRequest req = (CoapRequest) args[0];
235                 assertEquals(req.getUriPath(), "/" + OCFConstants.PREFIX_OIC
236                         + "/" + OCFConstants.KEEP_ALIVE_URI);
237
238                 latch.countDown();
239                 return null;
240             }
241         }).when(ctx).fireChannelRead(Mockito.any());
242
243         IRequest request = MessageBuilder
244                 .createRequest(RequestMethod.POST,
245                         "/" + OCFConstants.PREFIX_OIC + "/"
246                                 + OCFConstants.KEEP_ALIVE_URI,
247                         null, null, null);
248         coapAuthHandler.channelRead(ctx, request);
249     }
250 }