use of org.apache.chemistry.opencmis.client.api.Document in project alfresco-remote-api by Alfresco.
the class TestCMIS method testSecondaryTypes.
@Test
public void testSecondaryTypes() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person1 = network1.createUser(personInfo);
String person1Id = person1.getId();
final String siteName = "site" + System.currentTimeMillis();
TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {
@Override
public NodeRef doWork() throws Exception {
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
return folderNodeRef;
}
}, person1Id, network1.getId());
// Create a document...
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
Folder docLibrary = (Folder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
String name = "mydoc-" + GUID.generate() + ".txt";
final List<String> secondaryTypes = new ArrayList<String>();
secondaryTypes.add("P:cm:summarizable");
Map<String, Object> properties = new HashMap<String, Object>();
{
// create a document with 2 aspects
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
properties.put("cm:summary", "My summary");
properties.put(PropertyIds.NAME, name);
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
Document doc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
{
// check that the secondary types and properties are present
{
checkSecondaryTypes(doc, Collections.singleton("P:cm:summarizable"), null);
String summary = (String) doc.getProperty("cm:summary").getFirstValue();
assertEquals("My summary", summary);
}
{
doc = (Document) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary/" + name);
checkSecondaryTypes(doc, Collections.singleton("P:cm:summarizable"), null);
String summary = (String) doc.getProperty("cm:summary").getFirstValue();
assertEquals("My summary", summary);
}
}
// update property and check
{
properties = new HashMap<String, Object>();
{
properties.put("cm:summary", "My updated summary");
}
doc.updateProperties(properties);
{
doc = (Document) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary/" + name);
checkSecondaryTypes(doc, Collections.singleton("P:cm:summarizable"), null);
String summary = (String) doc.getProperty("cm:summary").getFirstValue();
assertEquals("My updated summary", summary);
}
{
checkSecondaryTypes(doc, Collections.singleton("P:cm:summarizable"), null);
String summary = (String) doc.getProperty("cm:summary").getFirstValue();
assertEquals("My updated summary", summary);
}
}
}
use of org.apache.chemistry.opencmis.client.api.Document in project alfresco-remote-api by Alfresco.
the class TestCMIS method aPrivateCopyShouldAllowTheAdditionOfAspects_CMIS_1_1_Version.
/**
* <p>Related to REPO-4613.</p>
* <p>A checkout should not lock the private working copy.</p>
* <p>Adding aspects or properties to a pwc should remain possible after a checkout.</p>
* @throws Exception
*/
@Test
public void aPrivateCopyShouldAllowTheAdditionOfAspects_CMIS_1_1_Version() throws Exception {
final String aspectName = "P:cm:summarizable";
final String propertyName = "cm:summary";
final String propertyValue = "My summary";
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person1 = network1.createUser(personInfo);
String person1Id = person1.getId();
final String siteName = "site" + System.currentTimeMillis();
TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {
@Override
public NodeRef doWork() throws Exception {
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
return folderNodeRef;
}
}, person1Id, network1.getId());
// Create a document...
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
Folder folder = (Folder) cmisSession.getObjectByPath(String.format(DOCUMENT_LIBRARY_PATH_PATTERN, siteName));
String fileName = String.format(TEST_DOCUMENT_NAME_PATTERN, GUID.generate());
// Create a document...
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
HashMap<String, Object> props = new HashMap<>();
props.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
props.put(PropertyIds.NAME, fileName);
final ContentStream cs = new ContentStreamImpl(fileName, "text/plain", "This is just a test");
final Document document = folder.createDocument(props, cs, VersioningState.MAJOR);
ObjectId pwcObjectId = document.checkOut();
CmisObject cmisObject = cmisSession.getObject(pwcObjectId.getId());
final Document pwc = (Document) cmisObject;
List<Object> aspects = pwc.getProperty(PropertyIds.SECONDARY_OBJECT_TYPE_IDS).getValues();
// asserts that we have the right aspect for the private working copy
assertTrue(aspects.contains("P:cm:workingcopy"));
aspects.add(aspectName);
props = new HashMap<>();
props.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, aspects);
props.put(propertyName, propertyValue);
pwc.updateProperties(props);
final ObjectId id = pwc.checkIn(true, null, null, "CheckIn comment");
Document checkedInDocument = (Document) cmisSession.getObject(id.getId());
List<String> secondaryTypeIds = checkedInDocument.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
// asserts the new aspect has been added to the original copy, via the check in from the private copy
assertTrue(secondaryTypeIds.contains(aspectName));
assertEquals(checkedInDocument.getPropertyValue(propertyName), propertyValue);
}
use of org.apache.chemistry.opencmis.client.api.Document in project alfresco-remote-api by Alfresco.
the class TestCMIS method testMNT_10687.
/* MNT-10687 related test - appendContent to PWC CMIS 1.1 */
@Test
public void testMNT_10687() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person1 = network1.createUser(personInfo);
String person1Id = person1.getId();
final String siteName = "site" + System.currentTimeMillis();
TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {
@Override
public NodeRef doWork() throws Exception {
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
return folderNodeRef;
}
}, person1Id, network1.getId());
// Create a document...
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
Folder docLibrary = (Folder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
String name = "mydoc-" + GUID.generate() + ".txt";
Map<String, Object> properties = new HashMap<String, Object>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
/* Create document */
Document doc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
/* Checkout document */
ObjectId pwcId = doc.checkOut();
Document pwc = (Document) cmisSession.getObject(pwcId.getId());
/* append content to PWC */
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent(" and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
pwc.appendContentStream(fileContent, true);
pwc.checkIn(false, null, null, "Check In");
ContentStream contentStream = doc.getObjectOfLatestVersion(false).getContentStream();
InputStream in = contentStream.getStream();
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer, "UTF-8");
String content = writer.toString();
assertEquals("Ipsum and so on", content);
}
use of org.apache.chemistry.opencmis.client.api.Document in project alfresco-remote-api by Alfresco.
the class TestCMIS method testScenario1.
/**
* Tests CMIS and non-CMIS public api interactions
*/
@SuppressWarnings("deprecation")
@Test
public void testScenario1() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
Iterator<String> personIt = network1.getPersonIds().iterator();
final String person = personIt.next();
assertNotNull(person);
Sites sitesProxy = publicApiClient.sites();
Comments commentsProxy = publicApiClient.comments();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_10, AlfrescoObjectFactoryImpl.class.getName());
ListResponse<MemberOfSite> sites = sitesProxy.getPersonSites(person, null);
assertTrue(sites.getList().size() > 0);
MemberOfSite siteMember = sites.getList().get(0);
String siteId = siteMember.getSite().getSiteId();
Folder documentLibrary = (Folder) cmisSession.getObjectByPath("/Sites/" + siteId + "/documentLibrary");
System.out.println("documentLibrary id = " + documentLibrary.getId());
Map<String, String> fileProps = new HashMap<String, String>();
{
fileProps.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
fileProps.put(PropertyIds.NAME, "mydoc-" + GUID.generate() + ".txt");
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
Document doc = documentLibrary.createDocument(fileProps, fileContent, VersioningState.MAJOR);
System.out.println("Document id = " + doc.getId());
Comment c = commentsProxy.createNodeComment(doc.getId(), new Comment("comment title 1", "comment 1"));
System.out.println("Comment = " + c);
// Now lock the document
String nodeRefStr = (String) doc.getPropertyValue("alfcmis:nodeRef");
final NodeRef nodeRef = new NodeRef(nodeRefStr);
final TenantRunAsWork<Void> runAsWork = new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
lockService.lock(nodeRef, LockType.WRITE_LOCK);
return null;
}
};
RetryingTransactionCallback<Void> txnWork = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
TenantUtil.runAsUserTenant(runAsWork, "bob", network1.getId());
return null;
}
};
transactionHelper.doInTransaction(txnWork);
// Now attempt to update the document's metadata
try {
doc.delete();
} catch (CmisUpdateConflictException e) {
// Expected: ACE-762 BM-0012: NodeLockedException not handled by CMIS
}
}
use of org.apache.chemistry.opencmis.client.api.Document in project alfresco-remote-api by Alfresco.
the class TestCMIS method testAspects.
@Test
@Ignore("Quarantined since 24 Sep 2018. TestCMIS.testAspects:1546->checkSecondaryTypes:492 Missing secondary types: [P:cm:titled, P:sys:localized]")
public void testAspects() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person1 = network1.createUser(personInfo);
String person1Id = person1.getId();
final List<NodeRef> folders = new ArrayList<NodeRef>();
final List<NodeRef> documents = new ArrayList<NodeRef>();
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
String siteName = "site" + System.currentTimeMillis();
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
folders.add(folderNodeRef);
for (int i = 0; i < 3; i++) {
name = GUID.generate();
NodeRef docNodeRef = repoService.createDocument(folderNodeRef, name, "test content");
assertFalse(repoService.getAspects(docNodeRef).contains(ContentModel.ASPECT_TITLED));
documents.add(docNodeRef);
}
return null;
}
}, person1Id, network1.getId());
final NodeRef doc1NodeRef = documents.get(0);
final NodeRef doc2NodeRef = documents.get(1);
final NodeRef doc3NodeRef = documents.get(2);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession atomCmisSession10 = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_10, AlfrescoObjectFactoryImpl.class.getName());
CmisSession atomCmisSession11 = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
CmisSession browserCmisSession11 = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11);
// Test that adding aspects works for both 1.0 and 1.1
// 1.0
{
AlfrescoDocument doc = (AlfrescoDocument) atomCmisSession10.getObject(doc1NodeRef.getId());
doc = (AlfrescoDocument) doc.addAspect("P:cm:titled");
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc1NodeRef);
assertTrue("Missing aspect in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
return null;
}
}, person1Id, network1.getId());
doc.removeAspect("P:cm:titled");
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc1NodeRef);
assertFalse("Unexpected aspect in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
return null;
}
}, person1Id, network1.getId());
}
// 1.1 atom (secondary types)
{
final Document doc = (Document) atomCmisSession11.getObject(doc2NodeRef.getId());
final List<SecondaryType> secondaryTypesList = doc.getSecondaryTypes();
final List<String> secondaryTypes = new ArrayList<String>();
if (secondaryTypesList != null) {
for (SecondaryType secondaryType : secondaryTypesList) {
secondaryTypes.add(secondaryType.getId());
}
}
secondaryTypes.add("P:sys:temporary");
secondaryTypes.add("P:cm:titled");
Map<String, Object> properties = new HashMap<String, Object>();
{
// create a document with 2 secondary types
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
}
Document doc1 = (Document) doc.updateProperties(properties);
checkSecondaryTypes(doc1, new HashSet<String>(Arrays.asList(new String[] { "P:sys:temporary", "P:cm:titled" })), null);
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc2NodeRef);
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TEMPORARY));
return null;
}
}, person1Id, network1.getId());
secondaryTypes.add("P:cm:author");
properties = new HashMap<String, Object>();
{
// create a document with 2 secondary types
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
}
Document doc2 = (Document) doc1.updateProperties(properties);
checkSecondaryTypes(doc2, new HashSet<String>(Arrays.asList(new String[] { "P:sys:temporary", "P:cm:titled", "P:cm:author" })), null);
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc2NodeRef);
String title = (String) repoService.getProperty(doc2NodeRef, ContentModel.PROP_TITLE);
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_AUTHOR));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TEMPORARY));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
assertEquals(null, title);
return null;
}
}, person1Id, network1.getId());
// remove a secondary type
secondaryTypes.remove("P:cm:titled");
properties = new HashMap<String, Object>();
{
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
}
Document doc3 = (Document) doc2.updateProperties(properties);
checkSecondaryTypes(doc3, new HashSet<String>(Arrays.asList(new String[] { "P:sys:temporary", "P:cm:author" })), new HashSet<String>(Arrays.asList(new String[] { "P:cm:titled" })));
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc2NodeRef);
String title = (String) repoService.getProperty(doc2NodeRef, ContentModel.PROP_TITLE);
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_AUTHOR));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TEMPORARY));
assertFalse("Unexpected aspect in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
assertEquals(null, title);
return null;
}
}, person1Id, network1.getId());
}
// 1.1 browser (secondary types)
{
Document doc = (Document) browserCmisSession11.getObject(doc3NodeRef.getId());
final List<SecondaryType> secondaryTypesList = doc.getSecondaryTypes();
final List<String> secondaryTypes = new ArrayList<String>();
if (secondaryTypesList != null) {
for (SecondaryType secondaryType : secondaryTypesList) {
secondaryTypes.add(secondaryType.getId());
}
}
secondaryTypes.add("P:sys:temporary");
secondaryTypes.add("P:cm:titled");
Map<String, Object> properties = new HashMap<String, Object>();
{
// create a document with 2 secondary types
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
}
Document doc1 = (Document) doc.updateProperties(properties);
checkSecondaryTypes(doc1, new HashSet<String>(Arrays.asList(new String[] { "P:sys:temporary", "P:cm:titled" })), null);
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc3NodeRef);
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TEMPORARY));
return null;
}
}, person1Id, network1.getId());
secondaryTypes.add("P:cm:author");
properties = new HashMap<String, Object>();
{
// create a document with 2 secondary types
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
}
Document doc2 = (Document) doc1.updateProperties(properties);
checkSecondaryTypes(doc2, new HashSet<String>(Arrays.asList(new String[] { "P:sys:temporary", "P:cm:titled", "P:cm:author" })), null);
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc3NodeRef);
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TEMPORARY));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_AUTHOR));
return null;
}
}, person1Id, network1.getId());
secondaryTypes.remove("P:cm:titled");
properties = new HashMap<String, Object>();
{
// create a document with 2 secondary types
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);
}
Document doc3 = (Document) doc2.updateProperties(properties);
checkSecondaryTypes(doc3, new HashSet<String>(Arrays.asList(new String[] { "P:sys:temporary", "P:cm:author" })), new HashSet<String>(Arrays.asList(new String[] { "P:cm:titled" })));
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
Set<QName> aspects = repoService.getAspects(doc3NodeRef);
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_AUTHOR));
assertTrue("Missing aspects in current set " + aspects, aspects.contains(ContentModel.ASPECT_TEMPORARY));
assertFalse("Unexpected aspect in current set " + aspects, aspects.contains(ContentModel.ASPECT_TITLED));
return null;
}
}, person1Id, network1.getId());
}
}
Aggregations