[IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountS...
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AclInviteTest.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.account;
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.Arrays;
32 import java.util.HashMap;
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.protocols.IRequest;
38 import org.iotivity.cloud.base.protocols.MessageBuilder;
39 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
40 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
41 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
42 import org.iotivity.cloud.ciserver.Constants;
43 import org.iotivity.cloud.ciserver.DeviceServerSystem;
44 import org.iotivity.cloud.util.Cbor;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.mockito.invocation.InvocationOnMock;
52 import org.mockito.stubbing.Answer;
53
54 public class AclInviteTest {
55     private static final String  TEST_RESOURCE_INVITE_URI = Constants.INVITE_FULL_URI;
56
57     private CoapDevice           mMockDevice              = mock(
58             CoapDevice.class);
59     private IRequest             mReq                     = null;
60     private DeviceServerSystem   mDeviceServerSystem      = new DeviceServerSystem();
61     private final CountDownLatch mLatch                   = new CountDownLatch(
62             1);
63
64     @Mock
65     private IRequestChannel      mRequestChannel;
66
67     @InjectMocks
68     private AclInvite            mAclInviteHandler        = new AclInvite();
69
70     @Before
71     public void setUp() throws Exception {
72         mReq = null;
73         Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
74         MockitoAnnotations.initMocks(this);
75         mDeviceServerSystem.addResource(mAclInviteHandler);
76         // callback mock
77         Mockito.doAnswer(new Answer<Object>() {
78             @Override
79             public CoapRequest answer(InvocationOnMock invocation)
80                     throws Throwable {
81                 Object[] args = invocation.getArguments();
82                 CoapRequest request = (CoapRequest) args[0];
83                 System.out.println(
84                         "\t----------payload : " + request.getPayloadString());
85                 System.out.println(
86                         "\t----------uripath : " + request.getUriPath());
87                 System.out.println(
88                         "\t---------uriquery : " + request.getUriQuery());
89                 mReq = request;
90                 mLatch.countDown();
91                 return null;
92             }
93         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
94                 Mockito.any(CoapDevice.class));
95     }
96
97     @Test
98     public void testPostRequestReceived() throws Exception {
99         System.out.println(
100                 "\t--------------OnRequestReceived(AS) Post Message Test------------");
101
102         HashMap<String, Object> payloadData = new HashMap<>();
103         HashMap<String, Object> invite = new HashMap<>();
104         invite.put("gid", "g0001");
105         invite.put("mid", "u0001");
106         payloadData.put("uid", "u0001");
107         payloadData.put("invite", Arrays.asList(invite));
108         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
109         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
110                 TEST_RESOURCE_INVITE_URI, null, ContentFormat.APPLICATION_CBOR,
111                 cbor.encodingPayloadToCbor(payloadData));
112         mAclInviteHandler.onRequestReceived(mMockDevice, request);
113
114         assertTrue(mLatch.await(1L, SECONDS));
115         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
116         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
117                 .containsKey("uid"));
118         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
119                 .containsKey("invite"));
120         ArrayList<HashMap<String, Object>> getinvite = (ArrayList<HashMap<String, Object>>) cbor
121                 .parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
122                 .get("invite");
123         assertTrue(getinvite.get(0).containsKey("gid"));
124         assertTrue(getinvite.get(0).containsKey("mid"));
125         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
126     }
127
128     @Test
129     public void testGetRequestReceivedAccept() throws Exception {
130         System.out.println(
131                 "\t--------------OnRequestReceived(AS) Get Message Test------------");
132
133         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
134                 TEST_RESOURCE_INVITE_URI, "gid=g0001;accept=1", null, null);
135         mAclInviteHandler.onRequestReceived(mMockDevice, request);
136
137         assertTrue(mLatch.await(1L, SECONDS));
138         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
139         assertTrue(mReq.getUriQueryMap().containsKey("gid"));
140         assertTrue(mReq.getUriQueryMap().containsKey("accept"));
141         assertTrue(mReq.getUriQueryMap().get("accept").get(0).equals("1"));
142         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
143     }
144
145     @Test
146     public void testGetRequestReceivedDeny() throws Exception {
147         System.out.println(
148                 "\t--------------OnRequestReceived(AS) Get Message Test------------");
149
150         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
151                 TEST_RESOURCE_INVITE_URI, "gid=g0001;accept=0", null, null);
152         mAclInviteHandler.onRequestReceived(mMockDevice, request);
153
154         assertTrue(mLatch.await(1L, SECONDS));
155         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
156         assertTrue(mReq.getUriQueryMap().containsKey("gid"));
157         assertTrue(mReq.getUriQueryMap().containsKey("accept"));
158         assertTrue(mReq.getUriQueryMap().get("accept").get(0).equals("0"));
159         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
160     }
161
162     @Test
163     public void testDeleteRequestReceived() throws Exception {
164         System.out.println(
165                 "\t--------------OnRequestReceived(AS) Delete Message Test------------");
166
167         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
168                 TEST_RESOURCE_INVITE_URI, "gid=g0001;mid=u0002", null, null);
169         mAclInviteHandler.onRequestReceived(mMockDevice, request);
170
171         assertTrue(mLatch.await(1L, SECONDS));
172         assertTrue(mReq.getUriQueryMap().containsKey("gid"));
173         assertTrue(mReq.getUriQueryMap().containsKey("mid"));
174         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
175         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
176     }
177 }