use of org.xwiki.rest.model.jaxb.Property in project xwiki-platform by xwiki.
the class ObjectsResourceTest method testPUTPropertyWithTextPlain.
@Test
public void testPUTPropertyWithTextPlain() throws Exception {
final String TAG_VALUE = UUID.randomUUID().toString();
/* Make sure that an Object with the TagClass exists. */
createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
Assert.assertNotNull(link);
getMethod = executeGet(link.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertFalse(objects.getObjectSummaries().isEmpty());
Object currentObject = null;
for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
if (objectSummary.getClassName().equals("XWiki.TagClass")) {
link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
Assert.assertNotNull(link);
getMethod = executeGet(link.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
break;
}
}
Assert.assertNotNull(currentObject);
Property tagsProperty = getProperty(currentObject, "tags");
Assert.assertNotNull(tagsProperty);
Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
Assert.assertNotNull(tagsPropertyLink);
PutMethod putMethod = executePut(tagsPropertyLink.getHref(), TAG_VALUE, MediaType.TEXT_PLAIN, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, currentObject.getClassName(), currentObject.getNumber()).toString());
Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
tagsProperty = getProperty(currentObject, "tags");
Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}
use of org.xwiki.rest.model.jaxb.Property in project xwiki-platform by xwiki.
the class ObjectsResourceTest method testRepresentation.
@Override
@Test
public void testRepresentation() throws Exception {
GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
Assert.assertNotNull(link);
getMethod = executeGet(link.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertFalse(objects.getObjectSummaries().isEmpty());
for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
getMethod = executeGet(link.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
checkLinks(objectSummary);
for (Property property : object.getProperties()) {
checkLinks(property);
}
}
}
use of org.xwiki.rest.model.jaxb.Property in project xwiki-platform by xwiki.
the class ObjectsResourceTest method testPOSTInvalidObject.
@Test
public void testPOSTInvalidObject() throws Exception {
final String TAG_VALUE = "TAG";
Property property = new Property();
property.setName("tags");
property.setValue(TAG_VALUE);
Object object = objectFactory.createObject();
object.getProperties().add(property);
PostMethod postMethod = executePostXml(buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), object, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_BAD_REQUEST, postMethod.getStatusCode());
}
use of org.xwiki.rest.model.jaxb.Property in project xwiki-platform by xwiki.
the class PageResourceTest method testPUTGETWithObject.
@Test
public void testPUTGETWithObject() throws Exception {
String pageURI = buildURI(PageResource.class, getWiki(), Arrays.asList("RESTTest"), "PageWithObject");
final String title = String.format("Title (%s)", UUID.randomUUID().toString());
final String content = String.format("This is a content (%d)", System.currentTimeMillis());
final String comment = String.format("Updated title and content (%d)", System.currentTimeMillis());
Page newPage = this.objectFactory.createPage();
newPage.setTitle(title);
newPage.setContent(content);
newPage.setComment(comment);
// Add object
final String TAG_VALUE = "TAG";
Property property = new Property();
property.setName("tags");
property.setValue(TAG_VALUE);
Object object = objectFactory.createObject();
object.setClassName("XWiki.TagClass");
object.getProperties().add(property);
newPage.setObjects(objectFactory.createObjects());
newPage.getObjects().getObjectSummaries().add(object);
// PUT
PutMethod putMethod = executePutXml(pageURI, newPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(), isIn(Arrays.asList(HttpStatus.SC_ACCEPTED, HttpStatus.SC_CREATED)));
Page modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
Assert.assertEquals(title, modifiedPage.getTitle());
Assert.assertEquals(content, modifiedPage.getContent());
Assert.assertEquals(comment, modifiedPage.getComment());
// GET
GetMethod getMethod = executeGet(pageURI + "?objects=true");
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertEquals(title, modifiedPage.getTitle());
Assert.assertEquals(content, modifiedPage.getContent());
Assert.assertEquals(comment, modifiedPage.getComment());
Assert.assertEquals(TAG_VALUE, getProperty((Object) modifiedPage.getObjects().getObjectSummaries().get(0), "tags").getValue());
// Send again but with empty object list
modifiedPage.getObjects().getObjectSummaries().clear();
// PUT
putMethod = executePutXml(pageURI, modifiedPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(), isIn(Arrays.asList(HttpStatus.SC_ACCEPTED)));
modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
Assert.assertEquals(title, modifiedPage.getTitle());
Assert.assertEquals(content, modifiedPage.getContent());
Assert.assertEquals(comment, modifiedPage.getComment());
// GET
getMethod = executeGet(pageURI + "?objects=true");
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertEquals(title, modifiedPage.getTitle());
Assert.assertEquals(content, modifiedPage.getContent());
Assert.assertEquals(comment, modifiedPage.getComment());
Assert.assertTrue(modifiedPage.getObjects().getObjectSummaries().isEmpty());
}
use of org.xwiki.rest.model.jaxb.Property in project xwiki-platform by xwiki.
the class TestUtils method setWikiPreference.
/**
* Sets the value of an existing property of XWiki.XWikiPreferences.
*
* @param propertyName name of the property to set
* @param value value to set to the property
* @since 9.7RC1
*/
public void setWikiPreference(String propertyName, String value) throws Exception {
DocumentReference documentReference = new DocumentReference(getCurrentWiki(), "XWiki", "XWikiPreferences");
ObjectReference objectReference = new ObjectReference("XWiki.XWikiPreferences[0]", documentReference);
Property property = RestTestUtils.property(propertyName, value);
org.xwiki.rest.model.jaxb.Object preferenceObject = rest().get(objectReference, false);
if (preferenceObject == null) {
// The object does not exist, create it
preferenceObject = RestTestUtils.object("XWiki.XWikiPreferences");
preferenceObject.withProperties(property);
TestUtils.assertStatusCodes(rest().executePost(ObjectsResource.class, preferenceObject, rest().toElements(documentReference)), true, STATUS_CREATED);
} else {
// The object exist just set the property (faster than updating the whole object)
ObjectPropertyReference propertyReference = new ObjectPropertyReference(propertyName, objectReference);
TestUtils.assertStatusCodes(rest().executePut(ObjectPropertyResource.class, property, rest().toElements(propertyReference)), true, STATUS_ACCEPTED);
}
}
Aggregations