From: Jung Seungho Date: Mon, 7 Nov 2016 05:08:26 +0000 (+0900) Subject: [IOT-1532] added GroupAclManager for managing group between acl X-Git-Tag: 1.3.0~846 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6be1588851fe44a539196c300ee3886cfb1849fc;p=platform%2Fupstream%2Fiotivity.git [IOT-1532] added GroupAclManager for managing group between acl Change-Id: I5d99b4fbfcd0e3464411754919356326a75def3c Signed-off-by: Minji Park Signed-off-by: Jung Seungho Reviewed-on: https://gerrit.iotivity.org/gerrit/13579 Reviewed-by: Yeonghun Nam Tested-by: jenkins-iotivity Reviewed-by: Jee Hyeok Kim (cherry picked from commit 35f6b5689995e5143ef7cbc1bace9688dd07d9a9) Reviewed-on: https://gerrit.iotivity.org/gerrit/14551 Reviewed-by: Uze Choi (cherry picked from commit 025cc2ce8e3dfdb4d99f9ddd70e9125b7d0840a4) Reviewed-on: https://gerrit.iotivity.org/gerrit/15299 --- diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/AceTable.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/AceTable.java new file mode 100644 index 0000000..bcc295c --- /dev/null +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/AceTable.java @@ -0,0 +1,83 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 Samsung Electronics All Rights Reserved. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // Licensed under the Apache License, Version 2.0 (the "License"); + * // you may not use this file except in compliance with the License. + * // You may obtain a copy of the License at + * // + * // http://www.apache.org/licenses/LICENSE-2.0 + * // + * // Unless required by applicable law or agreed to in writing, software + * // distributed under the License is distributed on an "AS IS" BASIS, + * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * // See the License for the specific language governing permissions and + * // limitations under the License. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +package org.iotivity.cloud.accountserver.db; + +public class AceTable { + + private String gid; + private String di; + private String uid; + private String aceid; + private String oid; + + public AceTable(String gid, String di, String uid, String aceid, String oid, + String href) { + this.gid = gid; + this.di = di; + this.uid = uid; + this.aceid = aceid; + this.oid = oid; + } + + public AceTable() { + } + + public String getGid() { + return gid; + } + + public void setGid(String gid) { + this.gid = gid; + } + + public String getDi() { + return di; + } + + public void setDi(String di) { + this.di = di; + } + + public String getUid() { + return uid; + } + + public void setUid(String uid) { + this.uid = uid; + } + + public String getAceid() { + return aceid; + } + + public void setAceid(String aceid) { + this.aceid = aceid; + } + + public String getOid() { + return oid; + } + + public void setOid(String oid) { + this.oid = oid; + } +} diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupAclManager.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupAclManager.java new file mode 100644 index 0000000..42a8935 --- /dev/null +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupAclManager.java @@ -0,0 +1,557 @@ +package org.iotivity.cloud.accountserver.resources.acl.group; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; + +import org.iotivity.cloud.accountserver.Constants; +import org.iotivity.cloud.accountserver.db.AccountDBManager; +import org.iotivity.cloud.accountserver.db.AceTable; +import org.iotivity.cloud.accountserver.db.GroupTable; +import org.iotivity.cloud.accountserver.resources.acl.id.AceResource; +import org.iotivity.cloud.accountserver.resources.acl.id.AclResource; +import org.iotivity.cloud.accountserver.util.TypeCastingManager; +import org.iotivity.cloud.base.exception.ServerException.BadRequestException; +import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException; + +/** + * + * This class provides a set of APIs to manage between AclManager and + * GroupManager + * + * + */ + +public class GroupAclManager { + private static GroupAclManager mGrAclManager = new GroupAclManager(); + private TypeCastingManager mTypeAceTable = new TypeCastingManager(); + private TypeCastingManager mTypeAceResource = new TypeCastingManager(); + + private GroupAclManager() { + } + + public static GroupAclManager getInstance() { + return mGrAclManager; + } + + /** + * API to add acl table by members + * + * @param gid + * group id + * @param permission + * resource permission of group acl in group + * @param members + * member list which add into group + */ + public void addAceByMembers(String gid, int permission, + ArrayList members) { + + // make default acelist for each new member + for (String device : getGroupDevices(gid)) { + addAceList(gid, device, makeAceList(members, permission, null)); + } + + // make acelist with specific resources for each new member + addAceByResources(gid, permission, getGroupResources(gid), members); + } + + /** + * API to add acl table by devices + * + * @param gid + * group id + * @param permission + * resource permission of group acl in group + * @param devices + * device list which add into group + */ + public void addAceByDevices(String gid, int permission, + ArrayList devices) { + + // make default acelist for each existing member + for (String device : devices) { + addAceList(gid, device, + makeAceList(getGroupMembers(gid), permission, null)); + } + } + + /** + * API to add acl table by resources + * + * @param gid + * group id + * @param permission + * resource permission of group acl in group + * @param resources + * resource list which add into group + */ + public void addAceByResources(String gid, int permission, + ArrayList> resources) { + + addAceByResources(gid, permission, resources, getGroupMembers(gid)); + } + + /** + * API to remove acl table by group + * + * @param gid + * group id + */ + public void removeAceByGroup(String gid) { + + HashMap condition = new HashMap<>(); + condition.put(Constants.KEYFIELD_GID, gid); + + ArrayList getAceTable = searchAceTableList(condition); + if (getAceTable.isEmpty()) { + return; + } + + removeAceList(getAceTable); + deleteAceRecords(condition); + } + + /** + * API to remove acl table by members + * + * @param members + * member list which remove in group + * @param gid + * group id + */ + public void removeAceByMembers(ArrayList members, String gid) { + for (String member : members) { + removeAceByMember(member, gid); + removeAceByOwner(member, gid); + } + } + + /** + * API to remove acl table by devices + * + * @param devices + * device list which remove in group + * @param gid + * group id + */ + public void removeAceByDevices(ArrayList devices, String gid) { + for (String device : devices) { + removeAceByDevice(device, gid); + } + } + + /** + * API to remove acl table by resources + * + * @param resources + * resource list which remove in group + * @param gid + * group id + */ + public void removeAceByResources( + ArrayList> resources, String gid) { + for (HashMap href : resources) { + removeAceByResource( + href.get(Constants.KEYFIELD_ACE_RESOURCE_HREF).toString(), + gid); + } + } + + /** + * API to get device owner id + * + * @param resources + * resource list which remove in group + * @param gid + * group id + */ + public String getDeviceOwnerId(String di) { + + String aclid = getAclid(di); + + HashMap acl = AclResource.getInstance().getAcl(aclid) + .getInfo(); + + if (acl == null || acl.containsKey(Constants.REQ_OWNER_ID) == false) { + throw new BadRequestException( + "Error while getting device owner id for " + di); + } + + return acl.get(Constants.REQ_OWNER_ID).toString(); + } + + private void removeAceByMember(String uid, String gid) { + HashMap condition = new HashMap<>(); + condition.put(Constants.KEYFIELD_UID, uid); + condition.put(Constants.KEYFIELD_GID, gid); + + ArrayList getAceTable = searchAceTableList(condition); + if (getAceTable.isEmpty()) { + return; + } + + removeAceList(getAceTable); + deleteAceRecords(condition); + } + + private void removeAceByOwner(String oid, String gid) { + HashMap condition = new HashMap<>(); + condition.put(Constants.KEYFIELD_OID, oid); + condition.put(Constants.KEYFIELD_GID, gid); + + ArrayList getAceTable = searchAceTableList(condition); + if (getAceTable.isEmpty()) { + return; + } + + removeAceList(getAceTable); + deleteAceRecords(condition); + } + + private void removeAceByDevice(String di, String gid) { + HashMap condition = new HashMap<>(); + condition.put(Constants.KEYFIELD_DI, di); + condition.put(Constants.KEYFIELD_GID, gid); + + ArrayList getAceTable = searchAceTableList(condition); + if (getAceTable.isEmpty()) { + return; + } + + removeAceList(getAceTable); + deleteAceRecords(condition); + } + + private void removeAceByResource(String href, String gid) { + String removeHref = extractHref(href); + String di = Arrays.asList(href.split("/")).get(2); + HashMap condition = new HashMap<>(); + condition.put(Constants.KEYFIELD_DI, di); + condition.put(Constants.KEYFIELD_GID, gid); + + ArrayList aceTableList = searchAceTableList(condition); + ArrayList> aceMapList = searchAceMapList( + aceTableList); + + for (HashMap aceMap : aceMapList) { + ArrayList> AceResourceList = (ArrayList) aceMap + .get(Constants.KEYFIELD_ACE_RESOURCE); + ListIterator> iterator = AceResourceList + .listIterator(); + while (iterator.hasNext()) { + AceResource res = new AceResource(); + res = mTypeAceResource.convertMaptoObject(iterator.next(), res); + + if (res.getHref().contains(removeHref)) { + iterator.remove(); + break; + } + } + + condition.put(Constants.KEYFIELD_UID, + aceMap.get(Constants.KEYFIELD_ACE_SUBJECT_ID)); + + ArrayList getAceTable = searchAceTableList(condition); + if (getAceTable.isEmpty()) { + return; + } + + if (AceResourceList.isEmpty()) { + removeAceList(getAceTable); + deleteAceRecords(condition); + } else { + updateAce(getAceTable.get(0), aceMap); + } + + } + } + + private ArrayList searchAceTableList( + HashMap condition) { + ArrayList getAceTableList = new ArrayList<>(); + ArrayList> getAceList = AccountDBManager + .getInstance().selectRecord(Constants.ACE_TABLE, condition); + if (getAceList == null) { + return null; + } + for (HashMap getAce : getAceList) { + AceTable getAceTable = new AceTable(); + getAceTable = mTypeAceTable.convertMaptoObject(getAce, getAceTable); + getAceTableList.add(getAceTable); + } + return getAceTableList; + } + + private ArrayList> searchAceMapList( + ArrayList aceList) { + ArrayList> getAceList = new ArrayList<>(); + for (AceTable getAce : aceList) { + getAceList.add(getAce(getAce)); + } + return getAceList; + } + + private void removeAceList(ArrayList aceTableList) { + for (AceTable removeAce : aceTableList) { + removeAce(removeAce); + } + } + + private void updateAce(AceTable updateAce, HashMap ace) { + AclResource.getInstance().updateACE(getAclid(updateAce), + getAceid(updateAce), ace); + } + + private void removeAce(AceTable removeAce) { + AclResource.getInstance().deleteAclACE(getAclid(removeAce), + getAceid(removeAce)); + } + + private HashMap getAce(AceTable getAce) { + return AclResource.getInstance().getAclACE(getAclid(getAce), + getAceid(getAce)); + } + + private String extractHref(String href) { + + List segment = new ArrayList( + Arrays.asList(href.split("/"))); + + // Remove prefix path + segment.remove(0); + segment.remove(0); + segment.remove(0); + + StringBuilder uriPath = new StringBuilder(); + for (String path : segment) { + uriPath.append("/" + path); + } + + return uriPath.toString(); + } + + private String getAceid(AceTable aceTable) { + if (aceTable.getAceid() == null) { + throw new InternalServerErrorException("aceid is invalid"); + } + return aceTable.getAceid(); + } + + private String getAclid(AceTable aceTable) { + String di = null; + if (aceTable.getDi() == null) { + throw new InternalServerErrorException("di is invalid"); + } + di = aceTable.getDi(); + + return getAclid(di); + } + + private String getAclid(String di) { + + HashMap acl = AclResource.getInstance().getAclid(di); + + if (acl == null || !acl.containsKey(Constants.KEYFIELD_ACLID)) { + throw new InternalServerErrorException("aclid is invalid"); + } + + return (String) acl.get(Constants.KEYFIELD_ACLID); + } + + private void addAceList(String gid, String di, + ArrayList> acelist) { + + String aclid = getAclid(di); + String oid = getDeviceOwnerId(di); + + acelist = (ArrayList>) AclResource.getInstance() + .addAclACE(aclid, acelist); + + insertAceRecords(gid, di, oid, acelist); + } + + private void addAceByResources(String gid, int permission, + ArrayList> resources, + ArrayList members) { + + HashMap>> sortedResources = sortResources( + resources); + + for (String di : sortedResources.keySet()) { + + // query aceid using gid-di in GroupACE table + HashMap condition = new HashMap<>(); + condition.put(Constants.KEYFIELD_GID, gid); + condition.put(Constants.KEYFIELD_DI, di); + ArrayList aceTables = searchAceTableList(condition); + + if (aceTables == null || aceTables.isEmpty()) { + // add new ace + addAceList(gid, di, makeAceList(members, permission, + sortedResources.get(di))); + continue; + } else { + // update ace + updateAceByResources(gid, permission, di, sortedResources, + aceTables); + } + } + } + + private void updateAceByResources(String gid, int permission, String di, + HashMap>> sortedResources, + ArrayList aceTables) { + + String aclid = getAclid(aceTables.get(0)); + String aceid = getAceid(aceTables.get(0)); + + @SuppressWarnings("unchecked") + ArrayList> updatedAceResources = (ArrayList>) AclResource + .getInstance().getAclACE(aclid, aceid) + .get(Constants.KEYFIELD_ACE_RESOURCE); + + if (updatedAceResources.isEmpty() == false && updatedAceResources.get(0) + .get(Constants.KEYFIELD_ACE_RESOURCE_HREF).equals("*")) { + + updatedAceResources.clear(); + } + + updatedAceResources.addAll(sortedResources.get(di)); + + for (AceTable acetable : aceTables) { + updateAce(acetable, makeAce(acetable.getUid(), permission, + updatedAceResources)); + } + } + + private ArrayList> makeAceList( + ArrayList members, int permission, + ArrayList> resources) { + + ArrayList> acelist = new ArrayList<>(); + + for (String member : members) { + acelist.add(makeAce(member, permission, resources)); + } + + return acelist; + } + + private HashMap makeAce(String mid, int permission, + ArrayList> resources) { + + if (resources == null) { + resources = new ArrayList<>(); + HashMap resource = new HashMap<>(); + resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, "*"); + resource.put(Constants.KEYFIELD_ACE_RESOURCE_RT, Arrays.asList("")); + resource.put(Constants.KEYFIELD_ACE_RESOURCE_IF, Arrays.asList("")); + + resources.add(resource); + } + + HashMap newAce = new HashMap<>(); + newAce.put(Constants.KEYFIELD_ACE_SUBJECT_ID, mid); + newAce.put(Constants.KEYFIELD_ACE_SUBJECT_TYPE, 1); + newAce.put(Constants.KEYFIELD_ACE_PERMISSION, permission); + newAce.put(Constants.KEYFIELD_ACE_RESOURCE, resources); + + return newAce; + } + + private void insertAceRecords(String gid, String di, String oid, + ArrayList> acelist) { + + for (HashMap ace : acelist) { + + AceTable aceTable = new AceTable(); + aceTable.setAceid(ace.get(Constants.KEYFIELD_ACE_ID).toString()); + aceTable.setUid( + ace.get(Constants.KEYFIELD_ACE_SUBJECT_ID).toString()); + aceTable.setGid(gid); + aceTable.setDi(di); + aceTable.setOid(oid); + + AccountDBManager.getInstance().insertAndReplaceRecord( + Constants.ACE_TABLE, + mTypeAceTable.convertObjectToMap(aceTable)); + } + } + + private void deleteAceRecords(HashMap condition) { + + AccountDBManager.getInstance().deleteRecord(Constants.ACE_TABLE, + condition); + } + + private ArrayList getGroupMembers(String gid) { + + GroupTable groupTable = GroupManager.getInstance().getGroupTable(gid); + ArrayList members = new ArrayList<>(); + + if (groupTable.getMembers() != null) { + members = groupTable.getMembers(); + } + return members; + } + + private ArrayList getGroupDevices(String gid) { + + GroupTable groupTable = GroupManager.getInstance().getGroupTable(gid); + ArrayList devices = new ArrayList<>(); + + if (groupTable.getDevices() != null) { + devices = groupTable.getDevices(); + } + return devices; + } + + private ArrayList> getGroupResources(String gid) { + + ArrayList> resources = new ArrayList<>(); + GroupTable groupTable = GroupManager.getInstance().getGroupTable(gid); + + if (groupTable.getResources() == null) { + return resources; + } + + for (Object resource : groupTable.getResources()) { + + @SuppressWarnings("unchecked") + HashMap resourceInfo = (HashMap) resource; + resources.add(resourceInfo); + } + + return resources; + } + + // classify resources in group according to device id + private HashMap>> sortResources( + ArrayList> resources) { + + HashMap>> sortedResources = new HashMap<>(); + + for (HashMap resource : resources) { + + HashMap resourceInfo = new HashMap<>(resource); + + String href = resourceInfo.get(Constants.KEYFIELD_ACE_RESOURCE_HREF) + .toString(); + String di = Arrays.asList(href.split("/")).get(2); + href = extractHref(href); + + resourceInfo.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, href); + + if (sortedResources.containsKey(di) == false) { + sortedResources.put(di, new ArrayList<>()); + } + + sortedResources.get(di).add(resourceInfo); + } + + return sortedResources; + } +} diff --git a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupAclManagerTest.java b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupAclManagerTest.java new file mode 100644 index 0000000..abc52cc --- /dev/null +++ b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupAclManagerTest.java @@ -0,0 +1,878 @@ +package org.iotivity.cloud.accountserver.resources.acl.group; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +import org.iotivity.cloud.accountserver.Constants; +import org.iotivity.cloud.accountserver.db.AccountDBManager; +import org.iotivity.cloud.accountserver.db.GroupTable; +import org.iotivity.cloud.accountserver.db.MongoDB; +import org.iotivity.cloud.accountserver.resources.acl.id.AclResource; +import org.iotivity.cloud.accountserver.util.TypeCastingManager; +import org.iotivity.cloud.util.Log; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class GroupAclManagerTest { + private String mGid1 = "g1"; + private String mGid2 = "g2"; + private String mDi1 = "d1"; + private String mDi2 = "d2"; + private String mUid1 = "u1"; + private String mUid2 = "u2"; + private String mUid3 = "u3"; + + private HashMap mResourceLight1ofD1 = new HashMap<>(); + private HashMap mResourceLight2ofD1 = new HashMap<>(); + private HashMap mResourceLight1ofD2 = new HashMap<>(); + private HashMap mResourceLight2ofD2 = new HashMap<>(); + private HashMap mResourceAll = new HashMap<>(); + private int mPermission = 15; + + public GroupAclManager mGroupAclManager = GroupAclManager + .getInstance(); + + @Before + public void setUp() throws Exception { + AclResource.getInstance().createAcl(mUid1, mDi1); + AclResource.getInstance().createAcl(mUid2, mDi2); + + mResourceLight1ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + "/di/d1/a/light/1"); + mResourceLight1ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_RT, + Arrays.asList("core.light")); + mResourceLight1ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_IF, + Arrays.asList("oic.if.baseline")); + + mResourceLight2ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + "/di/d1/a/light/2"); + mResourceLight2ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_RT, + Arrays.asList("core.light")); + mResourceLight2ofD1.put(Constants.KEYFIELD_ACE_RESOURCE_IF, + Arrays.asList("oic.if.baseline")); + + mResourceLight1ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + "/di/d2/a/light/1"); + mResourceLight1ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_RT, + Arrays.asList("core.light")); + mResourceLight1ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_IF, + Arrays.asList("oic.if.baseline")); + + mResourceLight2ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + "/di/d2/a/light/2"); + mResourceLight2ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_RT, + Arrays.asList("core.light")); + mResourceLight2ofD2.put(Constants.KEYFIELD_ACE_RESOURCE_IF, + Arrays.asList("oic.if.baseline")); + + mResourceAll.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, "*"); + mResourceAll.put(Constants.KEYFIELD_ACE_RESOURCE_RT, Arrays.asList("")); + mResourceAll.put(Constants.KEYFIELD_ACE_RESOURCE_IF, Arrays.asList("")); + } + + @After + public void resetAccountDatabase() throws Exception { + AclResource.getInstance().deleteAcl((String) AclResource.getInstance() + .getAclid(mDi1).get(Constants.KEYFIELD_ACLID)); + AclResource.getInstance().deleteAcl((String) AclResource.getInstance() + .getAclid(mDi2).get(Constants.KEYFIELD_ACLID)); + + MongoDB mongoDB = new MongoDB(Constants.DB_NAME); + mongoDB.deleteTable(Constants.GROUP_TABLE); + mongoDB.createTable(Constants.GROUP_TABLE); + mongoDB.deleteTable(Constants.ACL_TABLE); + mongoDB.createTable(Constants.ACL_TABLE); + mongoDB.deleteTable(Constants.ACE_TABLE); + mongoDB.createTable(Constants.ACE_TABLE); + } + + @Test + public void testAddAceByResourcesOnlyOwner() { + Log.v("--------------testAddAceByResourcesOnlyOwner------------"); + + // initialize group info -- + ArrayList members = new ArrayList<>(); + members.add(mUid1); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + setGroupInfo(mGid1, groupInfo); + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD1); + resources.add(mResourceLight2ofD1); + + mGroupAclManager.addAceByResources(mGid1, mPermission, resources); + + // check ACE not updated for d1-u1(device owner) + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition.put(Constants.KEYFIELD_UID, mUid1); + assertNull(getAceId(aceidCondition)); + } + + @Test + public void testAddAceByResources() { + Log.v("--------------testAddAceByResoruces------------"); + + // initialize group info -- + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD1); + resources.add(mResourceLight2ofD1); + + mGroupAclManager.addAceByResources(mGid1, mPermission, resources); + + // check ACE not updated for d1-u1(device owner) + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition1.put(Constants.KEYFIELD_UID, mUid1); + assertNull(getAceId(aceidCondition1)); + + // check ACETable and ACL for d1(light1, light2)-u2 + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid2); + String aceIdg1d1 = getAceId(aceidCondition2); + + HashMap aceCondition2 = new HashMap<>(); + aceCondition2.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1); + + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid1); + assertTrue(checkAceTable(aceCondition2, aceidCondition2)); + + // remove prefix in href + for (HashMap resource : resources) { + resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + extractHref( + resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF) + .toString())); + } + + assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2, resources)); + } + + @Test + public void testAddAceByResourcesNoMembers() { + Log.v("--------------testAddAceByResorucesNoMembers------------"); + setGroupInfo(mGid1, new HashMap<>()); + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD1); + resources.add(mResourceLight2ofD1); + + mGroupAclManager.addAceByResources(mGid1, mPermission, resources); + + // check ACE not updated for d1(light1, light2) + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.KEYFIELD_DI, mDi1); + assertNull(getAceId(aceidCondition)); + } + + @Test + public void testAddAceByDevices() { + Log.v("--------------testAddAceByDevices------------"); + + // initialize group info -- + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + + mGroupAclManager.addAceByDevices(mGid1, mPermission, devices); + + // check ACETable and ACL for d1-u2 + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition.put(Constants.KEYFIELD_UID, mUid2); + String aceIdg1d1 = getAceId(aceidCondition); + + HashMap aceCondition = new HashMap<>(); + aceCondition.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1); + + aceidCondition.clear(); + aceidCondition.put(Constants.KEYFIELD_OID, mUid1); + assertTrue(checkAceTable(aceCondition, aceidCondition)); + + assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2, + Arrays.asList(mResourceAll))); + } + + @Test + public void testAddAceByDevicesOnlyOwner() { + Log.v("--------------testAddAceByDevicesOnlyOwner------------"); + + // initialize group info -- + ArrayList members = new ArrayList<>(); + members.add(mUid1); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + + mGroupAclManager.addAceByDevices(mGid1, mPermission, devices); + + // check ACE not updated for d1-u1(device owner) + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition.put(Constants.KEYFIELD_UID, mUid1); + assertNull(getAceId(aceidCondition)); + } + + @Test + public void testAddAceByDevicesNoMembers() { + Log.v("--------------testAddAceByDevicesNoMembers------------"); + setGroupInfo(mGid1, new HashMap<>()); + + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + + mGroupAclManager.addAceByDevices(mGid1, mPermission, devices); + + // check ACE not updated for d1 + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.KEYFIELD_DI, mDi1); + assertNull(getAceId(aceidCondition)); + } + + @Test + public void testAddAceByMembersWithDevice() { + Log.v("--------------testAddAceByMembersWithDevice------------"); + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList members = new ArrayList<>(); + members.add(mUid2); + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + // check ACETable and ACL for d1-u2 + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition1.put(Constants.KEYFIELD_UID, mUid2); + String aceIdg1d1 = getAceId(aceidCondition1); + + HashMap aceCondition = new HashMap<>(); + aceCondition.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1); + + aceidCondition1.clear(); + aceidCondition1.put(Constants.KEYFIELD_OID, mUid1); + assertTrue(checkAceTable(aceCondition, aceidCondition1)); + + assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2, + Arrays.asList(mResourceAll))); + + // check ACE not updated for d2-u2(device owner) + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi2); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid2); + assertNull(getAceId(aceidCondition2)); + } + + @Test + public void testAddAceByMembersWithDeviceAndResource() { + Log.v("--------------testAddAceByMembersWithDeviceAndResource------------"); + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD1); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList members = new ArrayList<>(); + members.add(mUid2); + members.add(mUid3); + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + // remove prefix in href + ArrayList> resourcesWithExtractedHref = new ArrayList<>( + resources); + for (HashMap resource : resourcesWithExtractedHref) { + resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + extractHref( + resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF) + .toString())); + } + + /* d1(light1) */ + // check ACETable and ACL for d1-u2 + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition1.put(Constants.KEYFIELD_UID, mUid2); + String aceIdg1d1 = getAceId(aceidCondition1); + + HashMap aceCondition1 = new HashMap<>(); + aceCondition1.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1); + + aceidCondition1.clear(); + aceidCondition1.put(Constants.KEYFIELD_OID, mUid1); + assertTrue(checkAceTable(aceCondition1, aceidCondition1)); + assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid2, + resourcesWithExtractedHref)); + + // check ACETable and ACL for d1-u3 + aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition1.put(Constants.KEYFIELD_UID, mUid3); + aceIdg1d1 = getAceId(aceidCondition1); + + aceCondition1.put(Constants.KEYFIELD_ACE_ID, aceIdg1d1); + + aceidCondition1.clear(); + aceidCondition1.put(Constants.KEYFIELD_OID, mUid1); + assertTrue(checkAceTable(aceCondition1, aceidCondition1)); + assertTrue(checkAclTable(mDi1, aceIdg1d1, mUid3, + resourcesWithExtractedHref)); + + /* d2(all resources) */ + // check ACE not updated for d2-u2(device owner) + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi2); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid2); + assertNull(getAceId(aceidCondition2)); + + // check ACETable and ACL for d2-u3 + aceidCondition2.put(Constants.KEYFIELD_UID, mUid3); + String aceIdg1d2 = getAceId(aceidCondition2); + + HashMap aceCondition2 = new HashMap<>(); + aceCondition2.put(Constants.KEYFIELD_ACE_ID, aceIdg1d2); + + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid2); + assertTrue(checkAceTable(aceidCondition2, aceidCondition2)); + assertTrue(checkAclTable(mDi2, aceIdg1d2, mUid3, + Arrays.asList(mResourceAll))); + } + + @Test + public void testAddAceByMembersNoDevice() { + Log.v("--------------testAddAceByMembersNoDevice------------"); + setGroupInfo(mGid1, new HashMap<>()); + + ArrayList members = new ArrayList<>(); + members.add(mUid2); + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + // check ACE not updated + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.KEYFIELD_UID, mUid2); + assertNull(getAceId(aceidCondition)); + } + + @Test + public void testAddAceByMembersOwnerDevice() { + Log.v("--------------testAddAceByMembersOwnerDevice------------"); + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList members = new ArrayList<>(); + members.add(mUid2); + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + // check ACE not updated for d2-u2(device owner) + HashMap aceidCondition = new HashMap<>(); + aceidCondition.put(Constants.REQ_GROUP_ID, mGid1); + aceidCondition.put(Constants.REQ_GROUP_ID, mDi2); + aceidCondition.put(Constants.KEYFIELD_UID, mUid2); + assertNull(getAceId(aceidCondition)); + } + + @Test + public void testRemoveAceByGroup() { + Log.v("--------------testRemoveAceByGroup------------"); + + // initialize group info -- + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + setGroupInfo(mGid1, groupInfo); + setGroupInfo(mGid2, groupInfo); + // -- initialize group info + + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + + mGroupAclManager.addAceByDevices(mGid2, mPermission, devices); + + mGroupAclManager.addAceByDevices(mGid1, mPermission - 11, devices); + + mGroupAclManager.removeAceByGroup(mGid1); + + String aceIdg1d1, aceIdg2d1 = null; + // verify ACE Group1 + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.REQ_GROUP_ID, mGid1); + + // verify AceId is Null Cases Group1 + assertNull(getAceId(aceidCondition1)); + + // verify ACE Device1 in Group2 + HashMap aceCondition2 = new HashMap<>(); + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.REQ_GROUP_ID, mGid2); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi1); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid2); + + aceCondition2.put(Constants.KEYFIELD_ACE_ID, + aceIdg1d1 = getAceId(aceidCondition2)); + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid1); + + // verify AceId is Null Cases Group2 + assertTrue(checkAceTable(aceCondition2, aceidCondition2)); + // verify ACL Device1 Group2 + checkAclTable(mDi1, aceIdg1d1, mUid2, Arrays.asList(mResourceAll)); + + // verify ACE Device1 in Group2 + HashMap aceCondition3 = new HashMap<>(); + HashMap aceidCondition3 = new HashMap<>(); + aceidCondition3.put(Constants.REQ_GROUP_ID, mGid2); + aceidCondition3.put(Constants.KEYFIELD_DI, mDi2); + aceidCondition3.put(Constants.KEYFIELD_UID, mUid1); + + aceCondition3.put(Constants.KEYFIELD_ACE_ID, + aceIdg2d1 = getAceId(aceidCondition3)); + aceidCondition3.clear(); + aceidCondition3.put(Constants.KEYFIELD_OID, mUid2); + + // verify AceId is true Cases Group2 + assertTrue(checkAceTable(aceCondition3, aceidCondition3)); + // verify ACL Device2 Group2 + assertTrue(checkAclTable(mDi2, aceIdg2d1, mUid1, + Arrays.asList(mResourceAll))); + } + + @Test + public void testRemoveAceByMembers() { + Log.v("--------------testRemoveAceByMembers------------"); + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + members.add(mUid3); + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + members.remove(mUid2); + members.remove(mUid3); + mGroupAclManager.removeAceByMembers(members, mGid1); + + String aceIdg1d1 = null; + // verify ACE User1 + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.KEYFIELD_OID, mUid1); + + // verify AceId is Null Cases User1 + assertNull(getAceId(aceidCondition1)); + + // verify ACE User2 + HashMap aceCondition2 = new HashMap<>(); + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid3); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi2); + + aceCondition2.put(Constants.KEYFIELD_ACE_ID, + aceIdg1d1 = getAceId(aceidCondition2)); + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid2); + + // remove prefix in href + for (HashMap resource : resources) { + resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + extractHref( + resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF) + .toString())); + } + // verify AceId True + assertTrue(checkAceTable(aceCondition2, aceidCondition2)); + // verify ACL Device1 Group2 + assertTrue(checkAclTable(mDi2, aceIdg1d1, mUid3, resources)); + } + + @Test + public void testRemoveAceByDevices() { + Log.v("--------------testRemoveAceByDevices------------"); + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + members.add(mUid3); + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + devices.remove(mDi2); + mGroupAclManager.removeAceByDevices(devices, mGid1); + + String aceIdu1d2 = null, aceIdu3d2 = null; + // verify ACE Device1 + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi1); + + // verify AceId is Null Cases User1 + assertNull(getAceId(aceidCondition1)); + + // verify ACE ownerid=User2, subjectuuid=User1 + HashMap aceCondition2 = new HashMap<>(); + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid1); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi2); + + aceCondition2.put(Constants.KEYFIELD_ACE_ID, + aceIdu1d2 = getAceId(aceidCondition2)); + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid2); + + // verify ACE ownerid=User2, subjectuuid=User3 + HashMap aceCondition3 = new HashMap<>(); + HashMap aceidCondition3 = new HashMap<>(); + aceidCondition3.put(Constants.KEYFIELD_UID, mUid3); + aceidCondition3.put(Constants.KEYFIELD_DI, mDi2); + + aceCondition3.put(Constants.KEYFIELD_ACE_ID, + aceIdu3d2 = getAceId(aceidCondition3)); + aceidCondition3.clear(); + aceidCondition3.put(Constants.KEYFIELD_OID, mUid2); + + // remove prefix in href + for (HashMap resource : resources) { + resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + extractHref( + resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF) + .toString())); + } + + // verify AceId ownerid=User2, subjectuuid=User1 True + assertTrue(checkAceTable(aceCondition2, aceidCondition2)); + // verify ACL Device2 User1 + assertTrue(checkAclTable(mDi2, aceIdu1d2, mUid1, + Arrays.asList(mResourceLight1ofD2))); + + // verify AceId ownerid=User2, subjectuuid=User3 True + assertTrue(checkAceTable(aceCondition3, aceidCondition3)); + // verify ACL Device2 User1 + assertTrue(checkAclTable(mDi2, aceIdu3d2, mUid3, + Arrays.asList(mResourceLight1ofD2))); + + } + + @Test + public void testRemoveAceByResourcesDeleteAce() { + Log.v("--------------testRemoveAceByResourcesDeleteAce------------"); + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + mGroupAclManager.removeAceByResources( + new ArrayList<>(Arrays.asList(mResourceLight1ofD2)), mGid1); + + // verify ACE Device2 + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi2); + + // verify AceId is Null Cases User1 + assertNull(getAceId(aceidCondition1)); + + String aceIdu1d2 = null; + // verify ACE ownerid=u1, subjectuuid=u2 Deivce=d1 + HashMap aceCondition2 = new HashMap<>(); + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid2); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi1); + + aceCondition2.put(Constants.KEYFIELD_ACE_ID, + aceIdu1d2 = getAceId(aceidCondition2)); + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid1); + + // verify AceId ownerid=u1, subjectuuid=u2 True + assertTrue(checkAceTable(aceCondition2, aceidCondition2)); + // verify ACL Device2 User1 + assertTrue(checkAclTable(mDi1, aceIdu1d2, mUid2, + Arrays.asList(mResourceAll))); + } + + @Test + public void testRemoveAceByResourcesRemainAce() { + + // initialize group info -- + ArrayList devices = new ArrayList<>(); + devices.add(mDi1); + devices.add(mDi2); + ArrayList members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + + ArrayList> resources = new ArrayList<>(); + resources.add(mResourceLight1ofD2); + resources.add(mResourceLight2ofD2); + + HashMap groupInfo = new HashMap<>(); + groupInfo.put(Constants.KEYFIELD_GROUP_DEVICES, devices); + groupInfo.put(Constants.KEYFIELD_GROUP_RESOURCES, resources); + setGroupInfo(mGid1, groupInfo); + // -- initialize group info + + mGroupAclManager.addAceByMembers(mGid1, mPermission, members); + + mGroupAclManager.removeAceByResources( + new ArrayList<>(Arrays.asList(mResourceLight1ofD2)), mGid1); + + String aceIdu2d1 = null, aceIdu1d2 = null; + // verify ACE ownerid=u1, subjectuuid=u1 Deivce=d2 + HashMap aceCondition1 = new HashMap<>(); + HashMap aceidCondition1 = new HashMap<>(); + aceidCondition1.put(Constants.KEYFIELD_UID, mUid1); + aceidCondition1.put(Constants.KEYFIELD_DI, mDi2); + + aceCondition1.put(Constants.KEYFIELD_ACE_ID, + aceIdu1d2 = getAceId(aceidCondition1)); + aceidCondition1.clear(); + aceidCondition1.put(Constants.KEYFIELD_OID, mUid2); + + // remove prefix in href + for (HashMap resource : resources) { + resource.put(Constants.KEYFIELD_ACE_RESOURCE_HREF, + extractHref( + resource.get(Constants.KEYFIELD_ACE_RESOURCE_HREF) + .toString())); + } + // verify AceId ownerid=u2, subjectuuid=u1 True + assertTrue(checkAceTable(aceCondition1, aceidCondition1)); + // verify ACL Device2 User1 + assertTrue(checkAclTable(mDi2, aceIdu1d2, mUid1, + Arrays.asList(mResourceLight2ofD2))); + + // verify ACE ownerid=u1, subjectuuid=u2 Deivce=d1 + HashMap aceCondition2 = new HashMap<>(); + HashMap aceidCondition2 = new HashMap<>(); + aceidCondition2.put(Constants.KEYFIELD_UID, mUid2); + aceidCondition2.put(Constants.KEYFIELD_DI, mDi1); + + aceCondition2.put(Constants.KEYFIELD_ACE_ID, + aceIdu1d2 = getAceId(aceidCondition2)); + aceidCondition2.clear(); + aceidCondition2.put(Constants.KEYFIELD_OID, mUid1); + + // verify AceId ownerid=u1, subjectuuid=u2 True + assertTrue(checkAceTable(aceCondition2, aceidCondition2)); + // verify ACL Device2 User1 + assertTrue(checkAclTable(mDi1, aceIdu1d2, mUid2, + Arrays.asList(mResourceAll))); + } + + private boolean checkAclTable(String di, String aceid, String subjectUuid, + List> resource) { + Log.v("--------------checkAclTable : " + di + " ------------"); + HashMap getAclist = AclResource.getInstance() + .getAclACE((String) AclResource.getInstance().getAclid(di) + .get(Constants.KEYFIELD_ACLID), aceid); + Log.v("check result : " + getAclist); + if (getAclist.isEmpty()) { + return false; + } + + // check subject uuid + assertTrue(getAclist.containsKey(Constants.KEYFIELD_ACE_SUBJECT_ID)); + assertTrue(getAclist.get(Constants.KEYFIELD_ACE_SUBJECT_ID) + .equals(subjectUuid)); + + // check resource + assertTrue(getAclist.containsKey(Constants.KEYFIELD_ACE_RESOURCE)); + for (HashMap targetRes : resource) { + boolean isbreak = false; + for (HashMap res : (ArrayList>) getAclist + .get(Constants.KEYFIELD_ACE_RESOURCE)) { + assertTrue( + res.containsKey(Constants.KEYFIELD_ACE_RESOURCE_HREF)); + if (res.get(Constants.KEYFIELD_ACE_RESOURCE_HREF).equals( + targetRes.get(Constants.KEYFIELD_ACE_RESOURCE_HREF))) { + isbreak = true; + break; + } + } + if (isbreak) { + continue; + } + return false; + } + return true; + } + + private boolean checkAceTable(HashMap condition, + HashMap key) { + Log.v("--------------checkAceTable : " + condition.toString() + + " ------------"); + HashMap getAce = getAceTable(condition).get(0); + Log.v("check result : " + getAce); + if (getAce.isEmpty()) { + return false; + } + for (String keyelement : key.keySet()) { + assertTrue(getAce.containsKey(keyelement)); + assertTrue(getAce.get(keyelement).equals(key.get(keyelement))); + } + return true; + } + + private String getAceId(HashMap condition) { + + ArrayList> aceTables = AccountDBManager + .getInstance().selectRecord(Constants.ACE_TABLE, condition); + + if (aceTables == null || aceTables.isEmpty()) { + return null; + } + + return (String) aceTables.get(0).get(Constants.KEYFIELD_ACE_ID); + } + + private ArrayList> getAceTable( + HashMap condition) { + return AccountDBManager.getInstance().selectRecord(Constants.ACE_TABLE, + condition); + } + + private String extractHref(String href) { + + List segment = new ArrayList( + Arrays.asList(href.split("/"))); + + // Remove prefix path + segment.remove(0); + segment.remove(0); + segment.remove(0); + + StringBuilder uriPath = new StringBuilder(); + for (String path : segment) { + uriPath.append("/" + path); + } + + return uriPath.toString(); + } + + private void setGroupInfo(String gid, HashMap keyValue) { + + GroupTable groupTable = new GroupTable(); + groupTable.setGid(gid); + for (String key : keyValue.keySet()) { + groupTable.setPropertyValue(key, keyValue.get(key)); + } + + TypeCastingManager typeGroup = new TypeCastingManager(); + AccountDBManager.getInstance().insertAndReplaceRecord( + Constants.GROUP_TABLE, + typeGroup.convertObjectToMap(groupTable)); + } +}