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

File DBListClassPropertyValuesProviderTest.java

 

Code metrics

0
69
7
1
206
150
7
0.1
9.86
7
1

Classes

Class Line # Actions
DBListClassPropertyValuesProviderTest 65 69 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 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.Collections;
24    import java.util.HashMap;
25    import java.util.concurrent.Callable;
26   
27    import org.junit.jupiter.api.BeforeEach;
28    import org.junit.jupiter.api.Test;
29    import org.xwiki.model.reference.ClassPropertyReference;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReferenceSerializer;
32    import org.xwiki.query.Query;
33    import org.xwiki.query.QueryBuilder;
34    import org.xwiki.query.QueryFilter;
35    import org.xwiki.query.QueryParameter;
36    import org.xwiki.rest.XWikiRestException;
37    import org.xwiki.rest.model.jaxb.PropertyValue;
38    import org.xwiki.rest.model.jaxb.PropertyValues;
39    import org.xwiki.security.authorization.AuthorExecutor;
40    import org.xwiki.test.junit5.mockito.ComponentTest;
41    import org.xwiki.test.junit5.mockito.InjectMockComponents;
42    import org.xwiki.test.junit5.mockito.MockComponent;
43   
44    import com.xpn.xwiki.objects.classes.DBListClass;
45    import com.xpn.xwiki.objects.classes.DateClass;
46   
47    import static org.junit.jupiter.api.Assertions.assertEquals;
48    import static org.junit.jupiter.api.Assertions.assertSame;
49    import static org.junit.jupiter.api.Assertions.assertThrows;
50    import static org.mockito.ArgumentMatchers.any;
51    import static org.mockito.ArgumentMatchers.eq;
52    import static org.mockito.Mockito.mock;
53    import static org.mockito.Mockito.never;
54    import static org.mockito.Mockito.times;
55    import static org.mockito.Mockito.verify;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link DBListClassPropertyValuesProvider}.
60    *
61    * @version $Id: 718ca73cf3589e7bf93411a118f12857ecc45f62 $
62    * @since 9.8RC1
63    */
64    @ComponentTest
 
65    public class DBListClassPropertyValuesProviderTest extends AbstractListClassPropertyValuesProviderTest
66    {
67    @InjectMockComponents
68    private DBListClassPropertyValuesProvider provider;
69   
70    private DBListClass dbListClass = new DBListClass();
71   
72    @MockComponent
73    private EntityReferenceSerializer<String> entityReferenceSerializer;
74   
75    @MockComponent
76    private AuthorExecutor authorExecutor;
77   
78    @MockComponent
79    protected QueryBuilder<DBListClass> allowedValuesQueryBuilder;
80   
 
81  6 toggle @BeforeEach
82    public void configure() throws Exception
83    {
84  6 super.configure();
85   
86  6 addProperty("category", this.dbListClass, true);
87  6 addProperty("date", new DateClass(), false);
88   
89  6 when(this.xcontext.getWiki().getDocument(new ClassPropertyReference("status", this.classReference),
90    this.xcontext)).thenReturn(this.classDocument);
91    }
92   
 
93  1 toggle @Test
94    public void getValuesForMissingProperty() throws Exception
95    {
96  1 ClassPropertyReference propertyReference = new ClassPropertyReference("status", this.classReference);
97  1 when(this.entityReferenceSerializer.serialize(propertyReference)).thenReturn("status reference");
98  1 Throwable exception = assertThrows(XWikiRestException.class, () -> {
99  1 this.provider.getValues(propertyReference, 0);
100    });
101  1 assertEquals(exception.getMessage(), "Property [status reference] not found.");
102    }
103   
 
104  1 toggle @Test
105    public void getValuesForWrongProperty() throws Exception
106    {
107  1 ClassPropertyReference propertyReference = new ClassPropertyReference("date", this.classReference);
108  1 when(this.entityReferenceSerializer.serialize(propertyReference)).thenReturn("status reference");
109  1 Throwable exception = assertThrows(XWikiRestException.class, () -> {
110  1 this.provider.getValues(propertyReference, 0);
111    });
112  1 assertEquals(exception.getMessage(), "This [status reference] is not a [DBListClass] property.");
113    }
114   
 
115  1 toggle @Test
116    public void getValuesAllowed() throws Exception
117    {
118  1 ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
119  1 DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
120  1 PropertyValues values = new PropertyValues();
121  1 when(this.authorExecutor.call(any(), eq(authorReference), eq(this.dbListClass.getDocumentReference())))
122    .thenReturn(values);
123   
124  1 assertSame(values, this.provider.getValues(propertyReference, 3));
125   
126  1 assertSame(values, this.provider.getValues(propertyReference, 0, "text"));
127    }
128   
 
129  1 toggle @Test
130    public void getValuesMixedWithoutUsed() throws Exception
131    {
132  1 ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
133  1 DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
134  1 PropertyValues values = new PropertyValues();
135  1 values.getPropertyValues().add(new PropertyValue());
136  1 when(this.authorExecutor.call(any(), eq(authorReference), eq(this.dbListClass.getDocumentReference())))
137    .thenReturn(values);
138   
139  1 assertSame(values, this.provider.getValues(propertyReference, 1, "foo"));
140  1 assertEquals(1, values.getPropertyValues().size());
141   
142  1 verify(this.usedValuesQueryBuilder, never()).build(any());
143    }
144   
 
145  1 toggle @Test
146    public void getValuesMixedWithUsed() throws Exception
147    {
148  1 ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
149  1 DocumentReference authorReference = this.dbListClass.getOwnerDocument().getAuthorReference();
150   
151  1 PropertyValues values = new PropertyValues();
152  1 PropertyValue red = new PropertyValue();
153  1 red.setValue("red");
154  1 red.setMetaData(new HashMap<>());
155  1 red.getMetaData().put("label", "Red");
156  1 values.getPropertyValues().add(red);
157  1 when(this.authorExecutor.call(any(), eq(authorReference), eq(this.dbListClass.getDocumentReference())))
158    .thenReturn(values);
159   
160  1 Query query = mock(Query.class);
161  1 QueryParameter queryParameter = mock(QueryParameter.class);
162  1 when(this.usedValuesQueryBuilder.build(dbListClass)).thenReturn(query);
163  1 when(query.bindValue("text")).thenReturn(queryParameter);
164  1 when(queryParameter.anyChars()).thenReturn(queryParameter);
165  1 when(queryParameter.literal("bar")).thenReturn(queryParameter);
166  1 when(query.execute()).thenReturn(Arrays.asList(new Object[] { "blue", 21L }, new Object[] { "red", 17L }));
167   
168  1 assertSame(values, this.provider.getValues(propertyReference, 3, "bar"));
169   
170  1 verify(query).setLimit(2);
171  1 verify(query).addFilter(this.componentManager.getInstance(QueryFilter.class, "text"));
172  1 verify(queryParameter, times(2)).anyChars();
173   
174  1 assertEquals(2, values.getPropertyValues().size());
175  1 assertEquals("red", values.getPropertyValues().get(0).getValue());
176  1 assertEquals(17L, values.getPropertyValues().get(0).getMetaData().get("count"));
177  1 assertEquals("blue", values.getPropertyValues().get(1).getValue());
178  1 assertEquals(21L, values.getPropertyValues().get(1).getMetaData().get("count"));
179    }
180   
 
181  1 toggle @Test
182    public void getValue() throws Exception
183    {
184  1 ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
185  1 when(this.authorExecutor.call(any(), any(), any()))
186    .thenAnswer(answer -> ((Callable) answer.getArgument(0)).call());
187   
188  1 PropertyValue red = new PropertyValue();
189  1 red.setValue("red");
190  1 red.setMetaData(new HashMap<>());
191  1 red.getMetaData().put("label", "Red");
192   
193  1 Query query = mock(Query.class);
194  1 QueryParameter queryParameter = mock(QueryParameter.class);
195  1 when(this.allowedValuesQueryBuilder.build(dbListClass)).thenReturn(query);
196  1 when(query.bindValue("text")).thenReturn(queryParameter);
197  1 when(queryParameter.literal("red")).thenReturn(queryParameter);
198  1 when(query.execute()).thenReturn(Collections.singletonList(new Object[] { "red", "Red" }));
199   
200  1 PropertyValue propertyValue = this.provider.getValue(propertyReference, "red");
201  1 assertEquals(red.getValue(), propertyValue.getValue());
202  1 assertEquals(red.getMetaData(), propertyValue.getMetaData());
203   
204  1 verify(query).addFilter(this.componentManager.getInstance(QueryFilter.class, "text"));
205    }
206    }