use of org.apache.chemistry.opencmis.client.api.ObjectId in project copper-cms by PogeyanOSS.
the class CreateBigDocument method run.
@Override
public void run(Session session) {
CmisTestResult f;
// create a test folder
Folder testFolder = createTestFolder(session);
try {
String name = "bigdoc";
String objectTypeId = getDocumentTestTypeId();
String mimetype = "application/octet-stream";
// 10 MiB
final long size = 10 * 1024 * 1024;
InputStream in = new InputStream() {
private int counter = -1;
@Override
public int read() throws IOException {
counter++;
if (counter >= size) {
return -1;
}
return '0' + (counter / 10);
}
};
// create stream and properties
ContentStream contentStream = session.getObjectFactory().createContentStream(name, size, mimetype, in);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, name);
properties.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
// check type
TypeDefinition type = session.getTypeDefinition(objectTypeId);
if (!(type instanceof DocumentTypeDefinition)) {
addResult(createResult(FAILURE, "Type is not a document type! Type: " + objectTypeId, true));
return;
}
DocumentTypeDefinition docType = (DocumentTypeDefinition) type;
VersioningState versioningState = (Boolean.TRUE.equals(docType.isVersionable()) ? VersioningState.MAJOR : VersioningState.NONE);
// create and fetch the document
ObjectId id = session.createDocument(properties, testFolder, contentStream, versioningState);
Document doc = (Document) session.getObject(id, SELECT_ALL_NO_CACHE_OC);
// check the new document
addResult(checkObject(session, doc, getAllProperties(doc), "New document object spec compliance"));
// check the size
f = createResult(FAILURE, "Content stream length doesn't match the uploaded content!", true);
assertEquals(size, doc.getContentStreamLength(), null, f);
// delete it
// doc.delete(true);
} finally {
// delete the test folder
// deleteTestFolder();
}
}
use of org.apache.chemistry.opencmis.client.api.ObjectId 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."));
}
Aggregations