e2b52a7393ddd7796e0e50068165ee186df8d8e9
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / DevicePresenceTest.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.assertTrue;
28 import static org.mockito.Mockito.mock;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.concurrent.CountDownLatch;
34
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.Observe;
44 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
45 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
46 import org.iotivity.cloud.ciserver.Constants;
47 import org.iotivity.cloud.ciserver.DeviceServerSystem;
48 import org.iotivity.cloud.util.Cbor;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.mockito.invocation.InvocationOnMock;
56 import org.mockito.stubbing.Answer;
57
58 public class DevicePresenceTest {
59     private static final String DEVICE_PRS_REQ_URI  = Constants.DEVICE_PRESENCE_FULL_URI;
60     private String              mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
61     private CoapDevice          mMockDevice         = mock(CoapDevice.class);
62     private IRequest            mReq                = null;
63     private DeviceServerSystem  mDeviceServerSystem = new DeviceServerSystem();
64     final CountDownLatch        mLatch              = new CountDownLatch(1);
65     @Mock
66     private IRequestChannel     mRequestChannel;
67     @InjectMocks
68     private DevicePresence      mPrsHandler         = new DevicePresence();
69
70     @Before
71     public void setUp() throws Exception {
72         MockitoAnnotations.initMocks(this);
73         mDeviceServerSystem.addResource(mPrsHandler);
74         Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
75         Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
76         Mockito.doAnswer(new Answer<Object>() {
77             @Override
78             public CoapRequest answer(InvocationOnMock invocation)
79                     throws Throwable {
80                 Object[] args = invocation.getArguments();
81                 CoapRequest request = (CoapRequest) args[0];
82                 System.out.println(
83                         "\t----------payload : " + request.getPayloadString());
84                 System.out.println(
85                         "\t----------uripath : " + request.getUriPath());
86                 System.out.println(
87                         "\t---------uriquery : " + request.getUriQuery());
88                 mReq = request;
89                 mLatch.countDown();
90                 return request;
91             }
92         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
93                 Mockito.any(CoapDevice.class));
94     }
95
96     // @InjectMocks for testSpecificDeviceonResponseReceived
97     IRequest                             requestSpecificDevice = makePresenceSpecificDevice(
98             "device1");
99     @InjectMocks
100     DevicePresence.AccountReceiveHandler SpecificDeviceHandler = mPrsHandler.new AccountReceiveHandler(
101             requestSpecificDevice, mMockDevice);
102
103     @Test
104     public void testSpecificDeviceonResponseReceived() throws ClientException {
105         System.out.println(
106                 "\t--------------onResponseReceived(RD) Device Presence (specific deivce) Test------------");
107         IResponse response = responseFromAccountServer();
108         SpecificDeviceHandler.onResponseReceived(response);
109         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
110         assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
111         assertTrue(queryMap.get("di").contains("device1"));
112         assertTrue(mReq.getMethod() == RequestMethod.GET);
113     }
114
115     // @InjectMocks for testEntireDeviceonResponseReceived
116     IRequest                             requestEntireDevices = makePresenceEntireDevice();
117     @InjectMocks
118     DevicePresence.AccountReceiveHandler EntireDeviceHandler  = mPrsHandler.new AccountReceiveHandler(
119             requestEntireDevices, mMockDevice);
120
121     @Test
122     public void testEntireDeviceonResponseReceived() throws ClientException {
123         System.out.println(
124                 "\t--------------onResponseReceived(RD) Device Presence (entire deivces) Test------------");
125         IResponse response = responseFromAccountServer();
126         EntireDeviceHandler.onResponseReceived(response);
127         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
128         assertTrue(mReq.getMethod() == RequestMethod.GET);
129         assertTrue(queryMap.get("di").contains("device1"));
130         assertTrue(queryMap.get("di").contains("device2"));
131         assertTrue(queryMap.get("di").contains("device3"));
132         assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
133     }
134
135     @Test
136     public void testDevicePresenceObserverSpecificDeviceOnRequestReceived()
137             throws Exception {
138         System.out.println(
139                 "\t--------------OnRequestReceived(RD) Device Presence (observe specific device) Test------------");
140         IRequest request = makePresenceSpecificDevice(mDi);
141         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
142         assertTrue(mLatch.await(1L, SECONDS));
143         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
144         assertTrue(queryMap.containsKey("uid"));
145         assertTrue(queryMap.containsKey("members"));
146         assertTrue(mReq.getMethod().equals(RequestMethod.GET));
147         assertEquals(mReq.getUriPath(),
148                 Constants.GROUP_FULL_URI + "/mockUserId");
149     }
150
151     // @InjectMocks for testPresenceDeregisterSpecificDeviceOnRequestReceived
152     IRequest                             requestDeregisterSpecificDevice = makeDeregisterSpecificDevice(
153             "device1");
154     @InjectMocks
155     DevicePresence.AccountReceiveHandler DeregisterHandler               = mPrsHandler.new AccountReceiveHandler(
156             requestDeregisterSpecificDevice, mMockDevice);
157
158     @Test
159     public void testPresenceDeregisterSpecificDeviceOnRequestReceived()
160             throws Exception {
161         System.out.println(
162                 "\t--------------OnRequestReceived(RD) Device Presence (deregister specific device) Test------------");
163         IResponse response = responseFromAccountServer();
164         DeregisterHandler.onResponseReceived(response);
165         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
166         assertTrue(mReq.getMethod() == RequestMethod.GET);
167         assertTrue(queryMap.get("di").contains("device1"));
168         assertTrue(mReq.getObserve() == Observe.UNSUBSCRIBE);
169     }
170
171     @Test
172     public void testDevicePresenceObserverEntireDevicesOnRequestReceived()
173             throws Exception {
174         System.out.println(
175                 "\t--------------OnRequestReceived(RD) Device Presence (observe entire devices) Test------------");
176         IRequest request = makePresenceEntireDevice();
177         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
178         assertTrue(mLatch.await(1L, SECONDS));
179         assertTrue(mReq.getMethod().equals(RequestMethod.GET));
180         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
181         assertTrue(queryMap.containsKey("uid"));
182         assertTrue(queryMap.containsKey("members"));
183         assertEquals(mReq.getUriPath(),
184                 Constants.GROUP_FULL_URI + "/mockUserId");
185     }
186
187     public IRequest makePresenceEntireDevice() {
188         IRequest request = null;
189         request = MessageBuilder.createRequest(RequestMethod.GET,
190                 DEVICE_PRS_REQ_URI, null);
191         ((CoapRequest) request).setObserve(Observe.SUBSCRIBE);
192         return request;
193     }
194
195     private IRequest makePresenceSpecificDevice(String di) {
196         String query = "di=" + di;
197         IRequest request = null;
198         request = MessageBuilder.createRequest(RequestMethod.GET,
199                 DEVICE_PRS_REQ_URI, query);
200         ((CoapRequest) request).setObserve(Observe.SUBSCRIBE);
201         return request;
202     }
203
204     private IRequest makeDeregisterSpecificDevice(String di) {
205         String query = "di=" + di;
206         IRequest request = null;
207         request = MessageBuilder.createRequest(RequestMethod.GET,
208                 DEVICE_PRS_REQ_URI, query);
209         ((CoapRequest) request).setObserve(Observe.UNSUBSCRIBE);
210         return request;
211     }
212
213     private IResponse responseFromAccountServer() {
214         // make response which has "CONTENT" status
215         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
216         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
217         ArrayList<String> deviceList = new ArrayList<String>();
218         // assuming that there are three devices in the response msg from
219         // the AS
220         deviceList.add("device1");
221         deviceList.add("device2");
222         deviceList.add("device3");
223         responsePayload.put("devices", deviceList);
224         responsePayload.put("gid", "g0001");
225         responsePayload.put("gmid", "u0001");
226         ArrayList<String> midList = new ArrayList<String>();
227         midList.add("u0001");
228         responsePayload.put("members", midList);
229         IRequest requestFromCitoAs = MessageBuilder.createRequest(
230                 RequestMethod.GET, Constants.GROUP_FULL_URI + "/g0001",
231                 "uid=mockUserId;members=mockUserId");
232         IResponse response = MessageBuilder.createResponse(requestFromCitoAs,
233                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
234                 cbor.encodingPayloadToCbor(responsePayload));
235         return response;
236     }
237 }