Search in sources :

Example 36 with PutRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest in project records-management by Alfresco.

the class RmRestApiTest method testPutCustomReferenceDefinition.

public void testPutCustomReferenceDefinition() throws Exception {
    String[] generatedRefIds = postCustomReferenceDefinitions();
    final String pcRefId = generatedRefIds[0];
    final String bidiRefId = generatedRefIds[1];
    // GET the custom refs in order to retrieve the label/source/target
    String refDefnUrl = "/api/rma/admin/customreferencedefinitions/" + bidiRefId;
    Response rsp = sendRequest(new GetRequest(refDefnUrl), 200);
    String rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rspContent));
    refDefnUrl = "/api/rma/admin/customreferencedefinitions/" + pcRefId;
    rsp = sendRequest(new GetRequest(refDefnUrl), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    // Update the bidirectional reference.
    final String updatedBiDiLabel = "Updated label üøéîçå";
    String jsonString = new JSONStringer().object().key("label").value(updatedBiDiLabel).endObject().toString();
    refDefnUrl = "/api/rma/admin/customreferencedefinitions/" + bidiRefId;
    rsp = sendRequest(new PutRequest(refDefnUrl, jsonString, APPLICATION_JSON), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    String urlOfNewRefDef = jsonRsp.getString("url");
    assertNotNull("urlOfNewRefDef was null.", urlOfNewRefDef);
    // GET the bidi reference to ensure it's valid
    rsp = sendRequest(new GetRequest(refDefnUrl), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    JSONObject dataObject = jsonRsp.getJSONObject("data");
    assertNotNull("JSON data object was null", dataObject);
    JSONArray customRefsObject = dataObject.getJSONArray("customReferences");
    assertNotNull("JSON customReferences object was null", customRefsObject);
    assertEquals("Wrong customReferences length.", 1, customRefsObject.length());
    JSONObject newRefObject = customRefsObject.getJSONObject(0);
    assertEquals("Wrong property label.", updatedBiDiLabel, newRefObject.getString("label"));
    // Update the parent/child reference.
    final String updatedPcSource = "Updated source ∆Ωç√∫";
    final String updatedPcTarget = "Updated target ∆Ωç√∫";
    jsonString = new JSONStringer().object().key("source").value(updatedPcSource).key("target").value(updatedPcTarget).endObject().toString();
    refDefnUrl = "/api/rma/admin/customreferencedefinitions/" + pcRefId;
    rsp = sendRequest(new PutRequest(refDefnUrl, jsonString, APPLICATION_JSON), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    urlOfNewRefDef = jsonRsp.getString("url");
    assertNotNull("urlOfNewRefDef was null.", urlOfNewRefDef);
    // GET the parent/child reference to ensure it's valid
    refDefnUrl = "/api/rma/admin/customreferencedefinitions/" + pcRefId;
    rsp = sendRequest(new GetRequest(refDefnUrl), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    dataObject = jsonRsp.getJSONObject("data");
    assertNotNull("JSON data object was null", dataObject);
    customRefsObject = dataObject.getJSONArray("customReferences");
    assertNotNull("JSON customReferences object was null", customRefsObject);
    assertEquals("Wrong customReferences length.", 1, customRefsObject.length());
    newRefObject = customRefsObject.getJSONObject(0);
    assertEquals("Wrong reference source.", updatedPcSource, newRefObject.getString("source"));
    assertEquals("Wrong reference target.", updatedPcTarget, newRefObject.getString("target"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) JSONStringer(org.json.JSONStringer)

Example 37 with PutRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest in project records-management by Alfresco.

the class RmRestApiTest method testAudit.

public void testAudit() throws Exception {
    // call the list service to get audit events
    Response rsp = sendRequest(new GetRequest(GET_LIST_URL), 200);
    // System.out.println("GET : " + rsp.getContentAsString());
    assertEquals("application/json;charset=UTF-8", rsp.getContentType());
    // get response as JSON and check
    JSONObject jsonParsedObject = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    assertNotNull(jsonParsedObject);
    JSONObject data = jsonParsedObject.getJSONObject("data");
    JSONObject events = data.getJSONObject("auditEvents");
    JSONArray items = events.getJSONArray("items");
    assertEquals(auditService.getAuditEvents().size(), items.length());
    assertTrue(items.length() > 0);
    JSONObject item = items.getJSONObject(0);
    assertTrue(item.length() == 2);
    assertTrue(item.has("label"));
    assertTrue(item.has("value"));
    // get the full RM audit log and check response
    rsp = sendRequest(new GetRequest(RMA_AUDITLOG_URL), 200);
    assertEquals("application/json", rsp.getContentType());
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    // get the full RM audit log as an HTML report and check response
    rsp = sendRequest(new GetRequest(RMA_AUDITLOG_URL + "?format=html"), 200);
    assertEquals("text/html", rsp.getContentType());
    // export the full RM audit log and check response
    rsp = sendRequest(new GetRequest(RMA_AUDITLOG_URL + "?export=true"), 200);
    assertEquals("application/json", rsp.getContentType());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    // construct the URL
    String nodeUrl = recordCategory.toString().replace("://", "/");
    String auditUrl = MessageFormat.format(GET_NODE_AUDITLOG_URL_FORMAT, nodeUrl);
    // send request
    rsp = sendRequest(new GetRequest(auditUrl), 200);
    // check response
    assertEquals("application/json", rsp.getContentType());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    // get the audit log with all restrictions in place
    String filteredAuditUrl = auditUrl + "?user=gavinc&size=5&from=2009-01-01&to=2009-12-31&event=Login";
    rsp = sendRequest(new GetRequest(filteredAuditUrl), 200);
    // check response
    assertEquals("application/json", rsp.getContentType());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    // attempt to get the audit log with invalid restrictions in place
    filteredAuditUrl = auditUrl + "?user=fred&size=abc&from=2009&to=2010&property=wrong";
    rsp = sendRequest(new GetRequest(filteredAuditUrl), 200);
    assertEquals("application/json", rsp.getContentType());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    checkAuditStatus(true);
    // start the RM audit log
    JSONObject jsonPostData = new JSONObject();
    jsonPostData.put("enabled", true);
    String jsonPostString = jsonPostData.toString();
    rsp = sendRequest(new PutRequest(RMA_AUDITLOG_URL, jsonPostString, APPLICATION_JSON), 200);
    checkAuditStatus(true);
    // check the response
    // System.out.println(rsp.getContentAsString());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    JSONObject dataObj = (JSONObject) jsonRsp.get("data");
    assertNotNull("JSON 'data' object was null", dataObj);
    assertTrue(dataObj.getBoolean("enabled"));
    assertTrue(dataObj.has("started"));
    assertTrue(dataObj.has("stopped"));
    // stop the RM audit log
    jsonPostData = new JSONObject();
    jsonPostData.put("enabled", false);
    jsonPostString = jsonPostData.toString();
    rsp = sendRequest(new PutRequest(RMA_AUDITLOG_URL, jsonPostString, APPLICATION_JSON), 200);
    checkAuditStatus(false);
    // check the response
    // System.out.println(rsp.getContentAsString());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    dataObj = (JSONObject) jsonRsp.get("data");
    assertNotNull("JSON 'data' object was null", dataObj);
    assertFalse(dataObj.getBoolean("enabled"));
    // clear the RM audit log
    rsp = sendRequest(new DeleteRequest(RMA_AUDITLOG_URL), 200);
    // System.out.println(rsp.getContentAsString());
    jsonRsp = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    dataObj = (JSONObject) jsonRsp.get("data");
    assertNotNull("JSON 'data' object was null", dataObj);
    assertFalse(dataObj.getBoolean("enabled"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)

Example 38 with PutRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest in project records-management by Alfresco.

the class RmRestApiTest method putCustomPropDefinition.

private void putCustomPropDefinition(String label, String id) throws JSONException, IOException, UnsupportedEncodingException {
    String jsonString = new JSONStringer().object().key("label").value(label).endObject().toString();
    String propDefnUrl = "/api/rma/admin/custompropertydefinitions/" + id;
    Response rsp = sendRequest(new PutRequest(propDefnUrl, jsonString, APPLICATION_JSON), 200);
    String rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rspContent));
    String urlOfNewPropDef = jsonRsp.getString("url");
    assertNotNull("urlOfNewPropDef was null.", urlOfNewPropDef);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) JSONStringer(org.json.JSONStringer)

Example 39 with PutRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest in project records-management by Alfresco.

the class RmRestApiTest method testPutCustomPropertyDefinition.

public void testPutCustomPropertyDefinition() throws Exception {
    // POST to create a property definition with a known propId
    final String propertyLabel = "Original label åçîéøü";
    String propId = postCustomPropertyDefinition(propertyLabel, null);
    // PUT an updated label.
    final String updatedLabel = "Updated label πø^¨¥†®";
    String jsonString = new JSONStringer().object().key("label").value(updatedLabel).endObject().toString();
    String propDefnUrl = "/api/rma/admin/custompropertydefinitions/" + propId;
    Response rsp = sendRequest(new PutRequest(propDefnUrl, jsonString, APPLICATION_JSON), 200);
    // GET from the URL again to ensure it's valid
    rsp = sendRequest(new GetRequest(propDefnUrl), 200);
    String rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    // PUT an updated constraint ref.
    final String updatedConstraint = "rmc:tlList";
    jsonString = new JSONStringer().object().key("constraintRef").value(updatedConstraint).endObject().toString();
    propDefnUrl = "/api/rma/admin/custompropertydefinitions/" + propId;
    rsp = sendRequest(new PutRequest(propDefnUrl, jsonString, APPLICATION_JSON), 200);
    rspContent = rsp.getContentAsString();
    JSONObject jsonRsp = new JSONObject(new JSONTokener(rspContent));
    String urlOfNewPropDef = jsonRsp.getString("url");
    assertNotNull("urlOfNewPropDef was null.", urlOfNewPropDef);
    // GET from the URL again to ensure it's valid
    rsp = sendRequest(new GetRequest(propDefnUrl), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    JSONObject dataObject = jsonRsp.getJSONObject("data");
    assertNotNull("JSON data object was null", dataObject);
    JSONObject customPropsObject = dataObject.getJSONObject("customProperties");
    assertNotNull("JSON customProperties object was null", customPropsObject);
    assertEquals("Wrong customProperties length.", 1, customPropsObject.length());
    Object keyToSoleProp = customPropsObject.keys().next();
    JSONObject newPropObject = customPropsObject.getJSONObject((String) keyToSoleProp);
    assertEquals("Wrong property label.", updatedLabel, newPropObject.getString("label"));
    JSONArray constraintRefsArray = newPropObject.getJSONArray("constraintRefs");
    assertEquals("ConstraintRefsArray wrong length.", 1, constraintRefsArray.length());
    String retrievedUpdatedTitle = constraintRefsArray.getJSONObject(0).getString("name");
    assertEquals("Constraints had wrong name.", "rmc:tlList", retrievedUpdatedTitle);
    // PUT again to remove all constraints
    jsonString = new JSONStringer().object().key("constraintRef").value(null).endObject().toString();
    rsp = sendRequest(new PutRequest(propDefnUrl, jsonString, APPLICATION_JSON), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    // GET from the URL again to ensure it's valid
    rsp = sendRequest(new GetRequest(propDefnUrl), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    dataObject = jsonRsp.getJSONObject("data");
    assertNotNull("JSON data object was null", dataObject);
    customPropsObject = dataObject.getJSONObject("customProperties");
    assertNotNull("JSON customProperties object was null", customPropsObject);
    assertEquals("Wrong customProperties length.", 1, customPropsObject.length());
    keyToSoleProp = customPropsObject.keys().next();
    newPropObject = customPropsObject.getJSONObject((String) keyToSoleProp);
    assertEquals("Wrong property label.", updatedLabel, newPropObject.getString("label"));
    constraintRefsArray = newPropObject.getJSONArray("constraintRefs");
    assertEquals("ConstraintRefsArray wrong length.", 0, constraintRefsArray.length());
    // Finally PUT a constraint on a PropertyDefn that has been cleared of constraints.
    // This was raised as an issue
    final String readdedConstraint = "rmc:tlList";
    jsonString = new JSONStringer().object().key("constraintRef").value(readdedConstraint).endObject().toString();
    propDefnUrl = "/api/rma/admin/custompropertydefinitions/" + propId;
    rsp = sendRequest(new PutRequest(propDefnUrl, jsonString, APPLICATION_JSON), 200);
    rspContent = rsp.getContentAsString();
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    // System.out.println("PUTting a constraint back again.");
    // System.out.println(rspContent);
    // And GET from the URL again
    rsp = sendRequest(new GetRequest(propDefnUrl), 200);
    rspContent = rsp.getContentAsString();
    // System.out.println(rspContent);
    jsonRsp = new JSONObject(new JSONTokener(rspContent));
    dataObject = jsonRsp.getJSONObject("data");
    assertNotNull("JSON data object was null", dataObject);
    customPropsObject = dataObject.getJSONObject("customProperties");
    assertNotNull("JSON customProperties object was null", customPropsObject);
    assertEquals("Wrong customProperties length.", 1, customPropsObject.length());
    keyToSoleProp = customPropsObject.keys().next();
    newPropObject = customPropsObject.getJSONObject((String) keyToSoleProp);
    assertEquals("Wrong property label.", updatedLabel, newPropObject.getString("label"));
    constraintRefsArray = newPropObject.getJSONArray("constraintRefs");
    assertEquals("ConstraintRefsArray wrong length.", 1, constraintRefsArray.length());
    String readdedUpdatedTitle = constraintRefsArray.getJSONObject(0).getString("name");
    assertEquals("Constraints had wrong name.", "rmc:tlList", readdedUpdatedTitle);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) JSONObject(org.json.JSONObject) JSONStringer(org.json.JSONStringer)

Example 40 with PutRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest in project records-management by Alfresco.

the class DispositionRestApiTest method testPutDispositionAction.

public void testPutDispositionAction() throws Exception {
    NodeRef newRecordCategory = filePlanService.createRecordCategory(recordSeries, GUID.generate());
    dispositionService.createDispositionSchedule(newRecordCategory, null);
    // create an action definition to then update
    String categoryNodeUrl = newRecordCategory.toString().replace("://", "/");
    String postRequestUrl = MessageFormat.format(POST_ACTIONDEF_URL_FORMAT, categoryNodeUrl);
    JSONObject jsonPostData = new JSONObject();
    jsonPostData.put("name", "cutoff");
    String jsonPostString = jsonPostData.toString();
    sendRequest(new PostRequest(postRequestUrl, jsonPostString, APPLICATION_JSON), 200);
    // verify the action definition is present and retrieve it's id
    String getRequestUrl = MessageFormat.format(GET_SCHEDULE_URL_FORMAT, categoryNodeUrl);
    Response rsp = sendRequest(new GetRequest(getRequestUrl), 200);
    JSONObject json = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    JSONObject actionDef = json.getJSONObject("data").getJSONArray("actions").getJSONObject(0);
    String actionDefId = actionDef.getString("id");
    assertEquals("cutoff", actionDef.getString("name"));
    assertEquals("none|0", actionDef.getString("period"));
    assertFalse(actionDef.has("description"));
    assertFalse(actionDef.has("events"));
    // define body for PUT request
    String name = "destroy";
    String desc = "Destroy this record after 5 years";
    String period = "year|5";
    String location = "my location";
    String periodProperty = "rma:cutOffDate";
    boolean eligibleOnFirstCompleteEvent = false;
    jsonPostData = new JSONObject();
    jsonPostData.put("name", name);
    jsonPostData.put("description", desc);
    jsonPostData.put("period", period);
    jsonPostData.put("location", location);
    jsonPostData.put("periodProperty", periodProperty);
    jsonPostData.put("eligibleOnFirstCompleteEvent", eligibleOnFirstCompleteEvent);
    JSONArray events = new JSONArray();
    events.put("superseded");
    events.put("no_longer_needed");
    jsonPostData.put("events", events);
    jsonPostString = jsonPostData.toString();
    // try and update a non existent action definition to check for 404
    String putRequestUrl = MessageFormat.format(PUT_ACTIONDEF_URL_FORMAT, categoryNodeUrl, "xyz");
    rsp = sendRequest(new PutRequest(putRequestUrl, jsonPostString, APPLICATION_JSON), 404);
    // update the action definition
    putRequestUrl = MessageFormat.format(PUT_ACTIONDEF_URL_FORMAT, categoryNodeUrl, actionDefId);
    rsp = sendRequest(new PutRequest(putRequestUrl, jsonPostString, APPLICATION_JSON), 200);
    // check the update happened correctly
    json = new JSONObject(new JSONTokener(rsp.getContentAsString()));
    actionDef = json.getJSONObject("data");
    assertEquals(name, actionDef.getString("name"));
    assertEquals("Destroy", actionDef.getString("label"));
    assertEquals(desc, actionDef.getString("description"));
    assertEquals(period, actionDef.getString("period"));
    assertEquals(location, actionDef.getString("location"));
    assertEquals(periodProperty, actionDef.getString("periodProperty"));
    assertFalse(actionDef.getBoolean("eligibleOnFirstCompleteEvent"));
    assertEquals(2, actionDef.getJSONArray("events").length());
    assertEquals("superseded", actionDef.getJSONArray("events").getString(0));
    assertEquals("no_longer_needed", actionDef.getJSONArray("events").getString(1));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) 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