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

File DefaultClassPropertyValuesProviderTest.java

 

Code metrics

0
33
4
1
127
86
6
0.18
8.25
4
1.5

Classes

Class Line # Actions
DefaultClassPropertyValuesProviderTest 53 33 0% 6 2
0.945945994.6%
 

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 org.xwiki.rest.internal.resources.classes;
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.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.model.reference.ClassPropertyReference;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.EntityReferenceSerializer;
33    import org.xwiki.rest.XWikiRestException;
34    import org.xwiki.rest.model.jaxb.PropertyValues;
35    import org.xwiki.rest.resources.classes.ClassPropertyValuesProvider;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import com.xpn.xwiki.XWiki;
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.classes.BaseClass;
42    import com.xpn.xwiki.objects.classes.PropertyClass;
43   
44    import static org.junit.Assert.*;
45    import static org.mockito.Mockito.*;
46   
47    /**
48    * Unit tests for {@link DefaultClassPropertyValuesProvider}.
49    *
50    * @version $Id: 415266c35f98e7ea2f9eeb921a2f7735346f8a23 $
51    * @since 9.8RC1
52    */
 
53    public class DefaultClassPropertyValuesProviderTest
54    {
55    @Rule
56    public MockitoComponentMockingRule<ClassPropertyValuesProvider> mocker =
57    new MockitoComponentMockingRule<ClassPropertyValuesProvider>(DefaultClassPropertyValuesProvider.class);
58   
59    private Provider<XWikiContext> xcontextProvider;
60   
61    private DocumentReference classReference = new DocumentReference("wiki", "Some", "Class");
62   
 
63  3 toggle @Before
64    public void configure() throws Exception
65    {
66  3 this.xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
67  3 XWikiContext xcontext = mock(XWikiContext.class);
68  3 XWiki xwiki = mock(XWiki.class);
69  3 XWikiDocument classDocument = mock(XWikiDocument.class);
70  3 BaseClass xclass = mock(BaseClass.class);
71  3 PropertyClass propertyClass = mock(PropertyClass.class);
72   
73  3 when(this.xcontextProvider.get()).thenReturn(xcontext);
74  3 when(xcontext.getWiki()).thenReturn(xwiki);
75  3 when(xwiki.getDocument((EntityReference) this.classReference, xcontext)).thenReturn(classDocument);
76  3 when(classDocument.getXClass()).thenReturn(xclass);
77  3 when(xclass.get("category")).thenReturn(propertyClass);
78  3 when(propertyClass.getClassType()).thenReturn("DBList");
79    }
80   
 
81  1 toggle @Test
82    public void getValues() throws Exception
83    {
84  1 ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
85  1 ClassPropertyValuesProvider dbListValuesProvider = mock(ClassPropertyValuesProvider.class);
86  1 ComponentManager contextComponentManager = this.mocker.getInstance(ComponentManager.class, "context");
87  1 PropertyValues values = new PropertyValues();
88   
89  1 when(contextComponentManager.getInstance(ClassPropertyValuesProvider.class, "DBList"))
90    .thenReturn(dbListValuesProvider);
91  1 when(dbListValuesProvider.getValues(propertyReference, 13, "one", "two")).thenReturn(values);
92   
93  1 assertSame(values, this.mocker.getComponentUnderTest().getValues(propertyReference, 13, "one", "two"));
94    }
95   
 
96  1 toggle @Test
97    public void getValuesForMissingProperty() throws Exception
98    {
99  1 ClassPropertyReference propertyReference = new ClassPropertyReference("status", this.classReference);
100  1 EntityReferenceSerializer<String> entityReferenceSerializer =
101    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
102  1 when(entityReferenceSerializer.serialize(propertyReference)).thenReturn("status reference");
103   
104  1 try {
105  1 this.mocker.getComponentUnderTest().getValues(propertyReference, 13);
106  0 fail();
107    } catch (XWikiRestException expected) {
108  1 assertEquals("No such property [status reference].", expected.getMessage());
109    }
110    }
111   
 
112  1 toggle @Test
113    public void getValuesWithMissingProvider() throws Exception
114    {
115  1 ClassPropertyReference propertyReference = new ClassPropertyReference("category", this.classReference);
116  1 ComponentManager contextComponentManager = this.mocker.getInstance(ComponentManager.class, "context");
117  1 when(contextComponentManager.getInstance(ClassPropertyValuesProvider.class, "DBList"))
118    .thenThrow(new ComponentLookupException("Component not found."));
119   
120  1 try {
121  1 this.mocker.getComponentUnderTest().getValues(propertyReference, 13, "one");
122  0 fail();
123    } catch (XWikiRestException expected) {
124  1 assertEquals("There's no value provider registered for the [DBList] property type.", expected.getMessage());
125    }
126    }
127    }