HtmlResourceEmbedder implements ResourceEmbedder { @Override public String embed(Book book, String content) { book = Objects.requireNonNull(book); content = Objects.requireNonNull(content); Document document = htmlParser.parseInput(content, ""); Elements images = document.getElementsByTag("img"); for (Element img : images) { String imgSrcAttribute = img.attr("src"); try { URL urlFromString = new URL(imgSrcAttribute); Embedder embedder = embedderFactory.get(urlFromString); embedder.embed(book, urlFromString); img.attr("src", embedder.getHref(urlFromString)); } catch (MalformedURLException e) { LOGGER.warn("Error creating uri from '{}'", imgSrcAttribute, e); } catch (IOException e) { LOGGER.error("Error during embedding of '{}'", imgSrcAttribute, e); } } return document.toString(); } @Inject  HtmlResourceEmbedder(final EmbedderFactory embedderFactory, final Parser htmlParser); @Override String embed(Book book, String content);  }
@Test public void embed_InvalidURL_IgnoresThatResource() throws IOException { Book book = new Book(); org.jsoup.nodes.Document document = new org.jsoup.nodes.Document(""); Element body = document.appendElement("body"); Element paragraph = body.appendElement("p"); Element img1 = paragraph.appendElement("img"); img1.attr("src", "hyyp: Element img2 = paragraph.appendElement("img"); img2.attr("src", "http: embedder.embed(book, document.toString()); verify(mockEmbedder, times(1)).embed(eq(book), eq(new URL("http: }
@Test public void embed_Base64Images_IgnoresThatResource() throws IOException { Book book = new Book(); String content = "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAABQAAAAUABsvEyTgAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAaKSURBVHic7ZvdjlRFEMd/fQZYUMTVGIwQAwYVQQKRL4kXcqMkhgsTUdELTXwJH8nEB/BZvNBbw9csWUxw+dg97UVPT1d199nuYXtndhIqYWe2T3Wfrqp embedder.embed(book, content); verify(mockEmbedder, never()).embed(eq(book), any(URL.class)); }
@Test public void embed_UnrecognizedProtocol_IgnoresThatResource() throws IOException { Book book = new Book(); String content = "<img src=\"mmd:some_protocol\" />"; embedder.embed(book, content); verify(mockEmbedder, never()).embed(eq(book), any(URL.class)); }
@Test public void embed_AnchorLinkNode_NoResourceEmbedded() { Book book = new Book(); String content = "<p>Hello <a href=\"https: embedder.embed(book, content); assertTrue(book.getResources().isEmpty()); }
@Test public void embed_CodeNode_NoResourceEmbedded() { Book book = new Book(); String content = "<code>Code is not an embedded resources.</code>"; embedder.embed(book, content); assertTrue(book.getResources().isEmpty()); }
@Test public void embed_ImgTag_EmbedIsCalled() throws IOException { Book book = new Book(); String content = "<img src=\"http: embedder.embed(book, content); verify(mockEmbedder, times(1)).embed(eq(book), any(URL.class)); }
@Test public void embed_ImgTag_SrcIsReplacedWithEmbeddedResourceHref() throws IOException { Book book = new Book(); String content = "<img src=\"http: String embeddedResourceLocation = "Images/some-hash.jpg"; when(mockEmbedder.getHref(any(URL.class))).thenReturn(embeddedResourceLocation); String result = embedder.embed(book, content); Assert.assertTrue(result.contains(embeddedResourceLocation)); Assert.assertFalse(result.contains(content)); }
@Test public void embed_OneNestedExpImageNode_EmbedIsCalled() throws IOException { Book book = new Book(); String content = "<p><img src=\"http: embedder.embed(book, content); verify(mockEmbedder, times(1)).embed(eq(book), any(URL.class)); }
@Test public void embed_MultipleNestedExpImageNodes_EmbedIsCalled() throws IOException { String nestedNode1UrlPath = "http: String nestedNode2UrlPath = "http: String nestedNode3UrlPath = "http: Book book = new Book(); org.jsoup.nodes.Document document = new org.jsoup.nodes.Document(""); Element body = document.appendElement("body"); Element paragraph = body.appendElement("p"); Element img1 = paragraph.appendElement("img"); img1.attr("src", nestedNode1UrlPath); Element img2 = paragraph.appendElement("img"); img2.attr("src", nestedNode2UrlPath); Element img3 = paragraph.appendElement("img"); img3.attr("src", nestedNode3UrlPath); embedder.embed(book, document.toString()); URL nestedNode1Url = new URL(nestedNode1UrlPath); URL nestedNode2Url = new URL(nestedNode2UrlPath); URL nestedNode3Url = new URL(nestedNode3UrlPath); verify(mockEmbedder, times(1)).embed(eq(book), eq(nestedNode1Url)); verify(mockEmbedder, times(1)).embed(eq(book), eq(nestedNode2Url)); verify(mockEmbedder, times(1)).embed(eq(book), eq(nestedNode3Url)); }
@Test public void embed_NoEmbeddableNode_EmbedIsNotCalled() throws IOException { Book book = new Book(); String content = "<p><b>Nothing</b> in <i>here</i> is embeddable</p>"; embedder.embed(book, content); verify(mockEmbedder, never()).embed(eq(book), any(URL.class)); }
@Test(expected = NullPointerException.class) public void embed_NullContent_ThrowsNullPointerException() { Book book = new Book(); embedder.embed(book, null); }
@Test(expected = NullPointerException.class) public void embed_NullBook_ThrowsNullPointerException() { String content = "<p>Lorum ipsum.</p>"; embedder.embed(null, content); }