1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.platform.wiki.creationjob.internal

File WikiCreationJobTest.java

 

Code metrics

0
45
4
1
165
109
4
0.09
11.25
4
1

Classes

Class Line # Actions
WikiCreationJobTest 59 45 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.platform.wiki.creationjob.internal;
21   
22    import java.util.Arrays;
23   
24    import javax.inject.Provider;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.InOrder;
30    import org.slf4j.Marker;
31    import org.xwiki.component.util.DefaultParameterizedType;
32    import org.xwiki.context.Execution;
33    import org.xwiki.context.ExecutionContext;
34    import org.xwiki.context.ExecutionContextManager;
35    import org.xwiki.context.internal.DefaultExecution;
36    import org.xwiki.job.JobContext;
37    import org.xwiki.job.JobStatusStore;
38    import org.xwiki.job.event.status.JobProgressManager;
39    import org.xwiki.logging.LoggerManager;
40    import org.xwiki.observation.ObservationManager;
41    import org.xwiki.platform.wiki.creationjob.WikiCreationException;
42    import org.xwiki.platform.wiki.creationjob.WikiCreationRequest;
43    import org.xwiki.platform.wiki.creationjob.WikiCreationStep;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.mockito.ArgumentMatchers.any;
48    import static org.mockito.ArgumentMatchers.eq;
49    import static org.mockito.Mockito.calls;
50    import static org.mockito.Mockito.doThrow;
51    import static org.mockito.Mockito.inOrder;
52    import static org.mockito.Mockito.mock;
53    import static org.mockito.Mockito.verify;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * @version $Id: f67f187f4dd2a18cdcb490e2a37b70b6da44b499 $
58    */
 
59    public class WikiCreationJobTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<WikiCreationJob> mocker = new MockitoComponentMockingRule<>(WikiCreationJob.class);
63   
64    private ObservationManager observationManager;
65   
66    private LoggerManager loggerManager;
67   
68    private JobStatusStore store;
69   
70    private Provider<Execution> executionProvider;
71   
72    private Provider<ExecutionContextManager> executionContextManagerProvider;
73   
74    private JobContext jobContext;
75   
76    private JobProgressManager progressManager;
77   
78    private Execution execution = new DefaultExecution();
79   
80    private ExecutionContextManager executionContextManager;
81   
 
82  3 toggle @Before
83    public void setUp() throws Exception
84    {
85  3 observationManager = mocker.getInstance(ObservationManager.class);
86  3 loggerManager = mocker.getInstance(LoggerManager.class);
87  3 store = mocker.getInstance(JobStatusStore.class);
88  3 executionProvider = mock(Provider.class);
89  3 mocker.registerComponent(new DefaultParameterizedType(null, Provider.class, Execution.class),
90    executionProvider);
91  3 when(executionProvider.get()).thenReturn(execution);
92  3 executionContextManagerProvider = mock(Provider.class);
93  3 mocker.registerComponent(new DefaultParameterizedType(null, Provider.class,
94    ExecutionContextManager.class), executionContextManagerProvider);
95  3 executionContextManager = mock(ExecutionContextManager.class);
96  3 when(executionContextManagerProvider.get()).thenReturn(executionContextManager);
97  3 jobContext = mocker.getInstance(JobContext.class);
98  3 progressManager = mocker.getInstance(JobProgressManager.class);
99   
100  3 execution.pushContext(new ExecutionContext());
101    }
102   
103   
 
104  1 toggle @Test
105    public void runInternal() throws Exception
106    {
107    // Mocks
108  1 WikiCreationStep step1 = mock(WikiCreationStep.class);
109  1 WikiCreationStep step2 = mock(WikiCreationStep.class);
110  1 WikiCreationStep step3 = mock(WikiCreationStep.class);
111  1 when(step1.getOrder()).thenReturn(100);
112  1 when(step2.getOrder()).thenReturn(50);
113  1 when(step3.getOrder()).thenReturn(75);
114   
115  1 mocker.registerComponent(WikiCreationStep.class, "step1", step1);
116  1 mocker.registerComponent(WikiCreationStep.class, "step2", step2);
117  1 mocker.registerComponent(WikiCreationStep.class, "step3", step3);
118   
119    // Test
120  1 WikiCreationRequest request = new WikiCreationRequest();
121  1 request.setId(Arrays.asList("myrequest"));
122  1 mocker.getComponentUnderTest().start(request);
123   
124    // Verify
125  1 InOrder inOrder = inOrder(step1, step2, step3, progressManager);
126    // Verify that the steps are executed in the good order
127  1 inOrder.verify(progressManager).pushLevelProgress(eq(3), any(Object.class));
128  1 inOrder.verify(progressManager, calls(1)).startStep(any(Object.class));
129  1 inOrder.verify(step2).execute(any(WikiCreationRequest.class));
130  1 inOrder.verify(progressManager, calls(1)).startStep(any(Object.class));
131  1 inOrder.verify(step3).execute(any(WikiCreationRequest.class));
132  1 inOrder.verify(progressManager, calls(1)).startStep(any(Object.class));
133  1 inOrder.verify(step1).execute(any(WikiCreationRequest.class));
134  1 inOrder.verify(progressManager).popLevelProgress(any(Object.class));
135    }
136   
137   
 
138  1 toggle @Test
139    public void runInternalWithException() throws Exception
140    {
141    // Mocks
142  1 WikiCreationStep step1 = mock(WikiCreationStep.class);
143  1 mocker.registerComponent(WikiCreationStep.class, "step1", step1);
144  1 when(step1.getOrder()).thenReturn(100);
145   
146  1 WikiCreationException exception = new WikiCreationException("Error in the step");
147  1 doThrow(exception).when(step1).execute(any(WikiCreationRequest.class));
148   
149    // Test
150  1 WikiCreationRequest request = new WikiCreationRequest();
151  1 request.setId(Arrays.asList("myrequest"));
152  1 request.setWikiId("wikiId");
153  1 mocker.getComponentUnderTest().start(request);
154   
155    // Verify
156  1 verify(mocker.getMockedLogger()).error(any(Marker.class), eq("Exception thrown during job execution"),
157    eq(new WikiCreationException("Failed to execute creation steps on the wiki [wikiId].", exception)));
158    }
159   
 
160  1 toggle @Test
161    public void getType() throws Exception
162    {
163  1 assertEquals("wikicreationjob", mocker.getComponentUnderTest().getType());
164    }
165    }