Search in sources :

Example 1 with ExtensionBreakingQuestion

use of org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion 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 2 with ExtensionBreakingQuestion

use of org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion in project xwiki-platform by xwiki.

the class DocumentsDeletingListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    Job job = (Job) source;
    if (!job.getRequest().isInteractive()) {
        logger.warn("XAR Extension Documents Deleting Listener will not check the document in non-interactive mode.");
        return;
    }
    // Check if some pages belong to extensions
    Map<EntityReference, EntitySelection> concernedEntities = (Map<EntityReference, EntitySelection>) data;
    ExtensionBreakingQuestion question = new ExtensionBreakingQuestion(concernedEntities);
    for (EntitySelection entitySelection : concernedEntities.values()) {
        if (entitySelection.getEntityReference() instanceof DocumentReference) {
            checkIfPageBelongToExtensions(entitySelection, question);
        }
    }
    // Ask a confirmation to the user if some pages belong to extensions
    if (!question.getExtensions().isEmpty()) {
        // Conservative choice: we let the user enable the pages to delete.
        question.unselectAll();
        try {
            // The user can modify the question so it could disable some EntitySelection.
            // We add a timeout because when a refactoring job is running, it prevents others to run.
            // 5 minutes is probably enough for the user to decide if the process should go on.
            boolean ack = job.getStatus().ask(question, 5, TimeUnit.MINUTES);
            if (!ack) {
                // Without any confirmation, we must cancel the operation.
                String message = "The question has been asked, however no answer has been received.";
                this.logger.warn(message);
                CancelableEvent cancelableEvent = (CancelableEvent) event;
                cancelableEvent.cancel(message);
            }
        } catch (InterruptedException e) {
            this.logger.warn("Confirm question has been interrupted.");
            CancelableEvent cancelableEvent = (CancelableEvent) event;
            cancelableEvent.cancel("Question has been interrupted.");
        }
    }
}
Also used : EntitySelection(org.xwiki.refactoring.job.question.EntitySelection) EntityReference(org.xwiki.model.reference.EntityReference) ExtensionBreakingQuestion(org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion) Job(org.xwiki.job.Job) Map(java.util.Map) CancelableEvent(org.xwiki.observation.event.CancelableEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

ExtensionBreakingQuestion (org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion)2 Job (org.xwiki.job.Job)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 EntityReference (org.xwiki.model.reference.EntityReference)2 EntitySelection (org.xwiki.refactoring.job.question.EntitySelection)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 DocumentsDeletingEvent (org.xwiki.bridge.event.DocumentsDeletingEvent)1 ExtensionId (org.xwiki.extension.ExtensionId)1 XarInstalledExtension (org.xwiki.extension.xar.internal.repository.XarInstalledExtension)1 Request (org.xwiki.job.Request)1 JobStatus (org.xwiki.job.event.status.JobStatus)1 CancelableEvent (org.xwiki.observation.event.CancelableEvent)1