[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 / AclGroupTest.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.Arrays;
31 import java.util.HashMap;
32 import java.util.concurrent.CountDownLatch;
33
34 import org.iotivity.cloud.base.device.CoapDevice;
35 import org.iotivity.cloud.base.device.IRequestChannel;
36 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
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 AclGroupTest {
55     private static final String   TEST_RESOURCE_GROUP_URI = Constants.GROUP_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     Cbor<HashMap<Object, Object>> mCbor                   = new Cbor<>();
64
65     @Mock
66     private IRequestChannel       mRequestChannel;
67
68     @InjectMocks
69     private AclGroup              mAclGroupHandler        = new AclGroup();
70
71     @Before
72     public void setUp() throws Exception {
73         mReq = null;
74         Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
75         MockitoAnnotations.initMocks(this);
76         mDeviceServerSystem.addResource(mAclGroupHandler);
77         // callback mock
78         Mockito.doAnswer(new Answer<Object>() {
79             @Override
80             public CoapRequest answer(InvocationOnMock invocation)
81                     throws Throwable {
82                 Object[] args = invocation.getArguments();
83                 CoapRequest request = (CoapRequest) args[0];
84                 System.out.println(
85                         "\t----------payload : " + request.getPayloadString());
86                 System.out.println(
87                         "\t----------uripath : " + request.getUriPath());
88                 System.out.println(
89                         "\t---------uriquery : " + request.getUriQuery());
90                 mReq = request;
91                 mLatch.countDown();
92                 return null;
93             }
94         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
95                 Mockito.any(CoapDevice.class));
96     }
97
98     @Test
99     public void testCreateGroupRequestReceived() throws Exception {
100         System.out.println(
101                 "\t--------------OnRequestReceived(AS) Create Group Test------------");
102
103         HashMap<String, Object> payloadData = new HashMap<>();
104         payloadData.put("owner", "u1");
105         payloadData.put("members", Arrays.asList("ui"));
106         payloadData.put("gname", Arrays.asList("home"));
107
108         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
109                 TEST_RESOURCE_GROUP_URI, null, ContentFormat.APPLICATION_CBOR,
110                 mCbor.encodingPayloadToCbor(payloadData));
111         mAclGroupHandler.onRequestReceived(mMockDevice, request);
112
113         assertTrue(mLatch.await(1L, SECONDS));
114         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
115                 .containsKey("owner"));
116         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
117                 .containsKey("members"));
118         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
119                 .containsKey("gname"));
120         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
121         assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
122     }
123
124     @Test
125     public void testAddMemberRequestReceived() throws Exception {
126         System.out.println(
127                 "\t--------------OnRequestReceived(AS) Add Member Test------------");
128
129         HashMap<String, Object> payloadData = new HashMap<>();
130         payloadData.put("members", Arrays.asList("sampleMember"));
131         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
132                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=add",
133                 ContentFormat.APPLICATION_CBOR,
134                 mCbor.encodingPayloadToCbor(payloadData));
135         mAclGroupHandler.onRequestReceived(mMockDevice, request);
136
137         assertTrue(mLatch.await(1L, SECONDS));
138         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
139                 .containsKey("members"));
140         assertTrue(mReq.getUriQueryMap().containsKey("op"));
141         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
142         assertEquals(mReq.getUriPath(),
143                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
144     }
145
146     @Test
147     public void testAddDeviceRequestReceived() throws Exception {
148         System.out.println(
149                 "\t--------------OnRequestReceived(AS) Add Device Test------------");
150
151         HashMap<String, Object> payloadData = new HashMap<>();
152         payloadData.put("devices", Arrays.asList("sampleDevice"));
153         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
154                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=add",
155                 ContentFormat.APPLICATION_CBOR,
156                 mCbor.encodingPayloadToCbor(payloadData));
157         mAclGroupHandler.onRequestReceived(mMockDevice, request);
158
159         assertTrue(mLatch.await(1L, SECONDS));
160         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
161                 .containsKey("devices"));
162         assertTrue(mReq.getUriQueryMap().containsKey("op"));
163         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
164         assertEquals(mReq.getUriPath(),
165                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
166     }
167
168     @Test
169     public void testGetGroupListAndGetGroupInfoRequestReceived()
170             throws Exception {
171         System.out.println(
172                 "\t--------------OnRequestReceived(AS) Get Group List & Get Group Info Test------------");
173
174         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
175                 TEST_RESOURCE_GROUP_URI, "members=u1", null, null);
176         mAclGroupHandler.onRequestReceived(mMockDevice, request);
177
178         assertTrue(mLatch.await(1L, SECONDS));
179         assertTrue(mReq.getUriQueryMap().containsKey("members"));
180         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
181         assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
182     }
183
184     @Test
185     public void testDeleteGroupRequestReceived() throws Exception {
186         System.out.println(
187                 "\t--------------OnRequestReceived(AS) Delete Group Test------------");
188
189         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
190                 TEST_RESOURCE_GROUP_URI + "/g1", "owner=u1", null, null);
191         mAclGroupHandler.onRequestReceived(mMockDevice, request);
192
193         assertTrue(mLatch.await(1L, SECONDS));
194         assertTrue(mReq.getUriQueryMap().containsKey("owner"));
195         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
196         assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI + "/g1");
197     }
198
199     @Test
200     public void testDeleteMemberRequestReceived() throws Exception {
201         System.out.println(
202                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
203         HashMap<String, Object> payloadData = new HashMap<>();
204         payloadData.put("members", Arrays.asList("sampleMember"));
205         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
206                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=delete",
207                 ContentFormat.APPLICATION_CBOR,
208                 mCbor.encodingPayloadToCbor(payloadData));
209         mAclGroupHandler.onRequestReceived(mMockDevice, request);
210
211         assertTrue(mLatch.await(1L, SECONDS));
212         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
213                 .containsKey("members"));
214         assertTrue(mReq.getUriQueryMap().containsKey("op"));
215         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
216         assertEquals(mReq.getUriPath(),
217                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
218     }
219
220     @Test
221     public void testDeleteDeviceRequestReceived() throws Exception {
222         System.out.println(
223                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
224         HashMap<String, Object> payloadData = new HashMap<>();
225         payloadData.put("devices", Arrays.asList("sampleDevice"));
226         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
227                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=delete",
228                 ContentFormat.APPLICATION_CBOR,
229                 mCbor.encodingPayloadToCbor(payloadData));
230         mAclGroupHandler.onRequestReceived(mMockDevice, request);
231
232         assertTrue(mLatch.await(1L, SECONDS));
233         assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
234                 .containsKey("devices"));
235         assertTrue(mReq.getUriQueryMap().containsKey("op"));
236         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
237         assertEquals(mReq.getUriPath(),
238                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
239     }
240
241     @Test(expected = BadRequestException.class)
242     public void testExceptionDeviceRequestReceived() throws Exception {
243         System.out.println(
244                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
245
246         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
247                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup",
248                 "dilist=sampledi", null, null);
249         mAclGroupHandler.onRequestReceived(mMockDevice, request);
250     }
251 }