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