EncapsulatedAssertionBuilder implements PolicyAssertionBuilder { @Override public void buildAssertionElement(Element encapsulatedAssertionElement, PolicyBuilderContext policyBuilderContext) throws DocumentParseException { final Bundle bundle = policyBuilderContext.getBundle(); final AnnotatedBundle annotatedBundle = policyBuilderContext.getAnnotatedBundle(); final Policy policy = policyBuilderContext.getPolicy(); if (encapsulatedAssertionElement.hasAttribute(ENCASS_NAME)) { final String encassName = encapsulatedAssertionElement.getAttribute(ENCASS_NAME); Encass encass = getEncass(bundle, encassName, annotatedBundle); final String guid = findEncassReferencedGuid(policy, encass, encapsulatedAssertionElement, encassName); updateEncapsulatedAssertion(policyBuilderContext, encapsulatedAssertionElement, encass, encassName, guid); } else if (!isNoOpIfConfigMissing(encapsulatedAssertionElement)) { Element guidElement = getSingleChildElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID, true); Element nameElement = getSingleChildElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME, true); throw new EntityBuilderException("No encassName specified for encass in policy: '" + policy.getPath() + "' GUID: '" + (guidElement != null ? guidElement.getAttribute(STRING_VALUE) : null) + "' Name: '" + (nameElement != null ? nameElement.getAttribute(STRING_VALUE) : null) + "'"); } else { LOGGER.log(Level.FINE, "No encassName specified for encass in policy: \"{0}\". Since NoOp is true, this will be treated as a No Op.", policy.getPath()); } }  @Override void buildAssertionElement(Element encapsulatedAssertionElement, PolicyBuilderContext policyBuilderContext); @Override String getAssertionTagName(); static final String ZERO_GUID; }
@Test void testPrepareEncapsulatedAssertion() throws DocumentParseException { String policyPath = "my/policy/path.xml"; Encass encass = new Encass(); encass.setGuid("123"); encass.setName(TEST_ENCASS); encass.setPolicy(policyPath); bundle.getEncasses().put(TEST_ENCASS, encass); Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); document.appendChild(encapsulatedAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME); assertEquals(TEST_ENCASS, nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals(encass.getGuid(), guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); }
@Test void testPrepareEncapsulatedAssertionWithWrongGuidAndGoid() throws DocumentParseException { String policyPath = "my/policy/path.xml"; Encass encass = new Encass(); encass.setGuid("123"); encass.setName(TEST_ENCASS); encass.setPolicy(policyPath); Set<Annotation> annotations = new HashSet<>(); Annotation annotation = new Annotation(AnnotationType.BUNDLE_HINTS); annotation.setGuid(""); annotation.setId(""); annotations.add(annotation); encass.setAnnotations(annotations); bundle.getEncasses().put(TEST_ENCASS, encass); AnnotatedEntity annotatedEntity = encass.getAnnotatedEntity(); AnnotatedBundle annotatedBundle = new AnnotatedBundle(bundle, annotatedEntity, projectInfo); annotatedBundle.putAllEncasses(org.testcontainers.shaded.com.google.common.collect.ImmutableMap.of(TEST_ENCASS, encass)); annotatedBundle.getPolicies().put(policyPath, policy); Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); document.appendChild(encapsulatedAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(annotatedBundle); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME); String name = annotatedBundle.applyUniqueName(TEST_ENCASS, DEPLOYMENT, false); assertEquals(name, nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals(encass.getGuid(), guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); annotatedEntity = encass.getAnnotatedEntity(); annotations = new HashSet<>(); annotations.add(new Annotation(AnnotationType.SHARED)); annotation = new Annotation(AnnotationType.BUNDLE_HINTS); annotation.setGuid("wrongGuid"); annotation.setId("wrongId"); annotations.add(annotation); encass.setAnnotations(annotations); encass.setAnnotatedEntity(null); annotatedBundle = new AnnotatedBundle(bundle, annotatedEntity, projectInfo); annotatedBundle.putAllEncasses(org.testcontainers.shaded.com.google.common.collect.ImmutableMap.of(TEST_ENCASS, encass)); annotatedBundle.getPolicies().put(policyPath, policy); encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(annotatedBundle); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals(encass.getGuid(), guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); }
@Test void testPrepareEncapsulatedAssertionEncoded() throws DocumentParseException { String policyPath = "my/policy-_¯-¯_/_¯-¯_-path.xml"; Encass encass = new Encass(); encass.setGuid("123"); encass.setName(TEST_ENCASS); encass.setPolicy(policyPath); bundle.getEncasses().put(TEST_ENCASS, encass); Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); document.appendChild(encapsulatedAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME); assertEquals(TEST_ENCASS, nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals(encass.getGuid(), guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); }
@Test void testPrepareEncapsulatedAssertionEncassInDependencyBundle() throws DocumentParseException { String policyPath = "my/policy/path.xml"; Encass encass = new Encass(); encass.setGuid("123"); encass.setId("id1"); encass.setPolicy(policyPath); encass.setName(TEST_ENCASS); Bundle dependencyBundle = new Bundle(); dependencyBundle.getEncasses().put(TEST_ENCASS, encass); bundle.getDependencies().add(dependencyBundle); Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); document.appendChild(encapsulatedAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME); assertEquals(TEST_ENCASS, nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals(encass.getGuid(), guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); }
@Test void testPrepareEncapsulatedAssertionMissingEncass() { Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); document.appendChild(encapsulatedAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); EntityBuilderException exception = assertThrows(EntityBuilderException.class, () -> encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext)); assertTrue(exception.getMessage().contains(TEST_ENCASS)); }
@Test void testPrepareEncapsulatedAssertionMissingEncassWithNoOp() throws DocumentParseException { Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document); document.appendChild(encapsulatedAssertionElement); Element noOpElement = document.createElement(NO_OP_IF_CONFIG_MISSING); encapsulatedAssertionElement.appendChild(noOpElement); noOpElement.setAttribute(EncapsulatedAssertionBuilder.BOOLEAN_VALUE, "true"); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME); assertEquals(TEST_ENCASS, nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals(PolicyEntityBuilder.ZERO_GUID, guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); }
@Test void testPrepareEncapsulatedAssertionMissingPolicyPath() { Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document, "my-encass", "ad620794-a27f-4d94-85b7-669ba838367b"); document.appendChild(encapsulatedAssertionElement); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); EntityBuilderException exception = assertThrows(EntityBuilderException.class, () -> encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext)); assertTrue(exception.getMessage().contains(policy.getPath())); }
@Test void testPrepareEncapsulatedAssertionMissingPolicyPathWithNoOp() throws DocumentParseException { Element encapsulatedAssertionElement = createEncapsulatedAssertionElement(document, "my-encass", "ad620794-a27f-4d94-85b7-669ba838367b"); document.appendChild(encapsulatedAssertionElement); Element noOpElement = document.createElement(NO_OP_IF_CONFIG_MISSING); encapsulatedAssertionElement.appendChild(noOpElement); noOpElement.setAttribute(EncapsulatedAssertionBuilder.BOOLEAN_VALUE, "true"); policyBuilderContext = new PolicyBuilderContext("path.xml", document, bundle, new IdGenerator()); policyBuilderContext.withPolicy(policy); policyBuilderContext.withAnnotatedBundle(null); encapsulatedAssertionBuilder.buildAssertionElement(encapsulatedAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_NAME); assertEquals("my-encass", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element guidElement = getSingleElement(encapsulatedAssertionElement, ENCAPSULATED_ASSERTION_CONFIG_GUID); assertEquals("ad620794-a27f-4d94-85b7-669ba838367b", guidElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); }