### Question:
OfficeAttachmentImporter implements AttachmentImporter { @Override public String toHTML(AttachmentReference attachmentReference, Map<String, Object> parameters) throws Exception { boolean filterStyles = Boolean.valueOf(String.valueOf(parameters.get("filterStyles"))); if (Boolean.valueOf(String.valueOf(parameters.get("useOfficeViewer")))) { return this.officeMacroImporter .render(this.officeMacroImporter.buildXDOM(attachmentReference, filterStyles)); } else { return maybeConvertAttachmentContent(attachmentReference, filterStyles); } }  @Override String toHTML(AttachmentReference attachmentReference, Map<String, Object> parameters);  }### Answer:
@Test public void toHTMLWithOfficeViewer() throws Exception { OfficeMacroImporter officeMacroImporter = this.mocker.getInstance(OfficeMacroImporter.class); XDOM xdom = mock(XDOM.class); when(officeMacroImporter.buildXDOM(attachmentReference, false)).thenReturn(xdom); when(officeMacroImporter.render(xdom)).thenReturn("foo"); Map<String, Object> parameters = Collections.singletonMap("useOfficeViewer", "true"); assertEquals("foo", this.mocker.getComponentUnderTest().toHTML(attachmentReference, parameters)); }
@Test public void toHTMLRequiresEditRight() throws Exception { try { this.mocker.getComponentUnderTest().toHTML(attachmentReference, Collections.emptyMap()); fail(); } catch (RuntimeException expected) { assertEquals("Edit right is required in order to import [Some.Page@my.doc].", expected.getMessage()); } }
@Test public void toHTMLAttachmentNotFound() throws Exception { when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true); try { this.mocker.getComponentUnderTest().toHTML(attachmentReference, Collections.emptyMap()); fail(); } catch (RuntimeException expected) { assertEquals("Attachment not found: [Some.Page@my.doc].", expected.getMessage()); } }
@Test public void toHTMLOfficeServerNotConnected() throws Exception { when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true); when(this.documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("1.3"); try { this.mocker.getComponentUnderTest().toHTML(attachmentReference, Collections.emptyMap()); fail(); } catch (RuntimeException expected) { assertEquals("The office server is not connected.", expected.getMessage()); } }
@Test public void toHTML() throws Exception { when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true); when(this.documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("1.3"); when(this.officeServer.getState()).thenReturn(ServerState.CONNECTED); InputStream attachmentContent = mock(InputStream.class); when(this.documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent); XDOMOfficeDocumentBuilder documentBuilder = this.mocker.getInstance(XDOMOfficeDocumentBuilder.class); XDOMOfficeDocument xdomOfficeDocument = mock(XDOMOfficeDocument.class); when(documentBuilder.build(attachmentContent, "my.doc", attachmentReference.getDocumentReference(), true)) .thenReturn(xdomOfficeDocument); when(xdomOfficeDocument.getArtifacts()).thenReturn(Collections.emptyMap()); when(xdomOfficeDocument.getContentAsString("annotatedxhtml/1.0")).thenReturn("test"); Map<String, Object> parameters = Collections.singletonMap("filterStyles", "true"); assertEquals("test", this.mocker.getComponentUnderTest().toHTML(attachmentReference, parameters)); }