| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wiki.internal.descriptor.listener; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import org.junit.Before; |
| 26 |
|
import org.junit.Rule; |
| 27 |
|
import org.junit.Test; |
| 28 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
| 29 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
| 30 |
|
import org.xwiki.model.reference.DocumentReference; |
| 31 |
|
import org.xwiki.observation.event.Event; |
| 32 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 33 |
|
import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor; |
| 34 |
|
import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilder; |
| 35 |
|
import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper; |
| 36 |
|
import org.xwiki.wiki.internal.manager.WikiDescriptorCache; |
| 37 |
|
|
| 38 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 39 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 40 |
|
|
| 41 |
|
import static org.mockito.ArgumentMatchers.any; |
| 42 |
|
import static org.mockito.Mockito.mock; |
| 43 |
|
import static org.mockito.Mockito.never; |
| 44 |
|
import static org.mockito.Mockito.verify; |
| 45 |
|
import static org.mockito.Mockito.when; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@link |
| 49 |
|
|
| 50 |
|
@since |
| 51 |
|
@version |
| 52 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (44) |
Complexity: 3 |
Complexity Density: 0.07 |
|
| 53 |
|
public class WikiDescriptorListenerTest |
| 54 |
|
{ |
| 55 |
|
@Rule |
| 56 |
|
public org.xwiki.test.mockito.MockitoComponentMockingRule<WikiDescriptorListener> mocker = |
| 57 |
|
new MockitoComponentMockingRule(WikiDescriptorListener.class); |
| 58 |
|
|
| 59 |
|
private WikiDescriptorBuilder builder; |
| 60 |
|
|
| 61 |
|
private WikiDescriptorCache cache; |
| 62 |
|
|
| 63 |
|
private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper; |
| 64 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 65 |
2 |
@Before... |
| 66 |
|
public void setUp() throws Exception |
| 67 |
|
{ |
| 68 |
2 |
builder = mocker.getInstance(WikiDescriptorBuilder.class); |
| 69 |
2 |
cache = mocker.getInstance(WikiDescriptorCache.class); |
| 70 |
2 |
wikiDescriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class); |
| 71 |
|
} |
| 72 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 73 |
1 |
@Test... |
| 74 |
|
public void onDocumentDeletedEvent() throws Exception |
| 75 |
|
{ |
| 76 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 77 |
1 |
XWikiDocument originalDocument = mock(XWikiDocument.class); |
| 78 |
1 |
when(document.getOriginalDocument()).thenReturn(originalDocument); |
| 79 |
|
|
| 80 |
1 |
Event event = new DocumentDeletedEvent(); |
| 81 |
|
|
| 82 |
1 |
List<BaseObject> objects = new ArrayList<>(); |
| 83 |
1 |
BaseObject object = mock(BaseObject.class); |
| 84 |
1 |
objects.add(object); |
| 85 |
1 |
when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects); |
| 86 |
|
|
| 87 |
1 |
DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA"); |
| 88 |
1 |
when(originalDocument.getDocumentReference()).thenReturn(documentReference); |
| 89 |
|
|
| 90 |
1 |
when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia"); |
| 91 |
|
|
| 92 |
1 |
DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias"); |
| 93 |
1 |
when(cache.getFromId("subwikia")).thenReturn(descriptor); |
| 94 |
|
|
| 95 |
|
|
| 96 |
1 |
mocker.getComponentUnderTest().onEvent(event, document, null); |
| 97 |
|
|
| 98 |
|
|
| 99 |
1 |
verify(cache).remove(descriptor); |
| 100 |
1 |
verify(cache, never()).add(any(DefaultWikiDescriptor.class)); |
| 101 |
|
} |
| 102 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
| 103 |
1 |
@Test... |
| 104 |
|
public void onDocumentUpdatedEvent() throws Exception |
| 105 |
|
{ |
| 106 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 107 |
1 |
XWikiDocument originalDocument = mock(XWikiDocument.class); |
| 108 |
1 |
when(document.getOriginalDocument()).thenReturn(originalDocument); |
| 109 |
|
|
| 110 |
1 |
Event event = new DocumentUpdatedEvent(); |
| 111 |
|
|
| 112 |
1 |
List<BaseObject> objects = new ArrayList<>(); |
| 113 |
1 |
BaseObject object = mock(BaseObject.class); |
| 114 |
1 |
objects.add(object); |
| 115 |
1 |
when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects); |
| 116 |
|
|
| 117 |
1 |
DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA"); |
| 118 |
1 |
when(originalDocument.getDocumentReference()).thenReturn(documentReference); |
| 119 |
|
|
| 120 |
1 |
when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia"); |
| 121 |
|
|
| 122 |
1 |
DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias"); |
| 123 |
1 |
when(cache.getFromId("subwikia")).thenReturn(descriptor); |
| 124 |
|
|
| 125 |
|
|
| 126 |
1 |
List<BaseObject> newObjects = new ArrayList<>(); |
| 127 |
1 |
BaseObject newObject = mock(BaseObject.class); |
| 128 |
1 |
newObjects.add(newObject); |
| 129 |
|
|
| 130 |
1 |
when(document.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(newObjects); |
| 131 |
1 |
DefaultWikiDescriptor newDescriptor = new DefaultWikiDescriptor("subwikia", "newAlias"); |
| 132 |
1 |
when(builder.buildDescriptorObject(newObjects, document)).thenReturn(newDescriptor); |
| 133 |
|
|
| 134 |
|
|
| 135 |
1 |
mocker.getComponentUnderTest().onEvent(event, document, null); |
| 136 |
|
|
| 137 |
|
|
| 138 |
1 |
verify(cache).remove(descriptor); |
| 139 |
1 |
verify(cache).add(newDescriptor); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
} |