use of org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession in project alfresco-remote-api by Alfresco.
the class TestCMIS method testDeleteNonCurrentVersion.
/**
* Test delete version on versions other than latest (most recent) version (MNT-17228)
*/
@Test
public void testDeleteNonCurrentVersion() 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 person = network1.createUser(personInfo);
String personId = person.getId();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
Folder homeFolder = (Folder) cmisSession.getObjectByPath("/User Homes/" + personId);
assertNotNull(homeFolder.getId());
// Create a document
String name = String.format(TEST_DOCUMENT_NAME_PATTERN, GUID.generate());
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();
ByteArrayInputStream stream = new ByteArrayInputStream(GUID.generate().getBytes());
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(stream);
Document doc = homeFolder.createDocument(properties, fileContent, VersioningState.MAJOR);
String versionLabel = doc.getVersionLabel();
assertEquals("1.0", versionLabel);
Document docVersionToDelete = null;
Document latestDoc = doc;
int cnt = 4;
for (int i = 1; i <= cnt; i++) {
// Update content to create new versions (1.1, 1.2, 1.3, 1.4)
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on and so on " + i);
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
latestDoc.setContentStream(fileContent, true);
latestDoc = latestDoc.getObjectOfLatestVersion(false);
versionLabel = latestDoc.getVersionLabel();
assertEquals("1." + i, versionLabel);
assertEquals(1 + i, cmisSession.getAllVersions(latestDoc.getId()).size());
if (i == 2) {
// ie. 1.2
docVersionToDelete = latestDoc;
}
}
// Test delete with a user without permissions
String username2 = "user" + System.currentTimeMillis();
PersonInfo person2Info = new PersonInfo(username2, username2, username2, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person2 = network1.createUser(person2Info);
String person2Id = person2.getId();
TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
String nodeId = stripCMISSuffix(doc.getId());
NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeId);
// Give user person2 READ permissions to access the node
permissionService.setPermission(nodeRef, person2Id, PermissionService.READ, true);
return null;
}
}, network1.getId());
// Connect with person2
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2Id));
CmisSession cmisSession2 = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
CmisObject docVersionToDeleteBy2 = cmisSession2.getObject(docVersionToDelete.getId());
try {
// (-) Delete v 1.2 (without DELETE permission)
docVersionToDeleteBy2.delete(false);
fail("Node version was deleted without permissions.");
} catch (CmisPermissionDeniedException ex) {
// expected
}
// (+) Delete v 1.2 (with permission)
docVersionToDelete.delete(false);
// eg. 1.0, 1.2, 1.3, 1.4 (not 1.1)
assertEquals(cnt, cmisSession.getAllVersions(doc.getId()).size());
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession in project alfresco-remote-api by Alfresco.
the class TestCMIS method testMNT10430.
@Test
public void testMNT10430() 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();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
ObjectType objectType = cmisSession.getTypeDefinition("D:testcmis:maDoc");
// try and get the mandatory aspects
List<String> mandatoryAspects = ((AlfrescoType) objectType).getMandatoryAspects();
System.out.println("Mandatory Aspects");
for (String mandatoryAspect : mandatoryAspects) {
System.out.println(mandatoryAspect);
}
assertTrue("The aspects should have P:cm:generalclassifiable", mandatoryAspects.contains("P:cm:generalclassifiable"));
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession in project alfresco-remote-api by Alfresco.
the class TestCMIS method testCreationDate.
/**
* MNT-12680
* The creation date of version should be the same as creation date of the original node
* @throws Exception
*/
@Test
public void testCreationDate() throws Exception {
// create a site
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, "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.PUBLIC);
final TestSite site = network1.createSite(siteInfo);
final NodeRef resNode = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "testdoc.txt", "Test Doc1 Title", "Test Doc1 Description", "Test Content");
return resNode;
}
}, person1Id, network1.getId());
// create a document
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_10, AlfrescoObjectFactoryImpl.class.getName());
AlfrescoFolder docLibrary = (AlfrescoFolder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
Map<String, String> properties = new HashMap<String, String>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, "mydoc-" + GUID.generate() + ".txt");
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("some content");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
Document autoVersionedDoc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
String objectId = autoVersionedDoc.getId();
String bareObjectId = stripCMISSuffix(objectId);
// create versions
for (int i = 0; i < 3; i++) {
Document doc1 = (Document) cmisSession.getObject(bareObjectId);
ObjectId pwcId = doc1.checkOut();
Document pwc = (Document) cmisSession.getObject(pwcId.getId());
ContentStreamImpl contentStream = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent(GUID.generate());
ContentReader reader = writer.getReader();
contentStream.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
contentStream.setStream(reader.getContentInputStream());
}
pwc.checkIn(true, Collections.EMPTY_MAP, contentStream, "checkin " + i);
}
GregorianCalendar cDateFirst = cmisSession.getAllVersions(bareObjectId).get(0).getCreationDate();
GregorianCalendar cDateSecond = cmisSession.getAllVersions(bareObjectId).get(2).getCreationDate();
if (cDateFirst.before(cDateSecond) || cDateFirst.after(cDateSecond)) {
fail("The creation date of version should be the same as creation date of the original node");
}
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession in project alfresco-remote-api by Alfresco.
the class TestCMIS method testVersioning.
/**
* Test that updating properties does not automatically create a new version.
* Test that updating content creates a new version automatically.
*/
@Test
public void testVersioning() 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_10, AlfrescoObjectFactoryImpl.class.getName());
AlfrescoFolder docLibrary = (AlfrescoFolder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
Map<String, String> properties = new HashMap<String, String>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.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());
}
AlfrescoDocument doc = (AlfrescoDocument) docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
String versionLabel = doc.getVersionLabel();
// ...and check that updating its properties does not create a new version
properties = new HashMap<String, String>();
{
properties.put(PropertyIds.DESCRIPTION, GUID.generate());
}
AlfrescoDocument doc1 = (AlfrescoDocument) doc.updateProperties(properties);
doc1 = (AlfrescoDocument) doc1.getObjectOfLatestVersion(false);
String versionLabel1 = doc1.getVersionLabel();
assertTrue(Float.parseFloat(versionLabel) < Float.parseFloat(versionLabel1));
// ...and check that updating its content creates a new version
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
doc1.setContentStream(fileContent, true);
AlfrescoDocument doc2 = (AlfrescoDocument) doc1.getObjectOfLatestVersion(false);
@SuppressWarnings("unused") String versionLabel2 = doc2.getVersionLabel();
assertTrue("Set content stream should create a new version automatically", Float.parseFloat(versionLabel1) < Float.parseFloat(versionLabel2));
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession in project alfresco-remote-api by Alfresco.
the class TestCMIS method testAspects.
@Test
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