Search in sources :

Example 26 with PostRequest

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

the class ReplicationRestApiTest method testReplicationDefinitionsPost.

public void testReplicationDefinitionsPost() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new PostRequest(URL_DEFINITIONS, "", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new PostRequest(URL_DEFINITIONS, "", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // Ensure there aren't any to start with
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    assertEquals(0, replicationService.loadReplicationDefinitions().size());
    // If you don't give it name + description, it won't like you
    JSONObject json = new JSONObject();
    response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST);
    assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
    json.put("name", "New Definition");
    response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST);
    assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
    // If it has both, it'll work
    json.put("description", "Testing");
    response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    // Check we got the right information back
    String jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("New Definition", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // Check that the right stuff ended up in the repository
    ReplicationDefinition rd = replicationService.loadReplicationDefinition("New Definition");
    assertEquals("New Definition", rd.getReplicationName());
    assertEquals("Testing", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals(null, rd.getTargetName());
    assertEquals(0, rd.getPayload().size());
    assertEquals(true, rd.isEnabled());
    // Post with the full set of options
    json = new JSONObject();
    json.put("name", "Test");
    json.put("description", "Test Description");
    json.put("targetName", "Target");
    json.put("enabled", false);
    JSONArray payloadRefs = new JSONArray();
    payloadRefs.put(repositoryHelper.getCompanyHome().toString());
    payloadRefs.put(dataDictionary.toString());
    json.put("payload", payloadRefs);
    response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    // Check the response for this
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test", json.get("name"));
    assertEquals("Test Description", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals("Target", json.get("targetName"));
    assertEquals(2, json.getJSONArray("payload").length());
    JSONObject payload = json.getJSONArray("payload").getJSONObject(0);
    assertEquals(repositoryHelper.getCompanyHome().toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Company Home", payload.get("name"));
    assertEquals("/Company Home", payload.get("path"));
    payload = json.getJSONArray("payload").getJSONObject(1);
    assertEquals(dataDictionary.toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Data Dictionary", payload.get("name"));
    assertEquals("/Company Home/Data Dictionary", payload.get("path"));
    // Check the database for this
    rd = replicationService.loadReplicationDefinition("Test");
    assertEquals("Test", rd.getReplicationName());
    assertEquals("Test Description", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals("Target", rd.getTargetName());
    assertEquals(false, rd.isEnabled());
    assertEquals(2, rd.getPayload().size());
    assertEquals(repositoryHelper.getCompanyHome(), rd.getPayload().get(0));
    assertEquals(dataDictionary, rd.getPayload().get(1));
    // Ensure that the original one wasn't changed by anything
    rd = replicationService.loadReplicationDefinition("New Definition");
    assertEquals("New Definition", rd.getReplicationName());
    assertEquals("Testing", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals(null, rd.getTargetName());
    assertEquals(0, rd.getPayload().size());
    assertEquals(true, rd.isEnabled());
    // Ensure we can't create with a duplicate name
    json = new JSONObject();
    json.put("name", "Test");
    json.put("description", "New Duplicate");
    json.put("targetName", "New Duplicate Target");
    response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST);
    assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
    // Ensure that even though we got BAD REQUEST back, nothing changed
    rd = replicationService.loadReplicationDefinition("New Definition");
    assertEquals("New Definition", rd.getReplicationName());
    assertEquals("Testing", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals(null, rd.getTargetName());
    assertEquals(0, rd.getPayload().size());
    assertEquals(true, rd.isEnabled());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) JSONArray(org.json.JSONArray)

Example 27 with PostRequest

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

the class RuleServiceTest method createRule.

private JSONObject createRule(NodeRef ruleOwnerNodeRef, String title) throws Exception {
    JSONObject jsonRule = buildTestRule(title);
    Response response = sendRequest(new PostRequest(formatRulesUrl(ruleOwnerNodeRef, false), jsonRule.toString(), "application/json"), 200);
    JSONObject result = new JSONObject(response.getContentAsString());
    return result;
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 28 with PostRequest

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

the class RuleServiceTest method testQueueAction.

public void testQueueAction() throws Exception {
    String url = formateQueueActionUrl(false);
    JSONObject copyAction = buildCopyAction(testWorkNodeRef);
    copyAction.put("actionedUponNode", testNodeRef);
    // execute before response (should be successful)
    Response successResponse = sendRequest(new PostRequest(url, copyAction.toString(), "application/json"), 200);
    JSONObject successResult = new JSONObject(successResponse.getContentAsString());
    assertNotNull(successResult);
    assertTrue(successResult.has("data"));
    JSONObject successData = successResult.getJSONObject("data");
    assertTrue(successData.has("status"));
    assertEquals("success", successData.getString("status"));
    assertTrue(successData.has("actionedUponNode"));
    assertFalse(successData.has("exception"));
    assertTrue(successData.has("action"));
    // execute before response (should fail)
    sendRequest(new PostRequest(url, copyAction.toString(), "application/json"), 500);
    // execute after response (should fail but error should not present in response)
    String asyncUrl = formateQueueActionUrl(true);
    Response response = sendRequest(new PostRequest(asyncUrl, copyAction.toString(), "application/json"), 200);
    // wait while action executed
    Thread.sleep(1000);
    JSONObject result = new JSONObject(response.getContentAsString());
    assertNotNull(result);
    assertTrue(result.has("data"));
    JSONObject data = result.getJSONObject("data");
    assertTrue(data.has("status"));
    assertEquals("queued", data.getString("status"));
    assertTrue(data.has("actionedUponNode"));
    assertFalse(data.has("exception"));
    assertTrue(data.has("action"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 29 with PostRequest

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

the class AbstractSiteServiceTest method createSite.

protected JSONObject createSite(String sitePreset, String shortName, String title, String description, SiteVisibility visibility, int expectedStatus) throws Exception {
    JSONObject site = new JSONObject();
    site.put("sitePreset", sitePreset);
    site.put("shortName", shortName);
    site.put("title", title);
    site.put("description", description);
    site.put("visibility", visibility.toString());
    Response response = sendRequest(new PostRequest(URL_SITES, site.toString(), "application/json"), expectedStatus);
    this.createdSites.add(shortName);
    return new JSONObject(response.getContentAsString());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 30 with PostRequest

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

the class SiteServiceTest method testChangeMembershipRoleAsSiteAdmin.

public void testChangeMembershipRoleAsSiteAdmin() throws Exception {
    // Create a site
    String shortName = GUID.generate();
    createSite("myPreset", shortName, "myTitle", "myDescription", SiteVisibility.PUBLIC, 200);
    // Build the JSON membership object
    JSONObject membership = new JSONObject();
    membership.put("role", SiteModel.SITE_CONSUMER);
    JSONObject person = new JSONObject();
    person.put("userName", USER_TWO);
    membership.put("person", person);
    // Post the membership
    Response response = sendRequest(new PostRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
    JSONObject jsonObj = new JSONObject(response.getContentAsString());
    // Check the result
    assertEquals(SiteModel.SITE_CONSUMER, jsonObj.get("role"));
    assertEquals(USER_TWO, jsonObj.getJSONObject("authority").get("userName"));
    // try to change the user role as user3
    this.authenticationComponent.setCurrentUser(USER_THREE);
    membership.put("role", SiteModel.SITE_COLLABORATOR);
    sendRequest(new PutRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 500);
    assertEquals("User's role should not have been changed.", SiteModel.SITE_CONSUMER.toString(), siteService.getMembersRole(shortName, USER_TWO));
    // set the current user as site-admin
    this.authenticationComponent.setCurrentUser(USER_FOUR_AS_SITE_ADMIN);
    response = sendRequest(new PutRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
    jsonObj = new JSONObject(response.getContentAsString());
    // Check the result
    assertEquals(SiteModel.SITE_COLLABORATOR, jsonObj.get("role"));
    assertEquals(USER_TWO, jsonObj.getJSONObject("authority").get("userName"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)

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