1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.lesscss.internal.colortheme

File CurrentColorThemeGetterTest.java

 

Code metrics

0
32
5
1
137
93
5
0.16
6.4
5
1

Classes

Class Line # Actions
CurrentColorThemeGetterTest 53 32 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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.lesscss.internal.colortheme;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.component.util.DefaultParameterizedType;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.EntityReferenceSerializer;
31    import org.xwiki.model.reference.WikiReference;
32    import org.xwiki.security.authorization.AuthorizationManager;
33    import org.xwiki.security.authorization.Right;
34    import org.xwiki.test.mockito.MockitoComponentMockingRule;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36   
37    import com.xpn.xwiki.XWiki;
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.web.XWikiRequest;
40   
41    import static org.junit.Assert.assertEquals;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Test class for {@link org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter}.
49    *
50    * @since 6.4M2
51    * @version $Id: 85c711df63762cf710103bcb91be053397379c56 $
52    */
 
53    public class CurrentColorThemeGetterTest
54    {
55    @Rule
56    public MockitoComponentMockingRule<CurrentColorThemeGetter> mocker =
57    new MockitoComponentMockingRule<>(CurrentColorThemeGetter.class);
58   
59    private DocumentReferenceResolver<String> documentReferenceResolver;
60   
61    private EntityReferenceSerializer<String> entityReferenceSerializer;
62   
63    private Provider<XWikiContext> xcontextProvider;
64   
65    private WikiDescriptorManager wikiDescriptorManager;
66   
67    private AuthorizationManager authorizationManager;
68   
69    private XWikiContext xcontext;
70   
71    private XWiki xwiki;
72   
73    private XWikiRequest request;
74   
 
75  4 toggle @Before
76    public void setUp() throws Exception
77    {
78  4 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
79  4 authorizationManager = mocker.getInstance(AuthorizationManager.class);
80  4 documentReferenceResolver = mocker.getInstance(new DefaultParameterizedType(null, DocumentReferenceResolver.class,
81    String.class));
82  4 entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceSerializer.class,
83    String.class));
84  4 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
85  4 xcontext = mock(XWikiContext.class);
86  4 when(xcontextProvider.get()).thenReturn(xcontext);
87  4 xwiki = mock(XWiki.class);
88  4 when(xcontext.getWiki()).thenReturn(xwiki);
89   
90  4 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId");
91  4 request = mock(XWikiRequest.class);
92  4 when(xcontext.getRequest()).thenReturn(request);
93  4 DocumentReference colorThemeReference = new DocumentReference("wikiId", "XWiki", "MyColorTheme");
94  4 WikiReference mainWikiReference = new WikiReference("wikiId");
95  4 when(documentReferenceResolver.resolve(eq("myColorTheme"), eq(mainWikiReference))).thenReturn(colorThemeReference);
96  4 when(entityReferenceSerializer.serialize(colorThemeReference)).thenReturn("wikiId:ColorTheme.MyColorTheme");
97  4 when(xwiki.exists(colorThemeReference, xcontext)).thenReturn(true);
98  4 DocumentReference currentUser = new DocumentReference("xwiki", "XWiki", "CurrentUser");
99  4 when(xcontext.getUserReference()).thenReturn(currentUser);
100  4 when(authorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class),
101    any(DocumentReference.class))).thenReturn(true);
102    }
103   
 
104  1 toggle @Test
105    public void getCurrentColorThemeTestWhenRequestParameter() throws Exception
106    {
107  1 when(request.getParameter("colorTheme")).thenReturn("myColorTheme");
108  1 assertEquals("wikiId:ColorTheme.MyColorTheme", mocker.getComponentUnderTest().getCurrentColorTheme("default"));
109    }
110   
 
111  1 toggle @Test
112    public void getCurrentColorThemeTestWhenNoRequestParameter() throws Exception
113    {
114  1 when(xwiki.getUserPreference(eq("colorTheme"), any(XWikiContext.class))).thenReturn("myColorTheme");
115  1 assertEquals("wikiId:ColorTheme.MyColorTheme", mocker.getComponentUnderTest().getCurrentColorTheme("default"));
116    }
117   
 
118  1 toggle @Test
119    public void getCurrentColorThemeFallbackTest() throws Exception
120    {
121  1 when(request.getParameter("colorTheme")).thenReturn("myColorTheme");
122  1 when(xwiki.exists(any(DocumentReference.class), eq(xcontext))).thenReturn(false);
123  1 assertEquals("fallback", mocker.getComponentUnderTest().getCurrentColorTheme("fallback"));
124  1 assertEquals("error", mocker.getComponentUnderTest().getCurrentColorTheme("error"));
125    }
126   
 
127  1 toggle @Test
128    public void getCurrentColorWithAndWithoutRight() throws Exception
129    {
130  1 when(request.getParameter("colorTheme")).thenReturn("myColorTheme");
131  1 when(authorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class), any(DocumentReference.class)))
132    .thenReturn(false);
133  1 assertEquals("fallback", mocker.getComponentUnderTest().getCurrentColorTheme(true, "fallback"));
134  1 assertEquals("wikiId:ColorTheme.MyColorTheme",
135    mocker.getComponentUnderTest().getCurrentColorTheme(false, "fallback"));
136    }
137    }