### Question:
PDFURIResolver implements URIResolver { @Override public Source resolve(String href, String base) throws TransformerException { if (this.attachmentMap != null) { if (href.contains(TEX_ACTION)) { AttachmentReference reference = this.attachmentMap.get(href.replace(TEX_ACTION, "/download/")); if (reference != null) { ImageStorage storage = Utils.getComponent(ImageStorage.class); ImageData image = storage.get(reference.getName()); return new StreamSource(new ByteArrayInputStream(image.getData())); } } AttachmentReference reference = this.attachmentMap.get(href); if (reference != null) { try { XWikiDocument xdoc = this.context.getWiki().getDocument( reference.extractReference(EntityType.DOCUMENT), this.context); XWikiAttachment attachment = xdoc.getAttachment( reference.extractReference(EntityType.ATTACHMENT).getName()); return new StreamSource(attachment.getContentInputStream(this.context)); } catch (Exception e) { throw new TransformerException(String.format("Failed to resolve export URI [%s]", href), e); } } } return null; } PDFURIResolver(XWikiContext context); @Override Source resolve(String href, String base);  }### Answer:
@Test public void resolveWhenNoMap() throws Exception { XWikiContext context = mock(XWikiContext.class); PDFURIResolver resolver = new PDFURIResolver(context); Source source = resolver.resolve("href", "base"); assertNull(source); }
@Test public void resolveWhenMapItemExists() throws Exception { DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); AttachmentReference attachmentReference = new AttachmentReference("fileName", documentReference); com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class); XWikiDocument document = mock(XWikiDocument.class); XWikiAttachment attachment = mock(XWikiAttachment.class); XWikiContext context = mock(XWikiContext.class); String url = "encoded+url"; when(context.get("pdfExportImageURLMap")).thenReturn(Collections.singletonMap(url, attachmentReference)); when(context.getWiki()).thenReturn(xwiki); when(xwiki.getDocument(attachmentReference.extractReference(EntityType.DOCUMENT), context)).thenReturn( document); when(document.getAttachment("fileName")).thenReturn(attachment); when(attachment.getContentInputStream(context)).thenReturn(new ByteArrayInputStream("content".getBytes())); PDFURIResolver resolver = new PDFURIResolver(context); Source source = resolver.resolve(url, "base"); assertEquals(StreamSource.class, source.getClass()); assertEquals("content", IOUtils.readLines(((StreamSource) source).getInputStream()).get(0)); }