Search in sources :

Example 11 with PostRequest

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

the class FormRestApiGet_Test method testJsonContentParsesCorrectly.

public void testJsonContentParsesCorrectly() throws Exception {
    JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
    String jsonPostString = jsonPostData.toString();
    Response rsp = sendRequest(new PostRequest(FORM_DEF_URL, jsonPostString, APPLICATION_JSON), 200);
    String jsonResponseString = rsp.getContentAsString();
    Object jsonObject = new JSONUtils().toObject(jsonResponseString);
    assertNotNull("JSON object was null.", jsonObject);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) JSONUtils(org.springframework.extensions.webscripts.json.JSONUtils)

Example 12 with PostRequest

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

the class FormRestApiGet_Test method testJsonSelectedFields.

public void testJsonSelectedFields() throws Exception {
    JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
    JSONArray jsonFields = new JSONArray();
    jsonFields.put("cm:name");
    jsonFields.put("cm:title");
    jsonFields.put("cm:publisher");
    jsonPostData.put("fields", jsonFields);
    // Submit the JSON request.
    String jsonPostString = jsonPostData.toString();
    Response rsp = sendRequest(new PostRequest(FORM_DEF_URL, jsonPostString, APPLICATION_JSON), 200);
    String jsonResponseString = rsp.getContentAsString();
    JSONObject jsonParsedObject = new JSONObject(new JSONTokener(jsonResponseString));
    assertNotNull(jsonParsedObject);
    JSONObject rootDataObject = (JSONObject) jsonParsedObject.get("data");
    JSONObject definitionObject = (JSONObject) rootDataObject.get("definition");
    JSONArray fieldsArray = (JSONArray) definitionObject.get("fields");
    assertEquals("Expected 2 fields", 2, fieldsArray.length());
    // get the name and title definitions
    JSONObject nameField = (JSONObject) fieldsArray.get(0);
    JSONObject titleField = (JSONObject) fieldsArray.get(1);
    String nameFieldDataKey = nameField.getString("dataKeyName");
    String titleFieldDataKey = titleField.getString("dataKeyName");
    // get the data and check it
    JSONObject formDataObject = (JSONObject) rootDataObject.get("formData");
    assertNotNull("Expected to find cm:name data", formDataObject.get(nameFieldDataKey));
    assertNotNull("Expected to find cm:title data", formDataObject.get(titleFieldDataKey));
    assertEquals(TEST_FORM_TITLE, formDataObject.get("prop_cm_title"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 13 with PostRequest

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

the class FormRestApiJsonPost_Test method testSimpleJsonPostRequest.

public void testSimpleJsonPostRequest() throws IOException, JSONException {
    // Retrieve and store the original property value.
    Serializable originalDescription = nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_DESCRIPTION);
    assertEquals(TEST_FORM_DESCRIPTION, originalDescription);
    // get the original mimetype
    String originalMimetype = null;
    ContentData content = (ContentData) this.nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_CONTENT);
    if (content != null) {
        originalMimetype = content.getMimetype();
    }
    // Construct some JSON to represent a new value.
    JSONObject jsonPostData = new JSONObject();
    final String proposedNewDescription = "Modified Description";
    jsonPostData.put(PROP_CM_DESCRIPTION, proposedNewDescription);
    jsonPostData.put(PROP_MIMETYPE, MimetypeMap.MIMETYPE_HTML);
    // Submit the JSON request.
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
    // The nodeService should give us the modified property.
    Serializable modifiedDescription = nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_DESCRIPTION);
    assertEquals(proposedNewDescription, modifiedDescription);
    // get the modified mimetype
    String modifiedMimetype = null;
    content = (ContentData) this.nodeService.getProperty(referencingDocNodeRef, ContentModel.PROP_CONTENT);
    if (content != null) {
        modifiedMimetype = content.getMimetype();
    }
    assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);
// The Rest API should also give us the modified property.
/*
        Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
        JSONObject jsonGetResponse = new JSONObject(response.getContentAsString());
        JSONObject jsonDataObj = (JSONObject)jsonGetResponse.get("data");
        assertNotNull(jsonDataObj);

        JSONObject formData = (JSONObject)jsonDataObj.get("formData");
        assertNotNull(formData);
        String retrievedValue = (String)formData.get(PROP_CM_DESCRIPTION);
        assertEquals(modifiedDescription, retrievedValue);
        String retrievedMimetype = (String)formData.get(PROP_MIMETYPE);
        assertEquals(MimetypeMap.MIMETYPE_HTML, modifiedMimetype);*/
}
Also used : Serializable(java.io.Serializable) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) ContentData(org.alfresco.service.cmr.repository.ContentData) JSONObject(org.json.JSONObject)

Example 14 with PostRequest

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

the class FormRestApiJsonPost_Test method testAddNewAssociationsToNode.

/**
 * This test method attempts to add new associations between existing nodes.
 */
public void testAddNewAssociationsToNode() throws Exception {
    List<NodeRef> associatedNodes;
    checkOriginalAssocsBeforeChanges();
    // Add three additional associations
    JSONObject jsonPostData = new JSONObject();
    String assocsToAdd = associatedDoc_C + "," + associatedDoc_D + "," + associatedDoc_E;
    jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
    // Check the now updated associations via the node service
    List<AssociationRef> modifiedAssocs = nodeService.getTargetAssocs(referencingDocNodeRef, RegexQNamePattern.MATCH_ALL);
    assertEquals(5, modifiedAssocs.size());
    // Extract the target nodeRefs to make them easier to examine
    associatedNodes = new ArrayList<NodeRef>(5);
    for (AssociationRef assocRef : modifiedAssocs) {
        associatedNodes.add(assocRef.getTargetRef());
    }
    assertTrue(associatedNodes.contains(associatedDoc_A));
    assertTrue(associatedNodes.contains(associatedDoc_B));
    assertTrue(associatedNodes.contains(associatedDoc_C));
    assertTrue(associatedNodes.contains(associatedDoc_D));
    assertTrue(associatedNodes.contains(associatedDoc_E));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(referencingNodeUpdateUrl), 200);
        String jsonRspString = response.getContentAsString();
        JSONObject jsonGetResponse = new JSONObject(jsonRspString);
        JSONObject jsonData = (JSONObject)jsonGetResponse.get("data");
        assertNotNull(jsonData);

        JSONObject jsonFormData = (JSONObject)jsonData.get("formData");
        assertNotNull(jsonFormData);
        
        String jsonAssocs = (String)jsonFormData.get(ASSOC_CM_REFERENCES);
        
        // We expect exactly 5 assocs on the test node
        assertEquals(5, jsonAssocs.split(",").length);
        for (AssociationRef assocRef : modifiedAssocs)
        {
            assertTrue(jsonAssocs.contains(assocRef.getTargetRef().toString()));
        }*/
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) AssociationRef(org.alfresco.service.cmr.repository.AssociationRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 15 with PostRequest

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

the class FormRestApiJsonPost_Test method testAddAssocThatAlreadyExists.

/**
 * This test method attempts to add the same association twice. This attempt will
 * not succeed, but the test case is to confirm that there is no exception thrown
 * back across the REST API.
 */
public void testAddAssocThatAlreadyExists() throws Exception {
    checkOriginalAssocsBeforeChanges();
    // Add an association
    JSONObject jsonPostData = new JSONObject();
    String assocsToAdd = associatedDoc_C.toString();
    jsonPostData.put(ASSOC_CM_REFERENCES_ADDED, assocsToAdd);
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
    // Try to add the same association again
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

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