PolicyEntityBuilder implements EntityBuilder { @VisibleForTesting Entity buildPolicyEntity(Policy policy, AnnotatedBundle annotatedBundle, Bundle bundle, Document document) { String policyName = policy.getName(); String policyNameWithPath = EntityBuilderHelper.getPath(policy.getParentFolder(), policyName); policyNameWithPath = CharacterBlacklistUtil.decodePath(policyNameWithPath); AnnotatedEntity annotatedEntity = annotatedBundle != null ? annotatedBundle.getAnnotatedEntity() : null; boolean isRedeployableBundle = false; if (annotatedEntity != null) { isRedeployableBundle = annotatedEntity.isRedeployable(); policyName = annotatedBundle.applyUniqueName(policyName, BundleType.DEPLOYMENT, policy.isParentEntityShared()); policyNameWithPath = PathUtils.extractPath(policyNameWithPath) + policyName; } PolicyTags policyTags = getPolicyTags(policy, bundle); Element policyDetailElement = createElementWithAttributesAndChildren( document, POLICY_DETAIL, ImmutableMap.of(ATTRIBUTE_ID, policy.getId(), ATTRIBUTE_GUID, policy.getGuid(), ATTRIBUTE_FOLDER_ID, policy.getParentFolder().getId()), createElementWithTextContent(document, NAME, policyName), createElementWithTextContent(document, POLICY_TYPE, policyTags == null ? PolicyType.INCLUDE.getType() : policyTags.type.getType()) ); if (policyTags != null) { Builder<String, Object> builder = ImmutableMap.<String, Object>builder().put(PROPERTY_TAG, policyTags.tag); if (policyTags.subtag != null) { builder.put(PROPERTY_SUBTAG, policyTags.subtag); } buildAndAppendPropertiesElement( builder.build(), document, policyDetailElement ); } Element policyElement = createElementWithAttributes( document, BundleElementNames.POLICY, ImmutableMap.of(ATTRIBUTE_ID, policy.getId(), ATTRIBUTE_GUID, policy.getGuid()) ); policyElement.appendChild(policyDetailElement); Element resourcesElement = document.createElement(RESOURCES); Element resourceSetElement = createElementWithAttribute(document, RESOURCE_SET, PROPERTY_TAG, POLICY); Element resourceElement = createElementWithAttribute(document, RESOURCE, TYPE, POLICY); resourceElement.setTextContent(documentTools.elementToString(policy.getPolicyDocument())); resourceSetElement.appendChild(resourceElement); resourcesElement.appendChild(resourceSetElement); policyElement.appendChild(resourcesElement); Entity entity = EntityBuilderHelper.getEntityWithPathMapping(EntityTypes.POLICY_TYPE, policy.getPath(), policyNameWithPath, policy.getId(), policyElement, policy.isHasRouting(), policy); if (isRedeployableBundle) { entity.setMappingAction(MappingActions.NEW_OR_UPDATE); } else if (policy.isParentEntityShared()) { entity.setMappingAction(MappingActions.NEW_OR_EXISTING); } else { entity.setMappingAction(EntityBuilderHelper.getDefaultEntityMappingAction()); } return entity; } @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 buildPolicyEntityTest() { PolicyEntityBuilder policyEntityBuilder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); Policy policyToBuild = new Policy(); policyToBuild.setPath("/path"); policyToBuild.setName("path"); policyToBuild.setId("policy-id"); policyToBuild.setGuid("policy-guid-123"); Folder parentFolder = new Folder(); parentFolder.setId("folder-id"); policyToBuild.setParentFolder(parentFolder); Entity policyEntity = policyEntityBuilder.buildPolicyEntity(policyToBuild, null, bundle, document); assertEquals(policyToBuild.getId(), policyEntity.getId()); }
@Test void buildPolicyEntityTestPBS() { PolicyEntityBuilder policyEntityBuilder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); Policy policyToBuild = new Policy(); policyToBuild.setPath("my/policy/path.xml"); policyToBuild.setName("path"); policyToBuild.setId("policy-id"); policyToBuild.setGuid("policy-guid-123"); Folder parentFolder = new Folder(); parentFolder.setId("folder-id"); policyToBuild.setParentFolder(parentFolder); PolicyBackedService policyBackedService = new PolicyBackedService(); policyBackedService.setInterfaceName("pbs-interface"); policyBackedService.setOperations(Sets.newHashSet(new PolicyBackedServiceOperation("my-op", policyToBuild.getPath()))); bundle.getPolicyBackedServices().put("pbs", policyBackedService); Entity policyEntity = policyEntityBuilder.buildPolicyEntity(policyToBuild, null, bundle, document); assertEquals(policyToBuild.getId(), policyEntity.getId()); }
@Test void buildPolicyEntityTestGlobal() { PolicyEntityBuilder policyEntityBuilder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); Policy policyToBuild = new Policy(); policyToBuild.setPath("my/policy/global.xml"); policyToBuild.setName("global.xml"); policyToBuild.setId("global-policy-id"); policyToBuild.setGuid("global-policy-guid-123"); policyToBuild.setTag("global-policy"); policyToBuild.setPolicyType(PolicyType.GLOBAL); Folder myFolder = new Folder(); myFolder.setId("myId"); myFolder.setName("my"); myFolder.setParentFolder(Folder.ROOT_FOLDER); Folder parentFolder = new Folder(); parentFolder.setId("folder-id"); parentFolder.setName("policy"); parentFolder.setParentFolder(myFolder); policyToBuild.setParentFolder(parentFolder); Entity policyEntity = policyEntityBuilder.buildPolicyEntity(policyToBuild, null, bundle, document); assertEquals(policyToBuild.getId(), policyEntity.getId()); assertEquals(policyToBuild.getPath(), policyEntity.getName()); assertNotNull(policyEntity.getXml()); Element policyDetail = getSingleChildElement(policyEntity.getXml(), POLICY_DETAIL); assertNotNull(policyDetail); String type = getSingleChildElementTextContent(policyDetail, POLICY_TYPE); assertNotNull(type); assertEquals(PolicyType.GLOBAL.getType(), type); final Map<String, Object> properties = mapPropertiesElements(getSingleChildElement(policyDetail, PROPERTIES), PROPERTIES); assertFalse(properties.isEmpty()); assertEquals("global-policy", properties.get(PropertyConstants.PROPERTY_TAG)); assertNull(properties.get(PropertyConstants.PROPERTY_SUBTAG)); }
@Test void buildPolicyEntityTestInternal() { PolicyEntityBuilder policyEntityBuilder = new PolicyEntityBuilder(DocumentTools.INSTANCE, new IdGenerator(), policyXMLBuilder); Policy policyToBuild = new Policy(); policyToBuild.setPath("my/policy/internal.xml"); policyToBuild.setName("internal.xml"); policyToBuild.setId("internal-policy-id"); policyToBuild.setGuid("internal-policy-guid-123"); policyToBuild.setTag("internal-policy"); policyToBuild.setPolicyType(PolicyType.INTERNAL); Folder myFolder = new Folder(); myFolder.setId("myId"); myFolder.setName("my"); myFolder.setParentFolder(Folder.ROOT_FOLDER); Folder parentFolder = new Folder(); parentFolder.setId("folder-id"); parentFolder.setName("policy"); parentFolder.setParentFolder(myFolder); policyToBuild.setParentFolder(parentFolder); Entity policyEntity = policyEntityBuilder.buildPolicyEntity(policyToBuild, null, bundle, document); assertEquals(policyToBuild.getId(), policyEntity.getId()); assertEquals(policyToBuild.getPath(), policyEntity.getName()); assertNotNull(policyEntity.getXml()); Element policyDetail = getSingleChildElement(policyEntity.getXml(), POLICY_DETAIL); assertNotNull(policyDetail); String type = getSingleChildElementTextContent(policyDetail, POLICY_TYPE); assertNotNull(type); assertEquals(PolicyType.INTERNAL.getType(), type); final Map<String, Object> properties = mapPropertiesElements(getSingleChildElement(policyDetail, PROPERTIES), PROPERTIES); assertFalse(properties.isEmpty()); assertEquals("internal-policy", properties.get(PropertyConstants.PROPERTY_TAG)); assertNull(properties.get(PropertyConstants.PROPERTY_SUBTAG)); }