Search in sources :

Example 21 with PutRequest

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

the class AbstractWorkflowRestApiTest method testTaskInstancePut.

public void testTaskInstancePut() throws Exception {
    // Start workflow as USER1 and assign task to USER2.
    personManager.setUser(USER1);
    WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
    Map<QName, Serializable> params = new HashMap<QName, Serializable>();
    params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER2));
    Calendar dueDate = Calendar.getInstance();
    dueDate.set(Calendar.MILLISECOND, 0);
    params.put(WorkflowModel.PROP_DUE_DATE, new Date());
    params.put(WorkflowModel.PROP_PRIORITY, 2);
    params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
    WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
    String worfklowId = adhocPath.getInstance().getId();
    workflows.add(worfklowId);
    WorkflowTask startTask = workflowService.getStartTask(adhocPath.getInstance().getId());
    // Finish the start-task
    workflowService.endTask(startTask.getId(), null);
    WorkflowTask firstTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
    Response response = sendRequest(new GetRequest(URL_TASKS + "/" + firstTask.getId()), 200);
    JSONObject jsonProperties = new JSONObject(response.getContentAsString()).getJSONObject("data").getJSONObject("properties");
    // make some changes in existing properties
    jsonProperties.remove(qnameToString(WorkflowModel.ASSOC_PACKAGE));
    jsonProperties.put(qnameToString(WorkflowModel.PROP_COMMENT), "Edited comment");
    Calendar newDueDate = Calendar.getInstance();
    newDueDate.set(Calendar.MILLISECOND, 0);
    jsonProperties.put(qnameToString(WorkflowModel.PROP_DUE_DATE), ISO8601DateFormat.format(newDueDate.getTime()));
    jsonProperties.put(qnameToString(WorkflowModel.PROP_DESCRIPTION), "Edited description");
    jsonProperties.put(qnameToString(WorkflowModel.PROP_PRIORITY), 1);
    // Add some custom properties, which are not defined in typeDef
    jsonProperties.put("customIntegerProperty", 1234);
    jsonProperties.put("customBooleanProperty", Boolean.TRUE);
    jsonProperties.put("customStringProperty", "Property value");
    // test USER3 can not update the task
    personManager.setUser(USER3);
    Response unauthResponse = sendRequest(new PutRequest(URL_TASKS + "/" + firstTask.getId(), jsonProperties.toString(), "application/json"), 401);
    assertEquals(Status.STATUS_UNAUTHORIZED, unauthResponse.getStatus());
    // test USER2 (the task owner) can update the task
    personManager.setUser(USER2);
    Response putResponse = sendRequest(new PutRequest(URL_TASKS + "/" + firstTask.getId(), jsonProperties.toString(), "application/json"), 200);
    assertEquals(Status.STATUS_OK, putResponse.getStatus());
    String jsonStr = putResponse.getContentAsString();
    JSONObject json = new JSONObject(jsonStr);
    JSONObject result = json.getJSONObject("data");
    assertNotNull(result);
    JSONObject editedJsonProperties = result.getJSONObject("properties");
    editedJsonProperties.remove(qnameToString(ContentModel.PROP_CREATOR));
    compareProperties(jsonProperties, editedJsonProperties);
    // test USER1 (the task workflow initiator) can update the task
    personManager.setUser(USER1);
    putResponse = sendRequest(new PutRequest(URL_TASKS + "/" + firstTask.getId(), jsonProperties.toString(), "application/json"), 200);
    assertEquals(Status.STATUS_OK, putResponse.getStatus());
    jsonStr = putResponse.getContentAsString();
    json = new JSONObject(jsonStr);
    result = json.getJSONObject("data");
    assertNotNull(result);
    editedJsonProperties = result.getJSONObject("properties");
    editedJsonProperties.remove(qnameToString(ContentModel.PROP_CREATOR));
    compareProperties(jsonProperties, editedJsonProperties);
    // Reassign the task to USER3 using taskInstance PUT
    jsonProperties = new JSONObject();
    jsonProperties.put(qnameToString(ContentModel.PROP_OWNER), USER3);
    putResponse = sendRequest(new PutRequest(URL_TASKS + "/" + firstTask.getId(), jsonProperties.toString(), "application/json"), 200);
    assertEquals(Status.STATUS_OK, putResponse.getStatus());
    // test USER3 (now the task owner) can update the task
    personManager.setUser(USER3);
    jsonProperties.put(qnameToString(WorkflowModel.PROP_COMMENT), "Edited comment by USER3");
    putResponse = sendRequest(new PutRequest(URL_TASKS + "/" + firstTask.getId(), jsonProperties.toString(), "application/json"), 200);
    assertEquals(Status.STATUS_OK, putResponse.getStatus());
    jsonStr = putResponse.getContentAsString();
    json = new JSONObject(jsonStr);
    result = json.getJSONObject("data");
    assertNotNull(result);
    editedJsonProperties = result.getJSONObject("properties");
    editedJsonProperties.remove(qnameToString(ContentModel.PROP_CREATOR));
    compareProperties(jsonProperties, editedJsonProperties);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Calendar(java.util.Calendar) WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) WorkflowPath(org.alfresco.service.cmr.workflow.WorkflowPath) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) Date(java.util.Date) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)

Example 22 with PutRequest

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

the class AbstractWorkflowRestApiTest method testTaskInstancePutCompletedTask.

public void testTaskInstancePutCompletedTask() throws Exception {
    // Start workflow as USER1 and assign to self
    personManager.setUser(USER1);
    WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
    Map<QName, Serializable> params = new HashMap<QName, Serializable>();
    params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER1));
    params.put(WorkflowModel.PROP_DUE_DATE, new Date());
    params.put(WorkflowModel.PROP_PRIORITY, 1);
    params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
    WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
    String WorkflowId = adhocPath.getInstance().getId();
    workflows.add(WorkflowId);
    WorkflowTask startTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
    // Finish the start-task
    workflowService.endTask(startTask.getId(), null);
    WorkflowTask firstTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
    Response getResponse = sendRequest(new GetRequest(URL_TASKS + "/" + firstTask.getId()), 200);
    JSONObject jsonProperties = new JSONObject(getResponse.getContentAsString()).getJSONObject("data").getJSONObject("properties");
    // Make a change
    jsonProperties.put(qnameToString(WorkflowModel.PROP_DESCRIPTION), "Edited description");
    // Update task. An error is expected, since the task is completed (and not editable)
    sendRequest(new PutRequest(URL_TASKS + "/" + startTask.getId(), jsonProperties.toString(), "application/json"), Status.STATUS_UNAUTHORIZED);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) WorkflowPath(org.alfresco.service.cmr.workflow.WorkflowPath) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) Date(java.util.Date) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)

Example 23 with PutRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest 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 24 with PutRequest

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

the class InviteServiceTest method rejectInvite.

private JSONObject rejectInvite(String inviteId, String inviteTicket, int expectedStatus) throws Exception {
    // Invitee rejects invitation to a Site from Inviter
    String rejectInviteUrl = URL_INVITE + "/" + inviteId + "/" + inviteTicket + "/reject";
    Response response = sendRequest(new PutRequest(rejectInviteUrl, (byte[]) null, null), expectedStatus);
    JSONObject result = new JSONObject(response.getContentAsString());
    return result;
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)

Example 25 with PutRequest

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

the class LinksRestApiTest method testCreateEditDeleteEntry.

// Tests
/**
 * Creating, editing, fetching and deleting a link
 */
public void testCreateEditDeleteEntry() throws Exception {
    JSONObject link;
    JSONObject author;
    JSONObject permissions;
    String name;
    // None to start with
    link = getLinks(null, null);
    assertEquals("Incorrect JSON: " + link.toString(), true, link.has("total"));
    assertEquals(0, link.getInt("total"));
    // Won't be there to start with
    link = getLink(LINK_TITLE_ONE, Status.STATUS_NOT_FOUND);
    // Create
    // (We don't get much info back)
    link = createLink(LINK_TITLE_ONE, "Thing 1", LINK_URL_ONE, false, Status.STATUS_OK);
    assertEquals("Incorrect JSON: " + link.toString(), true, link.has("name"));
    name = getNameFromLink(link);
    assertEquals(name, link.getString("name"));
    assertEquals(name, link.getString("message"));
    // Fetch
    link = getLink(name, Status.STATUS_OK);
    assertEquals("Error found " + link.toString(), false, link.has("error"));
    assertEquals(LINK_TITLE_ONE, link.getString("title"));
    assertEquals("Thing 1", link.getString("description"));
    assertEquals(LINK_URL_ONE, link.getString("url"));
    assertEquals(false, link.getBoolean("internal"));
    assertEquals(0, link.getJSONArray("tags").length());
    assertEquals(true, link.has("author"));
    author = link.getJSONObject("author");
    assertEquals(USER_ONE, author.getString("username"));
    assertEquals(USERDETAILS_FIRSTNAME, author.getString("firstName"));
    assertEquals(USERDETAILS_LASTNAME, author.getString("lastName"));
    // Check the permissions
    assertEquals(true, link.has("permissions"));
    permissions = link.getJSONObject("permissions");
    assertEquals(true, permissions.getBoolean("edit"));
    assertEquals(true, permissions.getBoolean("delete"));
    // Check the noderef
    NodeRef nodeRef = new NodeRef(link.getString("nodeRef"));
    assertEquals(true, nodeService.exists(nodeRef));
    assertEquals(name, nodeService.getProperty(nodeRef, ContentModel.PROP_NAME));
    // Check the comments url
    assertEquals("/node/workspace/" + nodeRef.getStoreRef().getIdentifier() + "/" + nodeRef.getId() + "/comments", link.getString("commentsUrl"));
    // Check the created date: compare two java.util.Date objects.
    assertEquals(nodeService.getProperty(nodeRef, ContentModel.PROP_CREATED), ISO8601DateFormat.parse(link.getJSONObject("createdOnDate").getString("iso8601")));
    // Edit
    // We should get a simple message
    link = updateLink(name, LINK_TITLE_ONE, "More Thing 1", LINK_URL_ONE, true, Status.STATUS_OK);
    assertEquals("Incorrect JSON: " + link.toString(), true, link.has("message"));
    assertEquals("Incorrect JSON: " + link.toString(), true, link.getString("message").contains("updated"));
    // Fetch
    link = getLink(name, Status.STATUS_OK);
    assertEquals("Error found " + link.toString(), false, link.has("error"));
    assertEquals(LINK_TITLE_ONE, link.getString("title"));
    assertEquals("More Thing 1", link.getString("description"));
    assertEquals(LINK_URL_ONE, link.getString("url"));
    assertEquals(true, link.getBoolean("internal"));
    assertEquals(0, link.getJSONArray("tags").length());
    assertEquals(true, link.has("author"));
    author = link.getJSONObject("author");
    assertEquals(USER_ONE, author.getString("username"));
    assertEquals(USERDETAILS_FIRSTNAME, author.getString("firstName"));
    assertEquals(USERDETAILS_LASTNAME, author.getString("lastName"));
    // Fetch as a different user, permissions different
    this.authenticationComponent.setCurrentUser(USER_TWO);
    link = getLink(name, Status.STATUS_OK);
    // Check the basics
    assertEquals(LINK_TITLE_ONE, link.getString("title"));
    assertEquals("More Thing 1", link.getString("description"));
    assertEquals(LINK_URL_ONE, link.getString("url"));
    assertEquals(true, link.getBoolean("internal"));
    assertEquals(0, link.getJSONArray("tags").length());
    // Different user in the site, can edit but not delete
    assertEquals(true, link.has("permissions"));
    permissions = link.getJSONObject("permissions");
    assertEquals(true, permissions.getBoolean("edit"));
    assertEquals(false, permissions.getBoolean("delete"));
    this.authenticationComponent.setCurrentUser(USER_ONE);
    // Delete
    link = deleteLinks(Arrays.asList(new String[] { name }), Status.STATUS_OK);
    assertEquals("Incorrect JSON: " + link.toString(), true, link.has("message"));
    assertEquals("Incorrect JSON: " + link.toString(), true, link.getString("message").contains("deleted"));
    // Fetch, will have gone
    link = getLink(name, Status.STATUS_NOT_FOUND);
    // Can't delete again
    deleteLinks(Arrays.asList(new String[] { name }), Status.STATUS_NOT_FOUND);
    // Can't edit it when it's deleted
    sendRequest(new PutRequest(URL_LINKS_UPDATE + name, "{}", "application/json"), Status.STATUS_NOT_FOUND);
    // Do a single delete
    link = createLink(LINK_TITLE_ONE, "Thing 1", LINK_URL_ONE, false, Status.STATUS_OK);
    name = getNameFromLink(link);
    getLink(name, Status.STATUS_OK);
    deleteLink(name, Status.STATUS_NO_CONTENT);
    getLink(name, Status.STATUS_NOT_FOUND);
    deleteLink(name, Status.STATUS_NOT_FOUND);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)

Aggregations

PutRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)41 JSONObject (org.json.JSONObject)39 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)37 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)17 JSONArray (org.json.JSONArray)13 PostRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest)10 JSONTokener (org.json.JSONTokener)8 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 JSONStringer (org.json.JSONStringer)5 IOException (java.io.IOException)3 Serializable (java.io.Serializable)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 WorkflowDefinition (org.alfresco.service.cmr.workflow.WorkflowDefinition)3 WorkflowPath (org.alfresco.service.cmr.workflow.WorkflowPath)3 WorkflowTask (org.alfresco.service.cmr.workflow.WorkflowTask)3 QName (org.alfresco.service.namespace.QName)3 JSONException (org.json.JSONException)3 DeleteRequest (org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)3 ArrayList (java.util.ArrayList)1