Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / test / java / org / iotivity / cloud / testrdserver / DevicePresenceResourceTest.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 package org.iotivity.cloud.testrdserver;
23
24 import static java.util.concurrent.TimeUnit.SECONDS;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.concurrent.CountDownLatch;
31
32 import org.iotivity.cloud.base.device.CoapDevice;
33 import org.iotivity.cloud.base.protocols.IRequest;
34 import org.iotivity.cloud.base.protocols.IResponse;
35 import org.iotivity.cloud.base.protocols.MessageBuilder;
36 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
37 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
38 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
39 import org.iotivity.cloud.base.protocols.enums.Observe;
40 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
41 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
42 import org.iotivity.cloud.rdserver.Constants;
43 import org.iotivity.cloud.rdserver.resources.presence.device.DevicePresenceResource;
44 import org.iotivity.cloud.util.Cbor;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.Mockito;
49 import org.mockito.invocation.InvocationOnMock;
50 import org.mockito.stubbing.Answer;
51
52 public class DevicePresenceResourceTest {
53     private Cbor<HashMap<String, Object>> mCbor                       = new Cbor<>();
54     private DevicePresenceResource        mMockDevicePresenceResource = null;
55     private CoapDevice                    mMockDevice                 = null;
56     private CountDownLatch                mLatch                      = null;
57     private IResponse                     mResponse;
58
59     @Before
60     public void setUp() throws Exception {
61         mResponse = null;
62         mMockDevice = mock(CoapDevice.class);
63         mLatch = new CountDownLatch(1);
64         mMockDevicePresenceResource = new DevicePresenceResource();
65         // callback mock
66         Mockito.doAnswer(new Answer<Object>() {
67             @Override
68             public CoapResponse answer(InvocationOnMock invocation)
69                     throws Throwable {
70                 CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
71                 mLatch.countDown();
72                 mResponse = resp;
73                 return null;
74             }
75         }).when(mMockDevice).sendResponse(Mockito.anyObject());
76     }
77
78     @After
79     public void tearDown() throws Exception {
80         RDServerTestUtils.resetRDDatabase();
81     }
82
83     private IRequest makePresenceGetRequest(Observe obs) {
84         String query = Constants.DEVICE_ID + "=" + RDServerTestUtils.DI;
85         IRequest request = null;
86         if (obs.compareTo(Observe.SUBSCRIBE) == 0) {
87             request = MessageBuilder.createRequest(RequestMethod.GET,
88                     RDServerTestUtils.DEVICE_PRS_REQ_URI, query);
89         } else if (obs.compareTo(Observe.UNSUBSCRIBE) == 0) {
90             request = MessageBuilder.createRequest(RequestMethod.GET,
91                     RDServerTestUtils.DEVICE_PRS_REQ_URI, query);
92         }
93         ((CoapRequest) request).setObserve(obs);
94         return request;
95     }
96
97     @Test
98     public void testSubscribeRequest() throws Exception {
99         System.out.println("\t------testHandleGetSubscribeRequest");
100         IRequest request = makePresenceGetRequest(Observe.SUBSCRIBE);
101         mMockDevicePresenceResource.onDefaultRequestReceived(mMockDevice,
102                 request);
103         // assertion: if the response status is "CONTENT"
104         assertTrue(mLatch.await(2L, SECONDS));
105         assertTrue(checkResponseCode(mResponse, ResponseStatus.CONTENT));
106         assertTrue(checkPayloadProperty(mResponse, Constants.DEVICE_ID,
107                 RDServerTestUtils.DI));
108         assertTrue(checkPayloadProperty(mResponse, Constants.PRESENCE_STATE,
109                 Constants.PRESENCE_OFF));
110     }
111
112     @Test
113     public void testUnsubscribeRequest() throws Exception {
114         System.out.println("\t------testHandleGetUnsubscribeRequest");
115         IRequest request = makePresenceGetRequest(Observe.UNSUBSCRIBE);
116         mMockDevicePresenceResource.onDefaultRequestReceived(mMockDevice,
117                 request);
118         // assertion: if the response status is "CONTENT"
119         assertTrue(mLatch.await(2L, SECONDS));
120         assertTrue(checkResponseCode(mResponse, ResponseStatus.CONTENT));
121         assertTrue(checkPayloadProperty(mResponse, Constants.DEVICE_ID,
122                 RDServerTestUtils.DI));
123         assertTrue(checkPayloadProperty(mResponse, Constants.PRESENCE_STATE,
124                 Constants.PRESENCE_OFF));
125     }
126
127     @Test
128     public void testSubscribeRequest_existDevice() throws Exception {
129         System.out.println("\t------testSubscribeRequest_existDevice");
130         CoapDevice observerDevice = mock(CoapDevice.class);
131         CountDownLatch observerLatch = new CountDownLatch(2);
132         // callback mock for observer Device
133         Mockito.doAnswer(new Answer<Object>() {
134             @Override
135             public CoapResponse answer(InvocationOnMock invocation)
136                     throws Throwable {
137                 CoapResponse response = (CoapResponse) invocation
138                         .getArguments()[0];
139                 observerLatch.countDown();
140                 // assertion for observer device (subscribe response)
141                 if (observerLatch.getCount() == 1) {
142                     assertTrue(checkResponseCode(response,
143                             ResponseStatus.CONTENT));
144                 }
145                 if (observerLatch.getCount() == 0) {
146                     assertTrue(checkResponseCode(response,
147                             ResponseStatus.CONTENT));
148                     assertTrue(checkPayloadProperty(response,
149                             Constants.DEVICE_ID, RDServerTestUtils.DI));
150                     assertTrue(checkPayloadProperty(response,
151                             Constants.PRESENCE_STATE, Constants.PRESENCE_ON));
152                 }
153
154                 return null;
155             }
156
157         }).when(observerDevice).sendResponse(Mockito.anyObject());
158         // subscribe request (specific device)
159         IRequest subRequest = makePresenceGetRequest(Observe.SUBSCRIBE);
160         mMockDevicePresenceResource.onDefaultRequestReceived(observerDevice,
161                 subRequest);
162         // POST device presence off
163         HashMap<String, Object> payload = new HashMap<>();
164         payload.put(Constants.DEVICE_ID, RDServerTestUtils.DI);
165         payload.put(Constants.PRESENCE_STATE, Constants.PRESENCE_ON);
166         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
167                 RDServerTestUtils.DEVICE_PRS_REQ_URI, null,
168                 ContentFormat.APPLICATION_CBOR,
169                 mCbor.encodingPayloadToCbor(payload));
170         mMockDevicePresenceResource.onDefaultRequestReceived(mMockDevice,
171                 request);
172         // assertion for resource server device : responseStatus is "CHANGED"
173         assertTrue(mLatch.await(2L, SECONDS));
174         assertTrue(observerLatch.await(2L, SECONDS));
175         assertTrue(checkResponseCode(mResponse, ResponseStatus.CHANGED));
176     }
177
178     @Test
179     public void testUnSubscribeRequest_existDevice() throws Exception {
180         System.out.println("\t------testUnSubscribeRequest_existDevice");
181         CoapDevice observerDevice = mock(CoapDevice.class);
182         CountDownLatch observerLatch = new CountDownLatch(1);
183         // callback mock for observer Device
184         Mockito.doAnswer(new Answer<Object>() {
185             @Override
186             public CoapResponse answer(InvocationOnMock invocation)
187                     throws Throwable {
188                 CoapResponse response = (CoapResponse) invocation
189                         .getArguments()[0];
190                 observerLatch.countDown();
191                 // assertion for observer device (subscribe response)
192                 if (observerLatch.getCount() == 0) {
193                     assertTrue(checkResponseCode(response,
194                             ResponseStatus.CONTENT));
195                     assertTrue(checkPayloadProperty(response,
196                             Constants.DEVICE_ID, RDServerTestUtils.DI));
197                     assertTrue(checkPayloadProperty(response,
198                             Constants.PRESENCE_STATE, Constants.PRESENCE_OFF));
199                 }
200
201                 return null;
202             }
203
204         }).when(observerDevice).sendResponse(Mockito.anyObject());
205         // subscribe request (specific device)
206         IRequest subRequest = makePresenceGetRequest(Observe.UNSUBSCRIBE);
207         mMockDevicePresenceResource.onDefaultRequestReceived(observerDevice,
208                 subRequest);
209         HashMap<String, Object> payload = new HashMap<>();
210         payload.put(Constants.DEVICE_ID, RDServerTestUtils.DI);
211         payload.put(Constants.PRESENCE_STATE, Constants.PRESENCE_OFF);
212         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
213                 RDServerTestUtils.DEVICE_PRS_REQ_URI, null,
214                 ContentFormat.APPLICATION_CBOR,
215                 mCbor.encodingPayloadToCbor(payload));
216         mMockDevicePresenceResource.onDefaultRequestReceived(mMockDevice,
217                 request);
218         // assertion for resource server device : responseStatus is "CHANGED"
219         assertTrue(mLatch.await(2L, SECONDS));
220         assertTrue(observerLatch.await(2L, SECONDS));
221         assertTrue(checkResponseCode(mResponse, ResponseStatus.CHANGED));
222     }
223
224     private boolean checkPayloadProperty(IResponse response,
225             String propertyName, String propertyValue) {
226         HashMap<String, Object> payloadData = mCbor
227                 .parsePayloadFromCbor(response.getPayload(), HashMap.class);
228
229         ArrayList<HashMap<String, String>> prsList = (ArrayList<HashMap<String, String>>) payloadData
230                 .get(Constants.PRESENCE_LIST);
231
232         HashMap<String, String> mapData = prsList.get(0);
233         if (mapData.containsKey(propertyName)
234                 && mapData.get(propertyName).equals(propertyValue)) {
235             return true;
236         } else
237             return false;
238     }
239
240     private boolean checkResponseCode(IResponse response,
241             ResponseStatus responseStatus) {
242         if (responseStatus == response.getStatus())
243             return true;
244         else
245             return false;
246     }
247
248 }