Search in sources :

Example 86 with PostRequest

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

the class ReplicationRestApiTest method testReplicationDefinitionsNastyNames.

/**
 * Test that when creating and working with replication
 *  definitions with a name that includes "nasty"
 *  characters, things still work.
 * Related to ALF-4610.
 */
public void testReplicationDefinitionsNastyNames() throws Exception {
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    Response response;
    String jsonStr;
    String nastyName = "~!@#$%^&()_+-={}[];";
    String nastyNameURL = URLEncoder.encodeUriComponent(nastyName);
    // Create
    JSONObject json = new JSONObject();
    json.put("name", nastyName);
    json.put("description", "Nasty Characters");
    response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals(nastyName, json.get("name"));
    assertEquals("Nasty Characters", 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 it turned up
    assertEquals(1, replicationService.loadReplicationDefinitions().size());
    assertEquals(nastyName, replicationService.loadReplicationDefinitions().get(0).getReplicationName());
    // Fetch the details
    response = sendRequest(new GetRequest(URL_DEFINITION + nastyNameURL), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals(nastyName, json.get("name"));
    assertEquals("Nasty Characters", json.get("description"));
    assertEquals("New", json.get("status"));
    // Delete
    // Because some of the delete operations happen post-commit, and
    // because we don't have real transactions, fake it
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    // Call the delete webscript
    response = sendRequest(new DeleteRequest(URL_DEFINITION + nastyNameURL), Status.STATUS_NO_CONTENT);
    assertEquals(Status.STATUS_NO_CONTENT, response.getStatus());
    // Let the node service do its work
    txn.commit();
    Thread.sleep(50);
    // Check the details webscript to ensure it went
    response = sendRequest(new GetRequest(URL_DEFINITION + nastyNameURL), Status.STATUS_NOT_FOUND);
    assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
    // And check the service too
    assertEquals(0, replicationService.loadReplicationDefinitions().size());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) UserTransaction(javax.transaction.UserTransaction) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)

Example 87 with PostRequest

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

the class RuleServiceTest method testRuleReorder.

@SuppressWarnings("unused")
public void testRuleReorder() throws Exception {
    assertEquals(0, ruleService.getRules(testNodeRef).size());
    // Create 3 rules
    NodeRef rule1 = createRuleNodeRef(testNodeRef, "Rule 1");
    NodeRef rule2 = createRuleNodeRef(testNodeRef, "Rule 2");
    NodeRef rule3 = createRuleNodeRef(testNodeRef, "Rule 3");
    List<Rule> rules = ruleService.getRules(testNodeRef);
    assertEquals(3, rules.size());
    assertEquals("Rule 1", rules.get(0).getTitle());
    assertEquals("Rule 2", rules.get(1).getTitle());
    assertEquals("Rule 3", rules.get(2).getTitle());
    JSONObject action = new JSONObject();
    action.put("actionDefinitionName", "reorder-rules");
    action.put("actionedUponNode", testNodeRef.toString());
    JSONObject params = new JSONObject();
    JSONArray orderArray = new JSONArray();
    orderArray.put(rules.get(2).getNodeRef().toString());
    orderArray.put(rules.get(1).getNodeRef().toString());
    orderArray.put(rules.get(0).getNodeRef().toString());
    params.put("rules", orderArray);
    action.put("parameterValues", params);
    String url = formateQueueActionUrl(false);
    // execute before response (should be successful)
    Response successResponse = sendRequest(new PostRequest(url, action.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"));
    rules = ruleService.getRules(testNodeRef);
    assertEquals(3, rules.size());
    assertEquals("Rule 3", rules.get(0).getTitle());
    assertEquals("Rule 2", rules.get(1).getTitle());
    assertEquals("Rule 1", rules.get(2).getTitle());
}
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) JSONArray(org.json.JSONArray) Rule(org.alfresco.service.cmr.rule.Rule)

Example 88 with PostRequest

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

the class AbstractSiteServiceTest method addSiteMember.

protected void addSiteMember(String userName, String site) throws Exception {
    JSONObject membership = new JSONObject();
    membership.put("role", SiteModel.SITE_CONSUMER);
    JSONObject person = new JSONObject();
    person.put("userName", userName);
    membership.put("person", person);
    sendRequest(new PostRequest(URL_SITES + "/" + site + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 89 with PostRequest

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

the class SiteServiceTest method testCreateInvitation.

/**
 * Detailed test of Create Invitation Web Script
 *
 * Create Nominated Invitation
 *
 * Create Moderated Invitation
 *
 * @throws Exception
 */
public void testCreateInvitation() throws Exception {
    String shortName = GUID.generate();
    createSite("myPreset", shortName, "myTitle", "myDescription", SiteVisibility.PUBLIC, 200);
    String inviteComments = "Please sir, let me in";
    String userName = USER_TWO;
    String roleName = SiteModel.SITE_CONSUMER;
    String inviteId = null;
    /*
         * Negative test - wrong invitation type
         */
    {
        JSONObject newInvitation = new JSONObject();
        newInvitation.put("invitationType", "Grundge");
        newInvitation.put("inviteeRoleName", roleName);
        newInvitation.put("inviteeComments", inviteComments);
        newInvitation.put("inviteeUserName", userName);
        sendRequest(new PostRequest(URL_SITES + "/" + shortName + "/invitations", newInvitation.toString(), "application/json"), Status.STATUS_BAD_REQUEST);
    }
    /*
         * Negative test - missing Invitation type
         */
    {
        JSONObject newInvitation = new JSONObject();
        newInvitation.put("inviteeRoleName", roleName);
        newInvitation.put("inviteeComments", inviteComments);
        newInvitation.put("inviteeUserName", userName);
        sendRequest(new PostRequest(URL_SITES + "/" + shortName + "/invitations", newInvitation.toString(), "application/json"), Status.STATUS_BAD_REQUEST);
    }
    /*
         * Negative test - blank RoleName
         */
    {
        JSONObject newInvitation = new JSONObject();
        newInvitation.put("invitationType", "MODERATED");
        newInvitation.put("inviteeRoleName", "");
        newInvitation.put("inviteeComments", inviteComments);
        newInvitation.put("inviteeUserName", userName);
        sendRequest(new PostRequest(URL_SITES + "/" + shortName + "/invitations", newInvitation.toString(), "application/json"), Status.STATUS_BAD_REQUEST);
    }
    /*
         * Create a new moderated invitation
         */
    JSONObject newInvitation = new JSONObject();
    {
        newInvitation.put("invitationType", "MODERATED");
        newInvitation.put("inviteeRoleName", roleName);
        newInvitation.put("inviteeComments", inviteComments);
        newInvitation.put("inviteeUserName", userName);
        Response response = sendRequest(new PostRequest(URL_SITES + "/" + shortName + "/invitations", newInvitation.toString(), "application/json"), Status.STATUS_CREATED);
        JSONObject top = new JSONObject(response.getContentAsString());
        JSONObject data = top.getJSONObject("data");
        inviteId = data.getString("inviteId");
        assertEquals("invitationType", "MODERATED", data.getString("invitationType"));
        assertEquals("inviteeUserName is not set", userName, data.getString("inviteeUserName"));
        assertEquals("resourceName is not correct", shortName, data.getString("resourceName"));
        assertEquals("resourceType is not correct", "WEB_SITE", data.getString("resourceType"));
    }
    assertNotNull("inviteId is null", inviteId);
    assertTrue("inviteId is too small", inviteId.length() > 0);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 90 with PostRequest

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

the class SiteServiceTest method testPostMemberships.

public void testPostMemberships() 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);
    // Check the result
    assertEquals(SiteModel.SITE_CONSUMER, membership.get("role"));
    assertEquals(USER_TWO, membership.getJSONObject("person").get("userName"));
    // Get the membership list
    response = sendRequest(new GetRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS), 200);
    JSONArray result2 = new JSONArray(response.getContentAsString());
    assertNotNull(result2);
    assertEquals(2, result2.length());
    // Add another user, with a fully numeric username
    membership = new JSONObject();
    membership.put("role", SiteModel.SITE_CONTRIBUTOR);
    person = new JSONObject();
    person.put("userName", USER_NUMERIC);
    membership.put("person", person);
    response = sendRequest(new PostRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
    // Check the details are correct for one user
    membership = new JSONObject(response.getContentAsString());
    assertEquals(SiteModel.SITE_CONTRIBUTOR, membership.get("role"));
    assertEquals(USER_NUMERIC, membership.getJSONObject("authority").get("userName"));
    // Check the membership list
    response = sendRequest(new GetRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS), 200);
    String json = response.getContentAsString();
    JSONArray result3 = new JSONArray(json);
    assertNotNull(result3);
    assertEquals(3, result3.length());
    // Check the everyone has the correct membership
    // (The webscript returns the users in order to make testing easier)
    Map<String, JSONObject> membershipMap = new HashMap<String, JSONObject>();
    for (int i = 0; i < membership.length(); i++) {
        membershipMap.put(result3.getJSONObject(i).getString("role"), result3.getJSONObject(i));
    }
    membership = membershipMap.get(SiteModel.SITE_MANAGER);
    assertNotNull("The response did not contain " + SiteModel.SITE_MANAGER, membership);
    assertEquals(USER_ONE, membership.getJSONObject("authority").get("userName"));
    membership = membershipMap.get(SiteModel.SITE_CONSUMER);
    assertNotNull("The response did not contain " + SiteModel.SITE_CONSUMER, membership);
    assertEquals(USER_TWO, membership.getJSONObject("authority").get("userName"));
    membership = membershipMap.get(SiteModel.SITE_CONTRIBUTOR);
    assertNotNull("The response did not contain " + SiteModel.SITE_CONTRIBUTOR, membership);
    assertEquals(USER_NUMERIC, membership.getJSONObject("authority").get("userName"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray)

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