fixed CI unit test.
[iotivity.git] / cloud / account / src / test / java / org / iotivity / cloud / accountserver / resources / acl / invite / InviteResourceTest.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.accountserver.resources.acl.invite;
23
24 import static java.util.concurrent.TimeUnit.SECONDS;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.concurrent.CountDownLatch;
33
34 import org.iotivity.cloud.accountserver.Constants;
35 import org.iotivity.cloud.accountserver.db.MongoDB;
36 import org.iotivity.cloud.base.device.CoapDevice;
37 import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
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.coap.CoapResponse;
43 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
44 import org.iotivity.cloud.base.protocols.enums.Observe;
45 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
46 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
47 import org.iotivity.cloud.util.Cbor;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.Mockito;
51 import org.mockito.invocation.InvocationOnMock;
52 import org.mockito.stubbing.Answer;
53
54 public class InviteResourceTest {
55     private static final String           INVITE_URI        = Constants.INVITE_FULL_URI;
56
57     private static final String           TEST_INVITE_USER  = "u0001";
58     private static final String           TEST_INVITED_USER = "u0002";
59     private static final String           TEST_GROUP_ID     = "g0001";
60
61     private InviteResource                mInviteResource   = null;
62
63     private CountDownLatch                mLatch            = null;
64
65     private CoapDevice                    mMockDevice       = null;
66     private IResponse                     mResponse         = null;
67
68     private Cbor<HashMap<String, Object>> mCbor             = new Cbor<>();
69
70     @Before
71     public void setUp() throws Exception {
72
73         mInviteResource = new InviteResource();
74
75         mLatch = new CountDownLatch(1);
76
77         mMockDevice = Mockito.mock(CoapDevice.class);
78
79         mResponse = null;
80
81         // reset data base
82         MongoDB mongoDB = new MongoDB(Constants.DB_NAME);
83         mongoDB.deleteTable(Constants.INVITE_TABLE);
84         mongoDB.createTable(Constants.INVITE_TABLE);
85
86         Mockito.doAnswer(new Answer<Object>() {
87             @Override
88             public CoapResponse answer(InvocationOnMock invocation)
89                     throws Throwable {
90                 Object[] args = invocation.getArguments();
91                 CoapResponse resp = (CoapResponse) args[0];
92                 mResponse = resp;
93
94                 mLatch.countDown();
95
96                 return resp;
97             }
98         }).when(mMockDevice).sendResponse(Mockito.anyObject());
99
100     }
101
102     @Test
103     public void testSendInvitation() throws Exception {
104
105         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
106
107         assertTrue(mLatch.await(1L, SECONDS));
108         assertEquals(mResponse.getStatus(), ResponseStatus.CHANGED);
109     }
110
111     @Test(expected = PreconditionFailedException.class)
112     public void testSendInvitationWithoutGid() throws Exception {
113
114         sendInvitation(null, TEST_INVITED_USER);
115     }
116
117     @Test(expected = PreconditionFailedException.class)
118     public void testSendInvitationWithoutMid() throws Exception {
119
120         sendInvitation(TEST_GROUP_ID, null);
121     }
122
123     @Test
124     public void testInviteUserObserveInvition() throws Exception {
125
126         CountDownLatch latchInviteUser = new CountDownLatch(2);
127
128         CoapDevice mockInviteUser = mock(CoapDevice.class);
129
130         // invite user callback
131         Mockito.doAnswer(new Answer<Object>() {
132             @Override
133             public CoapResponse answer(InvocationOnMock invocation)
134                     throws Throwable {
135                 Object[] args = invocation.getArguments();
136                 CoapResponse resp = (CoapResponse) args[0];
137
138                 latchInviteUser.countDown();
139
140                 // check observe notification response
141                 if (latchInviteUser.getCount() == 0) {
142
143                     assertEquals(resp.getStatus(), ResponseStatus.CONTENT);
144                     assertTrue(
145                             checkResponsePayload(resp, Constants.RESP_INVITE));
146                     assertTrue(checkInviteInfo(resp.getPayload(),
147                             Constants.RESP_INVITE, TEST_GROUP_ID,
148                             TEST_INVITED_USER));
149                 }
150
151                 return resp;
152             }
153         }).when(mockInviteUser).sendResponse(Mockito.anyObject());
154
155         getInvitation(TEST_INVITE_USER, mockInviteUser, Observe.SUBSCRIBE);
156         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
157
158         assertTrue(latchInviteUser.await(2L, SECONDS));
159     }
160
161     @Test
162     public void testInvitedUserObserveInvitation() throws Exception {
163
164         CountDownLatch latchInvitedUser = new CountDownLatch(2);
165
166         CoapDevice mockInvitedUser = mock(CoapDevice.class);
167
168         // invited user callback
169         Mockito.doAnswer(new Answer<Object>() {
170             @Override
171             public CoapResponse answer(InvocationOnMock invocation)
172                     throws Throwable {
173                 Object[] args = invocation.getArguments();
174                 CoapResponse resp = (CoapResponse) args[0];
175
176                 latchInvitedUser.countDown();
177
178                 // check observe notification response
179                 if (latchInvitedUser.getCount() == 0) {
180
181                     assertEquals(resp.getStatus(), ResponseStatus.CONTENT);
182                     assertTrue(
183                             checkResponsePayload(resp, Constants.RESP_INVITED));
184                     assertTrue(checkInviteInfo(resp.getPayload(),
185                             Constants.RESP_INVITED, TEST_GROUP_ID,
186                             TEST_INVITE_USER));
187                 }
188
189                 return resp;
190             }
191         }).when(mockInvitedUser).sendResponse(Mockito.anyObject());
192
193         getInvitation(TEST_INVITED_USER, mockInvitedUser, Observe.SUBSCRIBE);
194         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
195
196         assertTrue(latchInvitedUser.await(2L, SECONDS));
197     }
198
199     @Test
200     public void testInviteUserCancelObserveInvitation() throws Exception {
201
202         getInvitation(TEST_INVITE_USER, mMockDevice, Observe.SUBSCRIBE);
203         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
204
205         getInvitation(TEST_INVITE_USER, mMockDevice, Observe.UNSUBSCRIBE);
206
207         assertEquals(mResponse.getStatus(), ResponseStatus.CONTENT);
208         assertTrue(checkInviteInfo(mResponse.getPayload(),
209                 Constants.RESP_INVITE, TEST_GROUP_ID, TEST_INVITED_USER));
210     }
211
212     @Test
213     public void testInvitedUserCancelObserveInvitation() throws Exception {
214
215         getInvitation(TEST_INVITED_USER, mMockDevice, Observe.SUBSCRIBE);
216         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
217
218         getInvitation(TEST_INVITED_USER, mMockDevice, Observe.UNSUBSCRIBE);
219
220         assertEquals(mResponse.getStatus(), ResponseStatus.CONTENT);
221         assertTrue(checkInviteInfo(mResponse.getPayload(),
222                 Constants.RESP_INVITED, TEST_GROUP_ID, TEST_INVITE_USER));
223     }
224
225     @Test
226     public void testInviteUserGetInvitation() throws Exception {
227
228         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
229
230         getInvitation(TEST_INVITE_USER, mMockDevice, Observe.NOTHING);
231
232         assertEquals(mResponse.getStatus(), ResponseStatus.CONTENT);
233         assertTrue(checkInviteInfo(mResponse.getPayload(),
234                 Constants.RESP_INVITE, TEST_GROUP_ID, TEST_INVITED_USER));
235     }
236
237     @Test
238     public void testInvitedUserGetInvitation() throws Exception {
239
240         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
241
242         getInvitation(TEST_INVITED_USER, mMockDevice, Observe.NOTHING);
243
244         assertEquals(mResponse.getStatus(), ResponseStatus.CONTENT);
245         assertTrue(checkInviteInfo(mResponse.getPayload(),
246                 Constants.RESP_INVITED, TEST_GROUP_ID, TEST_INVITE_USER));
247     }
248
249     @Test
250     public void testDeleteInvitation() throws Exception {
251
252         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
253
254         deleteInvitation(TEST_GROUP_ID, TEST_INVITED_USER, "");
255
256         assertTrue(mLatch.await(1L, SECONDS));
257         assertEquals(mResponse.getStatus(), ResponseStatus.DELETED);
258     }
259
260     @Test
261     public void testCancelInvitation() throws Exception {
262
263         sendInvitation(TEST_GROUP_ID, TEST_INVITED_USER);
264
265         deleteInvitation(TEST_GROUP_ID, TEST_INVITE_USER, TEST_INVITED_USER);
266
267         assertTrue(mLatch.await(1L, SECONDS));
268         assertEquals(mResponse.getStatus(), ResponseStatus.DELETED);
269     }
270
271     private void sendInvitation(String gid, String mid) {
272
273         IRequest request = makeSendInvitationRequest(gid, mid);
274
275         mInviteResource.onDefaultRequestReceived(mMockDevice, request);
276     }
277
278     private void deleteInvitation(String gid, String uid, String mid) {
279
280         String uriQuery = Constants.REQ_GROUP_ID + "=" + gid + ";"
281                 + Constants.REQ_UUID_ID + "=" + uid + ";" + Constants.REQ_MEMBER
282                 + "=" + mid;
283
284         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
285                 INVITE_URI, uriQuery);
286
287         mInviteResource.onDefaultRequestReceived(mMockDevice, request);
288     }
289
290     private void getInvitation(String uid, CoapDevice device,
291             Observe observeOpt) {
292
293         String uriQuery = Constants.REQ_UUID_ID + "=" + uid;
294         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
295                 INVITE_URI, uriQuery);
296
297         if (observeOpt.equals(Observe.NOTHING) == false) {
298             ((CoapRequest) request).setObserve(observeOpt);
299         }
300
301         mInviteResource.onDefaultRequestReceived(device, request);
302     }
303
304     private IRequest makeSendInvitationRequest(String gid, String mid) {
305
306         HashMap<String, Object> invitation = new HashMap<>();
307         invitation.put(Constants.REQ_GROUP_ID, gid);
308         invitation.put(Constants.REQ_MEMBER, mid);
309
310         HashMap<String, Object> payload = new HashMap<>();
311         payload.put(Constants.REQ_UUID_ID, TEST_INVITE_USER);
312         payload.put(Constants.REQ_INVITE, Arrays.asList(invitation));
313
314         return MessageBuilder.createRequest(RequestMethod.POST, INVITE_URI,
315                 null, ContentFormat.APPLICATION_CBOR,
316                 mCbor.encodingPayloadToCbor(payload));
317     }
318
319     private boolean checkResponsePayload(IResponse response,
320             String propertyName) {
321
322         HashMap<String, Object> payloadData = mCbor
323                 .parsePayloadFromCbor(response.getPayload(), HashMap.class);
324
325         return payloadData.get(propertyName) != null ? true : false;
326     }
327
328     private boolean checkInviteInfo(byte[] payload, String propertyKey,
329             String gid, String mid) {
330
331         HashMap<String, Object> paredPayload = mCbor
332                 .parsePayloadFromCbor(payload, HashMap.class);
333
334         @SuppressWarnings("unchecked")
335         ArrayList<HashMap<String, Object>> invitationList = (ArrayList<HashMap<String, Object>>) paredPayload
336                 .get(propertyKey);
337
338         for (HashMap<String, Object> invitation : invitationList) {
339
340             if (invitation.get(Constants.REQ_GROUP_ID).equals(gid)
341                     && invitation.get(Constants.REQ_MEMBER).equals(mid)) {
342
343                 return true;
344             }
345         }
346
347         return false;
348     }
349
350 }