Search in sources :

Example 1 with AsyncSession

use of org.apache.chemistry.opencmis.client.api.AsyncSession in project copper-cms by PogeyanOSS.

the class AsyncCreateAndDeleteFolderTest method run.

@Override
public void run(Session session) {
    CmisTestResult f;
    int numOfFolders = 100;
    // create an async session
    AsyncSession asyncSession = AsyncSessionFactoryImpl.newInstance().createAsyncSession(session, 10);
    // create a test folder
    Folder testFolder = createTestFolder(session);
    try {
        // create folders
        List<Future<ObjectId>> folderFutures = new ArrayList<Future<ObjectId>>();
        for (int i = 0; i < numOfFolders; i++) {
            String name = "asyncfolder" + i;
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put(PropertyIds.NAME, name);
            properties.put(PropertyIds.OBJECT_TYPE_ID, getFolderTestTypeId());
            Future<ObjectId> newFolder = asyncSession.createFolder(properties, testFolder);
            folderFutures.add(newFolder);
        }
        // wait for all folders being created
        List<ObjectId> folderIds = new ArrayList<ObjectId>();
        try {
            for (Future<ObjectId> folderFuture : folderFutures) {
                ObjectId id = folderFuture.get();
                folderIds.add(id);
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Folder could not been created! Exception: " + e.getMessage(), e, true));
        }
        // check children of test folder
        int count = countChildren(testFolder);
        f = createResult(FAILURE, "Test folder should have " + numOfFolders + " children but has " + count + "!");
        addResult(assertEquals(count, numOfFolders, null, f));
        // get folders
        Map<String, Future<CmisObject>> getObjectFutures = new HashMap<String, Future<CmisObject>>();
        for (ObjectId folderId : folderIds) {
            Future<CmisObject> getObjectFuture = asyncSession.getObject(folderId, SELECT_ALL_NO_CACHE_OC);
            getObjectFutures.put(folderId.getId(), getObjectFuture);
        }
        // wait for all folders being fetched
        List<String> paths = new ArrayList<String>();
        try {
            for (Map.Entry<String, Future<CmisObject>> getObjectFuture : getObjectFutures.entrySet()) {
                CmisObject object = getObjectFuture.getValue().get();
                f = createResult(FAILURE, "Fetching folder failed!");
                addResult(assertIsTrue(object instanceof Folder, null, f));
                if (object != null) {
                    f = createResult(FAILURE, "Fetched wrong folder!");
                    addResult(assertEquals(getObjectFuture.getKey(), object.getId(), null, f));
                    paths.add(((Folder) object).getPath());
                }
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Folders could not been fetched! Exception: " + e.getMessage(), e, true));
        }
        // get folders by path
        Map<String, Future<CmisObject>> getObjectByPathFutures = new HashMap<String, Future<CmisObject>>();
        for (String path : paths) {
            Future<CmisObject> getObjectByPathFuture = asyncSession.getObjectByPath(path, SELECT_ALL_NO_CACHE_OC);
            getObjectByPathFutures.put(path, getObjectByPathFuture);
        }
        // wait for all folders being fetched
        try {
            for (Map.Entry<String, Future<CmisObject>> getObjectByPathFuture : getObjectByPathFutures.entrySet()) {
                CmisObject object = getObjectByPathFuture.getValue().get();
                f = createResult(FAILURE, "Fetching folder failed!");
                addResult(assertIsTrue(object instanceof Folder, null, f));
                if (object != null) {
                    f = createResult(FAILURE, "Fetched wrong folder!");
                    addResult(assertEquals(getObjectByPathFuture.getKey(), ((Folder) object).getPath(), null, f));
                }
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Folders could not been fetched! Exception: " + e.getMessage(), e, true));
        }
        // delete folders
        List<Future<?>> delFutures = new ArrayList<Future<?>>();
        for (ObjectId folderId : folderIds) {
            Future<?> delFuture = asyncSession.deleteTree(folderId, true, UnfileObject.DELETE, true);
            delFutures.add(delFuture);
        }
        // wait for all folders being deleted
        try {
            for (Future<?> delFuture : delFutures) {
                delFuture.get();
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Folder could not been deleted! Exception: " + e.getMessage(), e, true));
        }
        // check children of test folder
        count = countChildren(testFolder);
        f = createResult(FAILURE, "Test folder should be empty but has " + count + " children!");
        addResult(assertEquals(count, 0, null, f));
    } finally {
        // delete the test folder
        deleteTestFolder();
        if (asyncSession instanceof AbstractExecutorServiceAsyncSession<?>) {
            ((AbstractExecutorServiceAsyncSession<?>) asyncSession).shutdown();
        }
    }
    addResult(createInfoResult("Tested the parallel creation and deletion of " + numOfFolders + " folders."));
}
Also used : ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) HashMap(java.util.HashMap) AbstractExecutorServiceAsyncSession(org.apache.chemistry.opencmis.client.runtime.async.AbstractExecutorServiceAsyncSession) AsyncSession(org.apache.chemistry.opencmis.client.api.AsyncSession) ArrayList(java.util.ArrayList) Folder(org.apache.chemistry.opencmis.client.api.Folder) AbstractExecutorServiceAsyncSession(org.apache.chemistry.opencmis.client.runtime.async.AbstractExecutorServiceAsyncSession) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) Future(java.util.concurrent.Future) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) UnfileObject(org.apache.chemistry.opencmis.commons.enums.UnfileObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AsyncSession

use of org.apache.chemistry.opencmis.client.api.AsyncSession in project copper-cms by PogeyanOSS.

the class AsyncCreateAndDeleteDocumentTest method run.

@Override
public void run(Session session) {
    CmisTestResult f;
    int numOfDocuments = 100;
    String mimeType = "text/plain";
    byte[] contentBytes = new byte[64 * 1024];
    for (int i = 0; i < contentBytes.length; i++) {
        contentBytes[i] = (byte) ('0' + i % 10);
    }
    // create an async session
    AsyncSession asyncSession = AsyncSessionFactoryImpl.newInstance().createAsyncSession(session, 10);
    // create a test folder
    Folder testFolder = createTestFolder(session);
    try {
        // create documents
        List<Future<ObjectId>> docFutures = new ArrayList<Future<ObjectId>>();
        for (int i = 0; i < numOfDocuments; i++) {
            String name = "asyncdoc" + i + ".txt";
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put(PropertyIds.NAME, name);
            properties.put(PropertyIds.OBJECT_TYPE_ID, getDocumentTestTypeId());
            ContentStream contentStream = new ContentStreamImpl(name, BigInteger.valueOf(contentBytes.length), mimeType, new ByteArrayInputStream(contentBytes));
            Future<ObjectId> newDocument = asyncSession.createDocument(properties, testFolder, contentStream, null);
            docFutures.add(newDocument);
        }
        // wait for all document being created
        List<ObjectId> docIds = new ArrayList<ObjectId>();
        try {
            for (Future<ObjectId> docFuture : docFutures) {
                ObjectId id = docFuture.get();
                docIds.add(id);
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Documents could not been created! Exception: " + e.getMessage(), e, true));
        }
        // check children of the test folder
        int count = countChildren(testFolder);
        f = createResult(FAILURE, "Test folder should have " + numOfDocuments + " children but has " + count + "!");
        addResult(assertEquals(count, numOfDocuments, null, f));
        // simple children test
        addResult(checkChildren(session, testFolder, "Test folder children check"));
        // get documents
        Map<String, Future<CmisObject>> getObjectFutures = new HashMap<String, Future<CmisObject>>();
        Map<String, Future<ContentStream>> contentStreamFutures = new HashMap<String, Future<ContentStream>>();
        Map<String, ByteArrayOutputStream> content = new HashMap<String, ByteArrayOutputStream>();
        for (ObjectId docId : docIds) {
            Future<CmisObject> getObjectFuture = asyncSession.getObject(docId, SELECT_ALL_NO_CACHE_OC);
            getObjectFutures.put(docId.getId(), getObjectFuture);
            ByteArrayOutputStream out = new ByteArrayOutputStream(contentBytes.length);
            content.put(docId.getId(), out);
            Future<ContentStream> contentStreamFuture = asyncSession.storeContentStream(docId, out);
            contentStreamFutures.put(docId.getId(), contentStreamFuture);
        }
        // wait for all document being fetched
        try {
            for (Map.Entry<String, Future<CmisObject>> getObjectFuture : getObjectFutures.entrySet()) {
                CmisObject object = getObjectFuture.getValue().get();
                f = createResult(FAILURE, "Fetching document failed!");
                addResult(assertIsTrue(object instanceof Document, null, f));
                if (object != null) {
                    f = createResult(FAILURE, "Fetched wrong document!");
                    addResult(assertEquals(getObjectFuture.getKey(), object.getId(), null, f));
                }
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Documents could not been fetched! Exception: " + e.getMessage(), e, true));
        }
        // wait for all document content being fetched
        try {
            for (Map.Entry<String, Future<ContentStream>> contentStreamFuture : contentStreamFutures.entrySet()) {
                ContentStream contentStream = contentStreamFuture.getValue().get();
                f = createResult(FAILURE, "Fetching document content failed!");
                addResult(assertNotNull(contentStream, null, f));
                if (contentStream != null) {
                    if (contentStream.getMimeType() == null) {
                        addResult(createResult(FAILURE, "Content MIME type is null!"));
                    } else {
                        f = createResult(WARNING, "Content MIME types don't match!");
                        addResult(assertIsTrue(contentStream.getMimeType().trim().toLowerCase(Locale.ENGLISH).startsWith(mimeType.toLowerCase(Locale.ENGLISH)), null, f));
                    }
                }
                ByteArrayOutputStream out = content.get(contentStreamFuture.getKey());
                byte[] readBytes = out.toByteArray();
                f = createResult(FAILURE, "Read content length doesn't match document content length!");
                addResult(assertEquals(contentBytes.length, readBytes.length, null, f));
                f = createResult(FAILURE, "Read content doesn't match document content!");
                addResult(assertEqualArray(contentBytes, readBytes, null, f));
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Document content could not been fetched! Exception: " + e.getMessage(), e, true));
        }
        // delete documents
        List<Future<?>> delFutures = new ArrayList<Future<?>>();
        for (ObjectId docId : docIds) {
            Future<?> delFuture = asyncSession.delete(docId);
            delFutures.add(delFuture);
        }
        // wait for all document being deleted
        try {
            for (Future<?> delFuture : delFutures) {
                delFuture.get();
            }
        } catch (Exception e) {
            addResult(createResult(UNEXPECTED_EXCEPTION, "Documents could not been deleted! Exception: " + e.getMessage(), e, true));
        }
        // check children of the test folder
        count = countChildren(testFolder);
        f = createResult(FAILURE, "Test folder should be empty but has " + count + " children!");
        addResult(assertEquals(count, 0, null, f));
    } finally {
        // delete the test folder
        deleteTestFolder();
        if (asyncSession instanceof AbstractExecutorServiceAsyncSession<?>) {
            ((AbstractExecutorServiceAsyncSession<?>) asyncSession).shutdown();
        }
    }
    addResult(createInfoResult("Tested the parallel creation and deletion of " + numOfDocuments + " documents."));
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) HashMap(java.util.HashMap) AbstractExecutorServiceAsyncSession(org.apache.chemistry.opencmis.client.runtime.async.AbstractExecutorServiceAsyncSession) AsyncSession(org.apache.chemistry.opencmis.client.api.AsyncSession) ArrayList(java.util.ArrayList) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractExecutorServiceAsyncSession(org.apache.chemistry.opencmis.client.runtime.async.AbstractExecutorServiceAsyncSession) ByteArrayInputStream(java.io.ByteArrayInputStream) Future(java.util.concurrent.Future) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Future (java.util.concurrent.Future)2 AsyncSession (org.apache.chemistry.opencmis.client.api.AsyncSession)2 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)2 Folder (org.apache.chemistry.opencmis.client.api.Folder)2 ObjectId (org.apache.chemistry.opencmis.client.api.ObjectId)2 AbstractExecutorServiceAsyncSession (org.apache.chemistry.opencmis.client.runtime.async.AbstractExecutorServiceAsyncSession)2 CmisTestResult (org.apache.chemistry.opencmis.tck.CmisTestResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Document (org.apache.chemistry.opencmis.client.api.Document)1 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)1 UnfileObject (org.apache.chemistry.opencmis.commons.enums.UnfileObject)1 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)1