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