| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
| 58 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (49) |
Complexity: 4 |
Complexity Density: 0.09 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 82 |
3 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
| 104 |
1 |
@Test... |
| 105 |
|
public void runInternal() throws Exception |
| 106 |
|
{ |
| 107 |
|
|
| 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 |
|
|
| 120 |
1 |
WikiCreationRequest request = new WikiCreationRequest(); |
| 121 |
1 |
request.setId(Arrays.asList("myrequest")); |
| 122 |
1 |
mocker.getComponentUnderTest().start(request); |
| 123 |
|
|
| 124 |
|
|
| 125 |
1 |
InOrder inOrder = inOrder(step1, step2, step3, progressManager); |
| 126 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 138 |
1 |
@Test... |
| 139 |
|
public void runInternalWithException() throws Exception |
| 140 |
|
{ |
| 141 |
|
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 160 |
1 |
@Test... |
| 161 |
|
public void getType() throws Exception |
| 162 |
|
{ |
| 163 |
1 |
assertEquals("wikicreationjob", mocker.getComponentUnderTest().getType()); |
| 164 |
|
} |
| 165 |
|
} |