1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
42 |
|
|
43 |
|
@version |
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 3 |
Complexity Density: 0.1 |
|
45 |
|
public class OfficeImporterScriptServiceTest |
46 |
|
{ |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@Rule |
51 |
|
public MockitoComponentMockingRule<ScriptService> mocker = new MockitoComponentMockingRule<ScriptService>( |
52 |
|
OfficeImporterScriptService.class, ScriptService.class, "officeimporter"); |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private OfficeImporterScriptService officeImporterScriptService; |
58 |
|
|
59 |
|
private DocumentAccessBridge documentAccessBridge; |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
61 |
2 |
@Before... |
62 |
|
public void setUp() throws Exception |
63 |
|
{ |
64 |
2 |
officeImporterScriptService = (OfficeImporterScriptService) mocker.getComponentUnderTest(); |
65 |
2 |
documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class); |
66 |
|
} |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
68 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
95 |
1 |
@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 |
|
} |