Search in sources :

Example 21 with Response

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

the class AuditWebScriptTest method testGetIsAuditEnabledGlobally.

public void testGetIsAuditEnabledGlobally() throws Exception {
    boolean wasEnabled = auditService.isAuditEnabled();
    Map<String, AuditApplication> checkApps = auditService.getAuditApplications();
    String url = "/api/audit/control";
    TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
    Response response = sendRequest(req, Status.STATUS_OK, admin);
    JSONObject json = new JSONObject(response.getContentAsString());
    boolean enabled = json.getBoolean(AbstractAuditWebScript.JSON_KEY_ENABLED);
    assertEquals("Mismatched global audit enabled", wasEnabled, enabled);
    JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
    assertEquals("Incorrect number of applications reported", checkApps.size(), apps.length());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) JSONObject(org.json.JSONObject) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) JSONArray(org.json.JSONArray)

Example 22 with Response

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

the class AuditWebScriptTest method testGetIsAuditEnabledRepo.

public void testGetIsAuditEnabledRepo() throws Exception {
    boolean wasEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, null);
    String url = "/api/audit/control/" + APP_REPOTEST_NAME + APP_REPOTEST_PATH;
    TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
    if (wasEnabled) {
        Response response = sendRequest(req, Status.STATUS_OK, admin);
        JSONObject json = new JSONObject(response.getContentAsString());
        JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
        assertEquals("Incorrect number of applications reported", 1, apps.length());
        JSONObject app = apps.getJSONObject(0);
        String appName = app.getString(AbstractAuditWebScript.JSON_KEY_NAME);
        String appPath = app.getString(AbstractAuditWebScript.JSON_KEY_PATH);
        boolean appEnabled = app.getBoolean(AbstractAuditWebScript.JSON_KEY_ENABLED);
        assertEquals("Mismatched application audit enabled", wasEnabled, appEnabled);
        assertEquals("Mismatched application audit name", APP_REPOTEST_NAME, appName);
        assertEquals("Mismatched application audit path", APP_REPOTEST_PATH, appPath);
    }
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 23 with Response

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

the class AuditWebScriptTest method testQueryAuditRepo.

@SuppressWarnings("unused")
public void testQueryAuditRepo() throws Exception {
    long now = System.currentTimeMillis();
    long future = Long.MAX_VALUE;
    auditService.setAuditEnabled(true);
    auditService.enableAudit(APP_REPOTEST_NAME, APP_REPOTEST_PATH);
    loginWithFailure(getName());
    // Query for audit entries that could not have happened
    String url = "/api/audit/query/" + APP_REPOTEST_NAME + "?fromTime=" + now + "&verbose=true";
    JSONArray jsonEntries = null;
    TestWebScriptServer.GetRequest req = null;
    Response response = null;
    JSONObject json = null;
    Long entryCount = null;
    // 1 second sleep to ensure that the audit is processed
    for (int i = 0; i < 60; i++) {
        req = new TestWebScriptServer.GetRequest(url);
        response = sendRequest(req, Status.STATUS_OK, admin);
        json = new JSONObject(response.getContentAsString());
        entryCount = json.getLong(AbstractAuditWebScript.JSON_KEY_ENTRY_COUNT);
        jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
        if (jsonEntries.length() > 0) {
            break;
        }
        Thread.sleep(1000);
    }
    assertTrue("Expected at least one entry", jsonEntries.length() > 0);
    assertEquals("Entry count and physical count don't match", new Long(jsonEntries.length()), entryCount);
    JSONObject jsonEntry = jsonEntries.getJSONObject(0);
    Long entryId = jsonEntry.getLong(AbstractAuditWebScript.JSON_KEY_ENTRY_ID);
    assertNotNull("No entry ID", entryId);
    String entryTimeStr = jsonEntry.getString(AbstractAuditWebScript.JSON_KEY_ENTRY_TIME);
    assertNotNull("No entry time String", entryTimeStr);
    // Check conversion
    Date entryTime = ISO8601DateFormat.parse((String) entryTimeStr);
    JSONObject jsonValues = jsonEntry.getJSONObject(AbstractAuditWebScript.JSON_KEY_ENTRY_VALUES);
    String entryUsername = jsonValues.getString("/repositorytest/login/error/user");
    assertEquals("Didn't find the login-failure-user", getName(), entryUsername);
    // Query using well-known ID
    // Search is inclusive on the 'from' side
    Long fromEntryId = entryId;
    // Search is exclusive on the 'to' side
    Long toEntryId = entryId.longValue() + 1L;
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "?fromId=" + fromEntryId + "&toId=" + toEntryId;
    req = new TestWebScriptServer.GetRequest(url);
    response = sendRequest(req, Status.STATUS_OK, admin);
    json = new JSONObject(response.getContentAsString());
    jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
    assertEquals("Incorrect number of search results", 1, jsonEntries.length());
    // Query using a non-existent entry path
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "/repositorytest/login/error/userXXX" + "?verbose=true";
    req = new TestWebScriptServer.GetRequest(url);
    response = sendRequest(req, Status.STATUS_OK, admin);
    json = new JSONObject(response.getContentAsString());
    jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
    assertTrue("Should not have found anything", jsonEntries.length() == 0);
    // Query using a good entry path
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "/repositorytest/login/error/user" + "?verbose=true";
    req = new TestWebScriptServer.GetRequest(url);
    response = sendRequest(req, Status.STATUS_OK, admin);
    json = new JSONObject(response.getContentAsString());
    jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
    assertTrue("Should have found entries", jsonEntries.length() > 0);
    // Now login with failure using a GUID and ensure that we can find it
    String missingUser = new Long(System.currentTimeMillis()).toString();
    // Query for event that has not happened
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "/repositorytest/login/error/user" + "?value=" + missingUser;
    req = new TestWebScriptServer.GetRequest(url);
    response = sendRequest(req, Status.STATUS_OK, admin);
    json = new JSONObject(response.getContentAsString());
    jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
    assertEquals("Incorrect number of search results", 0, jsonEntries.length());
    loginWithFailure(missingUser);
    // Query for event that has happened once
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "/repositorytest/login/error/user" + "?value=" + missingUser;
    // 1 second sleep to ensure that the audit is processed
    for (int i = 0; i < 60; i++) {
        req = new TestWebScriptServer.GetRequest(url);
        response = sendRequest(req, Status.STATUS_OK, admin);
        json = new JSONObject(response.getContentAsString());
        jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
        if (jsonEntries.length() == 1) {
            break;
        }
        Thread.sleep(1000);
    }
    assertEquals("Incorrect number of search results", 1, jsonEntries.length());
    // Query for event, but casting the value to the incorrect type
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "/repositorytest/login/error/user" + "?value=" + missingUser + "&valueType=java.lang.Long";
    req = new TestWebScriptServer.GetRequest(url);
    response = sendRequest(req, Status.STATUS_OK, admin);
    json = new JSONObject(response.getContentAsString());
    jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
    assertEquals("Incorrect number of search results", 0, jsonEntries.length());
    // Test what happens when the target data needs encoding
    now = System.currentTimeMillis();
    String oddUser = "%$£\\\"\'";
    loginWithFailure(oddUser);
    // Query for the event limiting to one by count and descending (i.e. get last)
    url = "/api/audit/query/" + APP_REPOTEST_NAME + "?forward=false&limit=1&verbose=true&fromTime=" + now;
    // 1 second sleep to ensure that the audit is processed
    for (int i = 0; i < 60; i++) {
        req = new TestWebScriptServer.GetRequest(url);
        response = sendRequest(req, Status.STATUS_OK, admin);
        json = new JSONObject(response.getContentAsString());
        jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
        if (jsonEntries.length() == 1) {
            break;
        }
        Thread.sleep(1000);
    }
    assertEquals("Incorrect number of search results", 1, jsonEntries.length());
    jsonEntry = jsonEntries.getJSONObject(0);
    entryId = jsonEntry.getLong(AbstractAuditWebScript.JSON_KEY_ENTRY_ID);
    assertNotNull("No entry ID", entryId);
    jsonValues = jsonEntry.getJSONObject(AbstractAuditWebScript.JSON_KEY_ENTRY_VALUES);
    entryUsername = jsonValues.getString("/repositorytest/login/error/user");
    assertEquals("Didn't find the login-failure-user", oddUser, entryUsername);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Date(java.util.Date)

Example 24 with Response

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

the class SubscriptionServiceRestApiTest method setSubscriptionListPrivate.

protected void setSubscriptionListPrivate(String user, boolean setPrivate) throws Exception {
    JSONObject privateObject = new JSONObject();
    privateObject.put("private", setPrivate);
    String url = getUrl(URL_PRIVATE, user);
    Response response = sendRequest(new PutRequest(url, privateObject.toString(), "application/json"), Status.STATUS_OK);
    JSONObject resultObject = new JSONObject(response.getContentAsString());
    assertTrue(resultObject.has("private"));
    assertEquals(setPrivate, resultObject.getBoolean("private"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)

Example 25 with Response

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

the class SubscriptionServiceRestApiTest method getFollowers.

protected List<String> getFollowers(String user) throws Exception {
    String url = getUrl(URL_FOLLOWERS, user);
    Response response = sendRequest(new GetRequest(url), Status.STATUS_OK);
    JSONObject resultObject = new JSONObject(response.getContentAsString());
    assertTrue(resultObject.has("people"));
    List<String> result = new ArrayList<String>();
    JSONArray people = resultObject.getJSONArray("people");
    for (int i = 0; i < people.length(); i++) {
        JSONObject person = people.getJSONObject(i);
        assertTrue(person.has("userName"));
        assertTrue(person.has("firstName"));
        assertTrue(person.has("lastName"));
        result.add(person.getString("userName"));
    }
    return result;
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Aggregations

Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)281 JSONObject (org.json.JSONObject)228 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)171 JSONArray (org.json.JSONArray)116 PostRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest)84 JSONTokener (org.json.JSONTokener)39 PutRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)37 NodeRef (org.alfresco.service.cmr.repository.NodeRef)34 DeleteRequest (org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)32 HashMap (java.util.HashMap)24 ArrayList (java.util.ArrayList)16 TestWebScriptServer (org.springframework.extensions.webscripts.TestWebScriptServer)16 Serializable (java.io.Serializable)14 QName (org.alfresco.service.namespace.QName)14 Date (java.util.Date)13 JSONStringer (org.json.JSONStringer)13 UserTransaction (javax.transaction.UserTransaction)12 WorkflowDefinition (org.alfresco.service.cmr.workflow.WorkflowDefinition)12 ReplicationDefinition (org.alfresco.service.cmr.replication.ReplicationDefinition)11 WorkflowPath (org.alfresco.service.cmr.workflow.WorkflowPath)11