| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.officeimporter.internal.builder; |
| 21 |
|
|
| 22 |
|
import java.io.ByteArrayInputStream; |
| 23 |
|
import java.io.InputStream; |
| 24 |
|
import java.io.Reader; |
| 25 |
|
import java.util.Arrays; |
| 26 |
|
import java.util.Collections; |
| 27 |
|
import java.util.HashMap; |
| 28 |
|
import java.util.List; |
| 29 |
|
import java.util.Map; |
| 30 |
|
|
| 31 |
|
import org.apache.commons.codec.binary.StringUtils; |
| 32 |
|
import org.apache.commons.io.IOUtils; |
| 33 |
|
import org.junit.Assert; |
| 34 |
|
import org.junit.Before; |
| 35 |
|
import org.junit.Rule; |
| 36 |
|
import org.junit.Test; |
| 37 |
|
import org.mockito.invocation.InvocationOnMock; |
| 38 |
|
import org.mockito.stubbing.Answer; |
| 39 |
|
import org.w3c.dom.Document; |
| 40 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 41 |
|
import org.xwiki.bridge.DocumentModelBridge; |
| 42 |
|
import org.xwiki.model.reference.DocumentReference; |
| 43 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
| 44 |
|
import org.xwiki.officeimporter.builder.PresentationBuilder; |
| 45 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
| 46 |
|
import org.xwiki.officeimporter.document.XDOMOfficeDocument; |
| 47 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
| 48 |
|
import org.xwiki.rendering.block.Block; |
| 49 |
|
import org.xwiki.rendering.block.ExpandedMacroBlock; |
| 50 |
|
import org.xwiki.rendering.block.XDOM; |
| 51 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
| 52 |
|
import org.xwiki.rendering.listener.MetaData; |
| 53 |
|
import org.xwiki.rendering.parser.Parser; |
| 54 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 55 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 56 |
|
import org.xwiki.xml.XMLUtils; |
| 57 |
|
import org.xwiki.xml.html.HTMLCleaner; |
| 58 |
|
import org.xwiki.xml.html.HTMLCleanerConfiguration; |
| 59 |
|
|
| 60 |
|
import static org.junit.Assert.*; |
| 61 |
|
import static org.mockito.ArgumentMatchers.*; |
| 62 |
|
import static org.mockito.Mockito.*; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@link |
| 66 |
|
|
| 67 |
|
@version |
| 68 |
|
@since |
| 69 |
|
|
| |
|
| 98% |
Uncovered Elements: 1 (51) |
Complexity: 5 |
Complexity Density: 0.11 |
|
| 70 |
|
public class DefaultPresentationBuilderTest |
| 71 |
|
{ |
| 72 |
|
@Rule |
| 73 |
|
public MockitoComponentMockingRule<PresentationBuilder> mocker = |
| 74 |
|
new MockitoComponentMockingRule<PresentationBuilder>(DefaultPresentationBuilder.class); |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
private Parser xhtmlParser; |
| 80 |
|
|
| 81 |
|
private OfficeConverter officeConverter; |
| 82 |
|
|
| 83 |
|
private HTMLCleaner officeHTMLCleaner; |
| 84 |
|
|
| 85 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
| 86 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 87 |
1 |
@Before... |
| 88 |
|
public void configure() throws Exception |
| 89 |
|
{ |
| 90 |
1 |
this.xhtmlParser = this.mocker.getInstance(Parser.class, "xhtml/1.0"); |
| 91 |
1 |
this.officeHTMLCleaner = this.mocker.getInstance(HTMLCleaner.class, "openoffice"); |
| 92 |
1 |
this.entityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING); |
| 93 |
|
|
| 94 |
1 |
this.officeConverter = mock(OfficeConverter.class); |
| 95 |
1 |
OfficeServer officeServer = this.mocker.getInstance(OfficeServer.class); |
| 96 |
1 |
when(officeServer.getConverter()).thenReturn(this.officeConverter); |
| 97 |
|
} |
| 98 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (36) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
| 99 |
1 |
@Test... |
| 100 |
|
public void build() throws Exception |
| 101 |
|
{ |
| 102 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page"); |
| 103 |
1 |
when(this.entityReferenceSerializer.serialize(documentReference)).thenReturn("wiki:Path.To.Page"); |
| 104 |
|
|
| 105 |
1 |
DocumentModelBridge document = mock(DocumentModelBridge.class); |
| 106 |
1 |
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class); |
| 107 |
1 |
when(dab.getDocument(documentReference)).thenReturn(document); |
| 108 |
1 |
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
| 109 |
|
|
| 110 |
1 |
InputStream officeFileStream = new ByteArrayInputStream("Presentation content".getBytes()); |
| 111 |
1 |
Map<String, byte[]> artifacts = new HashMap<String, byte[]>(); |
| 112 |
1 |
byte[] firstSlide = "first slide".getBytes(); |
| 113 |
1 |
byte[] secondSlide = "second slide".getBytes(); |
| 114 |
1 |
artifacts.put("img0.jpg", firstSlide); |
| 115 |
1 |
artifacts.put("img0.html", new byte[0]); |
| 116 |
1 |
artifacts.put("text0.html", new byte[0]); |
| 117 |
1 |
artifacts.put("img1.jpg", secondSlide); |
| 118 |
1 |
artifacts.put("img1.html", new byte[0]); |
| 119 |
1 |
artifacts.put("text1.html", new byte[0]); |
| 120 |
1 |
when(this.officeConverter.convert(Collections.singletonMap("file.odp", officeFileStream), "file.odp", |
| 121 |
|
"img0.html")).thenReturn(artifacts); |
| 122 |
|
|
| 123 |
1 |
HTMLCleanerConfiguration config = mock(HTMLCleanerConfiguration.class); |
| 124 |
1 |
when(this.officeHTMLCleaner.getDefaultConfiguration()).thenReturn(config); |
| 125 |
|
|
| 126 |
1 |
Document xhtmlDoc = XMLUtils.createDOMDocument(); |
| 127 |
1 |
xhtmlDoc.appendChild(xhtmlDoc.createElement("html")); |
| 128 |
1 |
String presentationHTML = "<p><img src=\"file-slide0.jpg\"/></p><p><img src=\"file-slide1.jpg\"/></p>"; |
| 129 |
1 |
when(this.officeHTMLCleaner.clean(any(Reader.class), eq(config))) |
| 130 |
|
.then(returnMatchingDocument(presentationHTML, xhtmlDoc)); |
| 131 |
|
|
| 132 |
1 |
XDOM galleryContent = new XDOM(Collections.<Block>emptyList()); |
| 133 |
1 |
when(this.xhtmlParser.parse(any(Reader.class))).thenReturn(galleryContent); |
| 134 |
|
|
| 135 |
1 |
XDOMOfficeDocument result = |
| 136 |
|
this.mocker.getComponentUnderTest().build(officeFileStream, "file.odp", documentReference); |
| 137 |
|
|
| 138 |
1 |
verify(config).setParameters(Collections.singletonMap("targetDocument", "wiki:Path.To.Page")); |
| 139 |
|
|
| 140 |
1 |
Map<String, byte[]> expectedArtifacts = new HashMap<String, byte[]>(); |
| 141 |
1 |
expectedArtifacts.put("file-slide0.jpg", firstSlide); |
| 142 |
1 |
expectedArtifacts.put("file-slide1.jpg", secondSlide); |
| 143 |
1 |
assertEquals(expectedArtifacts, result.getArtifacts()); |
| 144 |
|
|
| 145 |
1 |
assertEquals("wiki:Path.To.Page", result.getContentDocument().getMetaData().getMetaData(MetaData.BASE)); |
| 146 |
|
|
| 147 |
1 |
List<ExpandedMacroBlock> macros = |
| 148 |
|
result.getContentDocument().getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.CHILD); |
| 149 |
1 |
Assert.assertEquals(1, macros.size()); |
| 150 |
1 |
Assert.assertEquals("gallery", macros.get(0).getId()); |
| 151 |
1 |
Assert.assertEquals(galleryContent, macros.get(0).getChildren().get(0)); |
| 152 |
|
} |
| 153 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 154 |
1 |
private Answer<Document> returnMatchingDocument(final String content, final Document document)... |
| 155 |
|
{ |
| 156 |
1 |
return new Answer<Document>() |
| 157 |
|
{ |
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 158 |
1 |
@Override... |
| 159 |
|
public Document answer(InvocationOnMock invocation) throws Throwable |
| 160 |
|
{ |
| 161 |
1 |
Reader reader = invocation.getArgument(0); |
| 162 |
1 |
return StringUtils.equals(content, IOUtils.toString(reader)) ? document : null; |
| 163 |
|
} |
| 164 |
|
}; |
| 165 |
|
} |
| 166 |
|
} |