1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.rest

File SpacesResourceTest.java

 

Code metrics

2
46
3
1
148
94
4
0.09
15.33
3
1.33

Classes

Class Line # Actions
SpacesResourceTest 47 46 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 org.xwiki.test.rest;
21   
22    import java.io.StringReader;
23    import java.util.Arrays;
24   
25    import org.apache.commons.httpclient.HttpStatus;
26    import org.apache.commons.httpclient.methods.GetMethod;
27    import org.junit.Assert;
28    import org.junit.Test;
29    import org.restlet.engine.io.ReaderInputStream;
30    import org.xwiki.model.reference.AttachmentReference;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.rest.Relations;
33    import org.xwiki.rest.model.jaxb.Attachment;
34    import org.xwiki.rest.model.jaxb.Attachments;
35    import org.xwiki.rest.model.jaxb.Link;
36    import org.xwiki.rest.model.jaxb.SearchResult;
37    import org.xwiki.rest.model.jaxb.SearchResults;
38    import org.xwiki.rest.model.jaxb.Space;
39    import org.xwiki.rest.model.jaxb.Spaces;
40    import org.xwiki.rest.model.jaxb.Wiki;
41    import org.xwiki.rest.model.jaxb.Wikis;
42    import org.xwiki.rest.resources.spaces.SpaceAttachmentsResource;
43    import org.xwiki.rest.resources.spaces.SpaceSearchResource;
44    import org.xwiki.rest.resources.wikis.WikisResource;
45    import org.xwiki.test.rest.framework.AbstractHttpTest;
46   
 
47    public class SpacesResourceTest extends AbstractHttpTest
48    {
 
49  1 toggle @Override
50    @Test
51    public void testRepresentation() throws Exception
52    {
53    // Create a subspace
54  1 createPageIfDoesntExist(Arrays.asList("SpaceA", "SpaceB", "SpaceC"), "MyPage", "some content");
55   
56  1 GetMethod getMethod = executeGet(getFullUri(WikisResource.class));
57  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
58   
59  1 Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
60  1 Assert.assertTrue(wikis.getWikis().size() > 0);
61   
62  1 Wiki wiki = wikis.getWikis().get(0);
63  1 Link link = getFirstLinkByRelation(wiki, Relations.SPACES);
64  1 Assert.assertNotNull(link);
65   
66  1 getMethod = executeGet(link.getHref());
67  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
68   
69  1 Spaces spaces = (Spaces) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
70   
71  1 Assert.assertTrue(spaces.getSpaces().size() > 0);
72   
73  1 boolean nestedSpaceFound = false;
74   
75  1 for (Space space : spaces.getSpaces()) {
76  9 link = getFirstLinkByRelation(space, Relations.SEARCH);
77  9 Assert.assertNotNull(link);
78   
79  9 checkLinks(space);
80   
81  9 if ("xwiki:SpaceA.SpaceB.SpaceC".equals(space.getId())) {
82  1 Assert.assertEquals("SpaceC", space.getName());
83  1 nestedSpaceFound = true;
84    }
85   
86    /*
87    * Check that in the returned spaces there are not spaces not visible to user Guest, for example the
88    * "Scheduler" space
89    */
90  9 Assert.assertFalse(space.getName().equals("Scheduler"));
91    }
92   
93  1 Assert.assertTrue(nestedSpaceFound);
94    }
95   
 
96  1 toggle @Test
97    public void testSearch() throws Exception
98    {
99  1 DocumentReference reference = new DocumentReference(getWiki(), getTestClassName(), getTestMethodName());
100  1 this.testUtils.rest().delete(reference);
101  1 this.testUtils.rest().savePage(reference, "content " + getTestMethodName(), "title " + getTestMethodName());
102   
103  1 GetMethod getMethod = executeGet(String.format("%s?q=somethingthatcannotpossiblyexist",
104    buildURI(SpaceSearchResource.class, getWiki(), Arrays.asList(getTestClassName()))));
105  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
106   
107  1 SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
108   
109  1 Assert.assertEquals(0, searchResults.getSearchResults().size());
110   
111  1 getMethod = executeGet(String.format("%s?q=%s",
112    buildURI(SpaceSearchResource.class, getWiki(), Arrays.asList(getTestClassName())), getTestMethodName()));
113  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
114   
115  1 searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
116   
117  1 int resultSize = searchResults.getSearchResults().size();
118  1 Assert.assertTrue("Found " + resultSize + " result", resultSize == 1);
119   
120  1 for (SearchResult searchResult : searchResults.getSearchResults()) {
121  1 checkLinks(searchResult);
122    }
123    }
124   
 
125  1 toggle @Test
126    public void testAttachments() throws Exception
127    {
128  1 DocumentReference reference = new DocumentReference(getWiki(), getTestClassName(), getTestMethodName());
129  1 this.testUtils.rest().delete(reference);
130  1 this.testUtils.rest().savePage(reference);
131  1 this.testUtils.rest().attachFile(new AttachmentReference("attachment.txt", reference),
132    new ReaderInputStream(new StringReader("content")), true);
133   
134    // Matches Sandbox.WebHome@XWikLogo.png
135  1 GetMethod getMethod = executeGet(String.format("%s",
136    buildURI(SpaceAttachmentsResource.class, getWiki(), Arrays.asList(getTestClassName()))));
137  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
138   
139  1 Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
140   
141  1 Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
142   
143  1 for (Attachment attachment : attachments.getAttachments()) {
144  1 checkLinks(attachment);
145    }
146   
147    }
148    }