use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class DefaultOfficeResourceViewerTest method testViewPresentation.
@Test
public void testViewPresentation() throws Exception {
AttachmentResourceReference attachResourceRef = new AttachmentResourceReference("xwiki:Some.Page@presentation.odp");
DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page");
AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference);
AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference);
when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn(Arrays.asList(attachmentReference));
when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2");
ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL);
ExpandedMacroBlock galleryMacro = new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false);
galleryMacro.addChild(new ImageBlock(imageReference, true));
XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro));
Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]);
XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker);
PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class);
when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference)).thenReturn(xdomOfficeDocument);
Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference);
TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer", Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference);
Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TemporaryResourceReference.class, ExtendedURL.class);
ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer = mocker.getInstance(type, "standard/tmp");
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png"));
when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL);
XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters);
ImageBlock imageBlock = (ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0);
assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference());
galleryMacro = (ExpandedMacroBlock) output.getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0);
assertFalse(galleryMacro.getParent() instanceof XDOM);
assertEquals(Syntax.XWIKI_2_1, ((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX));
TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class);
verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class));
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class OfficeImporterScriptServiceTest method saveWithAppend.
@Test
public void saveWithAppend() throws Exception {
XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
String syntaxId = "test/1.0";
when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
when(documentAccessBridge.exists(documentReference)).thenReturn(true);
DocumentModelBridge document = mock(DocumentModelBridge.class);
when(documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
when(document.getSyntax()).thenReturn(new Syntax(new SyntaxType("test", "Test"), "1.0"));
when(documentAccessBridge.getDocumentContent(documentReference, null)).thenReturn("before");
when(doc.getContentAsString(syntaxId)).thenReturn("after");
assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, null, null, true));
verify(documentAccessBridge).setDocumentContent(documentReference, "before\nafter", "Updated by office importer.", false);
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class OfficeImporterScriptServiceTest method saveWithOverwrite.
@Test
public void saveWithOverwrite() throws Exception {
XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
DocumentReference parentReference = new DocumentReference("wiki", "Space", "Parent");
String syntaxId = "test/1.0";
String title = "Office Document Title";
String content = "Office Document Content";
String fileName = "logo.png";
byte[] fileContent = new byte[] { 65, 82 };
when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
when(doc.getContentAsString(syntaxId)).thenReturn(content);
when(doc.getArtifacts()).thenReturn(Collections.singletonMap(fileName, fileContent));
assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, parentReference, title, false));
verify(documentAccessBridge).setDocumentSyntaxId(documentReference, syntaxId);
verify(documentAccessBridge).setDocumentContent(documentReference, content, "Created by office importer.", false);
verify(documentAccessBridge).setDocumentParentReference(documentReference, parentReference);
verify(documentAccessBridge).setDocumentTitle(documentReference, title);
verify(documentAccessBridge).setAttachmentContent(new AttachmentReference(fileName, documentReference), fileContent);
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class DefaultPresentationBuilderTest method build.
@Test
public void build() throws Exception {
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
when(this.entityReferenceSerializer.serialize(documentReference)).thenReturn("wiki:Path.To.Page");
DocumentModelBridge document = mock(DocumentModelBridge.class);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
when(dab.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
InputStream officeFileStream = new ByteArrayInputStream("Presentation content".getBytes());
Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
byte[] firstSlide = "first slide".getBytes();
byte[] secondSlide = "second slide".getBytes();
artifacts.put("img0.jpg", firstSlide);
artifacts.put("img0.html", new byte[0]);
artifacts.put("text0.html", new byte[0]);
artifacts.put("img1.jpg", secondSlide);
artifacts.put("img1.html", new byte[0]);
artifacts.put("text1.html", new byte[0]);
when(this.officeConverter.convert(Collections.singletonMap("file.odp", officeFileStream), "file.odp", "img0.html")).thenReturn(artifacts);
HTMLCleanerConfiguration config = mock(HTMLCleanerConfiguration.class);
when(this.officeHTMLCleaner.getDefaultConfiguration()).thenReturn(config);
Document xhtmlDoc = XMLUtils.createDOMDocument();
xhtmlDoc.appendChild(xhtmlDoc.createElement("html"));
String presentationHTML = "<p><img src=\"file-slide0.jpg\"/></p><p><img src=\"file-slide1.jpg\"/></p>";
when(this.officeHTMLCleaner.clean(any(Reader.class), eq(config))).then(returnMatchingDocument(presentationHTML, xhtmlDoc));
XDOM galleryContent = new XDOM(Collections.<Block>emptyList());
when(this.xhtmlParser.parse(any(Reader.class))).thenReturn(galleryContent);
XDOMOfficeDocument result = this.mocker.getComponentUnderTest().build(officeFileStream, "file.odp", documentReference);
verify(config).setParameters(Collections.singletonMap("targetDocument", "wiki:Path.To.Page"));
Map<String, byte[]> expectedArtifacts = new HashMap<String, byte[]>();
expectedArtifacts.put("file-slide0.jpg", firstSlide);
expectedArtifacts.put("file-slide1.jpg", secondSlide);
assertEquals(expectedArtifacts, result.getArtifacts());
assertEquals("wiki:Path.To.Page", result.getContentDocument().getMetaData().getMetaData(MetaData.BASE));
List<ExpandedMacroBlock> macros = result.getContentDocument().getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.CHILD);
Assert.assertEquals(1, macros.size());
Assert.assertEquals("gallery", macros.get(0).getId());
Assert.assertEquals(galleryContent, macros.get(0).getChildren().get(0));
}
use of org.xwiki.officeimporter.document.XDOMOfficeDocument in project xwiki-platform by xwiki.
the class OfficeAttachmentImporterTest method toHTML.
@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));
}
Aggregations