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

File DefaultOfficeResourceViewerTest.java

 

Code metrics

0
87
7
1
379
230
8
0.09
12.43
7
1.14

Classes

Class Line # Actions
DefaultOfficeResourceViewerTest 81 87 0% 8 1
0.989361798.9%
 

Contributing tests

This file is covered by 6 tests. .

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.office.viewer.internal;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.File;
24    import java.io.InputStream;
25    import java.lang.reflect.Type;
26    import java.util.ArrayList;
27    import java.util.Arrays;
28    import java.util.Collections;
29    import java.util.HashMap;
30    import java.util.HashSet;
31    import java.util.Map;
32   
33    import org.jodconverter.document.DefaultDocumentFormatRegistry;
34    import org.junit.Assert;
35    import org.junit.Before;
36    import org.junit.Rule;
37    import org.junit.Test;
38    import org.xwiki.bridge.DocumentAccessBridge;
39    import org.xwiki.cache.Cache;
40    import org.xwiki.cache.CacheManager;
41    import org.xwiki.cache.config.CacheConfiguration;
42    import org.xwiki.component.util.DefaultParameterizedType;
43    import org.xwiki.model.reference.AttachmentReference;
44    import org.xwiki.model.reference.AttachmentReferenceResolver;
45    import org.xwiki.model.reference.DocumentReference;
46    import org.xwiki.model.reference.EntityReferenceSerializer;
47    import org.xwiki.office.viewer.OfficeResourceViewer;
48    import org.xwiki.officeimporter.builder.PresentationBuilder;
49    import org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder;
50    import org.xwiki.officeimporter.converter.OfficeConverter;
51    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
52    import org.xwiki.officeimporter.server.OfficeServer;
53    import org.xwiki.properties.ConverterManager;
54    import org.xwiki.rendering.block.Block;
55    import org.xwiki.rendering.block.ExpandedMacroBlock;
56    import org.xwiki.rendering.block.ImageBlock;
57    import org.xwiki.rendering.block.MetaDataBlock;
58    import org.xwiki.rendering.block.XDOM;
59    import org.xwiki.rendering.block.match.ClassBlockMatcher;
60    import org.xwiki.rendering.listener.MetaData;
61    import org.xwiki.rendering.listener.reference.AttachmentResourceReference;
62    import org.xwiki.rendering.listener.reference.ResourceReference;
63    import org.xwiki.rendering.listener.reference.ResourceType;
64    import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer;
65    import org.xwiki.rendering.syntax.Syntax;
66    import org.xwiki.resource.ResourceReferenceSerializer;
67    import org.xwiki.resource.temporary.TemporaryResourceReference;
68    import org.xwiki.resource.temporary.TemporaryResourceStore;
69    import org.xwiki.test.mockito.MockitoComponentMockingRule;
70    import org.xwiki.url.ExtendedURL;
71   
72    import static org.junit.Assert.*;
73    import static org.mockito.ArgumentMatchers.*;
74    import static org.mockito.Mockito.*;
75   
76    /**
77    * Test case for {@link DefaultOfficeResourceViewer}.
78    *
79    * @version $Id: 81d320e6dec469cb6878189b68b299bf54b8fba8 $
80    */
 
81    public class DefaultOfficeResourceViewerTest
82    {
83    private static final String ATTACHEMENT_NAME = "Test file.doc";
84   
85    /**
86    * An attachment reference to be used in tests.
87    */
88    private static final AttachmentReference ATTACHMENT_REFERENCE = new AttachmentReference(ATTACHEMENT_NAME,
89    new DocumentReference("xwiki", "Main", "Test"));
90   
91    /**
92    * String document reference to be used in tests.
93    */
94    private static final String STRING_DOCUMENT_REFERENCE = "xwiki:Main.Test";
95   
96    /**
97    * String attachment reference to be used in tests.
98    */
99    private static final String STRING_ATTACHMENT_REFERENCE = STRING_DOCUMENT_REFERENCE + '@' + ATTACHEMENT_NAME;
100   
101    private static final String STRING_ATTACHMENT_RESOURCE_REFERENCE = "attach:" + STRING_ATTACHMENT_REFERENCE;
102   
103    /**
104    * The attachment as {@link ResourceReference}.
105    */
106    private static final AttachmentResourceReference ATTACHMENT_RESOURCE_REFERENCE = new AttachmentResourceReference(
107    STRING_ATTACHMENT_REFERENCE);
108   
109    /**
110    * Default view parameters.
111    */
112    private static final Map<String, ?> DEFAULT_VIEW_PARAMETERS = Collections.singletonMap("ownerDocument",
113    ATTACHMENT_REFERENCE.getDocumentReference());
114   
115    /**
116    * The cache key corresponding to {@link #STRING_ATTACHMENT_REFERENCE} and {@link #DEFAULT_VIEW_PARAMETERS}.
117    */
118    private static final String CACHE_KEY = STRING_DOCUMENT_REFERENCE + '/' + ATTACHEMENT_NAME + '/'
119    + DEFAULT_VIEW_PARAMETERS.hashCode();
120   
121    /**
122    * Attachment version to be used in tests.
123    */
124    private static final String ATTACHMENT_VERSION = "1.1";
125   
126    /**
127    * A component manager that automatically mocks all dependencies of {@link DefaultOfficeResourceViewer}.
128    */
129    @Rule
130    public MockitoComponentMockingRule<OfficeResourceViewer> mocker =
131    new MockitoComponentMockingRule<OfficeResourceViewer>(DefaultOfficeResourceViewer.class);
132   
133    /**
134    * The mock {@link DocumentAccessBridge} instance used in tests.
135    */
136    private DocumentAccessBridge documentAccessBridge;
137   
138    /**
139    * The mock {@link XDOMOfficeDocumentBuilder} instance used in tests.
140    */
141    private XDOMOfficeDocumentBuilder officeDocumentBuilder;
142   
143    private ResourceReferenceTypeSerializer resourceReferenceSerializer;
144   
145    /**
146    * The mock {@link Cache} instance used in tests.
147    */
148    private Cache<OfficeDocumentView> attachmentCache;
149   
150    /**
151    * The mock {@link Cache} instance used in tests.
152    */
153    private Cache<OfficeDocumentView> externalCache;
154   
155    /**
156    * Test fixture.
157    *
158    * @throws Exception in case of an exception raised during the fixture preparation
159    */
 
160  6 toggle @Before
161    public void configure() throws Exception
162    {
163  6 final CacheManager cacheManager = mocker.getInstance(CacheManager.class);
164  6 attachmentCache = mock(Cache.class, "attachment");
165  6 externalCache = mock(Cache.class, "external");
166  6 when(cacheManager.<OfficeDocumentView>createNewCache(notNull(CacheConfiguration.class))).thenReturn(
167    attachmentCache, externalCache);
168   
169  6 EntityReferenceSerializer<String> entityReferenceSerializer =
170    mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
171  6 when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE)).thenReturn(STRING_ATTACHMENT_REFERENCE);
172  6 when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(
173    STRING_DOCUMENT_REFERENCE);
174   
175  6 AttachmentReferenceResolver<String> attachmentReferenceResolver =
176    mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
177  6 when(attachmentReferenceResolver.resolve(STRING_ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_REFERENCE);
178   
179  6 this.resourceReferenceSerializer = mocker.getInstance(ResourceReferenceTypeSerializer.class);
180  6 when(this.resourceReferenceSerializer.serialize(ATTACHMENT_RESOURCE_REFERENCE)).thenReturn(
181    STRING_ATTACHMENT_RESOURCE_REFERENCE);
182   
183  6 ConverterManager converterManager = mocker.getInstance(ConverterManager.class);
184  6 when(converterManager.convert(boolean.class, null)).thenReturn(false);
185  6 when(converterManager.convert(DocumentReference.class, ATTACHMENT_REFERENCE.getDocumentReference()))
186    .thenReturn(ATTACHMENT_REFERENCE.getDocumentReference());
187   
188  6 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
189  6 officeDocumentBuilder = mocker.getInstance(XDOMOfficeDocumentBuilder.class);
190   
191  6 OfficeServer officeServer = mocker.getInstance(OfficeServer.class);
192  6 OfficeConverter officeConverter = mock(OfficeConverter.class);
193  6 when(officeServer.getConverter()).thenReturn(officeConverter);
194  6 when(officeConverter.getFormatRegistry()).thenReturn(DefaultDocumentFormatRegistry.getInstance());
195    }
196   
197    /**
198    * Test creating a view for a non-existing attachment.
199    *
200    * @throws Exception if an error occurs
201    */
 
202  1 toggle @Test
203    public void testViewNonExistingOfficeAttachment() throws Exception
204    {
205  1 when(attachmentCache.get(CACHE_KEY)).thenReturn(null);
206  1 when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(
207    Collections.<AttachmentReference>emptyList());
208   
209  1 try {
210  1 mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, DEFAULT_VIEW_PARAMETERS);
211  0 Assert.fail("Expected exception.");
212    } catch (Exception e) {
213  1 Assert.assertEquals(String.format("Attachment [%s] does not exist.", ATTACHMENT_REFERENCE), e.getMessage());
214    }
215    }
216   
217    /**
218    * Tests creating a view for an existing office attachment.
219    *
220    * @throws Exception if an error occurs
221    */
 
222  1 toggle @Test
223    public void testViewExistingOfficeAttachmentWithCacheMiss() throws Exception
224    {
225  1 when(attachmentCache.get(CACHE_KEY)).thenReturn(null);
226  1 when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(
227    Arrays.asList(ATTACHMENT_REFERENCE));
228  1 when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_VERSION);
229   
230  1 ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
231  1 when(documentAccessBridge.getAttachmentContent(ATTACHMENT_REFERENCE)).thenReturn(attachmentContent);
232   
233  1 XDOMOfficeDocument xdomOfficeDocument =
234    new XDOMOfficeDocument(new XDOM(new ArrayList<Block>()), new HashMap<String, byte[]>(), mocker);
235  1 when(
236    officeDocumentBuilder.build(attachmentContent, ATTACHMENT_REFERENCE.getName(),
237    ATTACHMENT_REFERENCE.getDocumentReference(), false)).thenReturn(xdomOfficeDocument);
238   
239  1 mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE, DEFAULT_VIEW_PARAMETERS);
240   
241  1 verify(attachmentCache).set(eq(CACHE_KEY), notNull(AttachmentOfficeDocumentView.class));
242    }
243   
244    /**
245    * Tests creating a view for an office attachment which has already been viewed and cached.
246    *
247    * @throws Exception if an error occurs.
248    */
 
249  1 toggle @Test
250    public void testViewExistingOfficeAttachmentWithCacheHit() throws Exception
251    {
252  1 AttachmentOfficeDocumentView officeDocumentView =
253    new AttachmentOfficeDocumentView(ATTACHMENT_RESOURCE_REFERENCE, ATTACHMENT_REFERENCE, ATTACHMENT_VERSION,
254    new XDOM(new ArrayList<Block>()), new HashSet<File>());
255  1 when(attachmentCache.get(CACHE_KEY)).thenReturn(officeDocumentView);
256   
257  1 when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(
258    Arrays.asList(ATTACHMENT_REFERENCE));
259  1 when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_VERSION);
260   
261  1 Assert.assertNotNull(mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE,
262    DEFAULT_VIEW_PARAMETERS));
263    }
264   
265    /**
266    * Tests creating a view for an external office file which has already been viewed and cached.
267    *
268    * @throws Exception if an error occurs.
269    */
 
270  1 toggle @Test
271    public void testViewExistingOfficeFileWithCacheHit() throws Exception
272    {
273  1 ResourceReference resourceReference = new ResourceReference("http://resource", ResourceType.URL);
274   
275  1 when(this.resourceReferenceSerializer.serialize(resourceReference)).thenReturn(
276    "url:" + resourceReference.getReference());
277   
278  1 OfficeDocumentView officeDocumentView =
279    new OfficeDocumentView(resourceReference, new XDOM(new ArrayList<Block>()), new HashSet<File>());
280  1 when(
281    externalCache.get(STRING_DOCUMENT_REFERENCE + "/url:http://resource/" + DEFAULT_VIEW_PARAMETERS.hashCode()))
282    .thenReturn(officeDocumentView);
283   
284  1 Assert.assertNotNull(mocker.getComponentUnderTest().createView(resourceReference, DEFAULT_VIEW_PARAMETERS));
285    }
286   
287    /**
288    * Tests creating a view for an office attachment that has been viewed in past and whose version has been
289    * incremented.
290    *
291    * @throws Exception if an error occurs.
292    */
 
293  1 toggle @Test
294    public void testViewANewVersionOfAnExistingOfficeAttachment() throws Exception
295    {
296  1 AttachmentOfficeDocumentView officeDocumentView =
297    new AttachmentOfficeDocumentView(ATTACHMENT_RESOURCE_REFERENCE, ATTACHMENT_REFERENCE, ATTACHMENT_VERSION,
298    new XDOM(new ArrayList<Block>()), new HashSet<File>());
299  1 when(attachmentCache.get(CACHE_KEY)).thenReturn(officeDocumentView);
300   
301  1 when(documentAccessBridge.getAttachmentReferences(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(
302    Arrays.asList(ATTACHMENT_REFERENCE));
303  1 when(documentAccessBridge.getAttachmentVersion(ATTACHMENT_REFERENCE)).thenReturn("2.1");
304   
305  1 ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
306  1 when(documentAccessBridge.getAttachmentContent(ATTACHMENT_REFERENCE)).thenReturn(attachmentContent);
307   
308  1 XDOMOfficeDocument xdomOfficeDocument =
309    new XDOMOfficeDocument(new XDOM(new ArrayList<Block>()), new HashMap<String, byte[]>(), mocker);
310  1 when(
311    officeDocumentBuilder.build(attachmentContent, ATTACHMENT_REFERENCE.getName(),
312    ATTACHMENT_REFERENCE.getDocumentReference(), false)).thenReturn(xdomOfficeDocument);
313   
314  1 Assert.assertNotNull(mocker.getComponentUnderTest().createView(ATTACHMENT_RESOURCE_REFERENCE,
315    DEFAULT_VIEW_PARAMETERS));
316   
317  1 verify(attachmentCache).remove(CACHE_KEY);
318  1 verify(attachmentCache).set(eq(CACHE_KEY), notNull(AttachmentOfficeDocumentView.class));
319    }
320   
 
321  1 toggle @Test
322    public void testViewPresentation() throws Exception
323    {
324  1 AttachmentResourceReference attachResourceRef =
325    new AttachmentResourceReference("xwiki:Some.Page@presentation.odp");
326  1 DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page");
327  1 AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference);
328   
329  1 AttachmentReferenceResolver<String> attachmentReferenceResolver =
330    mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
331  1 when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference);
332   
333  1 when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn(
334    Arrays.asList(attachmentReference));
335  1 when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2");
336   
337  1 ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
338  1 when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
339   
340  1 ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL);
341  1 ExpandedMacroBlock galleryMacro =
342    new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false);
343  1 galleryMacro.addChild(new ImageBlock(imageReference, true));
344  1 XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro));
345   
346  1 Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]);
347  1 XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker);
348   
349  1 PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class);
350  1 when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference))
351    .thenReturn(xdomOfficeDocument);
352   
353  1 Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference);
354  1 TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer",
355    Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference);
356   
357  1 Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
358    TemporaryResourceReference.class, ExtendedURL.class);
359  1 ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer =
360    mocker.getInstance(type, "standard/tmp");
361  1 ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png"));
362  1 when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL);
363   
364  1 XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters);
365   
366  1 ImageBlock imageBlock =
367    (ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0);
368  1 assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference());
369   
370  1 galleryMacro = (ExpandedMacroBlock) output
371    .getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0);
372  1 assertFalse(galleryMacro.getParent() instanceof XDOM);
373  1 assertEquals(Syntax.XWIKI_2_1,
374    ((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX));
375   
376  1 TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class);
377  1 verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class));
378    }
379    }