use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.
the class TestCMIS method getTestSite.
private TestSite getTestSite() throws Exception {
if (testSite == null) {
getTestNetwork();
getTestPersonId();
String siteName = "site" + System.currentTimeMillis();
testSite = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {
@Override
public TestSite doWork() throws Exception {
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PUBLIC);
return testNetwork.createSite(siteInfo);
}
}, testPersonId, testNetwork.getId());
}
return testSite;
}
use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork 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.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.
the class TestCustomConstraint method testCreateConstraintAndAddToProperty.
@Test
public void testCreateConstraintAndAddToProperty() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testModelConstraint" + System.currentTimeMillis();
final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
// Create RegEx constraint
String regExConstraintName = "testFileNameRegEx" + System.currentTimeMillis();
CustomModelConstraint regExConstraint = new CustomModelConstraint();
regExConstraint.setName(regExConstraintName);
regExConstraint.setType("REGEX");
regExConstraint.setTitle("test RegEx title");
regExConstraint.setDescription("test RegEx desc");
// Create the RegEx constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
parameters.add(buildNamedValue("requiresMatch", "false"));
// Add the parameters into the constraint
regExConstraint.setParameters(parameters);
// Create constraint as a Model Administrator
post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 201);
// Retrieve the created constraint
HttpResponse response = getSingle("cmm/" + modelName + "/constraints", regExConstraintName, 200);
CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
// Retrieve all the model's constraints
Paging paging = getPaging(0, Integer.MAX_VALUE);
response = getAll("cmm/" + modelName + "/constraints", paging, 200);
List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
assertEquals(1, constraints.size());
// Create aspect
String aspectName = "testAspect1" + System.currentTimeMillis();
createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
// Update the Aspect by adding property
CustomAspect payload = new CustomAspect();
payload.setName(aspectName);
final String aspectPropName = "testAspect1Prop1" + System.currentTimeMillis();
CustomModelProperty aspectProp = new CustomModelProperty();
aspectProp.setName(aspectPropName);
aspectProp.setTitle("property title");
aspectProp.setDataType("d:text");
// Add the constraint ref
aspectProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(aspectProp);
payload.setProperties(props);
// Create the property
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 200);
// Activate the model
CustomModel updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.ACTIVE);
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
// Retrieve all the model's constraints
// Test to see if the API took care of duplicate constraints when referencing a constraint within a property.
response = getAll("cmm/" + modelName + "/constraints", paging, 200);
constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
assertEquals(1, constraints.size());
// Test RegEx constrain enforcement
{
final NodeService nodeService = repoService.getNodeService();
final QName aspectQName = QName.createQName("{" + namespacePair.getFirst() + "}" + aspectName);
TestNetwork testNetwork = getTestFixture().getRandomNetwork();
TestPerson person = testNetwork.createUser();
final String siteName = "site" + System.currentTimeMillis();
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
NodeRef nodeRef = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "Test Doc", "Test Content");
nodeService.addAspect(nodeRef, aspectQName, null);
assertTrue(nodeService.hasAspect(nodeRef, aspectQName));
try {
QName propQName = QName.createQName("{" + namespacePair.getFirst() + "}" + aspectPropName);
nodeService.setProperty(nodeRef, propQName, "Invalid$Char.");
fail("Invalid property value. Should have caused integrity violations.");
} catch (Exception e) {
// Expected
}
// Permanently remove model from repository
nodeService.addAspect(nodeRef, ContentModel.ASPECT_TEMPORARY, null);
nodeService.deleteNode(nodeRef);
return null;
}
}, person.getId(), testNetwork.getId());
}
setRequestContext(customModelAdmin);
// Deactivate the model
updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.DRAFT);
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
// Test update the namespace prefix (test to see if the API updates the constraints refs with this new prefix)
CustomModel updateModelPayload = new CustomModel();
String modifiedPrefix = namespacePair.getSecond() + "Modified";
updateModelPayload.setNamespacePrefix(modifiedPrefix);
updateModelPayload.setNamespaceUri(namespacePair.getFirst());
response = put("cmm", modelName, RestApiUtil.toJsonAsString(updateModelPayload), null, 200);
CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(modifiedPrefix, returnedModel.getNamespacePrefix());
assertEquals("The namespace URI shouldn't have changed.", namespacePair.getFirst(), returnedModel.getNamespaceUri());
// Test update the namespace URI
updateModelPayload = new CustomModel();
updateModelPayload.setNamespacePrefix(modifiedPrefix);
String modifiedURI = namespacePair.getFirst() + "Modified";
updateModelPayload.setNamespaceUri(modifiedURI);
response = put("cmm", modelName, RestApiUtil.toJsonAsString(updateModelPayload), null, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(modifiedURI, returnedModel.getNamespaceUri());
assertEquals("The namespace prefix shouldn't have changed.", modifiedPrefix, returnedModel.getNamespacePrefix());
}
use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.
the class TestPersonSites method testPersonSites.
@Test
public void testPersonSites() throws Exception {
Set<MemberOfSite> personSites = new TreeSet<MemberOfSite>();
// Get last site for use with personRemoveSite
TestSite personRemoveSite = sites.get(sites.size() - 1);
sites.remove(sites.size() - 1);
personSites.addAll(network1.getSiteMemberships(person11.getId()));
// Create some sites
personSites.addAll(TenantUtil.runAsUserTenant(new TenantRunAsWork<List<MemberOfSite>>() {
@Override
public List<MemberOfSite> doWork() throws Exception {
List<MemberOfSite> expectedSites = new ArrayList<MemberOfSite>();
TestSite site = network1.createSite(SiteVisibility.PRIVATE);
expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager));
site = network1.createSite(SiteVisibility.PUBLIC);
expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager));
site = network1.createSite(SiteVisibility.MODERATED);
expectedSites.add(new MemberOfSite(site, SiteRole.SiteManager));
return expectedSites;
}
}, person11.getId(), network1.getId()));
personSites.addAll(TenantUtil.runAsUserTenant(new TenantRunAsWork<List<MemberOfSite>>() {
@Override
public List<MemberOfSite> doWork() throws Exception {
List<MemberOfSite> expectedSites = new ArrayList<MemberOfSite>();
TestSite site = network1.createSite(SiteVisibility.PRIVATE);
site.inviteToSite(person11.getId(), SiteRole.SiteConsumer);
expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer));
site = network1.createSite(SiteVisibility.PUBLIC);
site.inviteToSite(person11.getId(), SiteRole.SiteConsumer);
expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer));
site = network1.createSite(SiteVisibility.MODERATED);
site.inviteToSite(person11.getId(), SiteRole.SiteConsumer);
expectedSites.add(new MemberOfSite(site, SiteRole.SiteConsumer));
return expectedSites;
}
}, person12.getId(), network1.getId()));
final List<MemberOfSite> expectedSites = new ArrayList<MemberOfSite>(personSites);
Sites sitesProxy = publicApiClient.sites();
// unknown user
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getPersonSites(GUID.generate(), null);
fail("");
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// Test Case cloud-2200
// Test Case cloud-2213
// user should be able to list their sites
{
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size());
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
ListResponse<MemberOfSite> resp = sitesProxy.getPersonSites(person11.getId(), createParams(paging, null));
checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
{
int skipCount = 2;
int maxItems = 8;
Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size());
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
ListResponse<MemberOfSite> resp = sitesProxy.getPersonSites(person11.getId(), createParams(paging, null));
checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
// "-me-" user
{
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size());
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
ListResponse<MemberOfSite> resp = sitesProxy.getPersonSites(org.alfresco.rest.api.People.DEFAULT_USER, createParams(paging, null));
checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
// a user in another tenant should not be able to list a user's sites
try {
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), null);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
sitesProxy.getPersonSites(person11.getId(), createParams(paging, null));
fail("");
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
}
// Test case cloud-1488
{
MemberOfSite memberOfSite = expectedSites.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
MemberOfSite ret = sitesProxy.getPersonSite(person11.getId(), memberOfSite.getSiteId());
memberOfSite.expected(ret);
}
try {
MemberOfSite memberOfSite = expectedSites.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getPersonSite(GUID.generate(), memberOfSite.getSiteId());
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getPersonSite(person11.getId(), GUID.generate());
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// unknown person id
try {
MemberOfSite memberOfSite = expectedSites.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getPersonSite(GUID.generate(), memberOfSite.getSiteId());
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getPersonSite(person11.getId(), GUID.generate());
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
{
// Tests removing a person from the site
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.remove("people", person11.getId(), "sites", personRemoveSite.getSiteId(), "Unable to DELETE a person site");
try {
sitesProxy.getPersonSite(person11.getId(), personRemoveSite.getSiteId());
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
}
// Not allowed methods
try {
MemberOfSite memberOfSite = expectedSites.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.create("people", person11.getId(), "sites", memberOfSite.getSiteId(), null, "Unable to POST to a person site");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.create("people", person11.getId(), "sites", null, null, "Unable to POST to person sites");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
MemberOfSite memberOfSite = expectedSites.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.update("people", person11.getId(), "sites", memberOfSite.getSiteId(), null, "Unable to PUT a person site");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.update("people", person11.getId(), "sites", null, null, "Unable to PUT person sites");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.remove("people", person11.getId(), "sites", null, "Unable to DELETE person sites");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
}
use of org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork in project alfresco-remote-api by Alfresco.
the class TestPersonSites method getSiteMembershipsForPersonAndNetwork.
/**
* Retrieves the site memberships associated to a user.
*
* @param paging
* The paging object
* @param params
* Public api parameters map.
* @param person
* The test person.
* @param network
* The test network.
* @return The site memberships associated to the give user.
* @throws Exception
*/
private ListResponse<MemberOfSite> getSiteMembershipsForPersonAndNetwork(final Paging paging, Map<String, String> params, TestPerson person, TestNetwork network, boolean runAsUserTenant) throws Exception {
final Sites sitesProxy = publicApiClient.sites();
publicApiClient.setRequestContext(new RequestContext(network.getId(), person.getId()));
ListResponse<MemberOfSite> resp;
if (runAsUserTenant) {
// get memberships
resp = TenantUtil.runAsUserTenant(new TenantRunAsWork<ListResponse<MemberOfSite>>() {
@Override
public ListResponse<MemberOfSite> doWork() throws Exception {
ListResponse<MemberOfSite> resp = sitesProxy.getPersonSites(person.getId(), createParams(paging, params));
return resp;
}
}, person.getId(), network.getId());
} else {
resp = sitesProxy.getPersonSites(person.getId(), createParams(paging, params));
}
return resp;
}
Aggregations