1d5898450bd103c9efe35ba71b851bfd5782e7a9
[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.doAnswer(new Answer<Object>() {
76             @Override
77             public CoapRequest answer(InvocationOnMock invocation)
78                     throws Throwable {
79                 Object[] args = invocation.getArguments();
80                 CoapRequest request = (CoapRequest) args[0];
81                 System.out.println(
82                         "\t----------payload : " + request.getPayloadString());
83                 System.out.println(
84                         "\t----------uripath : " + request.getUriPath());
85                 System.out.println(
86                         "\t---------uriquery : " + request.getUriQuery());
87                 mReq = request;
88                 mLatch.countDown();
89                 return request;
90             }
91         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
92                 Mockito.any(CoapDevice.class));
93     }
94
95     // @InjectMocks for testSpecificDeviceonResponseReceived
96     IRequest                             requestSpecificDevice = makePresenceSpecificDevice(
97             "device1");
98     @InjectMocks
99     DevicePresence.AccountReceiveHandler SpecificDeviceHandler = mPrsHandler.new AccountReceiveHandler(
100             requestSpecificDevice, mMockDevice);
101
102     @Test
103     public void testSpecificDeviceonResponseReceived() throws ClientException {
104         System.out.println(
105                 "\t--------------onResponseReceived(RD) Device Presence (specific deivce) Test------------");
106         IResponse response = responseFromAccountServer();
107         SpecificDeviceHandler.onResponseReceived(response);
108         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
109         assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
110         assertTrue(queryMap.get("di").contains("device1"));
111         assertTrue(mReq.getMethod() == RequestMethod.GET);
112     }
113
114     // @InjectMocks for testEntireDeviceonResponseReceived
115     IRequest                             requestEntireDevices = makePresenceEntireDevice();
116     @InjectMocks
117     DevicePresence.AccountReceiveHandler EntireDeviceHandler  = mPrsHandler.new AccountReceiveHandler(
118             requestEntireDevices, mMockDevice);
119
120     @Test
121     public void testEntireDeviceonResponseReceived() throws ClientException {
122         System.out.println(
123                 "\t--------------onResponseReceived(RD) Device Presence (entire deivces) Test------------");
124         IResponse response = responseFromAccountServer();
125         EntireDeviceHandler.onResponseReceived(response);
126         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
127         assertTrue(mReq.getMethod() == RequestMethod.GET);
128         assertTrue(queryMap.get("di").contains("device1"));
129         assertTrue(queryMap.get("di").contains("device2"));
130         assertTrue(queryMap.get("di").contains("device3"));
131         assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
132     }
133
134     @Test
135     public void testDevicePresenceObserverSpecificDeviceOnRequestReceived()
136             throws Exception {
137         System.out.println(
138                 "\t--------------OnRequestReceived(RD) Device Presence (observe specific device) Test------------");
139         IRequest request = makePresenceSpecificDevice(mDi);
140         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
141         assertTrue(mLatch.await(1L, SECONDS));
142         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
143         assertTrue(queryMap.containsKey("mid"));
144         assertTrue(mReq.getMethod().equals(RequestMethod.GET));
145         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
146     }
147
148     // @InjectMocks for testPresenceDeregisterSpecificDeviceOnRequestReceived
149     IRequest                             requestDeregisterSpecificDevice = makeDeregisterSpecificDevice(
150             "device1");
151     @InjectMocks
152     DevicePresence.AccountReceiveHandler DeregisterHandler               = mPrsHandler.new AccountReceiveHandler(
153             requestDeregisterSpecificDevice, mMockDevice);
154
155     @Test
156     public void testPresenceDeregisterSpecificDeviceOnRequestReceived()
157             throws Exception {
158         System.out.println(
159                 "\t--------------OnRequestReceived(RD) Device Presence (deregister specific device) Test------------");
160         IResponse response = responseFromAccountServer();
161         DeregisterHandler.onResponseReceived(response);
162         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
163         assertTrue(mReq.getMethod() == RequestMethod.GET);
164         assertTrue(queryMap.get("di").contains("device1"));
165         assertTrue(mReq.getObserve() == Observe.UNSUBSCRIBE);
166     }
167
168     @Test
169     public void testDevicePresenceObserverEntireDevicesOnRequestReceived()
170             throws Exception {
171         System.out.println(
172                 "\t--------------OnRequestReceived(RD) Device Presence (observe entire devices) Test------------");
173         IRequest request = makePresenceEntireDevice();
174         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
175         assertTrue(mLatch.await(1L, SECONDS));
176         assertTrue(mReq.getMethod().equals(RequestMethod.GET));
177         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
178         assertTrue(queryMap.containsKey("mid"));
179         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
180     }
181
182     public IRequest makePresenceEntireDevice() {
183         IRequest request = null;
184         request = MessageBuilder.createRequest(RequestMethod.GET,
185                 DEVICE_PRS_REQ_URI, null);
186         ((CoapRequest) request).setObserve(Observe.SUBSCRIBE);
187         return request;
188     }
189
190     private IRequest makePresenceSpecificDevice(String di) {
191         String query = "di=" + di;
192         IRequest request = null;
193         request = MessageBuilder.createRequest(RequestMethod.GET,
194                 DEVICE_PRS_REQ_URI, query);
195         ((CoapRequest) request).setObserve(Observe.SUBSCRIBE);
196         return request;
197     }
198
199     private IRequest makeDeregisterSpecificDevice(String di) {
200         String query = "di=" + di;
201         IRequest request = null;
202         request = MessageBuilder.createRequest(RequestMethod.GET,
203                 DEVICE_PRS_REQ_URI, query);
204         ((CoapRequest) request).setObserve(Observe.UNSUBSCRIBE);
205         return request;
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
214         // the AS
215         deviceList.add("device1");
216         deviceList.add("device2");
217         deviceList.add("device3");
218         responsePayload.put("dilist", deviceList);
219         responsePayload.put("gid", "g0001");
220         responsePayload.put("gmid", "u0001");
221         ArrayList<String> midList = new ArrayList<String>();
222         midList.add("u0001");
223         responsePayload.put("midlist", midList);
224         IRequest requestFromCitoAs = MessageBuilder.createRequest(
225                 RequestMethod.GET, Constants.GROUP_FULL_URI + "/g0001",
226                 "mid=null");
227         IResponse response = MessageBuilder.createResponse(requestFromCitoAs,
228                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
229                 cbor.encodingPayloadToCbor(responsePayload));
230         return response;
231     }
232 }