PolicyParser { public List<PolicyDef> parsePolicies(List<String> policies) { if (isDebugEnabled) { LOG.debug("==> PolicyParser parsePolicies"); } List<PolicyDef> policyDefs = new ArrayList<>(); for (String policy : policies) { PolicyDef policyDef = parsePolicy(policy); if (policyDef != null) { policyDefs.add(policyDef); } } if (isDebugEnabled) { LOG.debug("<== PolicyParser parsePolicies"); LOG.debug(policyDefs.toString()); } return policyDefs; }  List<PolicyDef> parsePolicies(List<String> policies); static final int POLICYNAME; static final int USER_INDEX; static final int USERNAME; static final int USER_AUTHORITIES; static final int GROUP_INDEX; static final int GROUPNAME; static final int GROUP_AUTHORITIES; static final int RESOURCE_INDEX; static final int RESOURCE_TYPE; static final int RESOURCE_NAME; }
@Test public void testParsePoliciesWithAllProperties() { List<String> policies = new ArrayList<>(); policies.add("hivePolicy;;usr1:r,usr2:rw;;grp1:rwu,grp2:u;;entity:*abc,operation:*xyz,type:PII"); Map<String, List<AtlasActionTypes>> groupMap = new HashMap<>(); List<AtlasActionTypes> accessList1 = new ArrayList<>(); accessList1.add(AtlasActionTypes.READ); accessList1.add(AtlasActionTypes.CREATE); accessList1.add(AtlasActionTypes.UPDATE); groupMap.put("grp1", accessList1); List<AtlasActionTypes> accessList2 = new ArrayList<>(); accessList2.add(AtlasActionTypes.UPDATE); groupMap.put("grp2", accessList2); Map<String, List<AtlasActionTypes>> usersMap = new HashMap<>(); List<AtlasActionTypes> usr1AccessList = new ArrayList<>(); usr1AccessList.add(AtlasActionTypes.READ); usersMap.put("usr1", usr1AccessList); List<AtlasActionTypes> usr2AccessList = new ArrayList<>(); usr2AccessList.add(AtlasActionTypes.READ); usr2AccessList.add(AtlasActionTypes.CREATE); usersMap.put("usr2", usr2AccessList); Map<AtlasResourceTypes, List<String>> resourceMap = new HashMap<>(); List<String> resource1List = new ArrayList<>(); resource1List.add("*abc"); resourceMap.put(AtlasResourceTypes.ENTITY, resource1List); List<String> resource2List = new ArrayList<>(); resource2List.add("*xyz"); resourceMap.put(AtlasResourceTypes.OPERATION, resource2List); List<String> resource3List = new ArrayList<>(); resource3List.add("PII"); resourceMap.put(AtlasResourceTypes.TYPE, resource3List); List<PolicyDef> policyDefs = new PolicyParser().parsePolicies(policies); for (PolicyDef def : policyDefs) { assertEquals(def.getPolicyName(), "hivePolicy"); assertEquals(def.getGroups(), groupMap); assertEquals(def.getUsers(), usersMap); assertEquals(def.getResources(), resourceMap); } }
@Test public void testParsePoliciesWithOutUserProperties() { List<String> policies = new ArrayList<>(); policies.add("hivePolicy;;;;grp1:rwu,grp2:u;;entity:*abc,operation:*xyz,type:PII"); Map<String, List<AtlasActionTypes>> groupMap = new HashMap<>(); List<AtlasActionTypes> accessList1 = new ArrayList<>(); accessList1.add(AtlasActionTypes.READ); accessList1.add(AtlasActionTypes.CREATE); accessList1.add(AtlasActionTypes.UPDATE); groupMap.put("grp1", accessList1); List<AtlasActionTypes> accessList2 = new ArrayList<>(); accessList2.add(AtlasActionTypes.UPDATE); groupMap.put("grp2", accessList2); Map<String, List<AtlasActionTypes>> usersMap = new HashMap<>(); Map<AtlasResourceTypes, List<String>> resourceMap = new HashMap<>(); List<String> resource1List = new ArrayList<>(); resource1List.add("*abc"); resourceMap.put(AtlasResourceTypes.ENTITY, resource1List); List<String> resource2List = new ArrayList<>(); resource2List.add("*xyz"); resourceMap.put(AtlasResourceTypes.OPERATION, resource2List); List<String> resource3List = new ArrayList<>(); resource3List.add("PII"); resourceMap.put(AtlasResourceTypes.TYPE, resource3List); List<PolicyDef> policyDefs = new PolicyParser().parsePolicies(policies); for (PolicyDef def : policyDefs) { assertEquals(def.getPolicyName(), "hivePolicy"); assertEquals(def.getGroups(), groupMap); assertEquals(def.getUsers(), usersMap); assertEquals(def.getResources(), resourceMap); } }
@Test public void testParsePoliciesWithOutGroupProperties() { List<String> policies = new ArrayList<>(); policies.add("hivePolicy;;usr1:r,usr2:rw;;;;entity:*abc,operation:*xyz,type:PII"); Map<String, List<AtlasActionTypes>> groupMap = new HashMap<>(); Map<String, List<AtlasActionTypes>> usersMap = new HashMap<>(); List<AtlasActionTypes> usr1AccessList = new ArrayList<>(); usr1AccessList.add(AtlasActionTypes.READ); usersMap.put("usr1", usr1AccessList); List<AtlasActionTypes> usr2AccessList = new ArrayList<>(); usr2AccessList.add(AtlasActionTypes.READ); usr2AccessList.add(AtlasActionTypes.CREATE); usersMap.put("usr2", usr2AccessList); Map<AtlasResourceTypes, List<String>> resourceMap = new HashMap<>(); List<String> resource1List = new ArrayList<>(); resource1List.add("*abc"); resourceMap.put(AtlasResourceTypes.ENTITY, resource1List); List<String> resource2List = new ArrayList<>(); resource2List.add("*xyz"); resourceMap.put(AtlasResourceTypes.OPERATION, resource2List); List<String> resource3List = new ArrayList<>(); resource3List.add("PII"); resourceMap.put(AtlasResourceTypes.TYPE, resource3List); List<PolicyDef> policyDefs = new PolicyParser().parsePolicies(policies); for (PolicyDef def : policyDefs) { assertEquals(def.getPolicyName(), "hivePolicy"); assertEquals(def.getGroups(), groupMap); assertEquals(def.getUsers(), usersMap); assertEquals(def.getResources(), resourceMap); } }