109b22a05a0876fdce701008e86361ffe03e16a2
[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(name = "mRDServer")
69     IRequestChannel             mRequestChannelRDServer;
70     @Mock(name = "mASServer")
71     IRequestChannel             mRequestChannelASServer;
72
73     @InjectMocks
74     private ResourceFind        mResHandler            = new ResourceFind();
75
76     @Before
77     public void setUp() throws Exception {
78         mRes = null;
79         mReq = null;
80         Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
81         MockitoAnnotations.initMocks(this);
82         mDeviceServerSystem.addResource(mResHandler);
83         // callback mock
84         Mockito.doAnswer(new Answer<Object>() {
85             @Override
86             public CoapResponse answer(InvocationOnMock invocation)
87                     throws Throwable {
88                 Object[] args = invocation.getArguments();
89                 CoapResponse resp = (CoapResponse) args[0];
90                 mRes = resp;
91                 return resp;
92             }
93         }).when(mockDevice).sendResponse(Mockito.anyObject());
94         Mockito.doAnswer(new Answer<Object>() {
95             @Override
96             public CoapRequest answer(InvocationOnMock invocation)
97                     throws Throwable {
98                 Object[] args = invocation.getArguments();
99                 CoapRequest request = (CoapRequest) args[0];
100                 System.out.println(
101                         "\t----------payload : " + request.getPayloadString());
102                 System.out.println(
103                         "\t----------uripath : " + request.getUriPath());
104                 System.out.println(
105                         "\t---------uriquery : " + request.getUriQuery());
106                 mReq = request;
107                 mLatch.countDown();
108                 return null;
109             }
110         }).when(mRequestChannelRDServer).sendRequest(
111                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
112
113         Mockito.doAnswer(new Answer<Object>() {
114             @Override
115             public CoapRequest answer(InvocationOnMock invocation)
116                     throws Throwable {
117                 Object[] args = invocation.getArguments();
118                 CoapRequest request = (CoapRequest) args[0];
119                 System.out.println(
120                         "\t----------payload : " + request.getPayloadString());
121                 System.out.println(
122                         "\t----------uripath : " + request.getUriPath());
123                 System.out.println(
124                         "\t---------uriquery : " + request.getUriQuery());
125                 mReq = request;
126                 mLatch.countDown();
127                 return null;
128             }
129         }).when(mRequestChannelASServer).sendRequest(
130                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
131     }
132
133     // @InjectMocks for testSpecificDeviceonResponseReceived
134     IRequest                           requestSpecificDevice = MessageBuilder
135             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
136                     "rt=core.light;di=" + "device1");
137     @InjectMocks
138     ResourceFind.AccountReceiveHandler specificDeviceHandler = mResHandler.new AccountReceiveHandler(
139             requestSpecificDevice, mockDevice);
140
141     @Test
142     public void testSpecificDeviceonResponseReceived()
143             throws ClientException, InterruptedException {
144         System.out.println(
145                 "\t--------------onResponseReceived(RD) Resource Find (specific deivce) Test------------");
146         IResponse response = responseFromAccountServer();
147         specificDeviceHandler.onResponseReceived(response);
148         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
149         assertTrue(mReq.getMethod() == RequestMethod.GET);
150         assertTrue(queryMap.get("rt").contains("core.light"));
151         assertTrue(queryMap.get("di").contains("device1"));
152         assertFalse(queryMap.get("di").contains("device2"));
153         assertFalse(queryMap.get("di").contains("device3"));
154     }
155
156     // @InjectMocks for testEntireDeviceonResponseReceived
157     IRequest                           requestEntireDevices = MessageBuilder
158             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
159                     "rt=core.light");
160     @InjectMocks
161     ResourceFind.AccountReceiveHandler handler              = mResHandler.new AccountReceiveHandler(
162             requestEntireDevices, mockDevice);
163
164     @Test
165     public void testEntireDeviceonResponseReceived() throws ClientException {
166         System.out.println(
167                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces) Test------------");
168         IResponse response = responseFromAccountServer();
169         handler.onResponseReceived(response);
170         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
171         assertTrue(mReq.getMethod() == RequestMethod.GET);
172         assertTrue(queryMap.get("rt").contains("core.light"));
173         assertTrue(queryMap.get("di").contains("device1"));
174         assertTrue(queryMap.get("di").contains("device2"));
175         assertTrue(queryMap.get("di").contains("device3"));
176     }
177
178     // @InjectMocks for testEntireDeviceonResponseReceived
179     IRequest                           requestEntireDevicesNoQuery = MessageBuilder
180             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, null);
181     @InjectMocks
182     ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = mResHandler.new AccountReceiveHandler(
183             requestEntireDevicesNoQuery, mockDevice);
184
185     @Test
186     public void testEntireDeviceNoQueryonResponseReceived()
187             throws ClientException {
188         System.out.println(
189                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces, No Query) Test------------");
190         IResponse response = responseFromAccountServer();
191         entireDevicesNoQueryHandler.onResponseReceived(response);
192         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
193         assertTrue(mReq.getMethod() == RequestMethod.GET);
194         assertTrue(queryMap.get("di").contains("device1"));
195         assertTrue(queryMap.get("di").contains("device2"));
196         assertTrue(queryMap.get("di").contains("device3"));
197     }
198
199     @Test
200     public void testResourceFindEntireDevicesOnRequestReceived()
201             throws Exception {
202         System.out.println(
203                 "\t--------------OnRequestReceived(RD) Resource Find (entire deivces) Test------------");
204         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
205                 TEST_RESOURCE_FIND_URI, "rt=core.light");
206         mResHandler.onRequestReceived(mockDevice, request);
207         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
208         assertTrue(mLatch.await(1L, SECONDS));
209         assertTrue(queryMap.containsKey("mid"));
210         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
211     }
212
213     @Test
214     public void testResourceFindSpecificDeviceOnRequestReceived()
215             throws Exception {
216         System.out.println(
217                 "\t--------------OnRequestReceived(RD) Resource Find (specific deivce) Test------------");
218         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
219                 TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di);
220         mResHandler.onRequestReceived(mockDevice, request);
221         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
222         // assertion: if the request packet from the CI contains the query
223         // which includes device ID and the accesstoken
224         assertTrue(mLatch.await(1L, SECONDS));
225         assertTrue(queryMap.containsKey("di"));
226         assertEquals(mReq.getUriPath(), Constants.WELL_KNOWN_FULL_URI);
227     }
228
229     private IResponse responseFromAccountServer() {
230         // make response which has "CONTENT" status
231         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
232         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
233         ArrayList<String> deviceList = new ArrayList<String>();
234         // assuming that there are three devices in the response msg from the AS
235         deviceList.add("device1");
236         deviceList.add("device2");
237         deviceList.add("device3");
238         responsePayload.put("dilist", deviceList);
239         responsePayload.put("gid", "g0001");
240         responsePayload.put("gmid", "u0001");
241         ArrayList<String> midList = new ArrayList<String>();
242         midList.add("u0001");
243         responsePayload.put("midlist", midList);
244         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
245                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
246                 cbor.encodingPayloadToCbor(responsePayload));
247         return response;
248     }
249 }