CI unit test included
[platform/upstream/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, di, userId, accessToken);
93         CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
94         devicePool.addDevice(coapDevice);
95     }
96
97     @Test
98     public void testRemoveNotRegisteredDevice() throws Exception {
99         CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken);
100         CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
101         devicePool.removeDevice(coapDevice);
102     }
103
104     @Test
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);
110     }
111
112     @Test
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);
118     }
119
120     @Test
121     public void testStopSystem() throws Exception {
122         deviceServerSystem.stopSystem();
123     }
124
125     @Test
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);
137     }
138
139     @Test
140     public void testChannelRead() throws InterruptedException {
141         ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
142
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();
149
150         IRequest request = MessageBuilder.createRequest(RequestMethod.GET, null,
151                 null);
152         Mockito.doAnswer(new Answer<Object>() {
153             @Override
154             public Object answer(InvocationOnMock invocation) throws Throwable {
155
156                 Object[] args = invocation.getArguments();
157                 IRequest req = (IRequest) args[0];
158
159                 assertEquals(req, request);
160
161                 latch.countDown();
162                 return null;
163             }
164
165         }).when(ctx).fireChannelRead(Mockito.anyObject());
166         coapLifecycleHandler.channelRead(ctx, request);
167         assertTrue(latch.await(1L, SECONDS));
168
169     }
170
171     @Test
172     public void coapAuthHandlerAccountChannelReadRequest()
173             throws InterruptedException {
174         System.out.println(
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());
182
183         Mockito.doAnswer(new Answer<Object>() {
184             @Override
185             public CoapRequest answer(InvocationOnMock invocation)
186                     throws Throwable {
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);
193                 assertTrue(cbor
194                         .parsePayloadFromCbor(req.getPayload(), HashMap.class)
195                         .containsKey("di"));
196                 latch.countDown();
197                 return null;
198             }
199         }).when(ctx).fireChannelRead(Mockito.any());
200
201         HashMap<String, Object> payloadData = new HashMap<>();
202         payloadData.put("di", "sampleDevice");
203
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);
211     }
212
213     @Test
214     public void coapAuthHandlerPingChannelReadRequest()
215             throws InterruptedException {
216         System.out.println(
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());
224
225         Mockito.doAnswer(new Answer<Object>() {
226             @Override
227             public CoapRequest answer(InvocationOnMock invocation)
228                     throws Throwable {
229                 Object[] args = invocation.getArguments();
230                 CoapRequest req = (CoapRequest) args[0];
231                 assertEquals(req.getUriPath(), "/" + OCFConstants.PREFIX_OIC
232                         + "/" + OCFConstants.KEEP_ALIVE_URI);
233
234                 latch.countDown();
235                 return null;
236             }
237         }).when(ctx).fireChannelRead(Mockito.any());
238
239         IRequest request = MessageBuilder
240                 .createRequest(RequestMethod.POST,
241                         "/" + OCFConstants.PREFIX_OIC + "/"
242                                 + OCFConstants.KEEP_ALIVE_URI,
243                         null, null, null);
244         coapAuthHandler.channelRead(ctx, request);
245     }
246 }