@Test public void testGetPoliciesCombinedAsPolicySet() throws UnsupportedEncodingException { final String policyId = "C2S*"; final String wildcard = "*"; final String policyCombiningAlgId = "permit-overrides"; final String policyCombiningAlgIdReturned = "urn:oasis:names:tc:xacml:1.0:policy-combining-algorithm:permit-overrides"; final PolicyServiceImpl spy = spy(sut); final PolicyContainerDto getPolicyResponseContainerDto = new PolicyContainerDto(); final PolicyDto p1 = new PolicyDto(); p1.setId(POLICY_ID1); p1.setPolicy(POLICY_STRING1.getBytes("UTF-8")); p1.setValid(true); final PolicyDto p2 = new PolicyDto(); p2.setId(POLICY_ID2); p2.setPolicy(POLICY_STRING2.getBytes("UTF-8")); p1.setValid(true); getPolicyResponseContainerDto.setPolicies(Arrays.asList(p1, p2)); doReturn(getPolicyResponseContainerDto).when(spy).getPolicies(policyId, wildcard); when( policyCombiningAlgIdValidator .validateAndReturn(policyCombiningAlgId)).thenReturn( policyCombiningAlgIdReturned); final PolicyDto response = spy.getPoliciesCombinedAsPolicySet(policyId, wildcard, null, policyCombiningAlgId); assertNotNull(response.getPolicy()); assertTrue(StringUtils.hasText(getNode( bytesToDocument(response.getPolicy()), XACMLXPath.XPATH_POLICY_SET_ID).get().getNodeValue())); assertEquals( policyCombiningAlgIdReturned, getNode(bytesToDocument(response.getPolicy()), XACMLXPath.XPATH_POLICY_SET_POLICY_COMBINING_ALG_ID) .get().getNodeValue()); }
@Override @Transactional(readOnly = true) public PolicyDto getPoliciesCombinedAsPolicySet(String policyId, String wildcard, String policySetId, String policyCombiningAlgId) { policyCombiningAlgId = policyCombiningAlgIdValidator .validateAndReturn(policyCombiningAlgId); final PolicyContainerDto policies = getPolicies(policyId, wildcard); final Document policySet = initPolicySetTemplate(); if (!StringUtils.hasText(policySetId)) { policySetId = UUID.randomUUID().toString(); } DOMUtils.getNode(policySet, XPATH_POLICY_SET_ID).get() .setNodeValue(policySetId); DOMUtils.getNode(policySet, XPATH_POLICY_SET_POLICY_COMBINING_ALG_ID) .get().setNodeValue(policyCombiningAlgId); policies.getPolicies() .stream() .map(PolicyDto::getPolicy) .map(DOMUtils::bytesToDocument) .map(Document::getDocumentElement) .map(policy -> policy.cloneNode(true)) .map(clonedPolicy -> policySet.importNode(clonedPolicy, true)) .forEach( importedPolicy -> policySet.getDocumentElement() .appendChild(importedPolicy)); final PolicyDto response = new PolicyDto(); response.setId(policySetId); response.setPolicy(DOMUtils.documentToBytes(policySet)); response.setValid(PolicyValidationUtils.validate(response.getPolicy())); debug(() -> new String(response.getPolicy())); return response; }
PolicyServiceImpl implements PolicyService { @Override @Transactional(readOnly = true) public PolicyDto getPoliciesCombinedAsPolicySet(String policyId, String wildcard, String policySetId, String policyCombiningAlgId) { policyCombiningAlgId = policyCombiningAlgIdValidator .validateAndReturn(policyCombiningAlgId); final PolicyContainerDto policies = getPolicies(policyId, wildcard); final Document policySet = initPolicySetTemplate(); if (!StringUtils.hasText(policySetId)) { policySetId = UUID.randomUUID().toString(); } DOMUtils.getNode(policySet, XPATH_POLICY_SET_ID).get() .setNodeValue(policySetId); DOMUtils.getNode(policySet, XPATH_POLICY_SET_POLICY_COMBINING_ALG_ID) .get().setNodeValue(policyCombiningAlgId); policies.getPolicies() .stream() .map(PolicyDto::getPolicy) .map(DOMUtils::bytesToDocument) .map(Document::getDocumentElement) .map(policy -> policy.cloneNode(true)) .map(clonedPolicy -> policySet.importNode(clonedPolicy, true)) .forEach( importedPolicy -> policySet.getDocumentElement() .appendChild(importedPolicy)); final PolicyDto response = new PolicyDto(); response.setId(policySetId); response.setPolicy(DOMUtils.documentToBytes(policySet)); response.setValid(PolicyValidationUtils.validate(response.getPolicy())); debug(() -> new String(response.getPolicy())); return response; } }
PolicyServiceImpl implements PolicyService { @Override @Transactional(readOnly = true) public PolicyDto getPoliciesCombinedAsPolicySet(String policyId, String wildcard, String policySetId, String policyCombiningAlgId) { policyCombiningAlgId = policyCombiningAlgIdValidator .validateAndReturn(policyCombiningAlgId); final PolicyContainerDto policies = getPolicies(policyId, wildcard); final Document policySet = initPolicySetTemplate(); if (!StringUtils.hasText(policySetId)) { policySetId = UUID.randomUUID().toString(); } DOMUtils.getNode(policySet, XPATH_POLICY_SET_ID).get() .setNodeValue(policySetId); DOMUtils.getNode(policySet, XPATH_POLICY_SET_POLICY_COMBINING_ALG_ID) .get().setNodeValue(policyCombiningAlgId); policies.getPolicies() .stream() .map(PolicyDto::getPolicy) .map(DOMUtils::bytesToDocument) .map(Document::getDocumentElement) .map(policy -> policy.cloneNode(true)) .map(clonedPolicy -> policySet.importNode(clonedPolicy, true)) .forEach( importedPolicy -> policySet.getDocumentElement() .appendChild(importedPolicy)); final PolicyDto response = new PolicyDto(); response.setId(policySetId); response.setPolicy(DOMUtils.documentToBytes(policySet)); response.setValid(PolicyValidationUtils.validate(response.getPolicy())); debug(() -> new String(response.getPolicy())); return response; }  }
PolicyServiceImpl implements PolicyService { @Override @Transactional(readOnly = true) public PolicyDto getPoliciesCombinedAsPolicySet(String policyId, String wildcard, String policySetId, String policyCombiningAlgId) { policyCombiningAlgId = policyCombiningAlgIdValidator .validateAndReturn(policyCombiningAlgId); final PolicyContainerDto policies = getPolicies(policyId, wildcard); final Document policySet = initPolicySetTemplate(); if (!StringUtils.hasText(policySetId)) { policySetId = UUID.randomUUID().toString(); } DOMUtils.getNode(policySet, XPATH_POLICY_SET_ID).get() .setNodeValue(policySetId); DOMUtils.getNode(policySet, XPATH_POLICY_SET_POLICY_COMBINING_ALG_ID) .get().setNodeValue(policyCombiningAlgId); policies.getPolicies() .stream() .map(PolicyDto::getPolicy) .map(DOMUtils::bytesToDocument) .map(Document::getDocumentElement) .map(policy -> policy.cloneNode(true)) .map(clonedPolicy -> policySet.importNode(clonedPolicy, true)) .forEach( importedPolicy -> policySet.getDocumentElement() .appendChild(importedPolicy)); final PolicyDto response = new PolicyDto(); response.setId(policySetId); response.setPolicy(DOMUtils.documentToBytes(policySet)); response.setValid(PolicyValidationUtils.validate(response.getPolicy())); debug(() -> new String(response.getPolicy())); return response; }  @Override @Transactional PolicyMetadataContainerDto addPolicies(
			PolicyContentContainerDto policyContentContainerDto, boolean force); @Override @Transactional void deletePolicy(String policyId); @Override @Transactional(readOnly = true) PolicyMetadataContainerDto getAllPolicyMetadata(); @Override @Transactional(readOnly = true) PolicyContainerDto getPolicies(String policyId, String wildcard); @Override @Transactional(readOnly = true) PolicyDto getPoliciesCombinedAsPolicySet(String policyId,
			String wildcard, String policySetId, String policyCombiningAlgId); @Override Map<String, String> getPolicyCombiningAlgIds(); @Override @Transactional PolicyMetadataDto updatePolicy(
			PolicyContentDto updatePolicyRequestDto, String policyId); }
PolicyServiceImpl implements PolicyService { @Override @Transactional(readOnly = true) public PolicyDto getPoliciesCombinedAsPolicySet(String policyId, String wildcard, String policySetId, String policyCombiningAlgId) { policyCombiningAlgId = policyCombiningAlgIdValidator .validateAndReturn(policyCombiningAlgId); final PolicyContainerDto policies = getPolicies(policyId, wildcard); final Document policySet = initPolicySetTemplate(); if (!StringUtils.hasText(policySetId)) { policySetId = UUID.randomUUID().toString(); } DOMUtils.getNode(policySet, XPATH_POLICY_SET_ID).get() .setNodeValue(policySetId); DOMUtils.getNode(policySet, XPATH_POLICY_SET_POLICY_COMBINING_ALG_ID) .get().setNodeValue(policyCombiningAlgId); policies.getPolicies() .stream() .map(PolicyDto::getPolicy) .map(DOMUtils::bytesToDocument) .map(Document::getDocumentElement) .map(policy -> policy.cloneNode(true)) .map(clonedPolicy -> policySet.importNode(clonedPolicy, true)) .forEach( importedPolicy -> policySet.getDocumentElement() .appendChild(importedPolicy)); final PolicyDto response = new PolicyDto(); response.setId(policySetId); response.setPolicy(DOMUtils.documentToBytes(policySet)); response.setValid(PolicyValidationUtils.validate(response.getPolicy())); debug(() -> new String(response.getPolicy())); return response; }  @Override @Transactional PolicyMetadataContainerDto addPolicies(
			PolicyContentContainerDto policyContentContainerDto, boolean force); @Override @Transactional void deletePolicy(String policyId); @Override @Transactional(readOnly = true) PolicyMetadataContainerDto getAllPolicyMetadata(); @Override @Transactional(readOnly = true) PolicyContainerDto getPolicies(String policyId, String wildcard); @Override @Transactional(readOnly = true) PolicyDto getPoliciesCombinedAsPolicySet(String policyId,
			String wildcard, String policySetId, String policyCombiningAlgId); @Override Map<String, String> getPolicyCombiningAlgIds(); @Override @Transactional PolicyMetadataDto updatePolicy(
			PolicyContentDto updatePolicyRequestDto, String policyId);  }