Search in sources :

Example 1 with Request

use of org.xwiki.job.Request in project xwiki-platform by xwiki.

the class DocumentsModifiedDuringDistributionListener method checkXARHandler.

private void checkXARHandler(Event event, XWikiDocument document, XWikiContext xcontext) {
    ExecutionContext context = this.execution.getContext();
    if (context != null) {
        XarExtensionPlan xarExtensionPlan = (XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN);
        if (xarExtensionPlan != null) {
            Request request = this.jobContext.getCurrentJob().getRequest();
            // It's a job started by the Distribution Wizard
            if (StringUtils.equals(request.<String>getProperty("context.action"), "distribution")) {
                String distributionWiki = request.getProperty("context.wiki");
                if (distributionWiki != null) {
                    DocumentReference reference = document.getDocumentReferenceWithLocale();
                    DocumentStatus.Action action = toAction(event);
                    LocalExtension previousExtension = xarExtensionPlan.getPreviousXarExtension(reference);
                    LocalExtension nextExtension = xarExtensionPlan.getNextXarExtension(reference);
                    addDocument(distributionWiki, document, action, previousExtension, nextExtension);
                }
            }
        }
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XarExtensionPlan(org.xwiki.extension.xar.internal.handler.XarExtensionPlan) Request(org.xwiki.job.Request) Action(org.xwiki.extension.distribution.internal.DocumentsModifiedDuringDistributionListener.DocumentStatus.Action) LocalExtension(org.xwiki.extension.LocalExtension) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with Request

use of org.xwiki.job.Request in project xwiki-platform by xwiki.

the class DocumentsDeletingListenerTest method testCancel.

@Test
public void testCancel() throws Exception {
    Request request = mock(Request.class);
    Job job = mock(Job.class);
    JobStatus status = mock(JobStatus.class);
    when(job.getRequest()).thenReturn(request);
    when(request.isInteractive()).thenReturn(true);
    when(job.getStatus()).thenReturn(status);
    Map<EntityReference, EntitySelection> concernedEntities = new HashMap<>();
    DocumentReference doc1 = new DocumentReference("a", "b", "c1");
    concernedEntities.put(doc1, new EntitySelection(doc1));
    XarInstalledExtension ext1 = mock(XarInstalledExtension.class);
    when(ext1.getId()).thenReturn(new ExtensionId("ext1"));
    when(repository.getXarInstalledExtensions(doc1)).thenReturn(Arrays.asList(ext1));
    InterruptedException e = new InterruptedException();
    doThrow(e).when(status).ask(any(), anyLong(), any());
    // Test
    DocumentsDeletingEvent event = mock(DocumentsDeletingEvent.class);
    mocker.getComponentUnderTest().onEvent(event, job, concernedEntities);
    // Check
    verify(status, times(1)).ask(any(), eq(5L), eq(TimeUnit.MINUTES));
    verify(event).cancel(eq("Question has been interrupted."));
    verify(mocker.getMockedLogger()).warn("Confirm question has been interrupted.");
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) HashMap(java.util.HashMap) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) EntitySelection(org.xwiki.refactoring.job.question.EntitySelection) Request(org.xwiki.job.Request) EntityReference(org.xwiki.model.reference.EntityReference) ExtensionId(org.xwiki.extension.ExtensionId) Job(org.xwiki.job.Job) DocumentReference(org.xwiki.model.reference.DocumentReference) DocumentsDeletingEvent(org.xwiki.bridge.event.DocumentsDeletingEvent) Test(org.junit.Test)

Example 3 with Request

use of org.xwiki.job.Request in project xwiki-platform by xwiki.

the class ExtensionJobEventConverter method fromRemote.

@Override
public boolean fromRemote(RemoteEventData remoteEvent, LocalEventData localEvent) {
    if (remoteEvent.getEvent() instanceof JobStartedEvent) {
        JobStartedEvent jobEvent = (JobStartedEvent) remoteEvent.getEvent();
        if (JOBS.contains(jobEvent.getJobType())) {
            Request request = jobEvent.getRequest();
            // Indicate the job has been triggered by a remote event
            if (!(request instanceof AbstractRequest)) {
                request = new DefaultRequest(request);
            }
            ((AbstractRequest) request).setRemote(true);
            // We don't want to directly simulate a new JobStartedEvent event but we want to start a new job
            // which will generate a new JobStartedEvent
            localEvent.setEvent(new RemoteExtensionJobStartedEvent(jobEvent.getJobType(), request));
            return true;
        }
    }
    return false;
}
Also used : DefaultRequest(org.xwiki.job.DefaultRequest) AbstractRequest(org.xwiki.job.AbstractRequest) Request(org.xwiki.job.Request) AbstractRequest(org.xwiki.job.AbstractRequest) DefaultRequest(org.xwiki.job.DefaultRequest) JobStartedEvent(org.xwiki.job.event.JobStartedEvent)

Example 4 with Request

use of org.xwiki.job.Request in project xwiki-platform by xwiki.

the class DocumentsDeletingListenerTest method test.

@Test
public void test() throws Exception {
    Request request = mock(Request.class);
    Job job = mock(Job.class);
    JobStatus status = mock(JobStatus.class);
    when(job.getRequest()).thenReturn(request);
    when(request.isInteractive()).thenReturn(true);
    when(job.getStatus()).thenReturn(status);
    Map<EntityReference, EntitySelection> concernedEntities = new HashMap<>();
    DocumentReference doc1 = new DocumentReference("a", "b", "c1");
    DocumentReference doc2 = new DocumentReference("a", "b", "c2");
    DocumentReference doc3 = new DocumentReference("a", "b", "c3");
    concernedEntities.put(doc1, new EntitySelection(doc1));
    concernedEntities.put(doc2, new EntitySelection(doc2));
    concernedEntities.put(doc3, new EntitySelection(doc3));
    XarInstalledExtension ext1 = mock(XarInstalledExtension.class);
    XarInstalledExtension ext2 = mock(XarInstalledExtension.class);
    when(ext1.getId()).thenReturn(new ExtensionId("ext1"));
    when(ext2.getId()).thenReturn(new ExtensionId("ext2"));
    when(repository.getXarInstalledExtensions(doc1)).thenReturn(Arrays.asList(ext1, ext2));
    when(repository.getXarInstalledExtensions(doc2)).thenReturn(Collections.emptyList());
    when(repository.getXarInstalledExtensions(doc3)).thenReturn(Arrays.asList(ext2));
    doAnswer(invocationOnMock -> {
        ExtensionBreakingQuestion question = invocationOnMock.getArgument(0);
        assertEquals(concernedEntities, question.getConcernedEntities());
        // Ext 1
        assertEquals(1, question.getExtension("ext1").getPages().size());
        assertTrue(question.getExtension("ext1").getPages().contains(concernedEntities.get(doc1)));
        // Ext 2
        assertEquals(2, question.getExtension("ext2").getPages().size());
        assertTrue(question.getExtension("ext2").getPages().contains(concernedEntities.get(doc1)));
        assertTrue(question.getExtension("ext2").getPages().contains(concernedEntities.get(doc3)));
        // Free pages
        assertEquals(1, question.getFreePages().size());
        assertTrue(question.getFreePages().contains(concernedEntities.get(doc2)));
        // Assert nothing is select by default
        for (EntitySelection selection : question.getConcernedEntities().values()) {
            assertFalse(selection.isSelected());
        }
        return null;
    }).when(status).ask(any(), anyLong(), any());
    // Test
    DocumentsDeletingEvent event = new DocumentsDeletingEvent();
    mocker.getComponentUnderTest().onEvent(event, job, concernedEntities);
    // Check
    verify(status, times(1)).ask(any(), eq(5L), eq(TimeUnit.MINUTES));
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) HashMap(java.util.HashMap) XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) EntitySelection(org.xwiki.refactoring.job.question.EntitySelection) Request(org.xwiki.job.Request) EntityReference(org.xwiki.model.reference.EntityReference) ExtensionBreakingQuestion(org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion) ExtensionId(org.xwiki.extension.ExtensionId) Job(org.xwiki.job.Job) DocumentReference(org.xwiki.model.reference.DocumentReference) DocumentsDeletingEvent(org.xwiki.bridge.event.DocumentsDeletingEvent) Test(org.junit.Test)

Example 5 with Request

use of org.xwiki.job.Request in project xwiki-platform by xwiki.

the class DocumentsDeletingListenerTest method testWhenNonInteractive.

@Test
public void testWhenNonInteractive() throws Exception {
    Request request = mock(Request.class);
    Job job = mock(Job.class);
    when(job.getRequest()).thenReturn(request);
    when(request.isInteractive()).thenReturn(false);
    JobStatus status = mock(JobStatus.class);
    when(job.getStatus()).thenReturn(status);
    // Test
    DocumentsDeletingEvent event = new DocumentsDeletingEvent();
    mocker.getComponentUnderTest().onEvent(event, job, null);
    // Verify
    verify(mocker.getMockedLogger()).warn("XAR Extension Documents Deleting Listener will not check the document in non-interactive mode.");
    verifyZeroInteractions(status);
    verifyZeroInteractions(repository);
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) Request(org.xwiki.job.Request) Job(org.xwiki.job.Job) DocumentsDeletingEvent(org.xwiki.bridge.event.DocumentsDeletingEvent) Test(org.junit.Test)

Aggregations

Request (org.xwiki.job.Request)5 Test (org.junit.Test)3 DocumentsDeletingEvent (org.xwiki.bridge.event.DocumentsDeletingEvent)3 Job (org.xwiki.job.Job)3 JobStatus (org.xwiki.job.event.status.JobStatus)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 HashMap (java.util.HashMap)2 ExtensionId (org.xwiki.extension.ExtensionId)2 XarInstalledExtension (org.xwiki.extension.xar.internal.repository.XarInstalledExtension)2 EntityReference (org.xwiki.model.reference.EntityReference)2 EntitySelection (org.xwiki.refactoring.job.question.EntitySelection)2 ExecutionContext (org.xwiki.context.ExecutionContext)1 LocalExtension (org.xwiki.extension.LocalExtension)1 Action (org.xwiki.extension.distribution.internal.DocumentsModifiedDuringDistributionListener.DocumentStatus.Action)1 ExtensionBreakingQuestion (org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion)1 XarExtensionPlan (org.xwiki.extension.xar.internal.handler.XarExtensionPlan)1 AbstractRequest (org.xwiki.job.AbstractRequest)1 DefaultRequest (org.xwiki.job.DefaultRequest)1 JobStartedEvent (org.xwiki.job.event.JobStartedEvent)1