Refactor logging to use slf4j
[iotivity.git] / cloud / account / src / test / java / org / iotivity / cloud / accountserver / resources / acl / group / GroupAclManagerTest.java
1 package org.iotivity.cloud.accountserver.resources.acl.group;
2
3 import static org.junit.Assert.assertNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.List;
10
11 import org.iotivity.cloud.accountserver.Constants;
12 import org.iotivity.cloud.accountserver.db.AccountDBManager;
13 import org.iotivity.cloud.accountserver.db.GroupTable;
14 import org.iotivity.cloud.accountserver.db.MongoDB;
15 import org.iotivity.cloud.accountserver.resources.acl.id.AclResource;
16 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20
21 public class GroupAclManagerTest {
22     private String                  mGid1               = "g1";
23     private String                  mGid2               = "g2";
24     private String                  mDi1                = "d1";
25     private String                  mDi2                = "d2";
26     private String                  mUid1               = "u1";
27     private String                  mUid2               = "u2";
28     private String                  mUid3               = "u3";
29
30     private HashMap<String, Object> mResourceLight1ofD1 = new HashMap<>();
31     private HashMap<String, Object> mResourceLight2ofD1 = new HashMap<>();
32     private HashMap<String, Object> mResourceLight1ofD2 = new HashMap<>();
33     private HashMap<String, Object> mResourceLight2ofD2 = new HashMap<>();
34     private HashMap<String, Object> mResourceAll        = new HashMap<>();
35     private int                     mPermission         = 15;
36
37     public GroupAclManager          mGroupAclManager    = GroupAclManager
38             .getInstance();
39
40     @Before
41     public void setUp() throws Exception {
42         AclResource.getInstance().createAcl(mUid1, mDi1);
43         AclResource.getInstance().createAcl(mUid2, mDi2);
44
45         mResourceLight1ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
46                 "/di/d1/a/light/1");
47         mResourceLight1ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_RT,
48                 Arrays.asList("core.light"));
49         mResourceLight1ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_IF,
50                 Arrays.asList("oic.if.baseline"));
51
52         mResourceLight2ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
53                 "/di/d1/a/light/2");
54         mResourceLight2ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_RT,
55                 Arrays.asList("core.light"));
56         mResourceLight2ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_IF,
57                 Arrays.asList("oic.if.baseline"));
58
59         mResourceLight1ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
60                 "/di/d2/a/light/1");
61         mResourceLight1ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_RT,
62                 Arrays.asList("core.light"));
63         mResourceLight1ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_IF,
64                 Arrays.asList("oic.if.baseline"));
65
66         mResourceLight2ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
67                 "/di/d2/a/light/2");
68         mResourceLight2ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_RT,
69                 Arrays.asList("core.light"));
70         mResourceLight2ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_IF,
71                 Arrays.asList("oic.if.baseline"));
72
73         mResourceAll.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, "*");
74         mResourceAll.put(Constants.KEYFIELD_ACE_RESOURCE_RT, Arrays.asList(""));
75         mResourceAll.put(Constants.KEYFIELD_ACE_RESOURCE_IF, Arrays.asList(""));
76     }
77
78     @After
79     public void resetAccountDatabase() throws Exception {
80         AclResource.getInstance().deleteAcl((String) AclResource.getInstance()
81                 .getAclid(mDi1).get(Constants.KEYFIELD_ACLID));
82         AclResource.getInstance().deleteAcl((String) AclResource.getInstance()
83                 .getAclid(mDi2).get(Constants.KEYFIELD_ACLID));
84
85         MongoDB mongoDB = new MongoDB("127.0.0.1", Constants.DB_NAME);
86         mongoDB.deleteTable(Constants.GROUP_TABLE);
87         mongoDB.createTable(Constants.GROUP_TABLE);
88         mongoDB.deleteTable(Constants.ACL_TABLE);
89         mongoDB.createTable(Constants.ACL_TABLE);
90         mongoDB.deleteTable(Constants.ACE_TABLE);
91         mongoDB.createTable(Constants.ACE_TABLE);
92     }
93
94     @Test
95     public void testAddAceByResourcesOnlyOwner() {
96         System.out.println("--------------testAddAceByResourcesOnlyOwner------------");
97
98         // initialize group info --
99         ArrayList<String> members = new ArrayList<>();
100         members.add(mUid1);
101
102         HashMap<String, Object> groupInfo = new HashMap<>();
103         groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members);
104         setGroupInfo(mGid1, groupInfo);
105
106         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
107         resources.add(mResourceLight1ofD1);
108         resources.add(mResourceLight2ofD1);
109
110         mGroupAclManager.addAceByResources(mGid1, mPermission, resources);
111
112         // check ACE not updated for d1-u1(device owner)
113         HashMap<String, Object> aceidCondition = new HashMap<>();
114         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
115         aceidCondition.put(Constants.KEYFIELD_DI, mDi1);
116         aceidCondition.put(Constants.KEYFIELD_UID, mUid1);
117         assertNull(getAceId(aceidCondition));
118     }
119
120     @Test
121     public void testAddAceByResources() {
122         System.out.println("--------------testAddAceByResoruces------------");
123
124         // initialize group info --
125         ArrayList<String> members = new ArrayList<>();
126         members.add(mUid1);
127         members.add(mUid2);
128
129         HashMap<String, Object> groupInfo = new HashMap<>();
130         groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members);
131         setGroupInfo(mGid1, groupInfo);
132         // -- initialize group info
133
134         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
135         resources.add(mResourceLight1ofD1);
136         resources.add(mResourceLight2ofD1);
137
138         mGroupAclManager.addAceByResources(mGid1, mPermission, resources);
139
140         // check ACE not updated for d1-u1(device owner)
141         HashMap<String, Object> aceidCondition1 = new HashMap<>();
142         aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1);
143         aceidCondition1.put(Constants.KEYFIELD_DI, mDi1);
144         aceidCondition1.put(Constants.KEYFIELD_UID, mUid1);
145         assertNull(getAceId(aceidCondition1));
146
147         // check ACETable and ACL for d1(light1, light2)-u2
148         HashMap<String, Object> aceidCondition2 = new HashMap<>();
149         aceidCondition2.put(Constants.REQ_GROUP_ID, mGid1);
150         aceidCondition2.put(Constants.KEYFIELD_DI, mDi1);
151         aceidCondition2.put(Constants.KEYFIELD_UID, mUid2);
152         String aceIdg1d1 = getAceId(aceidCondition2);
153
154         HashMap<String, Object> aceCondition2 = new HashMap<>();
155         aceCondition2.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1);
156
157         aceidCondition2.clear();
158         aceidCondition2.put(Constants.KEYFIELD_OID, mUid1);
159         assertTrue(checkAceTable(aceCondition2, aceidCondition2));
160
161         // remove prefix in href
162         for (HashMap<String, Object> resource : resources) {
163             resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
164                     extractHref(
165                             resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)
166                                     .toString()));
167         }
168
169         assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2, resources));
170     }
171
172     @Test
173     public void testAddAceByResourcesNoMembers() {
174         System.out.println("--------------testAddAceByResorucesNoMembers------------");
175         setGroupInfo(mGid1, new HashMap<>());
176
177         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
178         resources.add(mResourceLight1ofD1);
179         resources.add(mResourceLight2ofD1);
180
181         mGroupAclManager.addAceByResources(mGid1, mPermission, resources);
182
183         // check ACE not updated for d1(light1, light2)
184         HashMap<String, Object> aceidCondition = new HashMap<>();
185         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
186         aceidCondition.put(Constants.KEYFIELD_DI, mDi1);
187         assertNull(getAceId(aceidCondition));
188     }
189
190     @Test
191     public void testAddAceByDevices() {
192         System.out.println("--------------testAddAceByDevices------------");
193
194         // initialize group info --
195         ArrayList<String> members = new ArrayList<>();
196         members.add(mUid1);
197         members.add(mUid2);
198
199         HashMap<String, Object> groupInfo = new HashMap<>();
200         groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members);
201         setGroupInfo(mGid1, groupInfo);
202         // -- initialize group info
203
204         ArrayList<String> devices = new ArrayList<>();
205         devices.add(mDi1);
206
207         mGroupAclManager.addAceByDevices(mGid1, mPermission, devices);
208
209         // check ACETable and ACL for d1-u2
210         HashMap<String, Object> aceidCondition = new HashMap<>();
211         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
212         aceidCondition.put(Constants.KEYFIELD_DI, mDi1);
213         aceidCondition.put(Constants.KEYFIELD_UID, mUid2);
214         String aceIdg1d1 = getAceId(aceidCondition);
215
216         HashMap<String, Object> aceCondition = new HashMap<>();
217         aceCondition.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1);
218
219         aceidCondition.clear();
220         aceidCondition.put(Constants.KEYFIELD_OID, mUid1);
221         assertTrue(checkAceTable(aceCondition, aceidCondition));
222
223         assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2,
224                 Arrays.asList(mResourceAll)));
225     }
226
227     @Test
228     public void testAddAceByDevicesOnlyOwner() {
229         System.out.println("--------------testAddAceByDevicesOnlyOwner------------");
230
231         // initialize group info --
232         ArrayList<String> members = new ArrayList<>();
233         members.add(mUid1);
234
235         HashMap<String, Object> groupInfo = new HashMap<>();
236         groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members);
237         setGroupInfo(mGid1, groupInfo);
238         // -- initialize group info
239
240         ArrayList<String> devices = new ArrayList<>();
241         devices.add(mDi1);
242
243         mGroupAclManager.addAceByDevices(mGid1, mPermission, devices);
244
245         // check ACE not updated for d1-u1(device owner)
246         HashMap<String, Object> aceidCondition = new HashMap<>();
247         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
248         aceidCondition.put(Constants.KEYFIELD_DI, mDi1);
249         aceidCondition.put(Constants.KEYFIELD_UID, mUid1);
250         assertNull(getAceId(aceidCondition));
251     }
252
253     @Test
254     public void testAddAceByDevicesNoMembers() {
255         System.out.println("--------------testAddAceByDevicesNoMembers------------");
256         setGroupInfo(mGid1, new HashMap<>());
257
258         ArrayList<String> devices = new ArrayList<>();
259         devices.add(mDi1);
260
261         mGroupAclManager.addAceByDevices(mGid1, mPermission, devices);
262
263         // check ACE not updated for d1
264         HashMap<String, Object> aceidCondition = new HashMap<>();
265         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
266         aceidCondition.put(Constants.KEYFIELD_DI, mDi1);
267         assertNull(getAceId(aceidCondition));
268     }
269
270     @Test
271     public void testAddAceByMembersWithDevice() {
272         System.out.println("--------------testAddAceByMembersWithDevice------------");
273
274         // initialize group info --
275         ArrayList<String> devices = new ArrayList<>();
276         devices.add(mDi1);
277         devices.add(mDi2);
278
279         HashMap<String, Object> groupInfo = new HashMap<>();
280         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
281         setGroupInfo(mGid1, groupInfo);
282         // -- initialize group info
283
284         ArrayList<String> members = new ArrayList<>();
285         members.add(mUid2);
286
287         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
288
289         // check ACETable and ACL for d1-u2
290         HashMap<String, Object> aceidCondition1 = new HashMap<>();
291         aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1);
292         aceidCondition1.put(Constants.KEYFIELD_DI, mDi1);
293         aceidCondition1.put(Constants.KEYFIELD_UID, mUid2);
294         String aceIdg1d1 = getAceId(aceidCondition1);
295
296         HashMap<String, Object> aceCondition = new HashMap<>();
297         aceCondition.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1);
298
299         aceidCondition1.clear();
300         aceidCondition1.put(Constants.KEYFIELD_OID, mUid1);
301         assertTrue(checkAceTable(aceCondition, aceidCondition1));
302
303         assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2,
304                 Arrays.asList(mResourceAll)));
305
306         // check ACE not updated for d2-u2(device owner)
307         HashMap<String, Object> aceidCondition2 = new HashMap<>();
308         aceidCondition2.put(Constants.REQ_GROUP_ID, mGid1);
309         aceidCondition2.put(Constants.KEYFIELD_DI, mDi2);
310         aceidCondition2.put(Constants.KEYFIELD_UID, mUid2);
311         assertNull(getAceId(aceidCondition2));
312     }
313
314     @Test
315     public void testAddAceByMembersWithDeviceAndResource() {
316         System.out.println("--------------testAddAceByMembersWithDeviceAndResource------------");
317
318         // initialize group info --
319         ArrayList<String> devices = new ArrayList<>();
320         devices.add(mDi1);
321         devices.add(mDi2);
322
323         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
324         resources.add(mResourceLight1ofD1);
325
326         HashMap<String, Object> groupInfo = new HashMap<>();
327         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
328         groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources);
329         setGroupInfo(mGid1, groupInfo);
330         // -- initialize group info
331
332         ArrayList<String> members = new ArrayList<>();
333         members.add(mUid2);
334         members.add(mUid3);
335
336         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
337
338         // remove prefix in href
339         ArrayList<HashMap<String, Object>> resourcesWithExtractedHref = new ArrayList<>(
340                 resources);
341         for (HashMap<String, Object> resource : resourcesWithExtractedHref) {
342             resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
343                     extractHref(
344                             resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)
345                                     .toString()));
346         }
347
348         /* d1(light1) */
349         // check ACETable and ACL for d1-u2
350         HashMap<String, Object> aceidCondition1 = new HashMap<>();
351         aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1);
352         aceidCondition1.put(Constants.KEYFIELD_DI, mDi1);
353         aceidCondition1.put(Constants.KEYFIELD_UID, mUid2);
354         String aceIdg1d1 = getAceId(aceidCondition1);
355
356         HashMap<String, Object> aceCondition1 = new HashMap<>();
357         aceCondition1.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1);
358
359         aceidCondition1.clear();
360         aceidCondition1.put(Constants.KEYFIELD_OID, mUid1);
361         assertTrue(checkAceTable(aceCondition1, aceidCondition1));
362         assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2,
363                 resourcesWithExtractedHref));
364
365         // check ACETable and ACL for d1-u3
366         aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1);
367         aceidCondition1.put(Constants.KEYFIELD_DI, mDi1);
368         aceidCondition1.put(Constants.KEYFIELD_UID, mUid3);
369         aceIdg1d1 = getAceId(aceidCondition1);
370
371         aceCondition1.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1);
372
373         aceidCondition1.clear();
374         aceidCondition1.put(Constants.KEYFIELD_OID, mUid1);
375         assertTrue(checkAceTable(aceCondition1, aceidCondition1));
376         assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid3,
377                 resourcesWithExtractedHref));
378
379         /* d2(all resources) */
380         // check ACE not updated for d2-u2(device owner)
381         HashMap<String, Object> aceidCondition2 = new HashMap<>();
382         aceidCondition2.put(Constants.REQ_GROUP_ID, mGid1);
383         aceidCondition2.put(Constants.KEYFIELD_DI, mDi2);
384         aceidCondition2.put(Constants.KEYFIELD_UID, mUid2);
385         assertNull(getAceId(aceidCondition2));
386
387         // check ACETable and ACL for d2-u3
388         aceidCondition2.put(Constants.KEYFIELD_UID, mUid3);
389         String aceIdg1d2 = getAceId(aceidCondition2);
390
391         HashMap<String, Object> aceCondition2 = new HashMap<>();
392         aceCondition2.put(Constants.KEYFIELD_ACE_ID, aceIdg1d2);
393
394         aceidCondition2.clear();
395         aceidCondition2.put(Constants.KEYFIELD_OID, mUid2);
396         assertTrue(checkAceTable(aceidCondition2, aceidCondition2));
397         assertTrue(checkAclTable(mDi2, aceIdg1d2, mUid3,
398                 Arrays.asList(mResourceAll)));
399     }
400
401     @Test
402     public void testAddAceByMembersNoDevice() {
403         System.out.println("--------------testAddAceByMembersNoDevice------------");
404         setGroupInfo(mGid1, new HashMap<>());
405
406         ArrayList<String> members = new ArrayList<>();
407         members.add(mUid2);
408
409         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
410
411         // check ACE not updated
412         HashMap<String, Object> aceidCondition = new HashMap<>();
413         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
414         aceidCondition.put(Constants.KEYFIELD_UID, mUid2);
415         assertNull(getAceId(aceidCondition));
416     }
417
418     @Test
419     public void testAddAceByMembersOwnerDevice() {
420         System.out.println("--------------testAddAceByMembersOwnerDevice------------");
421
422         // initialize group info --
423         ArrayList<String> devices = new ArrayList<>();
424         devices.add(mDi2);
425
426         HashMap<String, Object> groupInfo = new HashMap<>();
427         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
428         setGroupInfo(mGid1, groupInfo);
429         // -- initialize group info
430
431         ArrayList<String> members = new ArrayList<>();
432         members.add(mUid2);
433
434         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
435
436         // check ACE not updated for d2-u2(device owner)
437         HashMap<String, Object> aceidCondition = new HashMap<>();
438         aceidCondition.put(Constants.REQ_GROUP_ID, mGid1);
439         aceidCondition.put(Constants.REQ_GROUP_ID, mDi2);
440         aceidCondition.put(Constants.KEYFIELD_UID, mUid2);
441         assertNull(getAceId(aceidCondition));
442     }
443
444     @Test
445     public void testRemoveAceByGroup() {
446         System.out.println("--------------testRemoveAceByGroup------------");
447
448         // initialize group info --
449         ArrayList<String> members = new ArrayList<>();
450         members.add(mUid1);
451         members.add(mUid2);
452
453         HashMap<String, Object> groupInfo = new HashMap<>();
454         groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members);
455         setGroupInfo(mGid1, groupInfo);
456         setGroupInfo(mGid2, groupInfo);
457         // -- initialize group info
458
459         ArrayList<String> devices = new ArrayList<>();
460         devices.add(mDi1);
461         devices.add(mDi2);
462
463         mGroupAclManager.addAceByDevices(mGid2, mPermission, devices);
464
465         mGroupAclManager.addAceByDevices(mGid1, mPermission - 11, devices);
466
467         mGroupAclManager.removeAceByGroup(mGid1);
468
469         String aceIdg1d1, aceIdg2d1 = null;
470         // verify ACE Group1
471         HashMap<String, Object> aceidCondition1 = new HashMap<>();
472         aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1);
473
474         // verify AceId is Null Cases Group1
475         assertNull(getAceId(aceidCondition1));
476
477         // verify ACE Device1 in Group2
478         HashMap<String, Object> aceCondition2 = new HashMap<>();
479         HashMap<String, Object> aceidCondition2 = new HashMap<>();
480         aceidCondition2.put(Constants.REQ_GROUP_ID, mGid2);
481         aceidCondition2.put(Constants.KEYFIELD_DI, mDi1);
482         aceidCondition2.put(Constants.KEYFIELD_UID, mUid2);
483
484         aceCondition2.put(Constants.KEYFIELD_ACE_ID,
485                 aceIdg1d1 = getAceId(aceidCondition2));
486         aceidCondition2.clear();
487         aceidCondition2.put(Constants.KEYFIELD_OID, mUid1);
488
489         // verify AceId is Null Cases Group2
490         assertTrue(checkAceTable(aceCondition2, aceidCondition2));
491         // verify ACL Device1 Group2
492         checkAclTable(mDi1, aceIdg1d1, mUid2, Arrays.asList(mResourceAll));
493
494         // verify ACE Device1 in Group2
495         HashMap<String, Object> aceCondition3 = new HashMap<>();
496         HashMap<String, Object> aceidCondition3 = new HashMap<>();
497         aceidCondition3.put(Constants.REQ_GROUP_ID, mGid2);
498         aceidCondition3.put(Constants.KEYFIELD_DI, mDi2);
499         aceidCondition3.put(Constants.KEYFIELD_UID, mUid1);
500
501         aceCondition3.put(Constants.KEYFIELD_ACE_ID,
502                 aceIdg2d1 = getAceId(aceidCondition3));
503         aceidCondition3.clear();
504         aceidCondition3.put(Constants.KEYFIELD_OID, mUid2);
505
506         // verify AceId is true Cases Group2
507         assertTrue(checkAceTable(aceCondition3, aceidCondition3));
508         // verify ACL Device2 Group2
509         assertTrue(checkAclTable(mDi2, aceIdg2d1, mUid1,
510                 Arrays.asList(mResourceAll)));
511     }
512
513     @Test
514     public void testRemoveAceByMembers() {
515         System.out.println("--------------testRemoveAceByMembers------------");
516
517         // initialize group info --
518         ArrayList<String> devices = new ArrayList<>();
519         devices.add(mDi1);
520         devices.add(mDi2);
521         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
522         resources.add(mResourceLight1ofD2);
523
524         HashMap<String, Object> groupInfo = new HashMap<>();
525         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
526         groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources);
527         setGroupInfo(mGid1, groupInfo);
528         // -- initialize group info
529
530         ArrayList<String> members = new ArrayList<>();
531         members.add(mUid1);
532         members.add(mUid2);
533         members.add(mUid3);
534
535         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
536
537         members.remove(mUid2);
538         members.remove(mUid3);
539         mGroupAclManager.removeAceByMembers(members, mGid1);
540
541         String aceIdg1d1 = null;
542         // verify ACE User1
543         HashMap<String, Object> aceidCondition1 = new HashMap<>();
544         aceidCondition1.put(Constants.KEYFIELD_OID, mUid1);
545
546         // verify AceId is Null Cases User1
547         assertNull(getAceId(aceidCondition1));
548
549         // verify ACE User2
550         HashMap<String, Object> aceCondition2 = new HashMap<>();
551         HashMap<String, Object> aceidCondition2 = new HashMap<>();
552         aceidCondition2.put(Constants.KEYFIELD_UID, mUid3);
553         aceidCondition2.put(Constants.KEYFIELD_DI, mDi2);
554
555         aceCondition2.put(Constants.KEYFIELD_ACE_ID,
556                 aceIdg1d1 = getAceId(aceidCondition2));
557         aceidCondition2.clear();
558         aceidCondition2.put(Constants.KEYFIELD_OID, mUid2);
559
560         // remove prefix in href
561         for (HashMap<String, Object> resource : resources) {
562             resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
563                     extractHref(
564                             resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)
565                                     .toString()));
566         }
567         // verify AceId True
568         assertTrue(checkAceTable(aceCondition2, aceidCondition2));
569         // verify ACL Device1 Group2
570         assertTrue(checkAclTable(mDi2, aceIdg1d1, mUid3, resources));
571     }
572
573     @Test
574     public void testRemoveAceByDevices() {
575         System.out.println("--------------testRemoveAceByDevices------------");
576
577         // initialize group info --
578         ArrayList<String> devices = new ArrayList<>();
579         devices.add(mDi1);
580         devices.add(mDi2);
581
582         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
583         resources.add(mResourceLight1ofD2);
584
585         HashMap<String, Object> groupInfo = new HashMap<>();
586         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
587         groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources);
588         setGroupInfo(mGid1, groupInfo);
589         // -- initialize group info
590
591         ArrayList<String> members = new ArrayList<>();
592         members.add(mUid1);
593         members.add(mUid2);
594         members.add(mUid3);
595
596         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
597
598         devices.remove(mDi2);
599         mGroupAclManager.removeAceByDevices(devices, mGid1);
600
601         String aceIdu1d2 = null, aceIdu3d2 = null;
602         // verify ACE Device1
603         HashMap<String, Object> aceidCondition1 = new HashMap<>();
604         aceidCondition1.put(Constants.KEYFIELD_DI, mDi1);
605
606         // verify AceId is Null Cases User1
607         assertNull(getAceId(aceidCondition1));
608
609         // verify ACE ownerid=User2, subjectuuid=User1
610         HashMap<String, Object> aceCondition2 = new HashMap<>();
611         HashMap<String, Object> aceidCondition2 = new HashMap<>();
612         aceidCondition2.put(Constants.KEYFIELD_UID, mUid1);
613         aceidCondition2.put(Constants.KEYFIELD_DI, mDi2);
614
615         aceCondition2.put(Constants.KEYFIELD_ACE_ID,
616                 aceIdu1d2 = getAceId(aceidCondition2));
617         aceidCondition2.clear();
618         aceidCondition2.put(Constants.KEYFIELD_OID, mUid2);
619
620         // verify ACE ownerid=User2, subjectuuid=User3
621         HashMap<String, Object> aceCondition3 = new HashMap<>();
622         HashMap<String, Object> aceidCondition3 = new HashMap<>();
623         aceidCondition3.put(Constants.KEYFIELD_UID, mUid3);
624         aceidCondition3.put(Constants.KEYFIELD_DI, mDi2);
625
626         aceCondition3.put(Constants.KEYFIELD_ACE_ID,
627                 aceIdu3d2 = getAceId(aceidCondition3));
628         aceidCondition3.clear();
629         aceidCondition3.put(Constants.KEYFIELD_OID, mUid2);
630
631         // remove prefix in href
632         for (HashMap<String, Object> resource : resources) {
633             resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
634                     extractHref(
635                             resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)
636                                     .toString()));
637         }
638
639         // verify AceId ownerid=User2, subjectuuid=User1 True
640         assertTrue(checkAceTable(aceCondition2, aceidCondition2));
641         // verify ACL Device2 User1
642         assertTrue(checkAclTable(mDi2, aceIdu1d2, mUid1,
643                 Arrays.asList(mResourceLight1ofD2)));
644
645         // verify AceId ownerid=User2, subjectuuid=User3 True
646         assertTrue(checkAceTable(aceCondition3, aceidCondition3));
647         // verify ACL Device2 User1
648         assertTrue(checkAclTable(mDi2, aceIdu3d2, mUid3,
649                 Arrays.asList(mResourceLight1ofD2)));
650
651     }
652
653     @Test
654     public void testRemoveAceByResourcesDeleteAce() {
655         System.out.println("--------------testRemoveAceByResourcesDeleteAce------------");
656
657         // initialize group info --
658         ArrayList<String> devices = new ArrayList<>();
659         devices.add(mDi1);
660         devices.add(mDi2);
661         ArrayList<String> members = new ArrayList<>();
662         members.add(mUid1);
663         members.add(mUid2);
664
665         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
666         resources.add(mResourceLight1ofD2);
667
668         HashMap<String, Object> groupInfo = new HashMap<>();
669         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
670         groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources);
671         setGroupInfo(mGid1, groupInfo);
672         // -- initialize group info
673
674         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
675
676         mGroupAclManager.removeAceByResources(
677                 new ArrayList<>(Arrays.asList(mResourceLight1ofD2)), mGid1);
678
679         // verify ACE Device2
680         HashMap<String, Object> aceidCondition1 = new HashMap<>();
681         aceidCondition1.put(Constants.KEYFIELD_DI, mDi2);
682
683         // verify AceId is Null Cases User1
684         assertNull(getAceId(aceidCondition1));
685
686         String aceIdu1d2 = null;
687         // verify ACE ownerid=u1, subjectuuid=u2 Deivce=d1
688         HashMap<String, Object> aceCondition2 = new HashMap<>();
689         HashMap<String, Object> aceidCondition2 = new HashMap<>();
690         aceidCondition2.put(Constants.KEYFIELD_UID, mUid2);
691         aceidCondition2.put(Constants.KEYFIELD_DI, mDi1);
692
693         aceCondition2.put(Constants.KEYFIELD_ACE_ID,
694                 aceIdu1d2 = getAceId(aceidCondition2));
695         aceidCondition2.clear();
696         aceidCondition2.put(Constants.KEYFIELD_OID, mUid1);
697
698         // verify AceId ownerid=u1, subjectuuid=u2 True
699         assertTrue(checkAceTable(aceCondition2, aceidCondition2));
700         // verify ACL Device2 User1
701         assertTrue(checkAclTable(mDi1, aceIdu1d2, mUid2,
702                 Arrays.asList(mResourceAll)));
703     }
704
705     @Test
706     public void testRemoveAceByResourcesRemainAce() {
707
708         // initialize group info --
709         ArrayList<String> devices = new ArrayList<>();
710         devices.add(mDi1);
711         devices.add(mDi2);
712         ArrayList<String> members = new ArrayList<>();
713         members.add(mUid1);
714         members.add(mUid2);
715
716         ArrayList<HashMap<String, Object>> resources = new ArrayList<>();
717         resources.add(mResourceLight1ofD2);
718         resources.add(mResourceLight2ofD2);
719
720         HashMap<String, Object> groupInfo = new HashMap<>();
721         groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices);
722         groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources);
723         setGroupInfo(mGid1, groupInfo);
724         // -- initialize group info
725
726         mGroupAclManager.addAceByMembers(mGid1, mPermission, members);
727
728         mGroupAclManager.removeAceByResources(
729                 new ArrayList<>(Arrays.asList(mResourceLight1ofD2)), mGid1);
730
731         String aceIdu2d1 = null, aceIdu1d2 = null;
732         // verify ACE ownerid=u1, subjectuuid=u1 Deivce=d2
733         HashMap<String, Object> aceCondition1 = new HashMap<>();
734         HashMap<String, Object> aceidCondition1 = new HashMap<>();
735         aceidCondition1.put(Constants.KEYFIELD_UID, mUid1);
736         aceidCondition1.put(Constants.KEYFIELD_DI, mDi2);
737
738         aceCondition1.put(Constants.KEYFIELD_ACE_ID,
739                 aceIdu1d2 = getAceId(aceidCondition1));
740         aceidCondition1.clear();
741         aceidCondition1.put(Constants.KEYFIELD_OID, mUid2);
742
743         // remove prefix in href
744         for (HashMap<String, Object> resource : resources) {
745             resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF,
746                     extractHref(
747                             resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)
748                                     .toString()));
749         }
750         // verify AceId ownerid=u2, subjectuuid=u1 True
751         assertTrue(checkAceTable(aceCondition1, aceidCondition1));
752         // verify ACL Device2 User1
753         assertTrue(checkAclTable(mDi2, aceIdu1d2, mUid1,
754                 Arrays.asList(mResourceLight2ofD2)));
755
756         // verify ACE ownerid=u1, subjectuuid=u2 Deivce=d1
757         HashMap<String, Object> aceCondition2 = new HashMap<>();
758         HashMap<String, Object> aceidCondition2 = new HashMap<>();
759         aceidCondition2.put(Constants.KEYFIELD_UID, mUid2);
760         aceidCondition2.put(Constants.KEYFIELD_DI, mDi1);
761
762         aceCondition2.put(Constants.KEYFIELD_ACE_ID,
763                 aceIdu1d2 = getAceId(aceidCondition2));
764         aceidCondition2.clear();
765         aceidCondition2.put(Constants.KEYFIELD_OID, mUid1);
766
767         // verify AceId ownerid=u1, subjectuuid=u2 True
768         assertTrue(checkAceTable(aceCondition2, aceidCondition2));
769         // verify ACL Device2 User1
770         assertTrue(checkAclTable(mDi1, aceIdu1d2, mUid2,
771                 Arrays.asList(mResourceAll)));
772     }
773
774     private boolean checkAclTable(String di, String aceid, String subjectUuid,
775             List<HashMap<String, Object>> resource) {
776         System.out.println("--------------checkAclTable : " + di + " ------------");
777         HashMap<String, Object> getAclist = AclResource.getInstance()
778                 .getAclACE((String) AclResource.getInstance().getAclid(di)
779                         .get(Constants.KEYFIELD_ACLID), aceid);
780         System.out.println("check result : " + getAclist);
781         if (getAclist.isEmpty()) {
782             return false;
783         }
784
785         // check subject uuid
786         assertTrue(getAclist.containsKey(Constants.KEYFIELD_ACE_SUBJECT_ID));
787         assertTrue(getAclist.get(Constants.KEYFIELD_ACE_SUBJECT_ID)
788                 .equals(subjectUuid));
789
790         // check resource
791         assertTrue(getAclist.containsKey(Constants.KEYFIELD_ACE_RESOURCE));
792         for (HashMap<String, Object> targetRes : resource) {
793             boolean isbreak = false;
794             for (HashMap<String, Object> res : (ArrayList<HashMap<String, Object>>) getAclist
795                     .get(Constants.KEYFIELD_ACE_RESOURCE)) {
796                 assertTrue(
797                         res.containsKey(Constants.KEYFIELD_ACE_RESOURCE_HREF));
798                 if (res.get(Constants.KEYFIELD_ACE_RESOURCE_HREF).equals(
799                         targetRes.get(Constants.KEYFIELD_ACE_RESOURCE_HREF))) {
800                     isbreak = true;
801                     break;
802                 }
803             }
804             if (isbreak) {
805                 continue;
806             }
807             return false;
808         }
809         return true;
810     }
811
812     private boolean checkAceTable(HashMap<String, Object> condition,
813             HashMap<String, Object> key) {
814         System.out.println("--------------checkAceTable : " + condition.toString()
815                 + " ------------");
816         HashMap<String, Object> getAce = getAceTable(condition).get(0);
817         System.out.println("check result : " + getAce);
818         if (getAce.isEmpty()) {
819             return false;
820         }
821         for (String keyelement : key.keySet()) {
822             assertTrue(getAce.containsKey(keyelement));
823             assertTrue(getAce.get(keyelement).equals(key.get(keyelement)));
824         }
825         return true;
826     }
827
828     private String getAceId(HashMap<String, Object> condition) {
829
830         ArrayList<HashMap<String, Object>> aceTables = AccountDBManager
831                 .getInstance().selectRecord(Constants.ACE_TABLE, condition);
832
833         if (aceTables == null || aceTables.isEmpty()) {
834             return null;
835         }
836
837         return (String) aceTables.get(0).get(Constants.KEYFIELD_ACE_ID);
838     }
839
840     private ArrayList<HashMap<String, Object>> getAceTable(
841             HashMap<String, Object> condition) {
842         return AccountDBManager.getInstance().selectRecord(Constants.ACE_TABLE,
843                 condition);
844     }
845
846     private String extractHref(String href) {
847
848         List<String> segment = new ArrayList<String>(
849                 Arrays.asList(href.split("/")));
850
851         // Remove prefix path
852         segment.remove(0);
853         segment.remove(0);
854         segment.remove(0);
855
856         StringBuilder uriPath = new StringBuilder();
857         for (String path : segment) {
858             uriPath.append("/" + path);
859         }
860
861         return uriPath.toString();
862     }
863
864     private void setGroupInfo(String gid, HashMap<String, Object> keyValue) {
865
866         GroupTable groupTable = new GroupTable();
867         groupTable.setGid(gid);
868         for (String key : keyValue.keySet()) {
869             groupTable.setPropertyValue(key, keyValue.get(key));
870         }
871
872         TypeCastingManager<GroupTable> typeGroup = new TypeCastingManager<GroupTable>();
873         AccountDBManager.getInstance().insertAndReplaceRecord(
874                 Constants.GROUP_TABLE,
875                 typeGroup.convertObjectToMap(groupTable));
876     }
877 }