TwineArchiveRepairer implements TwineRepairer { @Override public final void repair(final InputStream inputStream, final OutputStream outputStream) { Objects.requireNonNull(inputStream); Objects.requireNonNull(outputStream); try { outputStream.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>".getBytes(StandardCharsets.UTF_8)); outputStream.write("<tw-storiesdata>".getBytes(StandardCharsets.UTF_8)); try (InputStream in = new ByteArrayInputStream(IOUtils.toByteArray(inputStream))) { tidy.parse(in, outputStream); } outputStream.write("</tw-storiesdata>".getBytes(StandardCharsets.UTF_8)); } catch(IOException e) { throw new TwineRepairFailedException("Could not repair input stream", e); } } TwineArchiveRepairer(); @Override final void repair(final InputStream inputStream, final OutputStream outputStream);  }
@Test public void repair_ValidInput_ReturnsValidTwineXml() throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try(InputStream inputStream = getClass().getResourceAsStream("/html/valid-archive.html")) { repairer.repair(inputStream, outputStream); } validate(outputStream); Assert.assertTrue(true); outputStream.close(); }
@Test(expected = NullPointerException.class) public void repair_InputNull_ThrowsNullPointerException() throws Exception { try(OutputStream outputStream = new ByteArrayOutputStream()) { repairer.repair(null, outputStream); } }
@Test(expected = NullPointerException.class) public void repair_OutputNull_ThrowsNullPointerException() throws Exception { try(InputStream inputStream = getClass().getResourceAsStream("/html/valid-archive.html")) { repairer.repair(inputStream, null); } }
@Test(expected = TwineRepairFailedException.class) public void repair_OutputInvalid_ThrowsTwineRepairFailedException() throws Exception { final OutputStream os = Mockito.mock(OutputStream.class); Mockito.doThrow(IOException.class).when(os).write(ArgumentMatchers.any()); try(InputStream inputStream = getClass().getResourceAsStream("/html/valid-archive.html")) { repairer.repair(inputStream, os); } }
@Test(expected = IOException.class) public void repair_InputDoesNotExist_ThrowsIOException() throws Exception { File doesNotExist = new File("/xml/doesNotExist.xml"); OutputStream outputStream = new ByteArrayOutputStream(); try(InputStream inputStream = new FileInputStream(doesNotExist)) { repairer.repair(inputStream, outputStream); } }