1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.wiki.internal.descriptor.builder

File DefaultWikiDescriptorBuilderTest.java

 

Code metrics

0
55
3
1
178
115
3
0.05
18.33
3
1

Classes

Class Line # Actions
DefaultWikiDescriptorBuilderTest 66 55 0% 3 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.wiki.internal.descriptor.builder;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Named;
26   
27    import org.junit.jupiter.api.Test;
28    import org.junit.jupiter.api.extension.RegisterExtension;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31    import org.xwiki.model.reference.WikiReference;
32    import org.xwiki.test.LogLevel;
33    import org.xwiki.test.junit5.LogCaptureExtension;
34    import org.xwiki.test.junit5.mockito.InjectMockComponents;
35    import org.xwiki.test.junit5.mockito.MockComponent;
36    import org.xwiki.wiki.descriptor.WikiDescriptor;
37    import org.xwiki.wiki.internal.descriptor.document.XWikiServerClassDocumentInitializer;
38    import org.xwiki.wiki.internal.descriptor.properties.WikiPropertyGroupManager;
39    import org.xwiki.wiki.properties.WikiPropertyGroupException;
40   
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.objects.BaseObject;
43    import com.xpn.xwiki.test.MockitoOldcore;
44    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
45    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
46    import com.xpn.xwiki.test.reference.ReferenceComponentList;
47   
48    import ch.qos.logback.classic.spi.ILoggingEvent;
49   
50    import static org.junit.jupiter.api.Assertions.assertEquals;
51    import static org.junit.jupiter.api.Assertions.assertNull;
52    import static org.junit.jupiter.api.Assertions.assertSame;
53    import static org.mockito.ArgumentMatchers.any;
54    import static org.mockito.Mockito.doThrow;
55    import static org.mockito.Mockito.mock;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link org.xwiki.wiki.internal.descriptor.builder.DefaultWikiDescriptorBuilder}.
60    *
61    * @version $Id: aec6514b02e2cd7daa51775d3fe6c80efc8231c1 $
62    * @since 6.0M1
63    */
64    @OldcoreTest
65    @ReferenceComponentList
 
66    public class DefaultWikiDescriptorBuilderTest
67    {
68    @RegisterExtension
69    public LogCaptureExtension logCapture = new LogCaptureExtension(LogLevel.ERROR);
70   
71    @InjectMockitoOldcore
72    private MockitoOldcore oldcore;
73   
74    @MockComponent
75    @Named("user")
76    private DocumentReferenceResolver<String> userReferenceResolver;
77   
78    @MockComponent
79    private WikiPropertyGroupManager wikiPropertyGroupManager;
80   
81    @InjectMockComponents
82    private DefaultWikiDescriptorBuilder builder;
83   
 
84  1 toggle @Test
85    public void buildDescriptorObject() throws Exception
86    {
87    // Mocks
88  1 List<BaseObject> objects = new ArrayList<>();
89  1 BaseObject object1 = mock(BaseObject.class);
90  1 BaseObject object2 = mock(BaseObject.class);
91  1 BaseObject object3 = mock(BaseObject.class);
92    // Make sure that the first object is null to also verify this case since it can happen that we get holes
93    // with the XWikiDocument.getXObjects() API...
94  1 objects.add(null);
95  1 objects.add(object1);
96  1 objects.add(object2);
97  1 objects.add(null);
98  1 objects.add(object3);
99   
100  1 XWikiDocument document = mock(XWikiDocument.class);
101  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
102  1 when(document.getDocumentReference()).thenReturn(documentReference);
103   
104  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("subwiki1");
105  1 when(object2.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("alias1");
106  1 when(object3.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("alias2");
107   
108  1 DocumentReference mainPageReference = new DocumentReference("subwiki1", "Space", "MainPage");
109   
110  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE)).thenReturn("Space.MainPage");
111  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME))
112    .thenReturn("myPrettyName");
113  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_OWNER)).thenReturn("myOwner");
114  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION)).thenReturn("myDescription");
115   
116  1 DocumentReference ownerRef = new DocumentReference("subwiki1", "XWiki", "myOwner");
117  1 when(userReferenceResolver.resolve("myOwner", new WikiReference("subwiki1"))).thenReturn(ownerRef);
118   
119    // Test
120  1 WikiDescriptor result = this.builder.buildDescriptorObject(objects, document);
121   
122  1 assertEquals("subwiki1", result.getId());
123  1 assertEquals(3, result.getAliases().size());
124  1 assertEquals("subwiki1", result.getAliases().get(0));
125  1 assertEquals("alias1", result.getAliases().get(1));
126  1 assertEquals("alias2", result.getAliases().get(2));
127  1 assertEquals(mainPageReference, result.getMainPageReference());
128  1 assertEquals("myPrettyName", result.getPrettyName());
129  1 assertEquals("subwiki1:XWiki.myOwner", result.getOwnerId());
130  1 assertEquals("myDescription", result.getDescription());
131   
132    // Verify
133  1 wikiPropertyGroupManager.loadForDescriptor(any(WikiDescriptor.class));
134    }
135   
 
136  1 toggle @Test
137    public void buildDescriptorObjectWhenInvalidWiki() throws Exception
138    {
139    // Mocks
140  1 List<BaseObject> objects = new ArrayList<>();
141  1 BaseObject object1 = mock(BaseObject.class);
142  1 objects.add(object1);
143  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn(" ");
144   
145  1 XWikiDocument document = mock(XWikiDocument.class);
146   
147    // Test
148  1 WikiDescriptor result = this.builder.buildDescriptorObject(objects, document);
149  1 assertNull(result);
150    }
151   
 
152  1 toggle @Test
153    public void buildDescriptorObjectWhenException() throws Exception
154    {
155    // Mocks
156  1 List<BaseObject> objects = new ArrayList<>();
157  1 BaseObject object1 = mock(BaseObject.class);
158  1 objects.add(object1);
159   
160  1 XWikiDocument document = mock(XWikiDocument.class);
161  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
162  1 when(document.getDocumentReference()).thenReturn(documentReference);
163  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("subwiki1");
164  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE)).thenReturn("Space.MainPage");
165   
166  1 Exception exception = new WikiPropertyGroupException("error in wikiPropertyGroupManager.loadForDescriptor");
167  1 doThrow(exception).when(wikiPropertyGroupManager).loadForDescriptor(any(WikiDescriptor.class));
168   
169    // Test
170  1 this.builder.buildDescriptorObject(objects, document);
171   
172    // Verify
173  1 ILoggingEvent log = this.logCapture.getLogEvent(0);
174  1 assertEquals("Failed to load wiki property groups for wiki [{}].", log.getMessage());
175  1 assertEquals("subwiki1", log.getArgumentArray()[0]);
176  1 assertSame(exception.getMessage(), log.getThrowableProxy().getMessage());
177    }
178    }