XMLPolicyConverter implements PolicyConverter { @Override @SuppressWarnings("squid:S2095") public InputStream convertFromPolicyElement(Element policy) { final PipedOutputStream out = new PipedOutputStream(); new Thread(() -> documentTools.printXML(policy, out, false)).start(); try { return new PipedInputStream(out); } catch (IOException e) { throw new PolicyConverterException("Unable to create stream for converting policy xml.", e); } } @Inject  XMLPolicyConverter(DocumentTools documentTools); @Override String getPolicyTypeExtension(); @Override String getPolicyXML(Policy policy, String policyString); @Override boolean canConvert(String name, Element policy); @Override @SuppressWarnings("squid:S2095") // Should not close the input stream since we are returning it. It should be auto closed by the caller InputStream convertFromPolicyElement(Element policy); static final String EXTENSION; }
@Test void convertFromPolicyElement() throws IOException { doAnswer(invocation -> { IOUtils.write("out", invocation.getArgument(1), StandardCharsets.UTF_8); ((OutputStream) invocation.getArgument(1)).close(); return null; }).when(documentFileUtils).printXML(nullable(Element.class), any(OutputStream.class), any(Boolean.class)); InputStream xmlStream = xmlPolicyConverter.convertFromPolicyElement(null); String xmlString = IOUtils.toString(xmlStream, StandardCharsets.UTF_8); assertEquals("out", xmlString); }