TwineStoryEpubTransformer implements Transformer { @Override public final void transform(final TwStorydata story, final OutputStream outputStream) { EpubTransformOptions options = EpubTransformOptions.empty(); if(story.getXtwMetadata() != null) options = EpubTransformOptions.fromXtwMetadata(story.getXtwMetadata()); transform(story, outputStream, options); } @Inject  TwineStoryEpubTransformer(
            final MarkdownProcessor pdProcessor,
            final ResourceEmbedder resourceEmbedder); @Override final void transform(final TwStorydata story, final OutputStream outputStream); @Override final String getExtension(); final void transform(final TwStorydata story,
                                final OutputStream outputStream,
                                final EpubTransformOptions options);  }
@Test(expected = NullPointerException.class) public void convert_NullStory_ThrowsNullPointerException() throws Exception { try(OutputStream out = new ByteArrayOutputStream()) { convertor.transform(null, out); } }
@Test(expected = NullPointerException.class) public void convert_NullOutput_ThrowsNullPointerException() { convertor.transform(new TwStorydata(), null); }
@Test public void convert_ValidStory_WritesEpubFile() throws Exception { when(mockPegDownProcessor.markdownToHtml(any(Node.class))) .thenAnswer(invocation -> "<p>" + invocation.getArguments()[0] + "</p>"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); TwStoriesdata twStoriesdata = new TwStoriesdata(); TwStorydata twStorydata = new TwStorydata(); twStoriesdata.getTwStorydata().add(twStorydata); TwPassagedata twPassagedata = new TwPassagedata(); twPassagedata.setPid(1); twPassagedata.setName("Passage 1"); twPassagedata.setValue("Lorum ipsum dolor sit amet."); twPassagedata.setTags(""); TwPassagedata twPassagedata2 = new TwPassagedata(); twPassagedata2.setPid(2); twPassagedata2.setName("Passage 2"); twPassagedata2.setValue("Consectetur amet pizza."); twPassagedata2.setTags(""); twStorydata.getTwPassagedata().add(twPassagedata); twStorydata.getTwPassagedata().add(twPassagedata2); convertor.transform(twStorydata, outputStream); try(InputStream in = new ByteArrayInputStream(outputStream.toByteArray())) { EpubReader epubReader = new EpubReader(); Book book = epubReader.readEpub(in); assertEquals(twStorydata.getTwPassagedata().size() + 1, book.getResources().getAll().size()); } }
@Test public void transform_StyledStory_StylesheetAdded() throws Exception { when(mockPegDownProcessor.markdownToHtml(any(Node.class))) .thenAnswer(invocation -> "<p>" + invocation.getArguments()[0] + "</p>"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); TwStoriesdata twStoriesdata = new TwStoriesdata(); TwStorydata twStorydata = new TwStorydata(); twStoriesdata.getTwStorydata().add(twStorydata); Style style = new Style(); style.setRole("stylesheet"); style.setType("text/css"); style.setValue("* { background-color: #000000;"); twStorydata.setStyle(style); convertor.transform(twStorydata, outputStream); try(InputStream in = new ByteArrayInputStream(outputStream.toByteArray())) { EpubReader epubReader = new EpubReader(); Book book = epubReader.readEpub(in); Resources resources = book.getResources(); List<Resource> resourcesByMediaType = resources.getResourcesByMediaType(MediatypeService.CSS); assertFalse(resourcesByMediaType.isEmpty()); assertEquals("Story.css", resourcesByMediaType.get(0).getHref()); } }
@Test public void convert_StoryExtendedMetadata_WritesMetadataToEpub() throws Exception { String expectedTitle = "A Story with a Title"; String expectedLanguage = "en-US"; when(mockPegDownProcessor.markdownToHtml(any(Node.class))) .thenAnswer(invocation -> "<p>" + invocation.getArguments()[0] + "</p>"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); TwStoriesdata twStoriesdata = new TwStoriesdata(); TwStorydata twStorydata = new TwStorydata(); twStoriesdata.getTwStorydata().add(twStorydata); XtwMetadata xtwMetadata = new XtwMetadata(); xtwMetadata.getTitle().add(expectedTitle); xtwMetadata.setLanguage(expectedLanguage); twStorydata.setXtwMetadata(xtwMetadata); TwPassagedata twPassagedata = new TwPassagedata(); twPassagedata.setPid(1); twPassagedata.setName("Passage 1"); twPassagedata.setValue("Lorum ipsum dolor sit amet."); twPassagedata.setTags(""); TwPassagedata twPassagedata2 = new TwPassagedata(); twPassagedata2.setPid(2); twPassagedata2.setName("Passage 2"); twPassagedata2.setValue("Consectetur amet pizza."); twPassagedata2.setTags(""); twStorydata.getTwPassagedata().add(twPassagedata); twStorydata.getTwPassagedata().add(twPassagedata2); convertor.transform(twStorydata, outputStream); try(InputStream in = new ByteArrayInputStream(outputStream.toByteArray())) { EpubReader epubReader = new EpubReader(); Book book = epubReader.readEpub(in); assertEquals(twStorydata.getTwPassagedata().size() + 1, book.getResources().getAll().size()); assertEquals(expectedTitle, book.getMetadata().getFirstTitle()); assertEquals(expectedLanguage, book.getMetadata().getLanguage()); } }