MappingUtils { public static PolicyDto toPolicyDto(byte[] policy) { String id = null; final Document policyDoc = bytesToDocument(policy); final Optional<Node> policySetId = DOMUtils.getNode(policyDoc, XPATH_POLICY_SET_ID); if (policySetId.isPresent()) { id = policySetId.get().getNodeValue(); } else { final Optional<Node> policyId = DOMUtils.getNode(policyDoc, XPATH_POLICY_ID); if (policyId.isPresent()) { id = policyId.get().getNodeValue(); } } if (id == null) { throw new PolicyIdNotFoundException( "Cannot find the PolicyId/PolicySetId in one or more of the submitted policies!"); } else { final PolicyDto dto = new PolicyDto(); dto.setId(id); dto.setPolicy(policy); return dto; } }  static PolicyDto toPolicyDto(byte[] policy); static PolicyDto toPolicyDto(PolicyContentDto policyContentDto);  }
@Test public void testToPolicyDtoByteArray() throws SAXException, IOException { final byte[] bytes = POLICY_STRING.getBytes(DOMUtils.DEFAULT_ENCODING); final PolicyContentDto addPolicyRequestDto = new PolicyContentDto(); addPolicyRequestDto.setPolicy(bytes); final PolicyDto policyDto = MappingUtils.toPolicyDto(bytes); assertEquals(POLICY_ID, policyDto.getId()); assertXMLEqual(POLICY_STRING, new String(policyDto.getPolicy(), DOMUtils.DEFAULT_ENCODING)); }
@Test public void testToPolicyDtoByteArray_Throws_PolicyIdNotFoundException() throws SAXException, IOException { thrown.expect(PolicyIdNotFoundException.class); final byte[] bytes = POLICY_STRING_NO_ID .getBytes(DOMUtils.DEFAULT_ENCODING); final PolicyContentDto addPolicyRequestDto = new PolicyContentDto(); addPolicyRequestDto.setPolicy(bytes); MappingUtils.toPolicyDto(bytes); }
@Test public void testToPolicyDtoPolicyContentDto() throws SAXException, IOException { final byte[] bytes = POLICY_STRING.getBytes(DOMUtils.DEFAULT_ENCODING); final PolicyContentDto addPolicyRequestDto = new PolicyContentDto(); addPolicyRequestDto.setPolicy(bytes); final PolicyDto policyDto = MappingUtils .toPolicyDto(addPolicyRequestDto); assertEquals(POLICY_ID, policyDto.getId()); assertXMLEqual(POLICY_STRING, new String(policyDto.getPolicy(), DOMUtils.DEFAULT_ENCODING)); }
@Test public void testToPolicyDtoPolicyContentDto_Throws_PolicyIdNotFoundException() throws SAXException, IOException { thrown.expect(PolicyIdNotFoundException.class); final byte[] bytes = POLICY_STRING_NO_ID .getBytes(DOMUtils.DEFAULT_ENCODING); final PolicyContentDto addPolicyRequestDto = new PolicyContentDto(); addPolicyRequestDto.setPolicy(bytes); MappingUtils.toPolicyDto(addPolicyRequestDto); }