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

File OfficeAttachmentImporterTest.java

 

Code metrics

0
38
6
1
150
103
9
0.24
6.33
6
1.5

Classes

Class Line # Actions
OfficeAttachmentImporterTest 52 38 0% 9 3
0.931818293.2%
 

Contributing tests

This file is covered by 5 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.wysiwyg.internal.importer;
21   
22    import java.io.InputStream;
23    import java.util.Collections;
24    import java.util.Map;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.model.reference.AttachmentReference;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReferenceSerializer;
33    import org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder;
34    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
35    import org.xwiki.officeimporter.server.OfficeServer;
36    import org.xwiki.officeimporter.server.OfficeServer.ServerState;
37    import org.xwiki.rendering.block.XDOM;
38    import org.xwiki.security.authorization.ContextualAuthorizationManager;
39    import org.xwiki.security.authorization.Right;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41    import org.xwiki.wysiwyg.importer.AttachmentImporter;
42   
43    import static org.junit.Assert.*;
44    import static org.mockito.Mockito.*;
45   
46    /**
47    * Unit tests for {@link OfficeAttachmentImporter}.
48    *
49    * @version $Id: 482e53118ba5257f00c38bcc79d8df7df0b24e7d $
50    * @since 9.8
51    */
 
52    public class OfficeAttachmentImporterTest
53    {
54    @Rule
55    public MockitoComponentMockingRule<AttachmentImporter> mocker =
56    new MockitoComponentMockingRule<>(OfficeAttachmentImporter.class);
57   
58    private AttachmentReference attachmentReference =
59    new AttachmentReference("my.doc", new DocumentReference("wiki", "Some", "Page"));
60   
61    private ContextualAuthorizationManager authorization;
62   
63    private EntityReferenceSerializer<String> entityReferenceSerializer;
64   
65    private DocumentAccessBridge documentAccessBridge;
66   
67    private OfficeServer officeServer;
68   
 
69  5 toggle @Before
70    public void configure() throws Exception
71    {
72  5 this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
73  5 this.entityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
74  5 this.documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class);
75  5 this.officeServer = this.mocker.getInstance(OfficeServer.class);
76   
77  5 when(this.entityReferenceSerializer.serialize(attachmentReference)).thenReturn("Some.Page@my.doc");
78    }
79   
 
80  1 toggle @Test
81    public void toHTMLWithOfficeViewer() throws Exception
82    {
83  1 OfficeMacroImporter officeMacroImporter = this.mocker.getInstance(OfficeMacroImporter.class);
84  1 XDOM xdom = mock(XDOM.class);
85  1 when(officeMacroImporter.buildXDOM(attachmentReference, false)).thenReturn(xdom);
86  1 when(officeMacroImporter.render(xdom)).thenReturn("foo");
87   
88  1 Map<String, Object> parameters = Collections.singletonMap("useOfficeViewer", "true");
89  1 assertEquals("foo", this.mocker.getComponentUnderTest().toHTML(attachmentReference, parameters));
90    }
91   
 
92  1 toggle @Test
93    public void toHTMLRequiresEditRight() throws Exception
94    {
95  1 try {
96  1 this.mocker.getComponentUnderTest().toHTML(attachmentReference, Collections.emptyMap());
97  0 fail();
98    } catch (RuntimeException expected) {
99  1 assertEquals("Edit right is required in order to import [Some.Page@my.doc].", expected.getMessage());
100    }
101    }
102   
 
103  1 toggle @Test
104    public void toHTMLAttachmentNotFound() throws Exception
105    {
106  1 when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true);
107   
108  1 try {
109  1 this.mocker.getComponentUnderTest().toHTML(attachmentReference, Collections.emptyMap());
110  0 fail();
111    } catch (RuntimeException expected) {
112  1 assertEquals("Attachment not found: [Some.Page@my.doc].", expected.getMessage());
113    }
114    }
115   
 
116  1 toggle @Test
117    public void toHTMLOfficeServerNotConnected() throws Exception
118    {
119  1 when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true);
120  1 when(this.documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("1.3");
121   
122  1 try {
123  1 this.mocker.getComponentUnderTest().toHTML(attachmentReference, Collections.emptyMap());
124  0 fail();
125    } catch (RuntimeException expected) {
126  1 assertEquals("The office server is not connected.", expected.getMessage());
127    }
128    }
129   
 
130  1 toggle @Test
131    public void toHTML() throws Exception
132    {
133  1 when(this.authorization.hasAccess(Right.EDIT, attachmentReference)).thenReturn(true);
134  1 when(this.documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("1.3");
135  1 when(this.officeServer.getState()).thenReturn(ServerState.CONNECTED);
136   
137  1 InputStream attachmentContent = mock(InputStream.class);
138  1 when(this.documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
139   
140  1 XDOMOfficeDocumentBuilder documentBuilder = this.mocker.getInstance(XDOMOfficeDocumentBuilder.class);
141  1 XDOMOfficeDocument xdomOfficeDocument = mock(XDOMOfficeDocument.class);
142  1 when(documentBuilder.build(attachmentContent, "my.doc", attachmentReference.getDocumentReference(), true))
143    .thenReturn(xdomOfficeDocument);
144  1 when(xdomOfficeDocument.getArtifacts()).thenReturn(Collections.emptyMap());
145  1 when(xdomOfficeDocument.getContentAsString("annotatedxhtml/1.0")).thenReturn("test");
146   
147  1 Map<String, Object> parameters = Collections.singletonMap("filterStyles", "true");
148  1 assertEquals("test", this.mocker.getComponentUnderTest().toHTML(attachmentReference, parameters));
149    }
150    }