1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.officeimporter.internal.builder

File DefaultPresentationBuilderTest.java

 

Code metrics

2
45
4
1
166
116
5
0.11
11.25
4
1.25

Classes

Class Line # Actions
DefaultPresentationBuilderTest 70 45 0% 5 1
0.9803921698%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Test case for {@link DefaultPresentationBuilder}.
66    *
67    * @version $Id: 9755a72faf2d65f2bf51f59f356875ef57f3d724 $
68    * @since 2.1M1
69    */
 
70    public class DefaultPresentationBuilderTest
71    {
72    @Rule
73    public MockitoComponentMockingRule<PresentationBuilder> mocker =
74    new MockitoComponentMockingRule<PresentationBuilder>(DefaultPresentationBuilder.class);
75   
76    /**
77    * The component used to parse the presentation HTML.
78    */
79    private Parser xhtmlParser;
80   
81    private OfficeConverter officeConverter;
82   
83    private HTMLCleaner officeHTMLCleaner;
84   
85    private EntityReferenceSerializer<String> entityReferenceSerializer;
86   
 
87  1 toggle @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   
 
99  1 toggle @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.getTranslatedDocumentInstance(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   
 
154  1 toggle private Answer<Document> returnMatchingDocument(final String content, final Document document)
155    {
156  1 return new Answer<Document>()
157    {
 
158  1 toggle @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    }