1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.objects.classes

File ExplicitlyAllowedValuesDBListQueryBuilderTest.java

 

Code metrics

0
23
3
1
107
64
3
0.13
7.67
3
1

Classes

Class Line # Actions
ExplicitlyAllowedValuesDBListQueryBuilderTest 48 23 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 com.xpn.xwiki.internal.objects.classes;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.model.reference.DocumentReference;
26    import org.xwiki.query.Query;
27    import org.xwiki.query.QueryBuilder;
28    import org.xwiki.query.QueryManager;
29    import org.xwiki.security.authorization.AuthorExecutor;
30    import org.xwiki.security.authorization.AuthorizationManager;
31    import org.xwiki.security.authorization.Right;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.classes.BaseClass;
36    import com.xpn.xwiki.objects.classes.DBListClass;
37   
38    import static org.junit.Assert.*;
39    import static org.mockito.ArgumentMatchers.*;
40    import static org.mockito.Mockito.*;
41   
42    /**
43    * Unit tests for {@link ExplicitlyAllowedValuesDBListQueryBuilder}.
44    *
45    * @version $Id: 7951427791e932cb934d4c4a804ffd269c3cf895 $
46    * @since 9.8RC1
47    */
 
48    public class ExplicitlyAllowedValuesDBListQueryBuilderTest
49    {
50    @Rule
51    public MockitoComponentMockingRule<QueryBuilder<DBListClass>> mocker =
52    new MockitoComponentMockingRule<QueryBuilder<DBListClass>>(ExplicitlyAllowedValuesDBListQueryBuilder.class);
53   
54    private AuthorizationManager authorizationManager;
55   
56    private QueryManager secureQueryManager;
57   
58    private DBListClass dbListClass = new DBListClass();
59   
 
60  2 toggle @Before
61    public void configure() throws Exception
62    {
63  2 this.authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
64  2 this.secureQueryManager = this.mocker.getInstance(QueryManager.class, "secure");
65   
66  2 XWikiDocument ownerDocument = mock(XWikiDocument.class);
67  2 when(ownerDocument.getDocumentReference()).thenReturn(new DocumentReference("math", "Some", "Page"));
68  2 when(ownerDocument.getAuthorReference()).thenReturn(new DocumentReference("wiki", "Users", "alice"));
69   
70  2 BaseClass xclass = new BaseClass();
71  2 xclass.setDocumentReference(ownerDocument.getDocumentReference());
72   
73  2 this.dbListClass.setOwnerDocument(ownerDocument);
74  2 this.dbListClass.setObject(xclass);
75  2 this.dbListClass.setName("category");
76  2 this.dbListClass.setSql("select ...");
77    }
78   
 
79  1 toggle @Test
80    public void buildWithScriptRight() throws Exception
81    {
82  1 DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
83  1 when(this.authorizationManager.hasAccess(Right.SCRIPT, authorReference, dbListClass.getReference()))
84    .thenReturn(true);
85   
86  1 AuthorExecutor authorExector = this.mocker.getInstance(AuthorExecutor.class);
87  1 String evaluatedStatement = "test";
88  1 when(authorExector.call(any(), eq(authorReference), eq(this.dbListClass.getDocumentReference())))
89    .thenReturn(evaluatedStatement);
90   
91  1 Query query = mock(Query.class);
92  1 when(this.secureQueryManager.createQuery(evaluatedStatement, Query.HQL)).thenReturn(query);
93   
94  1 assertSame(query, this.mocker.getComponentUnderTest().build(this.dbListClass));
95    }
96   
 
97  1 toggle @Test
98    public void buildWithoutScriptRight() throws Exception
99    {
100  1 Query query = mock(Query.class);
101  1 when(this.secureQueryManager.createQuery(this.dbListClass.getSql(), Query.HQL)).thenReturn(query);
102   
103  1 assertSame(query, this.mocker.getComponentUnderTest().build(this.dbListClass));
104   
105  1 verify(query).setWiki("math");
106    }
107    }