1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.officeimporter.script

File OfficeImporterScriptServiceTest.java

 

Code metrics

0
31
3
1
117
70
3
0.1
10.33
3
1

Classes

Class Line # Actions
OfficeImporterScriptServiceTest 45 31 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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.officeimporter.script;
21   
22    import static org.junit.Assert.*;
23    import static org.mockito.Mockito.*;
24   
25    import java.util.Collections;
26   
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.bridge.DocumentAccessBridge;
31    import org.xwiki.bridge.DocumentModelBridge;
32    import org.xwiki.model.reference.AttachmentReference;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
35    import org.xwiki.rendering.syntax.Syntax;
36    import org.xwiki.rendering.syntax.SyntaxType;
37    import org.xwiki.script.service.ScriptService;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    /**
41    * Unit test for {@link org.xwiki.officeimporter.script.OfficeImporterScriptService}.
42    *
43    * @version $Id: 0e48326bf775e44951de99941788010355013db7 $
44    */
 
45    public class OfficeImporterScriptServiceTest
46    {
47    /**
48    * A component manager that automatically mocks all dependencies of the component under test.
49    */
50    @Rule
51    public MockitoComponentMockingRule<ScriptService> mocker = new MockitoComponentMockingRule<ScriptService>(
52    OfficeImporterScriptService.class, ScriptService.class, "officeimporter");
53   
54    /**
55    * The component being tested.
56    */
57    private OfficeImporterScriptService officeImporterScriptService;
58   
59    private DocumentAccessBridge documentAccessBridge;
60   
 
61  2 toggle @Before
62    public void setUp() throws Exception
63    {
64  2 officeImporterScriptService = (OfficeImporterScriptService) mocker.getComponentUnderTest();
65  2 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
66    }
67   
 
68  1 toggle @Test
69    public void saveWithOverwrite() throws Exception
70    {
71  1 XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
72  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
73  1 DocumentReference parentReference = new DocumentReference("wiki", "Space", "Parent");
74  1 String syntaxId = "test/1.0";
75  1 String title = "Office Document Title";
76  1 String content = "Office Document Content";
77  1 String fileName = "logo.png";
78  1 byte[] fileContent = new byte[] {65, 82};
79   
80  1 when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
81  1 when(doc.getContentAsString(syntaxId)).thenReturn(content);
82  1 when(doc.getArtifacts()).thenReturn(Collections.singletonMap(fileName, fileContent));
83   
84  1 assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, parentReference, title, false));
85   
86  1 verify(documentAccessBridge).setDocumentSyntaxId(documentReference, syntaxId);
87  1 verify(documentAccessBridge).setDocumentContent(documentReference, content, "Created by office importer.",
88    false);
89  1 verify(documentAccessBridge).setDocumentParentReference(documentReference, parentReference);
90  1 verify(documentAccessBridge).setDocumentTitle(documentReference, title);
91  1 verify(documentAccessBridge).setAttachmentContent(new AttachmentReference(fileName, documentReference),
92    fileContent);
93    }
94   
 
95  1 toggle @Test
96    public void saveWithAppend() throws Exception
97    {
98  1 XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
99  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
100  1 String syntaxId = "test/1.0";
101   
102  1 when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
103  1 when(documentAccessBridge.exists(documentReference)).thenReturn(true);
104   
105  1 DocumentModelBridge document = mock(DocumentModelBridge.class);
106  1 when(documentAccessBridge.getDocument(documentReference)).thenReturn(document);
107  1 when(document.getSyntax()).thenReturn(new Syntax(new SyntaxType("test", "Test"), "1.0"));
108   
109  1 when(documentAccessBridge.getDocumentContent(documentReference, null)).thenReturn("before");
110  1 when(doc.getContentAsString(syntaxId)).thenReturn("after");
111   
112  1 assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, null, null, true));
113   
114  1 verify(documentAccessBridge).setDocumentContent(documentReference, "before\nafter",
115    "Updated by office importer.", false);
116    }
117    }