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

File UsedValuesListQueryBuilderTest.java

 

Code metrics

0
31
4
1
152
102
4
0.13
7.75
4
1

Classes

Class Line # Actions
UsedValuesListQueryBuilderTest 55 31 0% 4 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 com.xpn.xwiki.internal.objects.classes;
21   
22    import javax.inject.Named;
23   
24    import org.junit.jupiter.api.BeforeEach;
25    import org.junit.jupiter.api.Test;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.model.reference.DocumentReferenceResolver;
28    import org.xwiki.model.reference.EntityReferenceSerializer;
29    import org.xwiki.query.Query;
30    import org.xwiki.query.QueryManager;
31    import org.xwiki.security.authorization.ContextualAuthorizationManager;
32    import org.xwiki.security.authorization.Right;
33    import org.xwiki.test.junit5.mockito.ComponentTest;
34    import org.xwiki.test.junit5.mockito.InjectMockComponents;
35    import org.xwiki.test.junit5.mockito.MockComponent;
36    import org.xwiki.test.mockito.MockitoComponentManager;
37   
38    import com.xpn.xwiki.objects.classes.BaseClass;
39    import com.xpn.xwiki.objects.classes.DBListClass;
40    import com.xpn.xwiki.objects.classes.ListClass;
41    import com.xpn.xwiki.web.Utils;
42   
43    import static org.junit.jupiter.api.Assertions.assertSame;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link UsedValuesListQueryBuilder}.
50    *
51    * @version $Id: ae3c1696ec5b225cb36e3cb44db30274bfe49372 $
52    * @since 9.8RC1
53    */
54    @ComponentTest
 
55    public class UsedValuesListQueryBuilderTest
56    {
57    @MockComponent
58    private QueryManager queryManager;
59   
60    @MockComponent
61    private ContextualAuthorizationManager authorization;
62   
63    @MockComponent
64    @Named("current")
65    private DocumentReferenceResolver<String> documentReferenceResolver;
66   
67    @MockComponent
68    @Named("local")
69    EntityReferenceSerializer<String> localEntityReferenceSerializer;
70   
71    private ListClass listClass;
72   
73    @InjectMockComponents
74    private UsedValuesListQueryBuilder usedValuesListQueryBuilder;
75   
 
76  3 toggle @BeforeEach
77    public void before(MockitoComponentManager componentManager) throws Exception
78    {
79  3 DocumentReference oneReference = new DocumentReference("wiki", "Page", "one");
80  3 when(this.documentReferenceResolver.resolve("alice")).thenReturn(oneReference);
81  3 when(this.authorization.hasAccess(Right.VIEW, oneReference)).thenReturn(false);
82   
83  3 DocumentReference twoReference = new DocumentReference("wiki", "Page", "two");
84  3 when(this.documentReferenceResolver.resolve("alice")).thenReturn(twoReference);
85  3 when(this.authorization.hasAccess(Right.VIEW, twoReference)).thenReturn(true);
86   
87  3 BaseClass xclass = new BaseClass();
88  3 xclass.setDocumentReference(new DocumentReference("apps", "Blog", "BlogPostClass"));
89   
90  3 Utils.setComponentManager(componentManager);
91  3 when(this.localEntityReferenceSerializer.serialize(xclass.getDocumentReference()))
92    .thenReturn("Blog.BlogPostClass");
93   
94  3 this.listClass = new DBListClass();
95  3 this.listClass.setName("category");
96  3 this.listClass.setObject(xclass);
97    }
98   
 
99  1 toggle @Test
100    public void buildForStringProperty() throws Exception
101    {
102  1 listClass.setMultiSelect(false);
103   
104  1 Query query = mock(Query.class);
105  1 when(this.queryManager.createQuery(
106    "select prop.value, count(*) as unfilterable0 " + "from BaseObject as obj, StringProperty as prop "
107    + "where obj.className = :className and obj.name <> :templateName"
108    + " and prop.id.id = obj.id and prop.id.name = :propertyName " + "group by prop.value "
109    + "order by count(*) desc",
110    Query.HQL)).thenReturn(query);
111   
112  1 assertSame(query, this.usedValuesListQueryBuilder.build(listClass));
113   
114  1 verify(query).bindValue("className", "Blog.BlogPostClass");
115  1 verify(query).bindValue("propertyName", "category");
116  1 verify(query).bindValue("templateName", "Blog.BlogPostTemplate");
117  1 verify(query).setWiki("apps");
118    }
119   
 
120  1 toggle @Test
121    public void buildForDBStringListProperty() throws Exception
122    {
123  1 listClass.setMultiSelect(true);
124  1 listClass.setRelationalStorage(true);
125   
126  1 Query query = mock(Query.class);
127  1 when(this.queryManager.createQuery("select listItem, count(*) as unfilterable0 "
128    + "from BaseObject as obj, DBStringListProperty as prop join prop.list listItem "
129    + "where obj.className = :className and obj.name <> :templateName"
130    + " and prop.id.id = obj.id and prop.id.name = :propertyName " + "group by listItem "
131    + "order by count(*) desc", Query.HQL)).thenReturn(query);
132   
133  1 assertSame(query, this.usedValuesListQueryBuilder.build(listClass));
134    }
135   
 
136  1 toggle @Test
137    public void buildForStringListProperty() throws Exception
138    {
139  1 listClass.setMultiSelect(true);
140  1 listClass.setRelationalStorage(false);
141   
142  1 Query query = mock(Query.class);
143  1 when(this.queryManager.createQuery(
144    "select prop.textValue, count(*) as unfilterable0 " + "from BaseObject as obj, StringListProperty as prop "
145    + "where obj.className = :className and obj.name <> :templateName"
146    + " and prop.id.id = obj.id and prop.id.name = :propertyName " + "group by prop.textValue "
147    + "order by count(*) desc",
148    Query.HQL)).thenReturn(query);
149   
150  1 assertSame(query, this.usedValuesListQueryBuilder.build(listClass));
151    }
152    }