Search in sources :

Example 26 with Job

use of org.xwiki.job.Job 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 27 with Job

use of org.xwiki.job.Job 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)

Example 28 with Job

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

the class ExtensionManagerScriptService method createUninstallPlan.

/**
 * Adds a new job to the job queue to perform the given uninstall plan request.
 * <p>
 * This method requires programming rights.
 *
 * @param uninstallRequest the uninstall plan request to perform
 * @return the {@link Job} object which can be used to monitor the progress of the uninstall plan process, or
 *         {@code null} in case of failure
 */
private Job createUninstallPlan(UninstallRequest uninstallRequest) {
    setError(null);
    if (!this.authorization.hasAccess(Right.PROGRAM)) {
        // Make sure only PR user can remove the right checking or change the users
        setRightsProperties(uninstallRequest);
    }
    Job job = null;
    try {
        job = this.jobExecutor.execute(UninstallPlanJob.JOBTYPE, uninstallRequest);
    } catch (JobException e) {
        setError(e);
    }
    return job;
}
Also used : JobException(org.xwiki.job.JobException) UpgradePlanJob(org.xwiki.extension.job.internal.UpgradePlanJob) UninstallPlanJob(org.xwiki.extension.job.internal.UninstallPlanJob) UninstallJob(org.xwiki.extension.job.internal.UninstallJob) InstallJob(org.xwiki.extension.job.internal.InstallJob) Job(org.xwiki.job.Job) InstallPlanJob(org.xwiki.extension.job.internal.InstallPlanJob) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob)

Example 29 with Job

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

the class ExtensionManagerScriptService method createInstallPlan.

/**
 * Start the asynchronous installation plan creation process for an extension.
 *
 * @param installRequest installation instructions
 * @return the {@link Job} object which can be used to monitor the progress of the installation process, or
 *         {@code null} in case of failure
 */
public Job createInstallPlan(InstallRequest installRequest) {
    setError(null);
    if (!this.authorization.hasAccess(Right.PROGRAM)) {
        // Make sure only PR user can remove the right checking or change the users
        setRightsProperties(installRequest);
    }
    Job job = null;
    try {
        job = this.jobExecutor.execute(InstallPlanJob.JOBTYPE, installRequest);
    } catch (JobException e) {
        setError(e);
    }
    return job;
}
Also used : JobException(org.xwiki.job.JobException) UpgradePlanJob(org.xwiki.extension.job.internal.UpgradePlanJob) UninstallPlanJob(org.xwiki.extension.job.internal.UninstallPlanJob) UninstallJob(org.xwiki.extension.job.internal.UninstallJob) InstallJob(org.xwiki.extension.job.internal.InstallJob) Job(org.xwiki.job.Job) InstallPlanJob(org.xwiki.extension.job.internal.InstallPlanJob) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob)

Example 30 with Job

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

the class ExtensionManagerScriptService method uninstall.

/**
 * Adds a new job to the job queue to perform the given uninstall request.
 * <p>
 * This method requires programming rights.
 *
 * @param uninstallRequest the uninstall request to perform
 * @return the {@link Job} object which can be used to monitor the progress of the uninstall process, or
 *         {@code null} in case of failure
 */
public Job uninstall(UninstallRequest uninstallRequest) {
    setError(null);
    // Force proper id
    String extensionId = uninstallRequest.getExtensions().iterator().next().getId();
    String namespace = uninstallRequest.getNamespaces() == null ? null : uninstallRequest.getNamespaces().iterator().next();
    uninstallRequest.setId(ExtensionRequest.getJobId(ExtensionRequest.JOBID_ACTION_PREFIX, extensionId, namespace));
    // Check rights
    if (!this.authorization.hasAccess(Right.PROGRAM)) {
        // Make sure only PR user can remove the right checking or change the users
        setRightsProperties(uninstallRequest);
    }
    // Start job
    Job job = null;
    try {
        job = this.jobExecutor.execute(UninstallJob.JOBTYPE, uninstallRequest);
    } catch (JobException e) {
        setError(e);
    }
    return job;
}
Also used : JobException(org.xwiki.job.JobException) UpgradePlanJob(org.xwiki.extension.job.internal.UpgradePlanJob) UninstallPlanJob(org.xwiki.extension.job.internal.UninstallPlanJob) UninstallJob(org.xwiki.extension.job.internal.UninstallJob) InstallJob(org.xwiki.extension.job.internal.InstallJob) Job(org.xwiki.job.Job) InstallPlanJob(org.xwiki.extension.job.internal.InstallPlanJob) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob)

Aggregations

Job (org.xwiki.job.Job)41 Test (org.junit.Test)11 JobStatus (org.xwiki.job.event.status.JobStatus)10 AbstractExtensionJob (org.xwiki.extension.job.internal.AbstractExtensionJob)8 InstallJob (org.xwiki.extension.job.internal.InstallJob)8 UninstallJob (org.xwiki.extension.job.internal.UninstallJob)8 JobException (org.xwiki.job.JobException)8 DocumentReference (org.xwiki.model.reference.DocumentReference)8 InstallRequest (org.xwiki.extension.job.InstallRequest)7 UpgradePlanJob (org.xwiki.extension.job.internal.UpgradePlanJob)7 InstallPlanJob (org.xwiki.extension.job.internal.InstallPlanJob)6 UninstallPlanJob (org.xwiki.extension.job.internal.UninstallPlanJob)6 XWikiException (com.xpn.xwiki.XWikiException)5 LogEvent (org.xwiki.logging.event.LogEvent)5 InstallException (org.xwiki.extension.InstallException)4 UninstallException (org.xwiki.extension.UninstallException)4 XarInstalledExtension (org.xwiki.extension.xar.internal.repository.XarInstalledExtension)4 EntityReference (org.xwiki.model.reference.EntityReference)4 WikiReference (org.xwiki.model.reference.WikiReference)4 IOException (java.io.IOException)3