1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.xar.script

File XarExtensionScriptServiceTest.java

 

Code metrics

0
36
3
1
133
94
3
0.08
12
3
1

Classes

Class Line # Actions
XarExtensionScriptServiceTest 59 36 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.extension.xar.script;
21   
22    import java.util.List;
23   
24    import org.junit.jupiter.api.BeforeEach;
25    import org.junit.jupiter.api.Test;
26    import org.mockito.ArgumentCaptor;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.context.Execution;
29    import org.xwiki.context.ExecutionContext;
30    import org.xwiki.extension.ExtensionId;
31    import org.xwiki.extension.internal.validator.AbstractExtensionValidator;
32    import org.xwiki.extension.job.ExtensionRequest;
33    import org.xwiki.extension.job.InstallRequest;
34    import org.xwiki.extension.version.Version;
35    import org.xwiki.extension.xar.internal.job.DiffXarJob;
36    import org.xwiki.extension.xar.internal.job.RepairXarJob;
37    import org.xwiki.job.Job;
38    import org.xwiki.job.JobException;
39    import org.xwiki.job.JobExecutor;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.security.authorization.ContextualAuthorizationManager;
42    import org.xwiki.security.authorization.Right;
43    import org.xwiki.test.junit5.mockito.ComponentTest;
44    import org.xwiki.test.junit5.mockito.InjectMockComponents;
45    import org.xwiki.test.junit5.mockito.MockComponent;
46   
47    import static org.junit.jupiter.api.Assertions.assertEquals;
48    import static org.mockito.ArgumentMatchers.eq;
49    import static org.mockito.Mockito.verify;
50    import static org.mockito.Mockito.when;
51   
52    /**
53    * Tests of {@link XarExtensionScriptService}.
54    *
55    * @version $Id: 88da20993bfaa78efb321f75a1b5359d280fb3ee $
56    * @since 11.0
57    */
58    @ComponentTest
 
59    public class XarExtensionScriptServiceTest
60    {
61    @InjectMockComponents
62    private XarExtensionScriptService xarExtensionScriptService;
63   
64    @MockComponent
65    private JobExecutor jobExecutor;
66   
67    @MockComponent
68    private ContextualAuthorizationManager authorization;
69   
70    @MockComponent
71    private DocumentAccessBridge documentAccessBridge;
72   
73    @MockComponent
74    private Execution execution;
75   
76    private ExecutionContext executionContext = new ExecutionContext();
77   
 
78  2 toggle @BeforeEach
79    public void setup()
80    {
81  2 when(execution.getContext()).thenReturn(executionContext);
82    }
83   
 
84  1 toggle @Test
85    public void repairInstalledExtension() throws JobException
86    {
87  1 when(this.authorization.hasAccess(Right.PROGRAM)).thenReturn(true);
88  1 String wiki = "subwiki";
89  1 String extensionId = "fakeextension";
90  1 String version = "1.3";
91  1 String wikiNamespace = "wiki:subwiki";
92  1 InstallRequest expectedInstallRequest = new InstallRequest();
93  1 List<String> jobId =
94    ExtensionRequest.getJobId(ExtensionRequest.JOBID_ACTION_PREFIX, extensionId, wikiNamespace);
95  1 expectedInstallRequest.setId(jobId);
96  1 DocumentReference userReference = new DocumentReference("wiki", "Alice", "WebHome");
97  1 when(this.documentAccessBridge.getCurrentUserReference()).thenReturn(userReference);
98  1 expectedInstallRequest.setProperty(AbstractExtensionValidator.PROPERTY_USERREFERENCE, userReference);
99  1 expectedInstallRequest.setExtensionProperty(AbstractExtensionValidator.PROPERTY_USERREFERENCE,
100    userReference.toString());
101  1 expectedInstallRequest.addExtension(new ExtensionId(extensionId, version));
102  1 expectedInstallRequest.addNamespace(wikiNamespace);
103   
104  1 this.xarExtensionScriptService.repairInstalledExtension(extensionId, version, wiki);
105  1 ArgumentCaptor<InstallRequest> installRequestArgumentCaptor = ArgumentCaptor.forClass(InstallRequest.class);
106  1 verify(this.jobExecutor).execute(eq(RepairXarJob.JOBTYPE), installRequestArgumentCaptor.capture());
107  1 InstallRequest installRequest = installRequestArgumentCaptor.getValue();
108  1 assertEquals(expectedInstallRequest.getId(), installRequest.getId());
109  1 assertEquals(expectedInstallRequest.getProperties(), installRequest.getProperties());
110    }
111   
 
112  1 toggle @Test
113    public void diff() throws JobException
114    {
115  1 when(this.authorization.hasAccess(Right.PROGRAM)).thenReturn(true);
116  1 String wiki = "subwiki";
117  1 String feature = "fakefeature";
118  1 String wikiNamespace = "wiki:subwiki";
119  1 InstallRequest expectedInstallRequest = new InstallRequest();
120  1 List<String> jobId =
121    ExtensionRequest.getJobId(ExtensionRequest.JOBID_ACTION_PREFIX, feature, wikiNamespace);
122  1 expectedInstallRequest.setId(jobId);
123  1 expectedInstallRequest.addExtension(new ExtensionId(feature, (Version) null));
124  1 expectedInstallRequest.addNamespace(wikiNamespace);
125   
126  1 this.xarExtensionScriptService.diff(feature, wiki);
127  1 ArgumentCaptor<InstallRequest> installRequestArgumentCaptor = ArgumentCaptor.forClass(InstallRequest.class);
128  1 verify(this.jobExecutor).execute(eq(DiffXarJob.JOB_TYPE), installRequestArgumentCaptor.capture());
129  1 InstallRequest installRequest = installRequestArgumentCaptor.getValue();
130  1 assertEquals(expectedInstallRequest.getId(), installRequest.getId());
131  1 assertEquals(expectedInstallRequest.getProperties(), installRequest.getProperties());
132    }
133    }