1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.xar.internal.delete

File DocumentsDeletingListenerTest.java

 

Code metrics

0
76
4
1
202
144
4
0.05
19
4
1

Classes

Class Line # Actions
DocumentsDeletingListenerTest 63 76 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.xar.internal.delete;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.Map;
26    import java.util.concurrent.TimeUnit;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.bridge.event.DocumentsDeletingEvent;
32    import org.xwiki.extension.ExtensionId;
33    import org.xwiki.extension.repository.InstalledExtensionRepository;
34    import org.xwiki.extension.xar.internal.delete.question.ExtensionBreakingQuestion;
35    import org.xwiki.extension.xar.internal.repository.XarInstalledExtension;
36    import org.xwiki.extension.xar.internal.repository.XarInstalledExtensionRepository;
37    import org.xwiki.job.Job;
38    import org.xwiki.job.Request;
39    import org.xwiki.job.event.status.JobStatus;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.model.reference.EntityReference;
42    import org.xwiki.refactoring.job.question.EntitySelection;
43    import org.xwiki.security.authorization.Right;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertFalse;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.ArgumentMatchers.any;
50    import static org.mockito.ArgumentMatchers.anyLong;
51    import static org.mockito.ArgumentMatchers.eq;
52    import static org.mockito.Mockito.doAnswer;
53    import static org.mockito.Mockito.doThrow;
54    import static org.mockito.Mockito.mock;
55    import static org.mockito.Mockito.times;
56    import static org.mockito.Mockito.verify;
57    import static org.mockito.Mockito.verifyZeroInteractions;
58    import static org.mockito.Mockito.when;
59   
60    /**
61    * @version $Id: 447ba68b041b4cfdb3db0b78787de627fae3be4e $
62    */
 
63    public class DocumentsDeletingListenerTest
64    {
65    @Rule
66    public MockitoComponentMockingRule<DocumentsDeletingListener> mocker =
67    new MockitoComponentMockingRule(DocumentsDeletingListener.class);
68   
69    protected XarInstalledExtensionRepository repository;
70   
 
71  3 toggle @Before
72    public void setUp() throws Exception
73    {
74  3 repository = mock(XarInstalledExtensionRepository.class);
75  3 mocker.registerComponent(InstalledExtensionRepository.class, "xar", repository);
76    }
77   
 
78  1 toggle @Test
79    public void test() throws Exception
80    {
81  1 Request request = mock(Request.class);
82  1 Job job = mock(Job.class);
83  1 JobStatus status = mock(JobStatus.class);
84  1 when(job.getRequest()).thenReturn(request);
85  1 when(request.isInteractive()).thenReturn(true);
86  1 when(job.getStatus()).thenReturn(status);
87   
88  1 Map<EntityReference, EntitySelection> concernedEntities = new HashMap<>();
89   
90  1 DocumentReference doc1 = new DocumentReference("a", "b", "c1");
91  1 DocumentReference doc2 = new DocumentReference("a", "b", "c2");
92  1 DocumentReference doc3 = new DocumentReference("a", "b", "c3");
93  1 DocumentReference doc4 = new DocumentReference("a", "b", "c4");
94   
95  1 concernedEntities.put(doc1, new EntitySelection(doc1));
96  1 concernedEntities.put(doc2, new EntitySelection(doc2));
97  1 concernedEntities.put(doc3, new EntitySelection(doc3));
98  1 concernedEntities.put(doc4, new EntitySelection(doc4));
99   
100  1 XarInstalledExtension ext1 = mock(XarInstalledExtension.class);
101  1 XarInstalledExtension ext2 = mock(XarInstalledExtension.class);
102  1 when(ext1.getId()).thenReturn(new ExtensionId("ext1"));
103  1 when(ext2.getId()).thenReturn(new ExtensionId("ext2"));
104  1 when(repository.getXarInstalledExtensions(doc1)).thenReturn(Arrays.asList(ext1, ext2));
105  1 when(repository.isAllowed(doc1, Right.DELETE)).thenReturn(false);
106  1 when(repository.getXarInstalledExtensions(doc2)).thenReturn(Collections.emptyList());
107  1 when(repository.isAllowed(doc2, Right.DELETE)).thenReturn(true);
108  1 when(repository.getXarInstalledExtensions(doc3)).thenReturn(Arrays.asList(ext2));
109  1 when(repository.isAllowed(doc3, Right.DELETE)).thenReturn(false);
110  1 when(repository.getXarInstalledExtensions(doc4)).thenReturn(Arrays.asList(ext1));
111  1 when(repository.isAllowed(doc4, Right.DELETE)).thenReturn(true);
112   
113  1 doAnswer(invocationOnMock -> {
114  1 ExtensionBreakingQuestion question = invocationOnMock.getArgument(0);
115  1 assertEquals(concernedEntities, question.getConcernedEntities());
116    // Ext 1
117  1 assertEquals(1, question.getExtension("ext1").getPages().size());
118  1 assertTrue(question.getExtension("ext1").getPages().contains(
119    concernedEntities.get(doc1)
120    ));
121   
122    // Ext 2
123  1 assertEquals(2, question.getExtension("ext2").getPages().size());
124  1 assertTrue(question.getExtension("ext2").getPages().contains(
125    concernedEntities.get(doc1)
126    ));
127  1 assertTrue(question.getExtension("ext2").getPages().contains(
128    concernedEntities.get(doc3)
129    ));
130   
131    // Free pages
132  1 assertEquals(2, question.getFreePages().size());
133  1 assertTrue(question.getFreePages().contains(concernedEntities.get(doc2)));
134  1 assertTrue(question.getFreePages().contains(concernedEntities.get(doc4)));
135   
136    // Assert nothing is select by default
137  1 for (EntitySelection selection : question.getConcernedEntities().values()) {
138  4 assertFalse(selection.isSelected());
139    }
140  1 return null;
141    }).when(status).ask(any(), anyLong(), any());
142   
143    // Test
144  1 DocumentsDeletingEvent event = new DocumentsDeletingEvent();
145  1 mocker.getComponentUnderTest().onEvent(event, job, concernedEntities);
146   
147    // Check
148  1 verify(status, times(1)).ask(any(), eq(5L), eq(TimeUnit.MINUTES));
149    }
150   
 
151  1 toggle @Test
152    public void testWhenNonInteractive() throws Exception
153    {
154  1 Request request = mock(Request.class);
155  1 Job job = mock(Job.class);
156  1 when(job.getRequest()).thenReturn(request);
157  1 when(request.isInteractive()).thenReturn(false);
158  1 JobStatus status = mock(JobStatus.class);
159  1 when(job.getStatus()).thenReturn(status);
160   
161    // Test
162  1 DocumentsDeletingEvent event = new DocumentsDeletingEvent();
163  1 mocker.getComponentUnderTest().onEvent(event, job, null);
164   
165    // Verify
166  1 verify(mocker.getMockedLogger()).warn(
167    "XAR Extension Documents Deleting Listener will not check the document in non-interactive mode.");
168  1 verifyZeroInteractions(status);
169  1 verifyZeroInteractions(repository);
170    }
171   
 
172  1 toggle @Test
173    public void testCancel() throws Exception
174    {
175  1 Request request = mock(Request.class);
176  1 Job job = mock(Job.class);
177  1 JobStatus status = mock(JobStatus.class);
178  1 when(job.getRequest()).thenReturn(request);
179  1 when(request.isInteractive()).thenReturn(true);
180  1 when(job.getStatus()).thenReturn(status);
181   
182  1 Map<EntityReference, EntitySelection> concernedEntities = new HashMap<>();
183  1 DocumentReference doc1 = new DocumentReference("a", "b", "c1");
184  1 concernedEntities.put(doc1, new EntitySelection(doc1));
185   
186  1 XarInstalledExtension ext1 = mock(XarInstalledExtension.class);
187  1 when(ext1.getId()).thenReturn(new ExtensionId("ext1"));
188  1 when(repository.getXarInstalledExtensions(doc1)).thenReturn(Arrays.asList(ext1));
189   
190  1 InterruptedException e = new InterruptedException();
191  1 doThrow(e).when(status).ask(any(), anyLong(), any());
192   
193    // Test
194  1 DocumentsDeletingEvent event = mock(DocumentsDeletingEvent.class);
195  1 mocker.getComponentUnderTest().onEvent(event, job, concernedEntities);
196   
197    // Check
198  1 verify(status, times(1)).ask(any(), eq(5L), eq(TimeUnit.MINUTES));
199  1 verify(event).cancel(eq("Question has been interrupted."));
200  1 verify(mocker.getMockedLogger()).warn("Confirm question has been interrupted.");
201    }
202    }