IncludeAssertionBuilder implements PolicyAssertionBuilder { @Override public void buildAssertionElement(Element includeAssertionElement, PolicyBuilderContext policyBuilderContext) { final Bundle bundle = policyBuilderContext.getBundle(); final AnnotatedBundle annotatedBundle = policyBuilderContext.getAnnotatedBundle(); final Policy policy = policyBuilderContext.getPolicy(); Element policyGuidElement; try { policyGuidElement = getSingleElement(includeAssertionElement, POLICY_GUID); } catch (DocumentParseException e) { throw new EntityBuilderException("Could not find PolicyGuid element in Include Assertion", e); } final String policyPath = policyGuidElement.getAttribute(POLICY_PATH); LOGGER.log(Level.FINE, "Looking for referenced policy include: {0}", policyPath); final AtomicReference<Policy> includedPolicy; if (annotatedBundle != null) { includedPolicy = new AtomicReference<>(annotatedBundle.getPolicies().get(policyPath)); } else { includedPolicy = new AtomicReference<>(bundle.getPolicies().get(policyPath)); } if (includedPolicy.get() != null) { policy.getDependencies().add(includedPolicy.get()); } else { bundle.getDependencies().forEach(b -> { Policy policyFromDependencies = Optional.ofNullable(b.getPolicies().get(policyPath)).orElse(b.getPolicies().get(PathUtils.extractName(policyPath))); if (policyFromDependencies != null) { if (!includedPolicy.compareAndSet(null, policyFromDependencies)) { throw new EntityBuilderException("Found multiple policies in dependency bundles with policy path: " + policyPath); } DependentBundle dependentBundle = b.getDependentBundleFrom(); if (dependentBundle != null && dependentBundle.getType() != null) { if (annotatedBundle != null) { annotatedBundle.addDependentBundle(dependentBundle); } else { bundle.addDependentBundle(dependentBundle); } } } }); final MissingGatewayEntity missingEntity = bundle.getMissingEntities().get(policyPath); if (missingEntity != null && missingEntity.isExcluded()) { LOGGER.log(Level.WARNING, "Resolving the referenced policy {0} as known excluded entity with guid: {1}", new Object[]{policyPath, missingEntity.getGuid()}); includedPolicy.set(new Policy()); includedPolicy.get().setGuid(missingEntity.getGuid()); } } if (includedPolicy.get() == null) { throw new EntityBuilderException("Could not find referenced policy include with path: " + policyPath); } policyGuidElement.setAttribute(STRING_VALUE, includedPolicy.get().getGuid()); policyGuidElement.removeAttribute(POLICY_PATH); }  @Override void buildAssertionElement(Element includeAssertionElement, PolicyBuilderContext policyBuilderContext); @Override String getAssertionTagName();  }
@Test void testPrepareIncludeAssertion() throws DocumentParseException { String policyPath = "my/policy/path.xml"; Policy policy = new Policy(); policy.setGuid("123-abc-567"); bundle.getPolicies().put(policyPath, policy); AnnotatedBundle annotatedBundle = new AnnotatedBundle(bundle, null, null); annotatedBundle.putAllPolicies(bundle.getPolicies()); Element includeAssertionElement = createIncludeAssertionElement(document, policyPath); document.appendChild(includeAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(annotatedBundle); includeAssertionBuilder.buildAssertionElement(includeAssertionElement, policyBuilderContext); Element policyGuidElement = getSingleElement(includeAssertionElement, POLICY_GUID); assertEquals(policy.getGuid(), policyGuidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); assertFalse(policyGuidElement.hasAttribute(IncludeAssertionBuilder.POLICY_PATH)); }
@Test void testPrepareIncludeAssertionEncodedPath() throws DocumentParseException { String policyPath = "my/policy-_¯-¯_/_¯-¯_-path.xml"; Policy policy = new Policy(); policy.setGuid("123-abc-567"); bundle.getPolicies().put(policyPath, policy); AnnotatedBundle annotatedBundle = new AnnotatedBundle(bundle, null, null); annotatedBundle.putAllPolicies(bundle.getPolicies()); Element includeAssertionElement = createIncludeAssertionElement(document, policyPath); document.appendChild(includeAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(annotatedBundle); includeAssertionBuilder.buildAssertionElement(includeAssertionElement, policyBuilderContext); Element policyGuidElement = getSingleElement(includeAssertionElement, POLICY_GUID); assertEquals(policy.getGuid(), policyGuidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); assertFalse(policyGuidElement.hasAttribute(IncludeAssertionBuilder.POLICY_PATH)); }
@Test void testPrepareIncludeAssertionNoPolicyGuid() { String policyPath = "my/policy/path.xml"; Policy policy = new Policy(); policy.setGuid("123-abc-567"); bundle.getPolicies().put(policyPath, policy); Element includeAssertion = document.createElement(INCLUDE); document.appendChild(includeAssertion); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(new AnnotatedBundle(bundle, null, null)); assertThrows(EntityBuilderException.class, () -> includeAssertionBuilder.buildAssertionElement(includeAssertion, policyBuilderContext)); }
@Test void testPrepareIncludeAssertionNoPolicyFound() { String policyPath = "my/policy/path.xml"; Policy policy = new Policy(); policy.setGuid("123-abc-567"); bundle.getPolicies().put(policyPath, policy); Element includeAssertionElement = createIncludeAssertionElement(document, "some/other/path.xml"); document.appendChild(includeAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(new AnnotatedBundle(bundle, null, null)); assertThrows(EntityBuilderException.class, () -> includeAssertionBuilder.buildAssertionElement(includeAssertionElement, policyBuilderContext)); }
@Test void testPrepareIncludeAssertionButPolicyAsMissingEntity() throws DocumentParseException { String policyPath = "folder1/policy1"; Policy policy = new Policy(); policy.setName("policy1"); policy.setGuid("123-abc-567"); policy.setPath("folder1"); MissingGatewayEntity missingGatewayEntity = new MissingGatewayEntity(); missingGatewayEntity.setType("POLICY"); missingGatewayEntity.setId("123456"); missingGatewayEntity.setGuid("123-abc-567"); missingGatewayEntity.setExcluded(true); bundle.getMissingEntities().put(policyPath, missingGatewayEntity); Element includeAssertionElement = createIncludeAssertionElement(document, policyPath); document.appendChild(includeAssertionElement); policyBuilderContext = new PolicyBuilderContext("policy", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(new AnnotatedBundle(bundle, null, null)); includeAssertionBuilder.buildAssertionElement(includeAssertionElement, policyBuilderContext); Element policyGuidElement = getSingleElement(includeAssertionElement, POLICY_GUID); assertEquals(policy.getGuid(), policyGuidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); assertFalse(policyGuidElement.hasAttribute(IncludeAssertionBuilder.POLICY_PATH)); }
@Test void testPrepareIncludeAssertionPolicyInDependentBundle() throws DocumentParseException { String policyPath = "my/policy/path.xml"; Policy policy = new Policy(); policy.setGuid("123-abc-567"); policy.setId("id1"); policy.setName("path.xml"); Bundle dependentBundle = new Bundle(); dependentBundle.getPolicies().put(policyPath, policy); bundle.getDependencies().add(dependentBundle); bundle.getDependencies().add(new Bundle()); Element includeAssertionElement = createIncludeAssertionElement(document, policyPath); document.appendChild(includeAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(new AnnotatedBundle(bundle, null, null)); includeAssertionBuilder.buildAssertionElement(includeAssertionElement, policyBuilderContext); Element policyGuidElement = getSingleElement(includeAssertionElement, POLICY_GUID); assertEquals(policy.getGuid(), policyGuidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); assertFalse(policyGuidElement.hasAttribute(IncludeAssertionBuilder.POLICY_PATH)); }
@Test void testPrepareIncludeAssertionPolicyInMultipleDependentBundle() { String policyPath = "my/policy/path.xml"; Policy policy = new Policy(); policy.setGuid("123-abc-567"); Bundle dependentBundle = new Bundle(); dependentBundle.getPolicies().put(policyPath, policy); bundle.getDependencies().add(dependentBundle); Bundle dependentBundle2 = new Bundle(); dependentBundle2.getPolicies().put(policyPath, policy); bundle.getDependencies().add(dependentBundle2); Element includeAssertionElement = createIncludeAssertionElement(document, policyPath); document.appendChild(includeAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(new AnnotatedBundle(bundle, null, null)); assertThrows(EntityBuilderException.class, () -> includeAssertionBuilder.buildAssertionElement(includeAssertionElement, policyBuilderContext)); }