Search in sources :

Example 41 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class BlogServiceTest method testCreateDraftPostWithTagsAndComment.

/**
 * @since 4.0
 */
@Test
@Category({ LuceneTests.class, RedundantTests.class })
public void testCreateDraftPostWithTagsAndComment() throws Exception {
    String[] tags = new String[] { "foo", "bar" };
    String title = "test";
    String content = "test";
    JSONObject item = createPost(title, content, tags, true, 200);
    // check that the values
    assertEquals(title, item.get("title"));
    assertEquals(content, item.get("content"));
    assertEquals(true, item.get("isDraft"));
    JSONArray reportedTags = (JSONArray) item.get("tags");
    assertEquals("Tags size was wrong.", 2, reportedTags.length());
    List<String> recoveredTagsList = Arrays.asList(new String[] { reportedTags.getString(0), reportedTags.getString(1) });
    assertEquals("Tags were wrong.", Arrays.asList(tags), recoveredTagsList);
    // comment on the blog post.
    NodeRef blogPostNode = new NodeRef(item.getString("nodeRef"));
    // Currently (mid-Swift dev) there is no Java CommentService, so we have to post a comment via the REST API.
    String commentsPostUrl = "/api/node/" + blogPostNode.getStoreRef().getProtocol() + "/" + blogPostNode.getStoreRef().getIdentifier() + "/" + blogPostNode.getId() + "/comments";
    String jsonToPost = new JSONStringer().object().key("title").value("Commented blog title").key("content").value("Some content.").endObject().toString();
    Response response = sendRequest(new PostRequest(commentsPostUrl, jsonToPost, "application/json"), 200);
    // check that other user doesn't have access to the draft
    this.authenticationComponent.setCurrentUser(USER_TWO);
    getPost(item.getString("name"), 404);
    this.authenticationComponent.setCurrentUser(USER_ONE);
    // Now we'll GET my-drafts to ensure that the post is there.
    response = sendRequest(new GetRequest(URL_MY_DRAFT_BLOG_POSTS), 200);
    JSONObject result = new JSONObject(response.getContentAsString());
    // Ensure it reports the tag correctly on GET.
    JSONArray items = result.getJSONArray("items");
    JSONArray tagsArray = items.getJSONObject(0).getJSONArray("tags");
    assertEquals("Wrong number of tags", 2, tagsArray.length());
    assertEquals("Tag wrong", tags[0], tagsArray.getString(0));
    assertEquals("Tag wrong", tags[1], tagsArray.getString(1));
    // Ensure the comment count is accurate
    assertEquals("Wrong comment count", 1, items.getJSONObject(0).getInt("commentCount"));
    // and that there is content at the commentsURL.
    String commentsUrl = "/api" + items.getJSONObject(0).getString("commentsUrl");
    response = sendRequest(new GetRequest(commentsUrl), 200);
    // Now get blog-post by tag.
    // 1. No such tag
    response = sendRequest(new GetRequest(URL_BLOG_POSTS + "?tag=NOSUCHTAG"), 200);
    result = new JSONObject(response.getContentAsString());
    assertEquals(0, result.getInt("total"));
    // tag created above
    response = sendRequest(new GetRequest(URL_BLOG_POSTS + "?tag=foo"), 200);
    result = new JSONObject(response.getContentAsString());
    assertEquals(1, result.getInt("total"));
// TODO More assertions on recovered node.
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) JSONStringer(org.json.JSONStringer) Category(org.junit.experimental.categories.Category) BaseWebScriptTest(org.alfresco.repo.web.scripts.BaseWebScriptTest) Test(org.junit.Test)

Example 42 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class BlogServiceTest method createComment.

private JSONObject createComment(String nodeRef, String title, String content, int expectedStatus) throws Exception {
    JSONObject comment = new JSONObject();
    comment.put("title", title);
    comment.put("content", content);
    comment.put("site", SITE_SHORT_NAME_BLOG);
    Response response = sendRequest(new PostRequest(getCommentsUrl(nodeRef), comment.toString(), "application/json"), expectedStatus);
    if (expectedStatus != 200) {
        return null;
    }
    // logger.debug("Comment created: " + response.getContentAsString());
    JSONObject result = new JSONObject(response.getContentAsString());
    return result.getJSONObject("item");
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 43 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project records-management by Alfresco.

the class RmRestApiTest method testFileAuditLogAsRecord.

public void testFileAuditLogAsRecord() throws Exception {
    // Attempt to store audit log at non existent destination, make sure we get 404
    JSONObject jsonPostData = new JSONObject();
    jsonPostData.put("destination", "workspace://SpacesStore/09ca1e02-1c87-4a53-97e7-xxxxxxxxxxxx");
    String jsonPostString = jsonPostData.toString();
    Response rsp = sendRequest(new PostRequest(RMA_AUDITLOG_URL, jsonPostString, APPLICATION_JSON), 404);
    // Attempt to store audit log at wrong type of destination, make sure we get 400
    jsonPostData = new JSONObject();
    jsonPostData.put("destination", recordCategory.toString());
    jsonPostString = jsonPostData.toString();
    rsp = sendRequest(new PostRequest(RMA_AUDITLOG_URL, jsonPostString, APPLICATION_JSON), 400);
    // Store the full audit log as a record
    jsonPostData = new JSONObject();
    jsonPostData.put("destination", recordFolder2);
    jsonPostString = jsonPostData.toString();
    rsp = sendRequest(new PostRequest(RMA_AUDITLOG_URL, jsonPostString, APPLICATION_JSON), 200);
    // check the response
    System.out.println(rsp.getContentAsString());
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    assertTrue(jsonRsp.has("success"));
    assertTrue(jsonRsp.getBoolean("success"));
    assertTrue(jsonRsp.has("record"));
    assertNotNull(jsonRsp.get("record"));
    assertTrue(nodeService.exists(new NodeRef(jsonRsp.getString("record"))));
    assertTrue(jsonRsp.has("recordName"));
    assertNotNull(jsonRsp.get("recordName"));
    assertTrue(jsonRsp.getString("recordName").startsWith("audit_"));
    // Store a filtered audit log as a record
    jsonPostData = new JSONObject();
    jsonPostData.put("destination", recordFolder2);
    jsonPostData.put("size", "50");
    jsonPostData.put("user", "gavinc");
    jsonPostData.put("event", "Update Metadata");
    jsonPostData.put("property", "{http://www.alfresco.org/model/content/1.0}modified");
    jsonPostString = jsonPostData.toString();
    rsp = sendRequest(new PostRequest(RMA_AUDITLOG_URL, jsonPostString, APPLICATION_JSON), 200);
    // check the response
    System.out.println(rsp.getContentAsString());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    assertTrue(jsonRsp.has("success"));
    assertTrue(jsonRsp.getBoolean("success"));
    assertTrue(jsonRsp.has("record"));
    assertNotNull(jsonRsp.get("record"));
    assertTrue(nodeService.exists(new NodeRef(jsonRsp.getString("record"))));
    assertTrue(jsonRsp.has("recordName"));
    assertNotNull(jsonRsp.get("recordName"));
    assertTrue(jsonRsp.getString("recordName").startsWith("audit_"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 44 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project records-management by Alfresco.

the class RmRestApiTest method testPostActionToNonExistentNode.

/**
 * This test method ensures that a POST of an RM action to a non-existent node
 * will result in a 404 status.
 *
 * @throws Exception
 */
public void testPostActionToNonExistentNode() throws Exception {
    NodeRef nonExistentNode = new NodeRef("workspace://SpacesStore/09ca1e02-1c87-4a53-97e7-xxxxxxxxxxxx");
    // Construct the JSON request.
    JSONObject jsonPostData = new JSONObject();
    jsonPostData.put("nodeRef", nonExistentNode.toString());
    // Although the request specifies a 'reviewed' action, it does not matter what
    // action is specified here, as the non-existent Node should trigger a 404
    // before the action is executed.
    jsonPostData.put("name", "reviewed");
    // Submit the JSON request.
    String jsonPostString = jsonPostData.toString();
    final int expectedStatus = 404;
    sendRequest(new PostRequest(RMA_ACTIONS_URL, jsonPostString, APPLICATION_JSON), expectedStatus);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 45 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project records-management by Alfresco.

the class RmRestApiTest method postCustomPropertyDefinition.

/**
 * Creates a new property definition using a POST call.
 * GETs the resultant property definition.
 *
 * @param propertyLabel the label to use
 * @param propId the propId to use - null to have one generated.
 * @return the propId of the new property definition
 */
private String postCustomPropertyDefinition(String propertyLabel, String propId) throws JSONException, IOException, UnsupportedEncodingException {
    String jsonString;
    if (propId == null) {
        jsonString = new JSONStringer().object().key("label").value(propertyLabel).key("description").value("Dynamically defined test property").key("mandatory").value(false).key("dataType").value("d:text").key("element").value("record").key("constraintRef").value("rmc:smList").endObject().toString();
    } else {
        jsonString = new JSONStringer().object().key("label").value(propertyLabel).key("description").value("Dynamically defined test property").key("mandatory").value(false).key("dataType").value("d:text").key("element").value("record").key("constraintRef").value("rmc:smList").key("propId").value(propId).endObject().toString();
    }
    // Submit the JSON request.
    final int expectedStatus = 200;
    Response rsp = sendRequest(new PostRequest("/api/rma/admin/custompropertydefinitions?element=record", jsonString, APPLICATION_JSON), expectedStatus);
    String rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rspContent));
    String urlOfNewPropDef = jsonRsp.getString("url");
    String newPropId = jsonRsp.getString("propId");
    assertNotNull("urlOfNewPropDef was null.", urlOfNewPropDef);
    // GET from the URL we're given to ensure it's valid
    rsp = sendRequest(new GetRequest(urlOfNewPropDef), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    JSONObject dataObject = jsonRsp.getJSONObject("data");
    assertNotNull("JSON data object was null", dataObject);
    JSONObject customPropsObject = dataObject.getJSONObject("customProperties");
    assertNotNull("JSON customProperties object was null", customPropsObject);
    assertEquals("Wrong customProperties length.", 1, customPropsObject.length());
    Object keyToSoleProp = customPropsObject.keys().next();
    // System.out.println("New property defn: " + keyToSoleProp);
    JSONObject newPropObject = customPropsObject.getJSONObject((String) keyToSoleProp);
    assertEquals("Wrong property label.", propertyLabel, newPropObject.getString("label"));
    return newPropId;
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer)

Aggregations

PostRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest)110 JSONObject (org.json.JSONObject)90 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)84 JSONArray (org.json.JSONArray)28 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)25 JSONTokener (org.json.JSONTokener)23 NodeRef (org.alfresco.service.cmr.repository.NodeRef)22 DeleteRequest (org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)14 PutRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)10 JSONStringer (org.json.JSONStringer)9 File (java.io.File)8 ZipFile (java.util.zip.ZipFile)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 JSONObject (org.json.simple.JSONObject)5 IOException (java.io.IOException)4 UserTransaction (javax.transaction.UserTransaction)4 FileInfo (org.alfresco.service.cmr.model.FileInfo)4 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)4 JSONException (org.json.JSONException)4 Serializable (java.io.Serializable)3