30b3a44e67c06397e6d4c9c6a36f428d3bd60c67
[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
64     @Mock
65     private IRequestChannel      mRequestChannel;
66
67     @InjectMocks
68     private AclGroup             mAclGroupHandler        = new AclGroup();
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(mAclGroupHandler);
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 testCreateGroupRequestReceived() throws Exception {
99         System.out.println(
100                 "\t--------------OnRequestReceived(AS) Create Group Test------------");
101
102         HashMap<String, Object> payloadData = new HashMap<>();
103         payloadData.put("gtype", "public");
104         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
105         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
106                 TEST_RESOURCE_GROUP_URI, null, ContentFormat.APPLICATION_CBOR,
107                 cbor.encodingPayloadToCbor(payloadData));
108         mAclGroupHandler.onRequestReceived(mMockDevice, request);
109
110         assertTrue(mLatch.await(1L, SECONDS));
111         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
112                 .containsKey("gtype"));
113         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
114                 .containsKey("gmid"));
115         assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
116     }
117
118     @Test
119     public void testAddMemberRequestReceived() throws Exception {
120         System.out.println(
121                 "\t--------------OnRequestReceived(AS) Add Member Test------------");
122
123         HashMap<String, Object> payloadData = new HashMap<>();
124         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
125         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
126                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null,
127                 ContentFormat.APPLICATION_CBOR,
128                 cbor.encodingPayloadToCbor(payloadData));
129         mAclGroupHandler.onRequestReceived(mMockDevice, request);
130
131         assertTrue(mLatch.await(1L, SECONDS));
132         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
133                 .containsKey("midlist"));
134         assertEquals(mReq.getUriPath(),
135                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
136     }
137
138     @Test
139     public void testAddDeviceRequestReceived() throws Exception {
140         System.out.println(
141                 "\t--------------OnRequestReceived(AS) Add Device Test------------");
142
143         HashMap<String, Object> payloadData = new HashMap<>();
144         payloadData.put("dilist", Arrays.asList("sampleDevice"));
145         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
146         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
147                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null,
148                 ContentFormat.APPLICATION_CBOR,
149                 cbor.encodingPayloadToCbor(payloadData));
150         mAclGroupHandler.onRequestReceived(mMockDevice, request);
151
152         assertTrue(mLatch.await(1L, SECONDS));
153         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
154                 .containsKey("dilist"));
155         assertEquals(mReq.getUriPath(),
156                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
157     }
158
159     @Test
160     public void testGetGroupListAndGetGroupInfoRequestReceived()
161             throws Exception {
162         System.out.println(
163                 "\t--------------OnRequestReceived(AS) Get Group List & Get Group Info Test------------");
164
165         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
166                 TEST_RESOURCE_GROUP_URI, null, null, null);
167         mAclGroupHandler.onRequestReceived(mMockDevice, request);
168
169         assertTrue(mLatch.await(1L, SECONDS));
170         assertTrue(mReq.getUriQueryMap().containsKey("mid"));
171         assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
172     }
173
174     @Test
175     public void testDeleteGroupRequestReceived() throws Exception {
176         System.out.println(
177                 "\t--------------OnRequestReceived(AS) Delete Group Test------------");
178
179         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
180                 TEST_RESOURCE_GROUP_URI, "gid=samplegid", null, null);
181         mAclGroupHandler.onRequestReceived(mMockDevice, request);
182
183         assertTrue(mLatch.await(1L, SECONDS));
184         assertTrue(mReq.getUriQueryMap().containsKey("gid"));
185         assertTrue(mReq.getUriQueryMap().containsKey("gmid"));
186         assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
187     }
188
189     @Test
190     public void testDeleteMemberRequestReceived() throws Exception {
191         System.out.println(
192                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
193
194         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
195                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, null,
196                 null);
197         mAclGroupHandler.onRequestReceived(mMockDevice, request);
198
199         assertTrue(mLatch.await(1L, SECONDS));
200         assertTrue(mReq.getUriQueryMap().containsKey("midlist"));
201         assertEquals(mReq.getUriPath(),
202                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
203     }
204
205     @Test
206     public void testDeleteDeviceRequestReceived() throws Exception {
207         System.out.println(
208                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
209
210         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
211                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup",
212                 "dilist=sampledi", null, null);
213         mAclGroupHandler.onRequestReceived(mMockDevice, request);
214
215         assertTrue(mLatch.await(1L, SECONDS));
216         assertTrue(mReq.getUriQueryMap().containsKey("dilist"));
217         assertEquals(mReq.getUriPath(),
218                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
219     }
220
221     @Test(expected = BadRequestException.class)
222     public void testExceptionDeviceRequestReceived() throws Exception {
223         System.out.println(
224                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
225
226         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
227                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup",
228                 "dilist=sampledi", null, null);
229         mAclGroupHandler.onRequestReceived(mMockDevice, request);
230     }
231 }