Search in sources :

Example 86 with Document

use of org.apache.chemistry.opencmis.client.api.Document in project cool-jconon by consiglionazionaledellericerche.

the class Call method downloadFile.

@GET
@Path("download-xls")
@Produces("application/vnd.ms-excel")
public Response downloadFile(@Context HttpServletRequest req, @QueryParam("objectId") String objectId, @QueryParam("fileName") String fileName) throws IOException {
    LOGGER.debug("Download excel file with objectId:" + objectId);
    ResponseBuilder rb;
    Session session = cmisService.getCurrentCMISSession(req);
    try {
        Document doc = (Document) session.getObject(objectId);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(doc.getContentStream().getStream(), out);
        rb = Response.ok(out.toByteArray());
        rb.header("Content-Disposition", "attachment; filename=\"" + fileName.concat(".xls\""));
        doc.delete();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        rb = Response.status(Status.INTERNAL_SERVER_ERROR);
    }
    return rb.build();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Document(org.apache.chemistry.opencmis.client.api.Document) ParseException(java.text.ParseException) IOException(java.io.IOException) ClientMessageException(it.cnr.cool.web.scripts.exception.ClientMessageException) Session(org.apache.chemistry.opencmis.client.api.Session)

Example 87 with Document

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

the class AbstractSessionTest method checkObject.

protected CmisTestResult checkObject(Session session, CmisObject object, String[] properties, String message) {
    List<CmisTestResult> results = new ArrayList<CmisTestResult>();
    CmisTestResult f;
    f = createResult(FAILURE, "Object is null!", true);
    addResult(results, assertNotNull(object, null, f));
    if (object != null) {
        f = createResult(FAILURE, "Object ID is not set!");
        addResult(results, assertStringNotEmpty(object.getId(), null, f));
        GregorianCalendar creationDate = null;
        GregorianCalendar lastModificationDate = null;
        // properties
        for (String propId : properties) {
            Property<?> prop = object.getProperty(propId);
            // values of non-spec properties are not checked here
            PropertyCheckEnum propertyCheck = PropertyCheckEnum.NO_VALUE_CHECK;
            // known properties that are strings and must be set
            if (PropertyIds.OBJECT_ID.equals(propId) || PropertyIds.BASE_TYPE_ID.equals(propId) || PropertyIds.OBJECT_TYPE_ID.equals(propId) || PropertyIds.PATH.equals(propId) || PropertyIds.SOURCE_ID.equals(propId) || PropertyIds.TARGET_ID.equals(propId)) {
                propertyCheck = PropertyCheckEnum.STRING_MUST_NOT_BE_EMPTY;
            }
            if (!(object instanceof Relationship)) {
                if (PropertyIds.CREATED_BY.equals(propId) || PropertyIds.LAST_MODIFIED_BY.equals(propId)) {
                    propertyCheck = PropertyCheckEnum.STRING_MUST_NOT_BE_EMPTY;
                }
            }
            // known properties that are strings and should be set
            if (PropertyIds.NAME.equals(propId) || PropertyIds.POLICY_TEXT.equals(propId)) {
                propertyCheck = PropertyCheckEnum.STRING_SHOULD_NOT_BE_EMPTY;
            }
            // known properties that are not strings and must be set
            if (PropertyIds.IS_IMMUTABLE.equals(propId)) {
                propertyCheck = PropertyCheckEnum.MUST_BE_SET;
            }
            if (!(object instanceof Relationship)) {
                if (PropertyIds.CREATION_DATE.equals(propId) || PropertyIds.LAST_MODIFICATION_DATE.equals(propId)) {
                    propertyCheck = PropertyCheckEnum.MUST_BE_SET;
                }
            }
            // special case: parent
            if (PropertyIds.PARENT_ID.equals(propId)) {
                if (object instanceof Folder) {
                    if (((Folder) object).isRootFolder()) {
                        propertyCheck = PropertyCheckEnum.MUST_NOT_BE_SET;
                    } else {
                        propertyCheck = PropertyCheckEnum.STRING_MUST_NOT_BE_EMPTY;
                    }
                } else {
                    addResult(results, createResult(FAILURE, "Property " + PropertyIds.PARENT_ID + " is only defined for folders!"));
                }
            }
            // special case: path
            if (PropertyIds.PATH.equals(propId) && prop.getFirstValue() != null) {
                Object path = prop.getFirstValue();
                if (path instanceof String) {
                    f = createResult(FAILURE, "Path does not start with '/': " + path);
                    addResult(results, assertIsTrue(((String) path).length() > 0 && ((String) path).charAt(0) == '/', null, f));
                } else {
                    addResult(results, createResult(FAILURE, "Property " + PropertyIds.PATH + " is not a string!"));
                }
            }
            // check property
            addResult(results, checkProperty(prop, "Property " + propId, propertyCheck));
            // catch creationDate and lastModificationDate
            if (PropertyIds.CREATION_DATE.equals(propId)) {
                if (prop != null) {
                    creationDate = (GregorianCalendar) prop.getFirstValue();
                }
            } else if (PropertyIds.LAST_MODIFICATION_DATE.equals(propId)) {
                if (prop != null) {
                    lastModificationDate = (GregorianCalendar) prop.getFirstValue();
                }
            }
        }
        // check creationDate <= lastModificationDate
        if (creationDate != null && lastModificationDate != null) {
            f = createResult(FAILURE, "Last modification date precedes creation date!");
            addResult(results, assertIsTrue(creationDate.getTimeInMillis() <= lastModificationDate.getTimeInMillis(), null, f));
            f = createResult(WARNING, "Creation date and last modification date have different timezones.");
            addResult(assertIsTrue(creationDate.getTimeZone().hasSameRules(lastModificationDate.getTimeZone()), null, f));
        }
        // allowable actions
        if ((object.getAllowableActions() == null) || (object.getAllowableActions().getAllowableActions() == null)) {
            addResult(results, createResult(FAILURE, "Object has no allowable actions!"));
        } else {
            Set<Action> actions = object.getAllowableActions().getAllowableActions();
            f = createResult(FAILURE, "Object has no CAN_GET_PROPERTIES allowable action!");
            addResult(results, assertAllowableAction(object, Action.CAN_GET_PROPERTIES, null, f));
            addResult(results, assertIsTrue(object.hasAllowableAction(Action.CAN_GET_PROPERTIES), null, f));
            if (object instanceof Document) {
                if (actions.contains(Action.CAN_CHECK_OUT) && actions.contains(Action.CAN_CHECK_IN)) {
                    addResult(results, createResult(FAILURE, "Document object has CAN_CHECK_OUT and CAN_CHECK_IN allowable actions!"));
                }
                if (actions.contains(Action.CAN_CHECK_OUT) && actions.contains(Action.CAN_CANCEL_CHECK_OUT)) {
                    addResult(results, createResult(FAILURE, "Document object has CAN_CHECK_OUT and CAN_CANCEL_CHECK_OUT allowable actions!"));
                }
                Document doc = (Document) object;
                DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
                if (doc.isVersionSeriesCheckedOut() != null) {
                    if (doc.isVersionSeriesCheckedOut()) {
                        f = createResult(WARNING, "Document is checked out and has CAN_CHECK_OUT allowable action!");
                        addResult(results, assertNotAllowableAction(object, Action.CAN_CHECK_OUT, null, f));
                        if (doc.getVersionSeriesCheckedOutId() == null) {
                            addResult(results, createResult(WARNING, "Document is checked out and but the property cmis:versionSeriesCheckedOutId is not set!"));
                        } else {
                            if (doc.getVersionSeriesCheckedOutId().equals(object.getId())) {
                                // object is PWC
                                f = createResult(FAILURE, "PWC doesn't have CAN_CHECK_IN allowable action!");
                                addResult(results, assertAllowableAction(object, Action.CAN_CHECK_IN, null, f));
                                f = createResult(FAILURE, "PWC doesn't have CAN_CANCEL_CHECK_OUT allowable action!");
                                addResult(results, assertAllowableAction(object, Action.CAN_CANCEL_CHECK_OUT, null, f));
                            } else {
                                // object is not PWC
                                f = createResult(WARNING, "Non-PWC has CAN_CHECK_IN allowable action!");
                                addResult(results, assertNotAllowableAction(object, Action.CAN_CHECK_IN, null, f));
                                f = createResult(WARNING, "Non-PWC has CAN_CANCEL_CHECK_OUT allowable action!");
                                addResult(results, assertNotAllowableAction(object, Action.CAN_CANCEL_CHECK_OUT, null, f));
                            }
                        }
                    } else {
                        f = createResult(FAILURE, "Document is not checked out and has CAN_CHECK_IN allowable action!");
                        addResult(results, assertNotAllowableAction(object, Action.CAN_CHECK_IN, null, f));
                        f = createResult(FAILURE, "Document is not checked out and has CAN_CANCEL_CHECK_OUT allowable action!");
                        addResult(results, assertNotAllowableAction(object, Action.CAN_CANCEL_CHECK_OUT, null, f));
                        // versionable check
                        if (docType.isVersionable()) {
                            // 
                            if (Boolean.TRUE.equals(doc.isLatestVersion())) {
                                f = createResult(WARNING, "Document is versionable and not checked but has no CAN_CHECK_OUT allowable action!");
                                addResult(results, assertAllowableAction(object, Action.CAN_CHECK_OUT, null, f));
                            }
                        } else {
                            f = createResult(FAILURE, "Document is not versionable but has CAN_CHECK_OUT allowable action!");
                            addResult(results, assertNotAllowableAction(object, Action.CAN_CHECK_OUT, null, f));
                        }
                    }
                } else {
                    addResult(results, createResult(WARNING, "Property cmis:isVersionSeriesCheckedOut is not set!"));
                }
                // immutable check
                if (Boolean.TRUE.equals(doc.isImmutable())) {
                    f = createResult(FAILURE, "Document is immutable and has CAN_UPDATE_PROPERTIES allowable action!");
                    addResult(results, assertNotAllowableAction(object, Action.CAN_UPDATE_PROPERTIES, null, f));
                    f = createResult(FAILURE, "Document is immutable and has CAN_DELETE_OBJECT allowable action!");
                    addResult(results, assertNotAllowableAction(object, Action.CAN_DELETE_OBJECT, null, f));
                }
            } else {
                f = createResult(FAILURE, "Non-Document object has CAN_CHECK_IN allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_CHECK_IN, null, f));
                f = createResult(FAILURE, "Non-Document object has CAN_CHECK_OUT allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_CHECK_OUT, null, f));
                f = createResult(FAILURE, "Non-Document object has CAN_CANCEL_CHECK_OUT allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_CANCEL_CHECK_OUT, null, f));
                f = createResult(FAILURE, "Non-Document object has CAN_GET_CONTENT_STREAM allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_GET_CONTENT_STREAM, null, f));
                f = createResult(FAILURE, "Non-Document object has CAN_DELETE_CONTENT_STREAM allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_DELETE_CONTENT_STREAM, null, f));
                f = createResult(FAILURE, "Non-Document object has CAN_GET_ALL_VERSIONS allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_GET_ALL_VERSIONS, null, f));
            }
            if (object instanceof Folder) {
                Folder folder = (Folder) object;
                if (folder.isRootFolder()) {
                    f = createResult(FAILURE, "Root folder has CAN_DELETE_OBJECT allowable action!");
                    addResult(results, assertNotAllowableAction(object, Action.CAN_DELETE_OBJECT, null, f));
                    f = createResult(FAILURE, "Root folder has CAN_GET_FOLDER_PARENT allowable action!");
                    addResult(results, assertNotAllowableAction(object, Action.CAN_GET_FOLDER_PARENT, null, f));
                    f = createResult(FAILURE, "Root folder has CAN_MOVE_OBJECT allowable action!");
                    addResult(results, assertNotAllowableAction(object, Action.CAN_MOVE_OBJECT, null, f));
                }
            } else {
                f = createResult(FAILURE, "Non-Folder object has CAN_GET_DESCENDANTS allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_GET_DESCENDANTS, null, f));
                f = createResult(FAILURE, "Non-Folder object has CAN_GET_FOLDER_PARENT allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_GET_FOLDER_PARENT, null, f));
                f = createResult(FAILURE, "Non-Folder object has CAN_GET_CHILDREN allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_GET_CHILDREN, null, f));
                f = createResult(FAILURE, "Non-Folder object has CAN_DELETE_TREE allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_DELETE_TREE, null, f));
                f = createResult(FAILURE, "Non-Folder object has CAN_GET_FOLDER_PARENT allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_GET_FOLDER_PARENT, null, f));
                f = createResult(FAILURE, "Non-Folder object has CAN_CREATE_DOCUMENT allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_CREATE_DOCUMENT, null, f));
                f = createResult(FAILURE, "Non-Folder object has CAN_CREATE_FOLDER allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_CREATE_FOLDER, null, f));
            }
            if (!(object instanceof FileableCmisObject) || (object instanceof Folder)) {
                f = createResult(FAILURE, "Non-Filable object or folder has CAN_ADD_OBJECT_TO_FOLDER allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_ADD_OBJECT_TO_FOLDER, null, f));
                f = createResult(FAILURE, "Non-Filable object or folder has CAN_REMOVE_OBJECT_FROM_FOLDER allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_REMOVE_OBJECT_FROM_FOLDER, null, f));
            }
            if (!(object instanceof FileableCmisObject)) {
                f = createResult(FAILURE, "Non-Fileable object has CAN_MOVE_OBJECT allowable action!");
                addResult(results, assertNotAllowableAction(object, Action.CAN_MOVE_OBJECT, null, f));
            }
            // get allowable actions again
            AllowableActions allowableActions = session.getBinding().getObjectService().getAllowableActions(session.getRepositoryInfo().getId(), object.getId(), null);
            if (allowableActions.getAllowableActions() == null) {
                addResult(results, createResult(FAILURE, "getAllowableActions() didn't returned allowable actions!"));
            } else {
                f = createResult(FAILURE, "Object allowable actions don't match the allowable actions returned by getAllowableActions()!");
                addResult(results, assertEqualSet(object.getAllowableActions().getAllowableActions(), allowableActions.getAllowableActions(), null, f));
            }
        }
        // check ACL
        if (object.getAcl() != null && object.getAcl().getAces() != null) {
            addResult(results, checkACL(session, object.getAcl(), true, "ACL"));
        }
        // check policies
        if (hasPolicies(session)) {
            try {
                List<ObjectData> appliedPolicies = session.getBinding().getPolicyService().getAppliedPolicies(session.getRepositoryInfo().getId(), object.getId(), "*", null);
                if (appliedPolicies == null) {
                    appliedPolicies = Collections.emptyList();
                }
                List<Policy> objectPolicies = object.getPolicies();
                if (objectPolicies == null) {
                    objectPolicies = Collections.emptyList();
                }
                f = createResult(FAILURE, "The number of policies returned by getAppliedPolicies() and the number of object policies don't match!");
                addResult(results, assertEquals(appliedPolicies.size(), objectPolicies.size(), null, f));
            } catch (CmisNotSupportedException e) {
                addResult(results, createResult(WARNING, "getAppliedPolicies() not supported for object: " + object.getId()));
            }
        }
        // check relationships
        checkRelationships(session, results, object);
        // check document content
        checkDocumentContent(session, results, object);
        // check renditions
        if (object.getRenditions() != null) {
            addResult(results, checkRenditions(session, object, "Rendition check"));
        }
        // check allowed child object type ids
        if (object instanceof Folder) {
            List<String> otids = object.getPropertyValue(PropertyIds.ALLOWED_CHILD_OBJECT_TYPE_IDS);
            if (otids != null) {
                for (String otid : otids) {
                    try {
                        session.getTypeDefinition(otid);
                    } catch (CmisBaseException e) {
                        addResult(results, createResult(FAILURE, "The cmis:allowedChildObjectTypeIds property contains the type ID '" + otid + "' but the type doesn't exists. Folder ID: " + object.getId()));
                    }
                }
            }
        }
        // check path
        if (object instanceof FileableCmisObject) {
            List<String> paths = ((FileableCmisObject) object).getPaths();
            if (object instanceof Folder) {
                f = createResult(FAILURE, "Folder does not have excatly one path! This is an OpenCMIS bug!");
                addResult(results, assertEquals(1, paths.size(), null, f));
            } else {
                if (Boolean.FALSE.equals(session.getRepositoryInfo().getCapabilities().isMultifilingSupported())) {
                    f = createResult(FAILURE, "Repository does not support multi-filing, but the object has more than one parent!");
                    addResult(results, assertIsTrue(paths.size() < 2, null, f));
                }
            }
        }
    }
    CmisTestResultImpl result = createResult(getWorst(results), message);
    result.getChildren().addAll(results);
    return result.getStatus().getLevel() <= OK.getLevel() ? null : result;
}
Also used : Policy(org.apache.chemistry.opencmis.client.api.Policy) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) Action(org.apache.chemistry.opencmis.commons.enums.Action) DocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition) ArrayList(java.util.ArrayList) GregorianCalendar(java.util.GregorianCalendar) ObjectData(org.apache.chemistry.opencmis.commons.data.ObjectData) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) Relationship(org.apache.chemistry.opencmis.client.api.Relationship) AllowableActions(org.apache.chemistry.opencmis.commons.data.AllowableActions) FileableCmisObject(org.apache.chemistry.opencmis.client.api.FileableCmisObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) FileableCmisObject(org.apache.chemistry.opencmis.client.api.FileableCmisObject)

Example 88 with Document

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

the class OperationContextTest method run.

@Override
public void run(Session session) {
    // create a test folder
    Folder testFolder = createTestFolder(session);
    try {
        // create a test document
        Document document = createDocument(session, testFolder, "testdoc.txt", CONTENT);
        // high-level API tests
        runHighLevelApiTests(session, testFolder, document);
        // low-level API tests
        runLowLevelApiTests(session, testFolder, document);
        // clean up
        document.delete(true);
    } finally {
        // delete the test folder
        deleteTestFolder();
    }
}
Also used : Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document)

Example 89 with Document

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

the class SetAndDeleteContentTest method testAppendStream.

private void testAppendStream(Session session, Folder testFolder, int bufferSize) {
    CmisTestResult f;
    // create an empty document
    Document doc = createDocument(session, testFolder, "appendstreamtest.txt", "");
    Document workDoc = doc;
    boolean checkedout = false;
    DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
    if (Boolean.TRUE.equals(docType.isVersionable())) {
        workDoc = (Document) session.getObject(doc.checkOut(), SELECT_ALL_NO_CACHE_OC);
        checkedout = true;
    }
    try {
        // create an overwrite OutputStream
        OutputStream out1 = workDoc.createOverwriteOutputStream("appendstreamtest", "text/plain", bufferSize);
        out1.write(IOUtils.toUTF8Bytes("line 1\n"));
        out1.write(IOUtils.toUTF8Bytes("line 2\n"));
        out1.flush();
        out1.write(IOUtils.toUTF8Bytes("line 3\n"));
        out1.close();
        // check document content
        workDoc.refresh();
        String content1 = getStringFromContentStream(workDoc.getContentStream());
        f = createResult(FAILURE, "Overwrite OutputStream: wrong content!");
        addResult(assertEquals("line 1\nline 2\nline 3\n", content1, null, f));
        // create an append OutputStream
        OutputStream out2 = workDoc.createAppendOutputStream(bufferSize);
        out2.write(IOUtils.toUTF8Bytes("line 4\n"));
        out2.write(IOUtils.toUTF8Bytes("line 5\n"));
        out2.flush();
        out2.write(IOUtils.toUTF8Bytes("line 6\n"));
        out2.close();
        // check document content
        workDoc.refresh();
        String content2 = getStringFromContentStream(workDoc.getContentStream());
        f = createResult(FAILURE, "Overwrite OutputStream: wrong content!");
        addResult(assertEquals("line 1\nline 2\nline 3\nline 4\nline 5\nline 6\n", content2, null, f));
    } catch (IOException e) {
        addResult(createResult(UNEXPECTED_EXCEPTION, "Appending content via an OutputStream failed! Exception: " + e.getMessage(), e, false));
    } finally {
        // cancel a possible check out
        if (checkedout) {
            workDoc.cancelCheckOut();
        }
        // remove the document
        deleteObject(doc);
    }
}
Also used : DocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Document(org.apache.chemistry.opencmis.client.api.Document)

Example 90 with Document

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

the class VerSmokeForCustomTypeTestWithoutContentStream method run.

public void run(Session session) {
    DocumentTypeDefinition docType = null;
    try {
        // create folder and document
        Folder testFolder = createTestFolder(session);
        ObjectType parentType = session.getTypeDefinition(getDocumentTestTypeId());
        docType = createTypeWithProperties(session, parentType);
        Document doc = testFolder.createDocument(getProperties("versiondeletetest", docType), null, null, null, null, null, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, doc, getAllProperties(doc), "Document spec compliance"));
        if (!docType.isVersionable()) {
            addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
            doc.delete(true);
            return;
        }
        // gather properties for later
        String[] propertiesToCheck = new String[doc.getType().getPropertyDefinitions().size()];
        int i = 0;
        for (String propId : doc.getType().getPropertyDefinitions().keySet()) {
            propertiesToCheck[i++] = propId;
        }
        Map<String, Object> writableProperties = new HashMap<String, Object>();
        for (Property<?> property : doc.getProperties()) {
            if (property.getDefinition().getUpdatability() == Updatability.READWRITE) {
                writableProperties.put(property.getId(), property.getValue());
            }
        }
        // check out
        ObjectId pwcId = doc.checkOut();
        Document pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 1"));
        checkCheckedOut(pwc);
        // check version series
        addResult(checkVersionSeries(session, pwc.getAllVersions(SELECT_ALL_NO_CACHE_OC), propertiesToCheck, "Test version series after check out"));
        // cancel checkout
        pwc.cancelCheckOut();
        doc.refresh();
        checkCheckedIn(doc);
        // check out again
        pwcId = doc.checkOut();
        pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 2"));
        checkCheckedOut(pwc);
        // check in
        ObjectId newVersionId = pwc.checkIn(true, null, null, "Test Version 2");
        Document newVersion = (Document) session.getObject(newVersionId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, newVersion, getAllProperties(newVersion), "New version compliance"));
        checkCheckedIn(newVersion);
        // check version history
        List<Document> versions = newVersion.getAllVersions(SELECT_ALL_NO_CACHE_OC);
        CmisTestResultImpl f = createResult(FAILURE, "Version series should have 2 versions but has " + versions.size() + "!");
        addResult(assertEquals(2, versions.size(), null, f));
        if (!versions.isEmpty()) {
            f = createResult(FAILURE, "Version history order is incorrect! The first version should be the new version.");
            addResult(assertEquals(newVersion.getId(), versions.get(0).getId(), null, f));
            f = createResult(FAILURE, "The new version should be the latest version, but cmis:isLatestVersion is not TRUE.");
            addResult(assertEquals(true, versions.get(0).isLatestVersion(), null, f));
            f = createResult(FAILURE, "The new version should be the latest major version, but cmis:isLatestMajorVersion is not TRUE.");
            addResult(assertEquals(true, versions.get(0).isLatestMajorVersion(), null, f));
        }
        if (versions.size() > 1) {
            f = createResult(FAILURE, "Version history order is incorrect! The second version should be the origin document.");
            addResult(assertEquals(doc.getId(), versions.get(1).getId(), null, f));
        }
        // check version series
        addResult(checkVersionSeries(session, versions, propertiesToCheck, "Test version series after check in"));
        // check out again
        pwcId = newVersion.checkOut();
        pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 3"));
        checkCheckedOut(pwc);
        // check in giving back all updateable properties
        ObjectId thirdVersionId = pwc.checkIn(true, writableProperties, getContentStream(), "Test Version 3");
        Document thirdVersion = (Document) session.getObject(thirdVersionId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, thirdVersion, getAllProperties(thirdVersion), "New version compliance"));
        // check out again
        pwcId = thirdVersion.checkOut();
        pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 4"));
        checkCheckedOut(pwc);
        // check in giving a new content stream
        String fourthContent = "new content";
        byte[] fourthContentBytes = IOUtils.toUTF8Bytes(fourthContent);
        ContentStream fourthContentStream = new ContentStreamImpl("version4", BigInteger.valueOf(fourthContentBytes.length), "text/plain", new ByteArrayInputStream(fourthContentBytes));
        ObjectId fourthVersionId = pwc.checkIn(true, null, fourthContentStream, "Test Version 5");
        Document fourthVersion = (Document) session.getObject(fourthVersionId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, fourthVersion, getAllProperties(fourthVersion), "New version compliance"));
        checkCheckedIn(fourthVersion);
        // check out again
        pwcId = fourthVersion.checkOut();
        pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 5"));
        checkCheckedOut(pwc);
        // check in giving properties and a new content stream
        String fifthContent = "brand-new content";
        byte[] fifthContentBytes = IOUtils.toUTF8Bytes(fifthContent);
        ContentStream fifthContentStream = new ContentStreamImpl("version5", BigInteger.valueOf(fifthContentBytes.length), "text/plain", new ByteArrayInputStream(fifthContentBytes));
        ObjectId fifthVersionId = pwc.checkIn(true, writableProperties, fifthContentStream, "Test Version 5");
        Document fifthVersion = (Document) session.getObject(fifthVersionId, SELECT_ALL_NO_CACHE_OC);
        addResult(checkObject(session, fifthVersion, getAllProperties(fifthVersion), "New version compliance"));
        checkCheckedIn(fifthVersion);
        // test the latest version
        Document latest = session.getLatestDocumentVersion(doc, SELECT_ALL_NO_CACHE_OC);
        f = createResult(FAILURE, "getObjectOfLatestVersion() did not return the expected version!");
        addResult(assertEquals(fifthVersion.getId(), latest.getId(), null, f));
        // repository
        try {
            pwcId = doc.checkOut();
            pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
            pwc.cancelCheckOut();
            addResult(createInfoResult("Repository allows check out on a version that is not the latest version."));
        } catch (CmisBaseException e) {
            addResult(createInfoResult("Repository only support check out on the latest version."));
        }
        // remove the document
        deleteObject(doc);
        // test if all versions have been deleted
        f = createResult(FAILURE, "Version 2 has not been deleted!");
        addResult(assertIsFalse(session.exists(newVersion), null, f));
        f = createResult(FAILURE, "Version 3 has not been deleted!");
        addResult(assertIsFalse(session.exists(thirdVersion), null, f));
        f = createResult(FAILURE, "Version 4 has not been deleted!");
        addResult(assertIsFalse(session.exists(fourthVersion), null, f));
        f = createResult(FAILURE, "Version 5 has not been deleted!");
        addResult(assertIsFalse(session.exists(fifthVersion), null, f));
    } finally {
        deleteTestFolder();
        deleteType(session, docType.getId());
    }
}
Also used : CmisTestResultImpl(org.apache.chemistry.opencmis.tck.impl.CmisTestResultImpl) ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) DocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition) HashMap(java.util.HashMap) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) ObjectType(org.apache.chemistry.opencmis.client.api.ObjectType) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException)

Aggregations

Document (org.apache.chemistry.opencmis.client.api.Document)110 Folder (org.apache.chemistry.opencmis.client.api.Folder)64 HashMap (java.util.HashMap)43 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)43 CmisTestResult (org.apache.chemistry.opencmis.tck.CmisTestResult)40 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)37 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)32 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)29 ByteArrayInputStream (java.io.ByteArrayInputStream)27 ObjectId (org.apache.chemistry.opencmis.client.api.ObjectId)25 DocumentTypeDefinition (org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition)23 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)21 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)20 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 ArrayList (java.util.ArrayList)19 Session (org.apache.chemistry.opencmis.client.api.Session)19 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)19 Test (org.junit.Test)19 NodeRef (org.alfresco.service.cmr.repository.NodeRef)17