Search in sources :

Example 61 with PostRequest

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

the class FormRestApiJsonPost_Test method testAddChildAssocThatAlreadyExists.

/**
 * This test method attempts to add the same child 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 testAddChildAssocThatAlreadyExists() throws Exception {
    checkOriginalChildAssocsBeforeChanges();
    // Add an association
    JSONObject jsonPostData = new JSONObject();
    String assocsToAdd = this.childDoc_C.toString();
    jsonPostData.put(ASSOC_SYS_CHILDREN_ADDED, assocsToAdd);
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
    // Try to add the same child association again
    sendRequest(new PostRequest(referencingNodeUpdateUrl, jsonPostString, APPLICATION_JSON), 200);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 62 with PostRequest

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

the class FormRestApiJsonPost_Test method testAddNewChildAssociationsToNode.

/**
 * This test method attempts to add new associations between existing nodes.
 */
public void testAddNewChildAssociationsToNode() throws Exception {
    List<NodeRef> associatedNodes;
    checkOriginalChildAssocsBeforeChanges();
    // Add three additional associations
    JSONObject jsonPostData = new JSONObject();
    String assocsToAdd = childDoc_C + "," + childDoc_D + "," + childDoc_E;
    jsonPostData.put(ASSOC_SYS_CHILDREN_ADDED, assocsToAdd);
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(containingNodeUpdateUrl.toString(), jsonPostString, APPLICATION_JSON), 200);
    // Check the now updated child associations via the node service
    List<ChildAssociationRef> modifiedAssocs = nodeService.getChildAssocs(containerNodeRef);
    assertEquals(5, modifiedAssocs.size());
    // Extract the target nodeRefs to make them easier to examine
    associatedNodes = new ArrayList<NodeRef>(5);
    for (ChildAssociationRef assocRef : modifiedAssocs) {
        associatedNodes.add(assocRef.getChildRef());
    }
    assertTrue(associatedNodes.contains(childDoc_A));
    assertTrue(associatedNodes.contains(childDoc_B));
    assertTrue(associatedNodes.contains(childDoc_C));
    assertTrue(associatedNodes.contains(childDoc_D));
    assertTrue(associatedNodes.contains(childDoc_E));
// The Rest API should also give us the modified assocs.
/*Response response = sendRequest(new GetRequest(containingNodeUpdateUrl), 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_SYS_CHILDREN);
        
        // We expect exactly 5 assocs on the test node
        assertEquals(5, jsonAssocs.split(",").length);
        for (ChildAssociationRef assocRef : modifiedAssocs)
        {
            String childNodeRef = assocRef.getChildRef().toString();
            assertTrue(jsonAssocs.contains(childNodeRef));
            assertTrue(NodeRef.isNodeRef(childNodeRef));
        }*/
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 63 with PostRequest

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

the class FormRestApiJsonPost_Test method testRemoveAssociationsFromNode.

/**
 * This test method attempts to remove an existing association between two existing
 * nodes.
 */
public void testRemoveAssociationsFromNode() throws Exception {
    List<NodeRef> associatedNodes;
    checkOriginalAssocsBeforeChanges();
    // Remove an association
    JSONObject jsonPostData = new JSONObject();
    String assocsToRemove = associatedDoc_B.toString();
    jsonPostData.put(ASSOC_CM_REFERENCES_REMOVED, assocsToRemove);
    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(1, 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));
// 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 1 assoc on the test node
        assertEquals(1, 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 64 with PostRequest

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

the class GroupsTest method testUpdateGroup.

/**
 * Detailed test of update group
 * @throws Exception
 */
public void testUpdateGroup() throws Exception {
    String myGroupName = "GT_UG";
    String myDisplayName = "GT_UGDisplay";
    String myNewDisplayName = "GT_UGDisplayNew";
    this.authenticationComponent.setSystemUserAsCurrentUser();
    try {
        /**
         * Create a root group
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            newGroupJSON.put("displayName", myDisplayName);
            sendRequest(new PostRequest(URL_ROOTGROUPS + "/" + myGroupName, newGroupJSON.toString(), "application/json"), Status.STATUS_CREATED);
        }
        /**
         * Now change its display name
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            newGroupJSON.put("displayName", myNewDisplayName);
            Response response = sendRequest(new PutRequest(URL_GROUPS + "/" + myGroupName, newGroupJSON.toString(), "application/json"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONObject data = top.getJSONObject("data");
            assertTrue(data.length() > 0);
            assertEquals("displayName wrong", myNewDisplayName, data.getString("displayName"));
        }
        /**
         * Now get it and verify that the name has changed
         */
        {
            Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myGroupName), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONObject data = top.getJSONObject("data");
            assertTrue(data.length() > 0);
            assertEquals("displayName wrong", myNewDisplayName, data.getString("displayName"));
        }
        /**
         * Negative test
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            newGroupJSON.put("displayName", myNewDisplayName);
            sendRequest(new PutRequest(URL_GROUPS + "/" + "rubbish", newGroupJSON.toString(), "application/json"), Status.STATUS_NOT_FOUND);
        }
    } finally {
        sendRequest(new DeleteRequest(URL_ROOTGROUPS + "/" + myGroupName), 0);
    }
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)

Example 65 with PostRequest

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

the class GroupsTest method testLinkChild.

/**
 * Detailed test of link group
 */
public void testLinkChild() throws Exception {
    String myRootGroup = "GT_LGROOT";
    try {
        this.authenticationComponent.setSystemUserAsCurrentUser();
        sendRequest(new DeleteRequest(URL_ROOTGROUPS + "/" + myRootGroup), 0);
        String groupLinkFullName = "";
        {
            Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + TEST_LINK), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONObject data = top.getJSONObject("data");
            assertTrue(data.length() > 0);
            groupLinkFullName = data.getString("fullName");
        }
        /**
         * Create a root group
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            newGroupJSON.put("displayName", myRootGroup);
            sendRequest(new PostRequest(URL_ROOTGROUPS + "/" + myRootGroup, newGroupJSON.toString(), "application/json"), Status.STATUS_CREATED);
        }
        /**
         * Link an existing group (GROUPB) to my root group.
         */
        /**
         * Negative test Link Group B without administrator access.
         */
        this.authenticationComponent.setCurrentUser(USER_ONE);
        {
            JSONObject newGroupJSON = new JSONObject();
            sendRequest(new PostRequest(URL_GROUPS + "/" + myRootGroup + "/children/" + groupLinkFullName, newGroupJSON.toString(), "application/json"), Status.STATUS_INTERNAL_SERVER_ERROR);
        }
        this.authenticationComponent.setSystemUserAsCurrentUser();
        /**
         * Link Group B
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            Response response = sendRequest(new PostRequest(URL_GROUPS + "/" + myRootGroup + "/children/" + groupLinkFullName, newGroupJSON.toString(), "application/json"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONObject data = top.getJSONObject("data");
        }
        /**
         * Link the group again - this fails
         * - duplicate groups (children with the same name) are not allowed
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            Response response = sendRequest(new PostRequest(URL_GROUPS + "/" + myRootGroup + "/children/" + groupLinkFullName, newGroupJSON.toString(), "application/json"), Status.STATUS_INTERNAL_SERVER_ERROR);
        }
        /**
         * Get All Children of myGroup which are GROUPS - should find GROUP B
         */
        {
            logger.debug("Get child GROUPS of myRootGroup");
            Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myRootGroup + "/children?authorityType=GROUP"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONArray data = top.getJSONArray("data");
            assertTrue("no child groups of myGroup", data.length() == 1);
            JSONObject subGroup = data.getJSONObject(0);
            assertEquals("shortName wrong", TEST_LINK, subGroup.getString("shortName"));
            assertEquals("authorityType wrong", "GROUP", subGroup.getString("authorityType"));
        }
        /**
         * Now link in an existing user
         */
        {
            JSONObject newGroupJSON = new JSONObject();
            String userOneFullName = USER_ONE;
            Response response = sendRequest(new PostRequest(URL_GROUPS + "/" + myRootGroup + "/children/" + userOneFullName, newGroupJSON.toString(), "application/json"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONObject data = top.getJSONObject("data");
        }
        /**
         * Get All Children of myGroup which are USERS - should find USER ONE
         */
        {
            logger.debug("Get child USERS of myRootGroup");
            Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myRootGroup + "/children?authorityType=USER"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONArray data = top.getJSONArray("data");
            assertTrue("no child groups of myGroup", data.length() == 1);
            JSONObject subGroup = data.getJSONObject(0);
            assertEquals("shortName wrong", USER_ONE, subGroup.getString("shortName"));
            assertEquals("authorityType wrong", "USER", subGroup.getString("authorityType"));
        }
        /**
         * Unlink Group B
         */
        {
            logger.debug("Unlink Test Link");
            Response response = sendRequest(new DeleteRequest(URL_GROUPS + "/" + myRootGroup + "/children/" + groupLinkFullName), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
        }
        /**
         * Get All Children of myGroup which are GROUPS - should no longer find GROUP B
         */
        {
            logger.debug("Get child GROUPS of myRootGroup");
            Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myRootGroup + "/children?authorityType=GROUP"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONArray data = top.getJSONArray("data");
        // TODO TEST failing
        // assertTrue("group B not removed", data.length() == 0);
        }
        /**
         * Create a new group (BUFFY)
         */
        String myNewGroup = "GROUP_BUFFY";
        {
            // Delete incase it already exists from a previous test run
            sendRequest(new DeleteRequest(URL_ROOTGROUPS + "/BUFFY"), 0);
            JSONObject newGroupJSON = new JSONObject();
            Response response = sendRequest(new PostRequest(URL_GROUPS + "/" + myRootGroup + "/children/" + myNewGroup, newGroupJSON.toString(), "application/json"), Status.STATUS_CREATED);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONObject data = top.getJSONObject("data");
            assertEquals("shortName wrong", "BUFFY", data.getString("shortName"));
            assertEquals("fullName wrong", myNewGroup, data.getString("fullName"));
            assertEquals("authorityType wrong", "GROUP", data.getString("authorityType"));
        }
        /**
         * Get All Children of myGroup which are GROUPS - should find GROUP(BUFFY)
         */
        {
            logger.debug("Get child GROUPS of myRootGroup");
            Response response = sendRequest(new GetRequest(URL_GROUPS + "/" + myRootGroup + "/children?authorityType=GROUP"), Status.STATUS_OK);
            JSONObject top = new JSONObject(response.getContentAsString());
            logger.debug(response.getContentAsString());
            JSONArray data = top.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject rootGroup = data.getJSONObject(i);
                if (rootGroup.getString("fullName").equals(myNewGroup)) {
                }
            }
        }
    /**
     * Negative tests
     */
    } finally {
        sendRequest(new DeleteRequest(URL_ROOTGROUPS + "/" + myRootGroup), 0);
    }
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)

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