fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourceFindTest.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.cloud.ciserver.resources.proxy.rd;
24
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.concurrent.CountDownLatch;
35
36 import org.iotivity.cloud.base.device.CoapDevice;
37 import org.iotivity.cloud.base.device.IRequestChannel;
38 import org.iotivity.cloud.base.exception.ClientException;
39 import org.iotivity.cloud.base.protocols.IRequest;
40 import org.iotivity.cloud.base.protocols.IResponse;
41 import org.iotivity.cloud.base.protocols.MessageBuilder;
42 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
43 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
44 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
45 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
46 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
47 import org.iotivity.cloud.ciserver.Constants;
48 import org.iotivity.cloud.ciserver.DeviceServerSystem;
49 import org.iotivity.cloud.util.Cbor;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.mockito.invocation.InvocationOnMock;
57 import org.mockito.stubbing.Answer;
58
59 public class ResourceFindTest {
60     private static final String TEST_RESOURCE_FIND_URI = Constants.WELL_KNOWN_FULL_URI;
61     private String              di                     = "B371C481-38E6-4D47-8320-7688D8A5B58C";
62     private CoapDevice          mockDevice             = mock(CoapDevice.class);
63     private IResponse           mRes                   = null;
64     private IRequest            mReq                   = null;
65     private DeviceServerSystem  mDeviceServerSystem    = new DeviceServerSystem();
66     final CountDownLatch        mLatch                 = new CountDownLatch(1);
67
68     @Mock
69     private IRequestChannel     mRequestChannel;
70
71     @InjectMocks
72     private ResourceFind        mResHandler            = new ResourceFind();
73
74     @Before
75     public void setUp() throws Exception {
76         mRes = null;
77         mReq = null;
78         Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
79         MockitoAnnotations.initMocks(this);
80         mDeviceServerSystem.addResource(mResHandler);
81         // callback mock
82         Mockito.doAnswer(new Answer<Object>() {
83             @Override
84             public CoapResponse answer(InvocationOnMock invocation)
85                     throws Throwable {
86                 Object[] args = invocation.getArguments();
87                 CoapResponse resp = (CoapResponse) args[0];
88                 mRes = resp;
89                 return resp;
90             }
91         }).when(mockDevice).sendResponse(Mockito.anyObject());
92         Mockito.doAnswer(new Answer<Object>() {
93             @Override
94             public CoapRequest answer(InvocationOnMock invocation)
95                     throws Throwable {
96                 Object[] args = invocation.getArguments();
97                 CoapRequest request = (CoapRequest) args[0];
98                 System.out.println(
99                         "\t----------payload : " + request.getPayloadString());
100                 System.out.println(
101                         "\t----------uripath : " + request.getUriPath());
102                 System.out.println(
103                         "\t---------uriquery : " + request.getUriQuery());
104                 mReq = request;
105                 mLatch.countDown();
106                 return null;
107             }
108         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
109                 Mockito.any(CoapDevice.class));
110     }
111
112     // @InjectMocks for testSpecificDeviceonResponseReceived
113     IRequest                           requestSpecificDevice = MessageBuilder
114             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
115                     "rt=core.light;di=" + "device1");
116     @InjectMocks
117     ResourceFind.AccountReceiveHandler specificDeviceHandler = mResHandler.new AccountReceiveHandler(
118             requestSpecificDevice, mockDevice);
119
120     @Test
121     public void testSpecificDeviceonResponseReceived()
122             throws ClientException, InterruptedException {
123         System.out.println(
124                 "\t--------------onResponseReceived(RD) Resource Find (specific deivce) Test------------");
125         IResponse response = responseFromAccountServer();
126         specificDeviceHandler.onResponseReceived(response);
127         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
128         assertTrue(mReq.getMethod() == RequestMethod.GET);
129         assertTrue(queryMap.get("rt").contains("core.light"));
130         assertTrue(queryMap.get("di").contains("device1"));
131         assertFalse(queryMap.get("di").contains("device2"));
132         assertFalse(queryMap.get("di").contains("device3"));
133     }
134
135     // @InjectMocks for testEntireDeviceonResponseReceived
136     IRequest                           requestEntireDevices = MessageBuilder
137             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
138                     "rt=core.light");
139     @InjectMocks
140     ResourceFind.AccountReceiveHandler handler              = mResHandler.new AccountReceiveHandler(
141             requestEntireDevices, mockDevice);
142
143     @Test
144     public void testEntireDeviceonResponseReceived() throws ClientException {
145         System.out.println(
146                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces) Test------------");
147         IResponse response = responseFromAccountServer();
148         handler.onResponseReceived(response);
149         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
150         assertTrue(mReq.getMethod() == RequestMethod.GET);
151         assertTrue(queryMap.get("rt").contains("core.light"));
152         assertTrue(queryMap.get("di").contains("device1"));
153         assertTrue(queryMap.get("di").contains("device2"));
154         assertTrue(queryMap.get("di").contains("device3"));
155     }
156
157     // @InjectMocks for testEntireDeviceonResponseReceived
158     IRequest                           requestEntireDevicesNoQuery = MessageBuilder
159             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, null);
160     @InjectMocks
161     ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = mResHandler.new AccountReceiveHandler(
162             requestEntireDevicesNoQuery, mockDevice);
163
164     @Test
165     public void testEntireDeviceNoQueryonResponseReceived()
166             throws ClientException {
167         System.out.println(
168                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces, No Query) Test------------");
169         IResponse response = responseFromAccountServer();
170         entireDevicesNoQueryHandler.onResponseReceived(response);
171         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
172         assertTrue(mReq.getMethod() == RequestMethod.GET);
173         assertTrue(queryMap.get("di").contains("device1"));
174         assertTrue(queryMap.get("di").contains("device2"));
175         assertTrue(queryMap.get("di").contains("device3"));
176     }
177
178     @Test
179     public void testResourceFindEntireDevicesOnRequestReceived()
180             throws Exception {
181         System.out.println(
182                 "\t--------------OnRequestReceived(RD) Resource Find (entire deivces) Test------------");
183         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
184                 TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di);
185         mResHandler.onRequestReceived(mockDevice, request);
186         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
187         assertTrue(mLatch.await(1L, SECONDS));
188         assertTrue(queryMap.containsKey("mid"));
189         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
190     }
191
192     @Test
193     public void testResourceFindSpecificDeviceOnRequestReceived()
194             throws Exception {
195         System.out.println(
196                 "\t--------------OnRequestReceived(RD) Resource Find (specific deivce) Test------------");
197         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
198                 TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di);
199         mResHandler.onRequestReceived(mockDevice, request);
200         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
201         // assertion: if the request packet from the CI contains the query
202         // which includes device ID and the accesstoken
203         assertTrue(mLatch.await(1L, SECONDS));
204         assertTrue(queryMap.containsKey("mid"));
205         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
206     }
207
208     private IResponse responseFromAccountServer() {
209         // make response which has "CONTENT" status
210         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
211         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
212         ArrayList<String> deviceList = new ArrayList<String>();
213         // assuming that there are three devices in the response msg from the AS
214         deviceList.add("device1");
215         deviceList.add("device2");
216         deviceList.add("device3");
217         responsePayload.put("dilist", deviceList);
218         responsePayload.put("gid", "g0001");
219         responsePayload.put("gmid", "u0001");
220         ArrayList<String> midList = new ArrayList<String>();
221         midList.add("u0001");
222         responsePayload.put("midlist", midList);
223         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
224                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
225                 cbor.encodingPayloadToCbor(responsePayload));
226         return response;
227     }
228 }