1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.url.internal.standard.entity

File WikiEntityResourceReferenceResolverTest.java

 

Code metrics

6
33
5
1
137
89
8
0.24
6.6
5
1.6

Classes

Class Line # Actions
WikiEntityResourceReferenceResolverTest 51 33 0% 8 4
0.9090909490.9%
 

Contributing tests

This file is covered by 1 test. .

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.url.internal.standard.entity;
21   
22    import java.net.URL;
23    import java.util.Arrays;
24    import java.util.Collections;
25   
26    import org.junit.Before;
27    import org.junit.Test;
28    import org.xwiki.component.util.ReflectionUtils;
29    import org.xwiki.model.EntityType;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.EntityReferenceResolver;
33    import org.xwiki.model.reference.WikiReference;
34    import org.xwiki.resource.ResourceReference;
35    import org.xwiki.resource.ResourceType;
36    import org.xwiki.resource.entity.EntityResourceReference;
37    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
38    import org.xwiki.url.ExtendedURL;
39    import org.xwiki.url.internal.standard.StandardURLConfiguration;
40    import org.xwiki.url.internal.standard.WikiReferenceExtractor;
41   
42    import static org.junit.Assert.assertEquals;
43    import static org.mockito.Mockito.*;
44   
45    /**
46    * Unit tests for {@link org.xwiki.url.internal.standard.entity.WikiEntityResourceReferenceResolver}.
47    *
48    * @version $Id: 2b6edaa2b9d044feffabdb539457bb5b39adb280 $
49    * @since 6.3M1
50    */
 
51    public class WikiEntityResourceReferenceResolverTest
52    {
53    private WikiEntityResourceReferenceResolver resolver;
54   
55    private WikiReference wikiReference = new WikiReference("somewiki");
56   
57    private WikiReferenceExtractor wikiReferenceExtractor;
58   
59    private EntityReferenceResolver<EntityReference> entityReferenceResolver;
60   
61    private EntityResourceActionLister entityResourceActionLister;
62   
63    private StandardURLConfiguration configuration;
64   
 
65  1 toggle @Before
66    public void setUp() throws Exception
67    {
68  1 this.resolver = new WikiEntityResourceReferenceResolver();
69   
70  1 this.wikiReferenceExtractor = mock(WikiReferenceExtractor.class);
71  1 ReflectionUtils.setFieldValue(this.resolver, "wikiExtractor", this.wikiReferenceExtractor);
72   
73  1 this.entityReferenceResolver = mock(EntityReferenceResolver.class);
74  1 ReflectionUtils.setFieldValue(this.resolver, "defaultReferenceEntityReferenceResolver",
75    this.entityReferenceResolver);
76   
77  1 this.configuration = mock(StandardURLConfiguration.class);
78  1 when(this.configuration.isViewActionHidden()).thenReturn(false);
79  1 ReflectionUtils.setFieldValue(this.resolver, "configuration", this.configuration);
80   
81  1 this.entityResourceActionLister = mock(EntityResourceActionLister.class);
82  1 when(this.entityResourceActionLister.listActions()).thenReturn(Arrays.asList("view", "download"));
83  1 ReflectionUtils.setFieldValue(this.resolver, "entityResourceActionLister", this.entityResourceActionLister);
84    }
85   
 
86  1 toggle @Test
87    public void resolve() throws Exception
88    {
89  1 ExtendedURL url = new ExtendedURL(new URL("http://localhost/wiki/somewiki/view/space/page"), null);
90  1 url.getSegments().remove(0);
91  1 when(wikiReferenceExtractor.extract(url)).thenReturn(this.wikiReference);
92   
93  1 EntityReference reference = buildEntityReference("somewiki", "space", "page");
94  1 EntityResourceReference result = (EntityResourceReference) testCreateResource(
95    "http://localhost/wiki/somewiki/view/space/page", "view", reference, reference, EntityType.DOCUMENT);
96   
97  1 assertEquals(new DocumentReference("somewiki", "space", "page"), result.getEntityReference());
98    }
99   
 
100  1 toggle private ResourceReference testCreateResource(String testURL, String expectedActionName,
101    EntityReference expectedReference, EntityReference returnedReference, EntityType expectedEntityType)
102    throws Exception
103    {
104  1 when(this.entityReferenceResolver.resolve(expectedReference, expectedEntityType)).thenReturn(
105    returnedReference);
106  1 ExtendedURL extendedURL = new ExtendedURL(new URL(testURL), null);
107    // Remove the resource type segment since this is what gets passed to specific Reference Resolvers.
108  1 extendedURL.getSegments().remove(0);
109  1 EntityResourceReference entityResource =
110    this.resolver.resolve(extendedURL, new ResourceType("wiki"), Collections.<String, Object>emptyMap());
111   
112  1 assertEquals(expectedActionName, entityResource.getAction().getActionName());
113  1 assertEquals(returnedReference, entityResource.getEntityReference());
114   
115  1 return entityResource;
116    }
117   
 
118  1 toggle private EntityReference buildEntityReference(String wiki, String space, String page)
119    {
120  1 return buildEntityReference(wiki, space, page, null);
121    }
122   
 
123  1 toggle private EntityReference buildEntityReference(String wiki, String space, String page, String attachment)
124    {
125  1 EntityReference entityReference = new WikiReference(wiki);
126  1 if (space != null) {
127  1 entityReference = new EntityReference(space, EntityType.SPACE, entityReference);
128    }
129  1 if (page != null) {
130  1 entityReference = new EntityReference(page, EntityType.DOCUMENT, entityReference);
131    }
132  1 if (attachment != null) {
133  0 entityReference = new EntityReference(attachment, EntityType.ATTACHMENT, entityReference);
134    }
135  1 return entityReference;
136    }
137    }