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