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

File ClassPropertyValuesResourceImplTest.java

 

Code metrics

0
40
4
1
157
108
6
0.15
10
4
1.5

Classes

Class Line # Actions
ClassPropertyValuesResourceImplTest 65 40 0% 6 2
0.9545454495.5%
 

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 java.net.URI;
23    import java.util.Arrays;
24   
25    import javax.inject.Provider;
26    import javax.ws.rs.WebApplicationException;
27    import javax.ws.rs.core.Response.Status;
28    import javax.ws.rs.core.UriInfo;
29   
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.util.ReflectionUtils;
35    import org.xwiki.context.Execution;
36    import org.xwiki.context.ExecutionContext;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.reference.ClassPropertyReference;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.DocumentReferenceResolver;
41    import org.xwiki.rest.Relations;
42    import org.xwiki.rest.model.jaxb.Link;
43    import org.xwiki.rest.model.jaxb.PropertyValues;
44    import org.xwiki.rest.resources.classes.ClassPropertyValuesProvider;
45    import org.xwiki.rest.resources.classes.ClassPropertyValuesResource;
46    import org.xwiki.security.authorization.ContextualAuthorizationManager;
47    import org.xwiki.security.authorization.Right;
48    import org.xwiki.test.mockito.MockitoComponentMockingRule;
49   
50    import com.xpn.xwiki.XWiki;
51    import com.xpn.xwiki.XWikiContext;
52    import com.xpn.xwiki.doc.XWikiDocument;
53    import com.xpn.xwiki.objects.classes.BaseClass;
54    import com.xpn.xwiki.objects.classes.DBListClass;
55   
56    import static org.junit.Assert.*;
57    import static org.mockito.Mockito.*;
58   
59    /**
60    * Unit tests for {@link ClassPropertyValuesResourceImpl}.
61    *
62    * @version $Id: 54ef59a72a65723362073012ae1451c8a0140702 $
63    * @since 9.8RC1
64    */
 
65    public class ClassPropertyValuesResourceImplTest
66    {
67    @Rule
68    public MockitoComponentMockingRule<ClassPropertyValuesResource> mocker =
69    new MockitoComponentMockingRule<ClassPropertyValuesResource>(ClassPropertyValuesResourceImpl.class);
70   
71    private DocumentReferenceResolver<String> resolver;
72   
73    private ContextualAuthorizationManager authorization;
74   
75    private ClassPropertyReference propertyReference =
76    new ClassPropertyReference("status", new DocumentReference("wiki", Arrays.asList("Path", "To"), "Class"));
77   
78    private BaseClass xclass = mock(BaseClass.class);
79   
80    private ClassPropertyValuesResource resource;
81   
 
82  3 toggle @Before
83    public void configure() throws Exception
84    {
85  3 this.resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
86  3 this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
87   
88  3 when(this.resolver.resolve("Path.To.Class", propertyReference.extractReference(EntityType.WIKI)))
89    .thenReturn((DocumentReference) propertyReference.getParent());
90   
91  3 XWikiContext xcontext = mock(XWikiContext.class);
92  3 XWiki xwiki = mock(XWiki.class);
93  3 XWikiDocument classDocument = mock(XWikiDocument.class);
94  3 when(xcontext.getWiki()).thenReturn(xwiki);
95  3 when(xwiki.getDocument(propertyReference, xcontext)).thenReturn(classDocument);
96  3 when(classDocument.getXClass()).thenReturn(this.xclass);
97   
98  3 ExecutionContext executionContext = new ExecutionContext();
99  3 executionContext.setProperty("xwikicontext", xcontext);
100  3 Execution execution = mock(Execution.class);
101  3 ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
102  3 when(componentManager.getInstance(Execution.class)).thenReturn(execution);
103  3 when(execution.getContext()).thenReturn(executionContext);
104   
105  3 Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
106  3 when(xcontextProvider.get()).thenReturn(xcontext);
107   
108  3 UriInfo uriInfo = mock(UriInfo.class);
109  3 when(uriInfo.getBaseUri()).thenReturn(new URI("/xwiki/rest"));
110  3 this.resource = this.mocker.getComponentUnderTest();
111  3 ReflectionUtils.setFieldValue(resource, "uriInfo", uriInfo);
112    }
113   
 
114  1 toggle @Test
115    public void getClassPropertyValuesUnauthorized() throws Exception
116    {
117  1 try {
118  1 this.resource.getClassPropertyValues("wiki", "Path.To.Class", "status", 6, Arrays.asList("text"), false);
119  0 fail();
120    } catch (WebApplicationException expected) {
121  1 assertEquals(Status.UNAUTHORIZED.getStatusCode(), expected.getResponse().getStatus());
122    }
123    }
124   
 
125  1 toggle @Test
126    public void getClassPropertyValuesNotFound() throws Exception
127    {
128  1 when(this.authorization.hasAccess(Right.VIEW, this.propertyReference)).thenReturn(true);
129   
130  1 try {
131  1 this.resource.getClassPropertyValues("wiki", "Path.To.Class", "status", 6, Arrays.asList("text"), false);
132  0 fail();
133    } catch (WebApplicationException expected) {
134  1 assertEquals(Status.NOT_FOUND.getStatusCode(), expected.getResponse().getStatus());
135    }
136    }
137   
 
138  1 toggle @Test
139    public void getClassPropertyValues() throws Exception
140    {
141  1 when(this.authorization.hasAccess(Right.VIEW, this.propertyReference)).thenReturn(true);
142  1 when(this.xclass.get("status")).thenReturn(new DBListClass());
143   
144  1 PropertyValues values = new PropertyValues();
145  1 ClassPropertyValuesProvider propertyValuesProvider = this.mocker.getInstance(ClassPropertyValuesProvider.class);
146  1 when(propertyValuesProvider.getValues(this.propertyReference, 6, "one", "two")).thenReturn(values);
147   
148  1 assertSame(values,
149    this.resource.getClassPropertyValues("wiki", "Path.To.Class", "status", 6, Arrays.asList("one", "two"),
150    false));
151   
152  1 assertEquals(1, values.getLinks().size());
153  1 Link propertyLink = values.getLinks().get(0);
154  1 assertEquals("/xwiki/rest/wikis/wiki/classes/Path.To.Class/properties/status", propertyLink.getHref());
155  1 assertEquals(Relations.PROPERTY, propertyLink.getRel());
156    }
157    }