Search in sources :

Example 1 with WikiCreationStep

use of org.xwiki.platform.wiki.creationjob.WikiCreationStep in project xwiki-platform by xwiki.

the class WikiCreationJobTest method runInternalWithException.

@Test
public void runInternalWithException() throws Exception {
    // Mocks
    WikiCreationStep step1 = mock(WikiCreationStep.class);
    mocker.registerComponent(WikiCreationStep.class, "step1", step1);
    when(step1.getOrder()).thenReturn(100);
    WikiCreationException exception = new WikiCreationException("Error in the step");
    doThrow(exception).when(step1).execute(any(WikiCreationRequest.class));
    // Test
    WikiCreationRequest request = new WikiCreationRequest();
    request.setId(Arrays.asList("myrequest"));
    request.setWikiId("wikiId");
    mocker.getComponentUnderTest().start(request);
    // Verify
    verify(mocker.getMockedLogger()).error(any(Marker.class), eq("Exception thrown during job execution"), eq(new WikiCreationException("Failed to execute creation steps on the wiki [wikiId].", exception)));
}
Also used : WikiCreationException(org.xwiki.platform.wiki.creationjob.WikiCreationException) WikiCreationRequest(org.xwiki.platform.wiki.creationjob.WikiCreationRequest) Marker(org.slf4j.Marker) WikiCreationStep(org.xwiki.platform.wiki.creationjob.WikiCreationStep) Test(org.junit.Test)

Example 2 with WikiCreationStep

use of org.xwiki.platform.wiki.creationjob.WikiCreationStep in project xwiki-platform by xwiki.

the class WikiCreationJobTest method runInternal.

@Test
public void runInternal() throws Exception {
    // Mocks
    WikiCreationStep step1 = mock(WikiCreationStep.class);
    WikiCreationStep step2 = mock(WikiCreationStep.class);
    WikiCreationStep step3 = mock(WikiCreationStep.class);
    when(step1.getOrder()).thenReturn(100);
    when(step2.getOrder()).thenReturn(50);
    when(step3.getOrder()).thenReturn(75);
    mocker.registerComponent(WikiCreationStep.class, "step1", step1);
    mocker.registerComponent(WikiCreationStep.class, "step2", step2);
    mocker.registerComponent(WikiCreationStep.class, "step3", step3);
    // Test
    WikiCreationRequest request = new WikiCreationRequest();
    request.setId(Arrays.asList("myrequest"));
    mocker.getComponentUnderTest().start(request);
    // Verify
    InOrder inOrder = inOrder(step1, step2, step3, progressManager);
    // Verify that the steps are executed in the good order
    inOrder.verify(progressManager).pushLevelProgress(eq(3), any(Object.class));
    inOrder.verify(progressManager, calls(1)).startStep(any(Object.class));
    inOrder.verify(step2).execute(any(WikiCreationRequest.class));
    inOrder.verify(progressManager, calls(1)).startStep(any(Object.class));
    inOrder.verify(step3).execute(any(WikiCreationRequest.class));
    inOrder.verify(progressManager, calls(1)).startStep(any(Object.class));
    inOrder.verify(step1).execute(any(WikiCreationRequest.class));
    inOrder.verify(progressManager).popLevelProgress(any(Object.class));
}
Also used : InOrder(org.mockito.InOrder) WikiCreationRequest(org.xwiki.platform.wiki.creationjob.WikiCreationRequest) WikiCreationStep(org.xwiki.platform.wiki.creationjob.WikiCreationStep) Test(org.junit.Test)

Example 3 with WikiCreationStep

use of org.xwiki.platform.wiki.creationjob.WikiCreationStep in project xwiki-platform by xwiki.

the class WikiCreationJob method runInternal.

@Override
protected void runInternal() throws Exception {
    try {
        // Consider that the owner id is a serialized user reference and put it in the context as the current user
        // so that all steps are executed under that user.
        this.xcontextProvider.get().setUserReference(this.defaultDocumentReferenceResolver.resolve(getRequest().getOwnerId()));
        List<WikiCreationStep> wikiCreationStepList = componentManager.getInstanceList(WikiCreationStep.class);
        // Some extra steps needs to be executed AFTER some others, so we have introduce a getOrder() method in the
        // interface. We use this method to sort the list of extra steps by this order.
        Collections.sort(wikiCreationStepList, new Comparator<WikiCreationStep>() {

            @Override
            public int compare(WikiCreationStep o1, WikiCreationStep o2) {
                return o1.getOrder() - o2.getOrder();
            }
        });
        // Now we can execute these extra steps
        this.progressManager.pushLevelProgress(wikiCreationStepList.size(), this);
        for (WikiCreationStep step : wikiCreationStepList) {
            this.progressManager.startStep(this);
            step.execute(request);
            this.progressManager.endStep(this);
        }
        this.progressManager.popLevelProgress(this);
    } catch (WikiCreationException | ComponentLookupException e) {
        throw new WikiCreationException(String.format("Failed to execute creation steps on the wiki [%s].", request.getWikiId()), e);
    }
}
Also used : WikiCreationException(org.xwiki.platform.wiki.creationjob.WikiCreationException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiCreationStep(org.xwiki.platform.wiki.creationjob.WikiCreationStep)

Aggregations

WikiCreationStep (org.xwiki.platform.wiki.creationjob.WikiCreationStep)3 Test (org.junit.Test)2 WikiCreationException (org.xwiki.platform.wiki.creationjob.WikiCreationException)2 WikiCreationRequest (org.xwiki.platform.wiki.creationjob.WikiCreationRequest)2 InOrder (org.mockito.InOrder)1 Marker (org.slf4j.Marker)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1