2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.cloud.ciserver.resources.proxy.rd;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.concurrent.CountDownLatch;
35 import org.iotivity.cloud.base.device.CoapDevice;
36 import org.iotivity.cloud.base.device.IRequestChannel;
37 import org.iotivity.cloud.base.exception.ClientException;
38 import org.iotivity.cloud.base.protocols.IRequest;
39 import org.iotivity.cloud.base.protocols.IResponse;
40 import org.iotivity.cloud.base.protocols.MessageBuilder;
41 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
42 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
43 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
44 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
45 import org.iotivity.cloud.ciserver.Constants;
46 import org.iotivity.cloud.ciserver.DeviceServerSystem;
47 import org.iotivity.cloud.util.Cbor;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.mockito.invocation.InvocationOnMock;
55 import org.mockito.stubbing.Answer;
57 public class ResourcePresenceTest {
58 public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
59 public static final String RES_PRS_URI = Constants.RESOURCE_PRESENCE_FULL_URI;
60 private String mDi = "B371C481-38E6-4D47-8320-7688D8A5B58C";
61 private CoapDevice mMockDevice = mock(CoapDevice.class);
62 private IResponse mRes = null;
63 private IRequest mReq = null;
64 private DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
65 final CountDownLatch latch = new CountDownLatch(1);
67 private IRequestChannel requestChannel;
69 private ResourcePresence adHandler = new ResourcePresence();
72 public void setUp() throws Exception {
75 Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
76 Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
77 MockitoAnnotations.initMocks(this);
78 deviceServerSystem.addResource(adHandler);
79 Mockito.doAnswer(new Answer<Object>() {
81 public CoapRequest answer(InvocationOnMock invocation)
83 Object[] args = invocation.getArguments();
84 CoapRequest request = (CoapRequest) args[0];
86 "\t----------payload : " + request.getPayloadString());
88 "\t----------uripath : " + request.getUriPath());
90 "\t---------uriquery : " + request.getUriQuery());
95 }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
96 Mockito.any(CoapDevice.class));
100 public void testResourcePresenceEntireResourcesOnRequestReceived()
103 "\t--------------OnRequestReceived(RD) Resource Presence (entire resource) Test------------");
104 IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
106 deviceServerSystem.onRequestReceived(mMockDevice, request);
107 HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
108 // assertion: if the request packet from the CI contains the query
109 // which includes the accesstoken and the di
110 assertTrue(latch.await(1L, SECONDS));
111 assertTrue(queryMap.containsKey("uid"));
112 assertTrue(queryMap.containsKey("members"));
113 assertEquals(mReq.getUriPath(),
114 Constants.GROUP_FULL_URI + "/mockUserId");
118 public void testResourcePresenceSpecificResourceOnRequestReceived()
121 "\t--------------OnRequestReceived(RD) Resource Presence (specific resources) Test------------");
122 IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
123 RES_PRS_URI, "di=" + mDi);
124 deviceServerSystem.onRequestReceived(mMockDevice, request);
125 HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
126 assertTrue(latch.await(1L, SECONDS));
127 assertTrue(queryMap.containsKey("uid"));
128 assertTrue(queryMap.containsKey("members"));
129 assertEquals(mReq.getUriPath(),
130 Constants.GROUP_FULL_URI + "/mockUserId");
133 IRequest requestEntireDevices = MessageBuilder
134 .createRequest(RequestMethod.GET, RES_PRS_URI, null);
136 ResourcePresence.AccountReceiveHandler entireDeviceHandler = adHandler.new AccountReceiveHandler(
137 requestEntireDevices, mMockDevice);
140 public void testEntireDeviceonResponseReceived() throws ClientException {
142 "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------");
143 IResponse responseFromAccountServer = responseFromAccountServer();
144 entireDeviceHandler.onResponseReceived(responseFromAccountServer);
145 HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
146 // assertion : if query has pre-requested multiple di list given from
148 assertTrue(mReq.getMethod() == RequestMethod.GET);
149 assertTrue(queryMap.get("di").contains("device1"));
150 assertTrue(queryMap.get("di").contains("device2"));
151 assertTrue(queryMap.get("di").contains("device3"));
154 IRequest requestSpecificDevice = MessageBuilder
155 .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1");
157 ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler(
158 requestSpecificDevice, mMockDevice);
161 public void testSpecificDeviceonResponseReceived() throws ClientException {
163 "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------");
164 IResponse response = responseFromAccountServer();
165 specificDeviceHandler.onResponseReceived(response);
166 HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
167 // assertion : if query has pre-requested di
168 assertTrue(queryMap.get("di").contains("device1"));
169 assertTrue(mReq.getMethod() == RequestMethod.GET);
172 private IResponse responseFromAccountServer() {
173 // make response which has "CONTENT" status
174 Cbor<HashMap<String, Object>> cbor = new Cbor<>();
175 HashMap<String, Object> responsePayload = new HashMap<String, Object>();
176 ArrayList<String> deviceList = new ArrayList<String>();
177 // assuming that there are three devices in the response msg from the AS
178 deviceList.add("device1");
179 deviceList.add("device2");
180 deviceList.add("device3");
181 responsePayload.put("devices", deviceList);
182 responsePayload.put("gid", "g0001");
183 ArrayList<String> midList = new ArrayList<String>();
184 midList.add("u0001");
185 responsePayload.put("members", midList);
186 IResponse response = MessageBuilder.createResponse(requestEntireDevices,
187 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
188 cbor.encodingPayloadToCbor(responsePayload));