1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rest.internal.resources.classes

File GroupsClassPropertyValuesProviderTest.java

 

Code metrics

0
35
3
1
131
85
3
0.09
11.67
3
1

Classes

Class Line # Actions
GroupsClassPropertyValuesProviderTest 56 35 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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.rest.internal.resources.classes;
21   
22    import java.util.Arrays;
23    import java.util.Map;
24   
25    import org.junit.jupiter.api.BeforeEach;
26    import org.junit.jupiter.api.Test;
27    import org.xwiki.model.reference.AttachmentReference;
28    import org.xwiki.model.reference.ClassPropertyReference;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.rendering.syntax.Syntax;
31    import org.xwiki.rest.model.jaxb.PropertyValues;
32    import org.xwiki.test.junit5.mockito.ComponentTest;
33    import org.xwiki.test.junit5.mockito.InjectMockComponents;
34    import org.xwiki.test.junit5.mockito.MockComponent;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36    import org.xwiki.wiki.user.UserScope;
37    import org.xwiki.wiki.user.WikiUserManager;
38   
39    import com.xpn.xwiki.doc.XWikiAttachment;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.classes.GroupsClass;
42   
43    import static org.junit.jupiter.api.Assertions.assertEquals;
44    import static org.junit.jupiter.api.Assertions.assertTrue;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.times;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Unit tests for {@link GroupsClassPropertyValuesProvider}.
52    *
53    * @version $Id: 5088dbbc81d6b900c3c5a69c2a8c072772e9ee68 $
54    */
55    @ComponentTest
 
56    public class GroupsClassPropertyValuesProviderTest extends AbstractListClassPropertyValuesProviderTest
57    {
58    @InjectMockComponents
59    private GroupsClassPropertyValuesProvider provider;
60   
61    @MockComponent
62    private WikiUserManager wikiUserManager;
63   
64    private ClassPropertyReference propertyReference = new ClassPropertyReference("band", this.classReference);
65   
 
66  2 toggle @BeforeEach
67    public void configure() throws Exception
68    {
69  2 super.configure();
70   
71  2 addProperty(this.propertyReference.getName(), new GroupsClass(), true);
72  2 when(this.xcontext.getWiki().getSkinFile("icons/xwiki/noavatargroup.png", true, this.xcontext))
73    .thenReturn("url/to/noavatar.png");
74    }
75   
 
76  1 toggle @Test
77    public void getValuesLocal() throws Exception
78    {
79  1 when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName()))
80    .thenReturn(UserScope.LOCAL_ONLY);
81   
82  1 DocumentReference devsReference = new DocumentReference("wiki", "Groups", "Devs");
83  1 XWikiDocument devsProfile = mock(XWikiDocument.class, "devs");
84  1 when(this.xcontext.getWiki().getDocument(devsReference, this.xcontext)).thenReturn(devsProfile);
85  1 when(devsProfile.getRenderedTitle(Syntax.PLAIN_1_0, this.xcontext)).thenReturn("Developers");
86   
87  1 DocumentReference adminsReference = new DocumentReference("wiki", "Groups", "Admins");
88  1 XWikiDocument adminsProfile = mock(XWikiDocument.class, "admins");
89  1 XWikiAttachment notAnImageAttachment = mock(XWikiAttachment.class, "noAnImage");
90  1 XWikiAttachment imageAttachment = mock(XWikiAttachment.class, "image");
91  1 AttachmentReference imageAttachmentReference = new AttachmentReference("admins.png", adminsReference);
92  1 when(this.xcontext.getWiki().getDocument(adminsReference, this.xcontext)).thenReturn(adminsProfile);
93  1 when(adminsProfile.getRenderedTitle(Syntax.PLAIN_1_0, this.xcontext)).thenReturn("Administrators");
94  1 when(adminsProfile.getAttachmentList()).thenReturn(Arrays.asList(notAnImageAttachment, imageAttachment));
95  1 when(imageAttachment.isImage(this.xcontext)).thenReturn(true);
96  1 when(imageAttachment.getReference()).thenReturn(imageAttachmentReference);
97  1 when(this.xcontext.getWiki().getURL(imageAttachmentReference, "download",
98    "width=30&height=30&keepAspectRatio=true", null, this.xcontext)).thenReturn("url/to/admins/image");
99   
100  1 when(this.allowedValuesQuery.execute()).thenReturn(Arrays.asList(devsReference, adminsReference));
101   
102  1 PropertyValues values = this.provider.getValues(this.propertyReference, 5, "foo");
103   
104  1 assertEquals(2, values.getPropertyValues().size());
105   
106  1 assertEquals("Developers", values.getPropertyValues().get(0).getMetaData().get("label"));
107  1 assertTrue(values.getPropertyValues().get(0).getMetaData().get("icon") instanceof Map);
108   
109  1 assertEquals("Administrators", values.getPropertyValues().get(1).getMetaData().get("label"));
110  1 assertTrue(values.getPropertyValues().get(1).getMetaData().get("icon") instanceof Map);
111  1 Map icon = (Map) values.getPropertyValues().get(1).getMetaData().get("icon");
112  1 assertEquals("url/to/admins/image", icon.get("url"));
113  1 assertEquals("IMAGE", icon.get("iconSetType"));
114    }
115   
 
116  1 toggle @Test
117    public void getValuesLocalAndGlobal() throws Exception
118    {
119    // We can have local groups.
120  1 when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName()))
121    .thenReturn(UserScope.GLOBAL_ONLY);
122   
123  1 WikiDescriptorManager wikiDescriptorManager = this.componentManager.getInstance(WikiDescriptorManager.class);
124  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("chess");
125   
126  1 this.provider.getValues(this.propertyReference, 5, "foo");
127   
128  1 verify(this.allowedValuesQuery).setWiki("chess");
129  1 verify(this.allowedValuesQuery, times(2)).execute();
130    }
131    }