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

File ObjectsResourceTest.java

 

Code metrics

14
270
18
1
634
446
25
0.09
15
18
1.39

Classes

Class Line # Actions
ObjectsResourceTest 54 270 0% 25 13
0.9569536495.7%
 

Contributing tests

This file is covered by 15 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.util.Arrays;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26    import java.util.UUID;
27   
28    import javax.ws.rs.core.MediaType;
29   
30    import org.apache.commons.httpclient.HttpStatus;
31    import org.apache.commons.httpclient.NameValuePair;
32    import org.apache.commons.httpclient.methods.DeleteMethod;
33    import org.apache.commons.httpclient.methods.GetMethod;
34    import org.apache.commons.httpclient.methods.PostMethod;
35    import org.apache.commons.httpclient.methods.PutMethod;
36    import org.junit.Assert;
37    import org.junit.Before;
38    import org.junit.Test;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.rest.Relations;
41    import org.xwiki.rest.model.jaxb.Link;
42    import org.xwiki.rest.model.jaxb.Object;
43    import org.xwiki.rest.model.jaxb.ObjectSummary;
44    import org.xwiki.rest.model.jaxb.Objects;
45    import org.xwiki.rest.model.jaxb.Page;
46    import org.xwiki.rest.model.jaxb.Property;
47    import org.xwiki.rest.resources.objects.ObjectAtPageVersionResource;
48    import org.xwiki.rest.resources.objects.ObjectResource;
49    import org.xwiki.rest.resources.objects.ObjectsResource;
50    import org.xwiki.rest.resources.pages.PageResource;
51    import org.xwiki.test.rest.framework.AbstractHttpTest;
52    import org.xwiki.test.ui.TestUtils;
53   
 
54    public class ObjectsResourceTest extends AbstractHttpTest
55    {
56    private String wikiName;
57   
58    private List<String> spaces;
59   
60    private String pageName;
61   
62    private DocumentReference reference;
63   
 
64  15 toggle @Before
65    @Override
66    public void setUp() throws Exception
67    {
68  15 super.setUp();
69   
70  15 this.wikiName = getWiki();
71  15 this.spaces = Arrays.asList(getTestClassName());
72  15 this.pageName = getTestMethodName();
73   
74  15 this.reference = new DocumentReference(this.wikiName, this.spaces, this.pageName);
75   
76    // Create a clean test page.
77  15 this.testUtils.rest().delete(this.reference);
78  15 this.testUtils.rest().savePage(this.reference);
79   
80  15 GetMethod getMethod =
81    executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
82  15 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
83   
84  15 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
85  15 Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
86   
87    /* Create a tag object if it doesn't exist yet */
88  15 if (link == null) {
89  15 Object object = objectFactory.createObject();
90  15 object.setClassName("XWiki.TagClass");
91   
92  15 PostMethod postMethod = executePostXml(
93    buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), object,
94    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
95  15 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
96    }
97    }
98   
 
99  1 toggle @Override
100    @Test
101    public void testRepresentation() throws Exception
102    {
103  1 GetMethod getMethod =
104    executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
105  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
106   
107  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
108  1 Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
109  1 Assert.assertNotNull(link);
110   
111  1 getMethod = executeGet(link.getHref());
112  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
113   
114  1 Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
115   
116  1 Assert.assertFalse(objects.getObjectSummaries().isEmpty());
117   
118  1 for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
119  1 link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
120  1 getMethod = executeGet(link.getHref());
121  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
122   
123  1 Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
124   
125  1 checkLinks(objectSummary);
126   
127  1 for (Property property : object.getProperties()) {
128  1 checkLinks(property);
129    }
130    }
131    }
132   
 
133  1 toggle @Test
134    public void testGETNotExistingObject() throws Exception
135    {
136  1 GetMethod getMethod = executeGet(
137    buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, "NOTEXISTING", 0).toString());
138  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode());
139    }
140   
 
141  26 toggle public Property getProperty(Object object, String propertyName)
142    {
143  26 for (Property property : object.getProperties()) {
144  26 if (property.getName().equals(propertyName)) {
145  26 return property;
146    }
147    }
148   
149  0 return null;
150    }
151   
 
152  1 toggle @Test
153    public void testPOSTObject() throws Exception
154    {
155  1 final String TAG_VALUE = "TAG";
156   
157  1 Property property = new Property();
158  1 property.setName("tags");
159  1 property.setValue(TAG_VALUE);
160  1 Object object = objectFactory.createObject();
161  1 object.setClassName("XWiki.TagClass");
162  1 object.getProperties().add(property);
163   
164  1 PostMethod postMethod =
165    executePostXml(buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), object,
166    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
167  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
168   
169  1 object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
170   
171  1 Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());
172   
173  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
174    object.getClassName(), object.getNumber()).toString());
175  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
176   
177  1 object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
178   
179  1 Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());
180    }
181   
 
182  1 toggle @Test
183    public void testPOSTInvalidObject() throws Exception
184    {
185  1 final String TAG_VALUE = "TAG";
186   
187  1 Property property = new Property();
188  1 property.setName("tags");
189  1 property.setValue(TAG_VALUE);
190  1 Object object = objectFactory.createObject();
191  1 object.getProperties().add(property);
192   
193  1 PostMethod postMethod =
194    executePostXml(buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), object,
195    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
196  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_BAD_REQUEST, postMethod.getStatusCode());
197    }
198   
 
199  1 toggle @Test
200    public void testPOSTObjectNotAuthorized() throws Exception
201    {
202  1 final String TAG_VALUE = "TAG";
203   
204  1 Property property = new Property();
205  1 property.setName("tags");
206  1 property.setValue(TAG_VALUE);
207  1 Object object = objectFactory.createObject();
208  1 object.setClassName("XWiki.TagClass");
209  1 object.getProperties().add(property);
210   
211  1 PostMethod postMethod =
212    executePostXml(buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), object);
213  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_UNAUTHORIZED, postMethod.getStatusCode());
214    }
215   
 
216  1 toggle @Test
217    public void testPUTObject() throws Exception
218    {
219  1 final String TAG_VALUE = UUID.randomUUID().toString();
220   
221  1 Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
222   
223  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
224    objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
225  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
226   
227  1 Object objectSummary = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
228  1 getProperty(objectSummary, "tags").setValue(TAG_VALUE);
229   
230  1 PutMethod putMethod = executePutXml(
231    buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(),
232    objectToBePut.getNumber()).toString(),
233    objectSummary, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
234    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
235  1 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
236   
237  1 Object updatedObjectSummary = (Object) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
238   
239  1 Assert.assertEquals(TAG_VALUE, getProperty(updatedObjectSummary, "tags").getValue());
240  1 Assert.assertEquals(objectSummary.getClassName(), updatedObjectSummary.getClassName());
241  1 Assert.assertEquals(objectSummary.getNumber(), updatedObjectSummary.getNumber());
242    }
243   
 
244  1 toggle @Test
245    public void testPUTObjectUnauthorized() throws Exception
246    {
247  1 final String TAG_VALUE = UUID.randomUUID().toString();
248   
249  1 Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
250   
251  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
252    objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
253  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
254   
255  1 Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
256  1 String originalTagValue = getProperty(object, "tags").getValue();
257  1 getProperty(object, "tags").setValue(TAG_VALUE);
258   
259  1 PutMethod putMethod = executePutXml(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
260    objectToBePut.getClassName(), objectToBePut.getNumber()).toString(), object);
261  1 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_UNAUTHORIZED, putMethod.getStatusCode());
262   
263  1 getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
264    objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
265  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
266   
267  1 object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
268   
269  1 Assert.assertEquals(originalTagValue, getProperty(object, "tags").getValue());
270    }
271   
 
272  1 toggle @Test
273    public void testDELETEObject() throws Exception
274    {
275  1 Object objectToBeDeleted = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
276   
277  1 DeleteMethod deleteMethod = executeDelete(
278    buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBeDeleted.getClassName(),
279    objectToBeDeleted.getNumber()).toString(),
280    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
281  1 Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_NO_CONTENT, deleteMethod.getStatusCode());
282   
283  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
284    objectToBeDeleted.getClassName(), objectToBeDeleted.getNumber()).toString());
285  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode());
286    }
287   
 
288  1 toggle @Test
289    public void testDELETEObjectUnAuthorized() throws Exception
290    {
291  1 Object objectToBeDeleted = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
292   
293  1 DeleteMethod deleteMethod = executeDelete(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
294    objectToBeDeleted.getClassName(), objectToBeDeleted.getNumber()).toString());
295  1 Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_UNAUTHORIZED, deleteMethod.getStatusCode());
296   
297  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
298    objectToBeDeleted.getClassName(), objectToBeDeleted.getNumber()).toString());
299  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
300    }
301   
 
302  1 toggle @Test
303    public void testPUTProperty() throws Exception
304    {
305  1 final String TAG_VALUE = UUID.randomUUID().toString();
306   
307    /* Make sure that an Object with the TagClass exists. */
308  1 createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
309   
310  1 GetMethod getMethod =
311    executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
312  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
313   
314  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
315  1 Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
316  1 Assert.assertNotNull(link);
317   
318  1 getMethod = executeGet(link.getHref());
319  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
320   
321  1 Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
322   
323  1 Assert.assertFalse(objects.getObjectSummaries().isEmpty());
324   
325  1 Object currentObject = null;
326   
327  1 for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
328  1 if (objectSummary.getClassName().equals("XWiki.TagClass")) {
329  1 link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
330  1 Assert.assertNotNull(link);
331  1 getMethod = executeGet(link.getHref());
332  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
333   
334  1 currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
335  1 break;
336    }
337    }
338   
339  1 Assert.assertNotNull(currentObject);
340   
341  1 Property tagsProperty = getProperty(currentObject, "tags");
342   
343  1 Assert.assertNotNull(tagsProperty);
344   
345  1 Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
346   
347  1 Assert.assertNotNull(tagsPropertyLink);
348   
349  1 Property newTags = objectFactory.createProperty();
350  1 newTags.setValue(TAG_VALUE);
351   
352  1 PutMethod putMethod = executePutXml(tagsPropertyLink.getHref(), newTags,
353    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
354  1 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
355   
356  1 getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
357    currentObject.getClassName(), currentObject.getNumber()).toString());
358  1 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
359   
360  1 currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
361   
362  1 tagsProperty = getProperty(currentObject, "tags");
363   
364  1 Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
365    }
366   
 
367  1 toggle @Test
368    public void testPUTPropertyWithTextPlain() throws Exception
369    {
370  1 final String TAG_VALUE = UUID.randomUUID().toString();
371   
372    /* Make sure that an Object with the TagClass exists. */
373  1 createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
374   
375  1 GetMethod getMethod =
376    executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
377  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
378   
379  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
380  1 Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
381  1 Assert.assertNotNull(link);
382   
383  1 getMethod = executeGet(link.getHref());
384  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
385   
386  1 Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
387   
388  1 Assert.assertFalse(objects.getObjectSummaries().isEmpty());
389   
390  1 Object currentObject = null;
391   
392  1 for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
393  1 if (objectSummary.getClassName().equals("XWiki.TagClass")) {
394  1 link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
395  1 Assert.assertNotNull(link);
396  1 getMethod = executeGet(link.getHref());
397  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
398   
399  1 currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
400  1 break;
401    }
402    }
403   
404  1 Assert.assertNotNull(currentObject);
405   
406  1 Property tagsProperty = getProperty(currentObject, "tags");
407   
408  1 Assert.assertNotNull(tagsProperty);
409   
410  1 Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
411   
412  1 Assert.assertNotNull(tagsPropertyLink);
413   
414  1 PutMethod putMethod = executePut(tagsPropertyLink.getHref(), TAG_VALUE, MediaType.TEXT_PLAIN,
415    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
416  1 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
417   
418  1 getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
419    currentObject.getClassName(), currentObject.getNumber()).toString());
420  1 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
421   
422  1 currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
423   
424  1 tagsProperty = getProperty(currentObject, "tags");
425   
426  1 Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
427    }
428   
 
429  9 toggle private Object createObjectIfDoesNotExists(String className, List<String> spaces, String pageName) throws Exception
430    {
431  9 createPageIfDoesntExist(spaces, pageName, "");
432   
433  9 GetMethod getMethod = executeGet(buildURI(ObjectsResource.class, getWiki(), spaces, pageName).toString());
434  9 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
435   
436  9 Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
437   
438  9 for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
439  9 if (objectSummary.getClassName().equals(className)) {
440  9 Link link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
441  9 Assert.assertNotNull(link);
442  9 getMethod = executeGet(link.getHref());
443  9 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
444   
445  9 Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
446   
447  9 return object;
448    }
449    }
450   
451    /* If no object of that class is found, then create a new one */
452  0 Object object = objectFactory.createObject();
453  0 object.setClassName(className);
454   
455  0 PostMethod postMethod = executePostXml(buildURI(ObjectsResource.class, getWiki(), spaces, pageName).toString(),
456    object, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
457  0 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
458   
459  0 object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
460   
461  0 return object;
462    }
463   
 
464  1 toggle @Test
465    public void testPUTObjectFormUrlEncoded() throws Exception
466    {
467  1 final String TAG_VALUE = UUID.randomUUID().toString();
468   
469  1 Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
470   
471  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
472    objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
473  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
474   
475  1 Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
476   
477  1 NameValuePair[] nameValuePairs = new NameValuePair[1];
478  1 nameValuePairs[0] = new NameValuePair("property#tags", TAG_VALUE);
479   
480  1 PostMethod postMethod = executePostForm(
481    String.format("%s?method=PUT",
482    buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(),
483    objectToBePut.getNumber()).toString()),
484    nameValuePairs, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
485    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
486   
487  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());
488   
489  1 Object updatedObjectSummary = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
490   
491  1 Assert.assertEquals(TAG_VALUE, getProperty(updatedObjectSummary, "tags").getValue());
492  1 Assert.assertEquals(object.getClassName(), updatedObjectSummary.getClassName());
493  1 Assert.assertEquals(object.getNumber(), updatedObjectSummary.getNumber());
494    }
495   
 
496  1 toggle @Test
497    public void testPOSTObjectFormUrlEncoded() throws Exception
498    {
499  1 final String TAG_VALUE = "TAG";
500   
501  1 NameValuePair[] nameValuePairs = new NameValuePair[2];
502  1 nameValuePairs[0] = new NameValuePair("className", "XWiki.TagClass");
503  1 nameValuePairs[1] = new NameValuePair("property#tags", TAG_VALUE);
504   
505  1 PostMethod postMethod = executePostForm(
506    buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), nameValuePairs,
507    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
508  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
509   
510  1 Object object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
511   
512  1 Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());
513   
514  1 GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
515    object.getClassName(), object.getNumber()).toString());
516  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
517   
518  1 object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
519   
520  1 Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());
521    }
522   
 
523  1 toggle @Test
524    public void testPUTPropertyFormUrlEncoded() throws Exception
525    {
526  1 final String TAG_VALUE = UUID.randomUUID().toString();
527   
528    /* Make sure that an Object with the TagClass exists. */
529  1 createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
530   
531  1 GetMethod getMethod =
532    executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
533  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
534   
535  1 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
536  1 Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
537  1 Assert.assertNotNull(link);
538   
539  1 getMethod = executeGet(link.getHref());
540  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
541   
542  1 Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
543   
544  1 Assert.assertFalse(objects.getObjectSummaries().isEmpty());
545   
546  1 Object currentObject = null;
547   
548  1 for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
549  1 if (objectSummary.getClassName().equals("XWiki.TagClass")) {
550  1 link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
551  1 Assert.assertNotNull(link);
552  1 getMethod = executeGet(link.getHref());
553  1 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
554   
555  1 currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
556  1 break;
557    }
558    }
559   
560  1 Assert.assertNotNull(currentObject);
561   
562  1 Property tagsProperty = getProperty(currentObject, "tags");
563   
564  1 Assert.assertNotNull(tagsProperty);
565   
566  1 Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
567   
568  1 Assert.assertNotNull(tagsPropertyLink);
569   
570  1 NameValuePair[] nameValuePairs = new NameValuePair[1];
571  1 nameValuePairs[0] = new NameValuePair("property#tags", TAG_VALUE);
572   
573  1 PostMethod postMethod =
574    executePostForm(String.format("%s?method=PUT", tagsPropertyLink.getHref()), nameValuePairs,
575    TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
576  1 Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());
577   
578  1 getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName,
579    currentObject.getClassName(), currentObject.getNumber()).toString());
580  1 Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
581   
582  1 currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
583   
584  1 tagsProperty = getProperty(currentObject, "tags");
585   
586  1 Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
587    }
588   
 
589  1 toggle @Test
590    public void testGETObjectAtPageVersion() throws Exception
591    {
592  1 Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
593   
594  1 Map<String, String> versionToValueMap = new HashMap<String, String>();
595  6 for (int i = 0; i < 5; i++) {
596  5 String value = String.format("Value%d", i);
597   
598  5 Property property = getProperty(objectToBePut, "tags");
599  5 property.setValue(value);
600   
601  5 PutMethod putMethod = executePutXml(
602    buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(),
603    objectToBePut.getNumber()).toString(),
604    objectToBePut, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(),
605    TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
606  5 Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
607   
608  5 GetMethod getMethod =
609    executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
610  5 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
611   
612  5 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
613   
614  5 versionToValueMap.put(page.getVersion(), value);
615    }
616   
617  1 for (String version : versionToValueMap.keySet()) {
618  5 GetMethod getMethod = executeGet(buildURI(ObjectAtPageVersionResource.class, getWiki(), this.spaces,
619    this.pageName, version, objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
620  5 Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
621   
622  5 Object currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
623   
624  5 Property property = getProperty(currentObject, "tags");
625   
626  5 Assert.assertEquals(versionToValueMap.get(version), property.getValue());
627   
628  5 checkLinks(currentObject);
629  5 for (Property p : currentObject.getProperties()) {
630  5 checkLinks(p);
631    }
632    }
633    }
634    }