PolicyEntityBuilder implements EntityBuilder { public List<Entity> build(Bundle bundle, BundleType bundleType, Document document) { if (bundle instanceof AnnotatedBundle) { AnnotatedBundle annotatedBundle = (AnnotatedBundle) bundle; Map<String, Policy> map = Optional.ofNullable(bundle.getPolicies()).orElse(Collections.emptyMap()); return buildEntities(map, annotatedBundle, annotatedBundle.getFullBundle(), bundleType, document); } else { return buildEntities(bundle.getPolicies(), null, bundle, bundleType, document); } } @Inject  PolicyEntityBuilder(DocumentTools documentTools, IdGenerator idGenerator, PolicyXMLBuilder policyXMLBuilder); List<Entity> buildEntities(Map<String, ?> policyMap, AnnotatedBundle annotatedBundle, Bundle bundle, BundleType bundleType, Document document); List<Entity> build(Bundle bundle, BundleType bundleType, Document document); @NotNull @Override Integer getOrder(); static void resolvePossibleMissingEncapsulatedAssertionDependencies(Bundle bundle, Element encapsulatedAssertionElement); static final String POLICY; static final String ZERO_GUID; }
@Test void build() { Element policyElement = createElementWithAttributesAndChildren( document, "wsp:Policy", ImmutableMap.of("xmlns:L7p", "http: createSetVariableAssertion(document, "var", "value"), createIncludeAssertionElement(document, "include"), createHardcodedAssertionElement(document, "response") ); Element encassElement = createEncapsulatedAssertionElement(document, TEST_ENCASS, "encass"); encassElement.setAttribute(EncapsulatedAssertionBuilder.ENCASS_NAME, TEST_ENCASS); policyElement.appendChild(encassElement); document.appendChild(policyElement); policy.setPolicyXML(DocumentTools.INSTANCE.elementToString(document.getDocumentElement())); policy.setParentFolder(Folder.ROOT_FOLDER); policy.setGuid("policyGuid"); policy.setId("policyID"); policy.setName(policy.getPath()); Encass encass = new Encass(); encass.setGuid("encass"); encass.setName(TEST_ENCASS); Set<Annotation> annotations = new HashSet<>(); Annotation annotation = new Annotation(AnnotationType.SHARED); annotations.add(annotation); policy.setAnnotations(annotations); bundle.getEncasses().put(TEST_ENCASS, encass); bundle.getPolicies().put("Policy", policy); Policy include = new Policy(); include.setParentFolder(Folder.ROOT_FOLDER); include.setPath("test/policy/include"); include.setName(include.getPath()); include.setId("includeID"); include.setGuid("includeGuid"); annotations = new HashSet<>(); annotation = new Annotation(AnnotationType.SHARED); annotations.add(annotation); include.setAnnotations(annotations); include.setPolicyXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<wsp:Policy xmlns:L7p=\"http: " <wsp:All wsp:Usage=\"Required\">\n" + " <L7p:CommentAssertion>\n" + " <L7p:Comment stringValue=\"Policy Fragment: includedPolicy\"/>\n" + " </L7p:CommentAssertion>\n" + " </wsp:All>\n" + "</wsp:Policy>"); bundle.getPolicies().put("include", include); PolicyEntityBuilder builder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); final List<Entity> entities = builder.build(bundle, BundleType.DEPLOYMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertFalse(entities.isEmpty()); assertEquals(2, entities.size()); entities.stream().collect(toMap(Entity::getId, identity())).forEach((i, e) -> { assertEquals(EntityTypes.POLICY_TYPE, e.getType()); assertNotNull(e.getXml()); assertTrue(e.getMappingProperties().containsKey(MAP_BY)); assertEquals(MappingProperties.PATH, e.getMappingProperties().get(MAP_BY)); assertTrue(e.getMappingProperties().containsKey(MAP_TO)); assertEquals(e.getName(), e.getMappingProperties().get(MAP_TO)); assertTrue("test/policy/include".equals(e.getName()) || "test/policy/path.xml".equals(e.getName())); assertTrue("includeID".equals(e.getId()) || "policyID".equals(e.getId())); }); }
@Test void buildDeploymentBundleWithWrongGuidAndId() { Element policyElement = createElementWithAttributesAndChildren( document, "wsp:Policy", ImmutableMap.of("xmlns:L7p", "http: createSetVariableAssertion(document, "var", "value"), createHardcodedAssertionElement(document, "response") ); Element encassElement = createEncapsulatedAssertionElement(document, TEST_ENCASS, "encass"); encassElement.setAttribute(EncapsulatedAssertionBuilder.ENCASS_NAME, TEST_ENCASS); policyElement.appendChild(encassElement); document.appendChild(policyElement); policy.setPolicyXML(DocumentTools.INSTANCE.elementToString(document.getDocumentElement())); policy.setParentFolder(Folder.ROOT_FOLDER); policy.setGuid("policyGuid"); policy.setId("policyID"); policy.setName(policy.getPath()); Set<Annotation> annotations = new HashSet<>(); Annotation annotation = new Annotation(AnnotationType.BUNDLE_HINTS); annotation.setGuid(""); annotation.setId(""); annotations.add(annotation); annotation = new Annotation(AnnotationType.SHARED); annotations.add(annotation); policy.setAnnotations(annotations); policy.setParentEntityShared(policy.isShared()); Encass encass = new Encass(); encass.setGuid("encassGuid"); encass.setName(TEST_ENCASS); Annotation encassAnnotation = new Annotation(AnnotationType.SHARED); annotations = new HashSet<>(); annotations.add(encassAnnotation); encass.setAnnotations(annotations); encass.setParentEntityShared(encass.isShared()); bundle.getEncasses().put(TEST_ENCASS, encass); bundle.getPolicies().put("Policy", policy); AnnotatedEntity annotatedEntity = encass.getAnnotatedEntity(); annotatedEntity.setEntityName(encass.getName()); AnnotatedBundle annotatedBundle = new AnnotatedBundle(bundle, annotatedEntity, new ProjectInfo("", "", "")); annotatedBundle.putAllEncasses(org.testcontainers.shaded.com.google.common.collect.ImmutableMap.of(TEST_ENCASS, encass)); annotatedBundle.getPolicies().put("Policy", policy); PolicyEntityBuilder builder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); List<Entity> entities = builder.build(annotatedBundle, BundleType.DEPLOYMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertFalse(entities.isEmpty()); assertEquals(1, entities.size()); Entity e = entities.get(0); assertEquals(EntityTypes.POLICY_TYPE, e.getType()); assertEquals(e.getId(), "policyID"); annotations = new HashSet<>(); annotation = new Annotation(AnnotationType.SHARED); annotations.add(annotation); annotation = new Annotation(AnnotationType.BUNDLE_HINTS); annotation.setGuid("wrongGuid"); annotation.setId("wrongId"); annotations.add(annotation); policy.setAnnotations(annotations); policy.setAnnotatedEntity(null); builder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); entities = builder.build(annotatedBundle, BundleType.DEPLOYMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertFalse(entities.isEmpty()); assertEquals(1, entities.size()); e = entities.get(0); assertEquals(EntityTypes.POLICY_TYPE, e.getType()); assertEquals(e.getId(), "policyID"); }