Search in sources :

Example 1 with AclChangeSet

use of org.alfresco.repo.solr.AclChangeSet in project alfresco-remote-api by Alfresco.

the class SOLRWebScriptTest method aclReadersGetImpl.

private void aclReadersGetImpl() throws Exception {
    List<AclChangeSet> aclChangeSets = solrTrackingComponent.getAclChangeSets(null, null, null, null, 1024);
    List<Long> aclChangeSetIds = new ArrayList<Long>(50);
    for (AclChangeSet aclChangeSet : aclChangeSets) {
        if (aclChangeSet.getAclCount() > 0) {
            aclChangeSetIds.add(aclChangeSet.getId());
            break;
        }
    }
    if (aclChangeSetIds.size() == 0) {
    // No ACLs; not likely
    }
    List<Acl> acls = solrTrackingComponent.getAcls(aclChangeSetIds, null, 1024);
    List<Long> aclIds = new ArrayList<Long>(acls.size());
    JSONObject json = new JSONObject();
    JSONArray aclIdsJSON = new JSONArray();
    for (Acl acl : acls) {
        Long aclId = acl.getId();
        aclIds.add(aclId);
        aclIdsJSON.put(aclId);
    }
    json.put("aclIds", aclIdsJSON);
    // Now get the readers
    List<AclReaders> aclsReaders = solrTrackingComponent.getAclsReaders(aclIds);
    assertEquals("Should have same number of ACLs as supplied", aclIds.size(), aclsReaders.size());
    assertTrue("Must have *some* ACLs here", aclIds.size() > 0);
    Map<Long, Set<String>> readersByAclId = new HashMap<Long, Set<String>>();
    for (AclReaders aclReaders : aclsReaders) {
        readersByAclId.put(aclReaders.getAclId(), aclReaders.getReaders());
    }
    Map<Long, Set<String>> deniedByAclId = new HashMap<Long, Set<String>>();
    for (AclReaders aclReaders : aclsReaders) {
        assertNotNull("AclReaders should not contain null denial set", aclReaders.getDenied());
        deniedByAclId.put(aclReaders.getAclId(), aclReaders.getDenied());
    }
    // Now query using the webscript
    String url = "/api/solr/aclsReaders";
    TestWebScriptServer.PostRequest req = new TestWebScriptServer.PostRequest(url, json.toString(), "application/json");
    Response response = sendRequest(req, Status.STATUS_OK, admin);
    if (logger.isDebugEnabled()) {
        logger.debug(response.getContentAsString());
    }
    json = new JSONObject(response.getContentAsString());
    JSONArray aclsReadersJSON = json.getJSONArray("aclsReaders");
    // Check
    assertEquals("Script and API returned different number of results", readersByAclId.size(), aclsReadersJSON.length());
    // Iterate of the JSON and ensure that the list of ACL readers is correct
    for (int i = 0; i < aclsReadersJSON.length(); i++) {
        // Choose an ACL and check the readers
        JSONObject aclReadersJSON = aclsReadersJSON.getJSONObject(i);
        Long aclIdJSON = aclReadersJSON.getLong("aclId");
        Set<String> readersCheck = readersByAclId.get(aclIdJSON);
        JSONArray readersJSON = aclReadersJSON.getJSONArray("readers");
        assertEquals("Readers list for ACL " + aclIdJSON + " is wrong. ", readersCheck.size(), readersJSON.length());
        for (int j = 0; j < readersJSON.length(); j++) {
            String readerJSON = readersJSON.getString(j);
            assertTrue("Found reader not in check set: " + readerJSON, readersCheck.contains(readerJSON));
        }
        Set<String> deniedCheck = deniedByAclId.get(aclIdJSON);
        JSONArray deniedJSON = aclReadersJSON.getJSONArray("denied");
        assertEquals("Denied list for ACL " + aclIdJSON + " is wrong. ", deniedCheck.size(), deniedJSON.length());
        for (int j = 0; j < deniedJSON.length(); j++) {
            String denyJSON = deniedJSON.getString(j);
            assertTrue("Found denied authority not in check set: " + denyJSON, deniedCheck.contains(denyJSON));
        }
    }
}
Also used : AclChangeSet(org.alfresco.repo.solr.AclChangeSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Acl(org.alfresco.repo.solr.Acl) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) AclChangeSet(org.alfresco.repo.solr.AclChangeSet) AclReaders(org.alfresco.repo.solr.AclReaders)

Example 2 with AclChangeSet

use of org.alfresco.repo.solr.AclChangeSet in project alfresco-remote-api by Alfresco.

the class SOLRWebScriptTest method testAclsGet.

public void testAclsGet() throws Exception {
    List<AclChangeSet> aclChangeSets = solrTrackingComponent.getAclChangeSets(null, null, null, null, 100);
    if (aclChangeSets.size() == 0) {
        // Can't test, but very unlikely
        return;
    }
    // Build JSON using these
    JSONObject json = new JSONObject();
    JSONArray aclChangeSetIdsJSON = new JSONArray();
    int count = 0;
    List<Long> aclChangeSetIds = new ArrayList<Long>();
    for (AclChangeSet aclChangeSet : aclChangeSets) {
        if (count >= 512) {
            break;
        }
        if (aclChangeSet.getAclCount() == 0) {
            // No ACLs
            continue;
        }
        Long aclChangeSetId = aclChangeSet.getId();
        aclChangeSetIdsJSON.put(aclChangeSetId);
        aclChangeSetIds.add(aclChangeSetId);
        count++;
    }
    json.put("aclChangeSetIds", aclChangeSetIdsJSON);
    String url = "/api/solr/acls";
    TestWebScriptServer.PostRequest req = new TestWebScriptServer.PostRequest(url, json.toString(), "application/json");
    Response response = sendRequest(req, Status.STATUS_OK, admin);
    if (logger.isDebugEnabled()) {
        logger.debug(response.getContentAsString());
    }
    json = new JSONObject(response.getContentAsString());
    JSONArray acls = json.getJSONArray("acls");
    // Check
    List<Acl> aclsCheck = solrTrackingComponent.getAcls(aclChangeSetIds, null, 512);
    assertEquals("Script and API returned different number of results", aclsCheck.size(), acls.length());
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Acl(org.alfresco.repo.solr.Acl) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) AclChangeSet(org.alfresco.repo.solr.AclChangeSet)

Aggregations

ArrayList (java.util.ArrayList)2 Acl (org.alfresco.repo.solr.Acl)2 AclChangeSet (org.alfresco.repo.solr.AclChangeSet)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 TestWebScriptServer (org.springframework.extensions.webscripts.TestWebScriptServer)2 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 AclReaders (org.alfresco.repo.solr.AclReaders)1