PolicyLoader implements BundleEntityLoader { @Override public void load(Bundle bundle, Element element) { final Element policyElement = getSingleChildElement(getSingleChildElement(element, RESOURCE), POLICY); final String guid = policyElement.getAttribute(ATTRIBUTE_GUID); final Element policyDetails = getSingleChildElement(policyElement, POLICY_DETAIL); final String policyType = getSingleChildElementTextContent(policyDetails, POLICY_TYPE); final Map<String, Object> policyDetailProperties = mapPropertiesElements(getSingleChildElement(policyDetails, PROPERTIES, true), PROPERTIES); final String policyTag = (String) policyDetailProperties.get(PROPERTY_TAG); boolean hasRouting = false; if (policyDetailProperties.get(PROPERTY_HAS_ROUTING) != null) { hasRouting = BooleanUtils.toBoolean(policyDetailProperties.get(PROPERTY_HAS_ROUTING).toString()); } if (!PolicyType.isValidType(policyType, policyTag)) { LOGGER.log(Level.WARNING, () -> { if (policyTag != null) { return String.format("Skipping unsupported PolicyType: %s, with tag %s", policyType, policyTag); } return String.format("Skipping unsupported PolicyType: %s", policyType); }); return; } final String id = policyDetails.getAttribute(ATTRIBUTE_ID); final String folderId = policyDetails.getAttribute(ATTRIBUTE_FOLDER_ID); Element nameElement = getSingleChildElement(policyDetails, NAME); final String name = CharacterBlacklistUtil.filterAndReplace(nameElement.getTextContent()); final Element resources = getSingleChildElement(policyElement, RESOURCES); final Element resourceSet = getSingleChildElement(resources, RESOURCE_SET); final Element resource = getSingleChildElement(resourceSet, RESOURCE); final String policyString = resource.getTextContent(); final PolicyType type = PolicyType.fromType(policyType); final String policySubtag = (String) policyDetailProperties.get(PROPERTY_SUBTAG); Folder parentFolder = getFolder(bundle, folderId); Policy policy = type.createPolicyObject(); policy.setPath(getPath(parentFolder, name)); policy.setName(name); policy.setParentFolder(parentFolder); policy.setGuid(guid); policy.setId(id); policy.setTag(policyTag); policy.setSubtag(policySubtag); policy.setPolicyType(type); policy.setPolicyDocument(policyElement); policy.setPolicyXML(policyString); policy.setHasRouting(hasRouting); Map<String, Policy> bundlePolicies = bundle.getPolicies(); if (bundle.getLoadingMode() == VALIDATE) { Policy policyWithSameName = bundle.getPolicies().values().stream().filter(p -> Objects.equals(policy.getName(), p.getName())).findAny().orElse(null); if (policyWithSameName != null) { throw new BundleLoadException("Duplicate policies found with name '" + policyWithSameName.getName() + "': " + Joiner.on(", ").join(Arrays.asList(policyWithSameName.getPath(), policy.getPath()))); } } if (bundlePolicies.containsKey(policy.getPath())) { String duplicatePathName = handleDuplicatePathName(bundlePolicies, policy); policy.setName(duplicatePathName.substring(duplicatePathName.lastIndexOf('/') + 1)); policy.setPath(duplicatePathName); } bundle.getPolicies().put(policy.getPath(), policy); if (type == GLOBAL) { bundle.getEntities(GlobalPolicy.class).put(policy.getPath(), (GlobalPolicy) policy); } else if (type == INTERNAL && firstNonNull(policyTag, EMPTY).startsWith("audit")) { bundle.getEntities(AuditPolicy.class).put(policy.getPath(), (AuditPolicy) policy); } }  @Override void load(Bundle bundle, Element element); @Override String getEntityType();  }
@Test void test() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); f1.setName(TEST_FOLDER_1); f1.setPath(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.SERVICE_OPERATION.getType(), TEST_FOLDER_1, TEST_POLICY_XML, null, false)); assertFalse(bundle.getPolicies().isEmpty()); assertEquals(1, bundle.getPolicies().size()); String path = PathUtils.unixPath(TEST_FOLDER_1, TEST_POLICY_NAME); Policy policy = bundle.getPolicies().get(path); assertNotNull(policy); assertEquals(TEST_POLICY_ID, policy.getId()); assertEquals(TEST_POLICY_NAME, policy.getName()); assertEquals(path, policy.getPath()); assertEquals(TEST_GUID, policy.getGuid()); assertEquals(f1, policy.getParentFolder()); assertFalse(policy.isHasRouting()); }
@Test void testUnsupportedPolicyType() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, "Unsupported", TEST_FOLDER_1 , TEST_POLICY_XML, null, false)); assertTrue(bundle.getPolicies().isEmpty()); }
@Test void testMissingFolder() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); assertThrows(BundleLoadException.class, () -> loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.SERVICE_OPERATION.getType(), TEST_FOLDER_1, TEST_POLICY_XML, null, false))); }
@Test void testRepeatedFolders() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); f1.setName(TEST_FOLDER_1); f1.setPath(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); bundle.getFolders().put(TEST_FOLDER_1 + "_1", f1); assertThrows(BundleLoadException.class, () -> loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.SERVICE_OPERATION.getType(), TEST_FOLDER_1, TEST_POLICY_XML, null, false))); }
@Test void testGlobalPolicy() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); f1.setName(TEST_FOLDER_1); f1.setPath(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.GLOBAL.getType(), TEST_FOLDER_1, TEST_POLICY_XML, null, false)); assertFalse(bundle.getPolicies().isEmpty()); assertEquals(1, bundle.getPolicies().size()); String path = PathUtils.unixPath(TEST_FOLDER_1, TEST_POLICY_NAME); Policy policy = bundle.getPolicies().get(path); assertNotNull(policy); assertEquals(TEST_POLICY_ID, policy.getId()); assertEquals(TEST_POLICY_NAME, policy.getName()); assertEquals(path, policy.getPath()); assertEquals(TEST_GUID, policy.getGuid()); assertEquals(f1, policy.getParentFolder()); assertTrue(policy instanceof GlobalPolicy); assertTrue(bundle.getEntities(GlobalPolicy.class).values().contains(policy)); }
@Test void testAuditPolicy() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); f1.setName(TEST_FOLDER_1); f1.setPath(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.INTERNAL.getType(), TEST_FOLDER_1, TEST_POLICY_XML, "audit-lookup", false)); assertFalse(bundle.getPolicies().isEmpty()); assertEquals(1, bundle.getPolicies().size()); String path = PathUtils.unixPath(TEST_FOLDER_1, TEST_POLICY_NAME); Policy policy = bundle.getPolicies().get(path); assertNotNull(policy); assertEquals(TEST_POLICY_ID, policy.getId()); assertEquals(TEST_POLICY_NAME, policy.getName()); assertEquals(path, policy.getPath()); assertEquals(TEST_GUID, policy.getGuid()); assertEquals(f1, policy.getParentFolder()); assertTrue(policy instanceof AuditPolicy); assertTrue(bundle.getEntities(AuditPolicy.class).values().contains(policy)); }
@Test void testAuditPolicyWithInvalidTags() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); f1.setName(TEST_FOLDER_1); f1.setPath(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.INTERNAL.getType(), TEST_FOLDER_1, TEST_POLICY_XML, "test", false)); assertTrue(bundle.getPolicies().isEmpty()); assertTrue(bundle.getEntities(AuditPolicy.class).isEmpty()); }
@Test void testHasRoutingWithRoutingAssertion() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); f1.setName(TEST_FOLDER_1); f1.setPath(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); loader.load(bundle, createPolicyBundleXml(doc, TEST_POLICY_ID, TEST_POLICY_NAME, PolicyType.SERVICE_OPERATION.getType(), TEST_FOLDER_1, TEST_POLICY_XML_WITH_ROUTING, null, true)); assertFalse(bundle.getPolicies().isEmpty()); assertEquals(1, bundle.getPolicies().size()); String path = PathUtils.unixPath(TEST_FOLDER_1, TEST_POLICY_NAME); Policy policy = bundle.getPolicies().get(path); assertNotNull(policy); assertEquals(TEST_POLICY_ID, policy.getId()); assertEquals(TEST_POLICY_NAME, policy.getName()); assertEquals(path, policy.getPath()); assertEquals(TEST_GUID, policy.getGuid()); assertEquals(f1, policy.getParentFolder()); assertTrue(policy.isHasRouting()); }