diff --git a/source/java/org/alfresco/repo/cmis/rest/CMISScript.java b/source/java/org/alfresco/repo/cmis/rest/CMISScript.java index 85c8574439..b7a54241e4 100644 --- a/source/java/org/alfresco/repo/cmis/rest/CMISScript.java +++ b/source/java/org/alfresco/repo/cmis/rest/CMISScript.java @@ -1219,18 +1219,19 @@ public class CMISScript extends BaseScopableProcessorExtension /** * Gets the list of policy objects currently applied to a target object. * - * @param objectId - * the object id + * @param source + * source node * @param filter * property filter * @throws WebScriptException on error */ - public PagedResults getAppliedPolicies(String objectId, String filter, Page page) + public PagedResults getAppliedPolicies(ScriptNode source, String filter, Page page) { List policies; try { - policies = cmisService.getAppliedPolicies(objectId, filter); + policies = cmisService.getAppliedPolicies((String) cmisService.getProperty(source.getNodeRef(), + CMISDictionaryModel.PROP_OBJECT_ID), filter); } catch (CMISServiceException e) { diff --git a/source/java/org/alfresco/repo/cmis/rest/test/AspectTest.java b/source/java/org/alfresco/repo/cmis/rest/test/AspectTest.java index 7be9effc94..80eb09dc6d 100644 --- a/source/java/org/alfresco/repo/cmis/rest/test/AspectTest.java +++ b/source/java/org/alfresco/repo/cmis/rest/test/AspectTest.java @@ -18,9 +18,7 @@ */ package org.alfresco.repo.cmis.rest.test; -import java.io.Reader; import java.io.StringReader; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -29,22 +27,13 @@ import java.util.Set; import javax.xml.namespace.QName; -import org.alfresco.repo.cmis.rest.AlfrescoCMISExtensionFactory; -import org.alfresco.repo.web.scripts.BaseWebScriptTest; -import org.apache.abdera.Abdera; -import org.apache.abdera.factory.Factory; import org.apache.abdera.i18n.iri.IRI; -import org.apache.abdera.model.Document; import org.apache.abdera.model.Element; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Link; -import org.apache.abdera.model.Service; -import org.apache.abdera.parser.Parser; import org.apache.chemistry.abdera.ext.CMISConstants; import org.apache.chemistry.abdera.ext.CMISObject; import org.apache.chemistry.abdera.ext.CMISProperty; -import org.apache.chemistry.tck.atompub.client.CMISClient; -import org.apache.chemistry.tck.atompub.utils.ResourceLoader; import org.junit.Assert; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; @@ -54,74 +43,11 @@ import org.springframework.extensions.webscripts.TestWebScriptServer.Response; /** * Tests Alfresco CMIS REST API extensions for Aspects. + * + * @author dward */ -public class AspectTest extends BaseWebScriptTest +public class AspectTest extends BaseCMISTest { - private static final QName ELEMENT_PROPERTIES = new QName("http://www.alfresco.org", "properties"); - private static final QName ELEMENT_APPLIED_ASPECTS = new QName("http://www.alfresco.org", "appliedAspects"); - - private static final String URL_CMIS = "/cmis"; - - private Abdera abdera; - private Parser parser; - private Factory factory; - - public AspectTest() { - // construct Abdera Service - abdera = new Abdera(); - factory = abdera.getFactory(); - factory.registerExtension(new AlfrescoCMISExtensionFactory()); - parser = factory.newParser(); - - // construct test templates - localTemplates = new ResourceLoader('/' + AspectTest.class.getPackage().getName().replace('.', '/') + '/'); - - // Create a dummy client. We won't / can't use it to make requests. - cmisClient = new CMISClient(null, null, null, null); - } - private CMISClient cmisClient; - private ResourceLoader localTemplates; - private Service cmisService; - private Entry testCaseFolder; - - @Override - public void setUp() throws Exception - { - super.setUp(); - - setDefaultRunAs("admin"); - - Request req = new GetRequest(URL_CMIS); - Response res = sendRequest(req, 200); - String xml = res.getContentAsString(); - Assert.assertNotNull(xml); - Assert.assertTrue(xml.length() > 0); - cmisService = parse(new StringReader(xml)); - Assert.assertNotNull(cmisService); - IRI rootFolderHREF = cmisClient.getRootCollection(cmisClient.getWorkspace(cmisService)); - Assert.assertNotNull(rootFolderHREF); - String folderName = getClass().getSimpleName() + System.currentTimeMillis() + " - " + getName(); - testCaseFolder = createObject(rootFolderHREF, folderName, "cmis:folder"); - } - - private T parse(Reader doc) { - Document entryDoc = parser.parse(doc); - return entryDoc.getRoot(); - } - - private Entry createObject(IRI parent, String name, String type) throws Exception { - String createFolder = localTemplates.load("AspectTest.createObject.atomentry.xml"); - createFolder = createFolder.replace("${NAME}", name); - createFolder = createFolder.replace("${TYPE}", type); - Request req = new PostRequest(parent.toString(), createFolder, CMISConstants.MIMETYPE_ENTRY); - Response res = sendRequest(req, 201); - Assert.assertNotNull(res); - String xml = res.getContentAsString(); - Entry entry = parse(new StringReader(xml)); - Assert.assertNotNull(entry); - return entry; - } - public void testAspectSet() throws Exception { // create document for checkout @@ -213,16 +139,4 @@ public class AspectTest extends BaseWebScriptTest } } } - - private void assertContains(Set actual, String... expected) - { - Assert.assertTrue(actual.containsAll(Arrays.asList(expected))); - } - - private void assertDoesNotContain(Set actual, String... unexpected) - { - Set copy = new HashSet(actual); - copy.retainAll(Arrays.asList(unexpected)); - Assert.assertTrue(copy.isEmpty()); - } } diff --git a/source/java/org/alfresco/repo/cmis/rest/test/AspectTest.createObject.atomentry.xml b/source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.createObject.atomentry.xml similarity index 100% rename from source/java/org/alfresco/repo/cmis/rest/test/AspectTest.createObject.atomentry.xml rename to source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.createObject.atomentry.xml diff --git a/source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.java b/source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.java new file mode 100644 index 0000000000..738a805dfd --- /dev/null +++ b/source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.cmis.rest.test; + +import java.io.Reader; +import java.io.StringReader; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import javax.xml.namespace.QName; + +import org.alfresco.repo.cmis.rest.AlfrescoCMISExtensionFactory; +import org.alfresco.repo.web.scripts.BaseWebScriptTest; +import org.apache.abdera.Abdera; +import org.apache.abdera.factory.Factory; +import org.apache.abdera.i18n.iri.IRI; +import org.apache.abdera.model.Document; +import org.apache.abdera.model.Element; +import org.apache.abdera.model.Entry; +import org.apache.abdera.model.Service; +import org.apache.abdera.parser.ParseException; +import org.apache.abdera.parser.Parser; +import org.apache.chemistry.abdera.ext.CMISConstants; +import org.apache.chemistry.tck.atompub.client.CMISClient; +import org.apache.chemistry.tck.atompub.utils.ResourceLoader; +import org.junit.Assert; +import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.Request; +import org.springframework.extensions.webscripts.TestWebScriptServer.Response; + +/** + * Base class for Alfresco specific CMIS REST API tests. + * + * @author dward + */ +public abstract class BaseCMISTest extends BaseWebScriptTest +{ + protected static final QName ELEMENT_PROPERTIES = new QName("http://www.alfresco.org", "properties"); + protected static final QName ELEMENT_APPLIED_ASPECTS = new QName("http://www.alfresco.org", "appliedAspects"); + + protected static final String URL_CMIS = "/cmis"; + + protected Abdera abdera; + protected Parser parser; + protected Factory factory; + + public BaseCMISTest() + { + // construct Abdera Service + abdera = new Abdera(); + factory = abdera.getFactory(); + factory.registerExtension(new AlfrescoCMISExtensionFactory()); + parser = factory.newParser(); + + // construct test templates + localTemplates = new ResourceLoader('/' + getClass().getPackage().getName().replace('.', '/') + '/'); + + // Create a dummy client. We won't / can't use it to make requests. + cmisClient = new CMISClient(null, null, null, null); + } + + protected CMISClient cmisClient; + protected ResourceLoader localTemplates; + protected Service cmisService; + protected Entry testCaseFolder; + + @Override + public void setUp() throws Exception + { + super.setUp(); + + setDefaultRunAs("admin"); + + Request req = new GetRequest(URL_CMIS); + Response res = sendRequest(req, 200); + String xml = res.getContentAsString(); + Assert.assertNotNull(xml); + Assert.assertTrue(xml.length() > 0); + cmisService = parse(new StringReader(xml)); + Assert.assertNotNull(cmisService); + IRI rootFolderHREF = cmisClient.getRootCollection(cmisClient.getWorkspace(cmisService)); + Assert.assertNotNull(rootFolderHREF); + String folderName = getClass().getSimpleName() + System.currentTimeMillis() + " - " + getName(); + testCaseFolder = createObject(rootFolderHREF, folderName, "cmis:folder"); + } + + protected T parse(Reader doc) + { + Document entryDoc = parser.parse(doc); + return entryDoc.getRoot(); + } + + protected T fetch(IRI href, Map args) throws Exception + { + Request get = new GetRequest(href.toString()).setArgs(args); + Response res = sendRequest(get, 200); + Assert.assertNotNull(res); + String xml = res.getContentAsString(); + T result = parse(new StringReader(xml)); + Assert.assertNotNull(result); + return result; + } + + protected Entry createObject(IRI parent, String name, String type) throws Exception + { + return createObject(parent, name, type, 201); + } + + protected Entry createObject(IRI parent, String name, String type, int expectedStatus) throws Exception + { + String createObject = localTemplates.load("BaseCMISTest.createObject.atomentry.xml"); + createObject = createObject.replace("${NAME}", name); + createObject = createObject.replace("${TYPE}", type); + Request req = new PostRequest(parent.toString(), createObject, CMISConstants.MIMETYPE_ENTRY); + Response res = sendRequest(req, expectedStatus); + Assert.assertNotNull(res); + try + { + String xml = res.getContentAsString(); + return parse(new StringReader(xml)); + } + catch (ParseException e) + { + return null; + } + } + + protected void assertContains(Set actual, String... expected) + { + Assert.assertTrue(actual.containsAll(Arrays.asList(expected))); + } + + protected void assertDoesNotContain(Set actual, String... unexpected) + { + Set copy = new HashSet(actual); + copy.retainAll(Arrays.asList(unexpected)); + Assert.assertTrue(copy.isEmpty()); + } +} diff --git a/source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.applyPolicy.atomentry.xml b/source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.applyPolicy.atomentry.xml new file mode 100644 index 0000000000..1918046898 --- /dev/null +++ b/source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.applyPolicy.atomentry.xml @@ -0,0 +1,17 @@ + + + urn:uuid:00000000-0000-0000-0000-000000000000 + ${OBJECTID} + 2010-01-01T00:00:00Z + admin + + ${OBJECTID} (summary) + + + + ${OBJECTID} + + + + diff --git a/source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.java b/source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.java new file mode 100644 index 0000000000..6bbf6a8802 --- /dev/null +++ b/source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.cmis.rest.test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.abdera.i18n.iri.IRI; +import org.apache.abdera.model.Entry; +import org.apache.abdera.model.Feed; +import org.apache.abdera.model.Link; +import org.apache.chemistry.abdera.ext.CMISConstants; +import org.apache.chemistry.abdera.ext.CMISTypeDefinition; +import org.junit.Assert; +import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.Request; + +/** + * Tests Alfresco CMIS Policy implementation. + * + * @author dward + */ +public class PolicyTest extends BaseCMISTest +{ + public void testPolicies() throws Exception + { + // Try creating an object with the cmis:policy base type (expect a constraint exception) + Link children = cmisClient.getChildrenLink(testCaseFolder); + createObject(children.getHref(), getName(), "cmis:policy", 409); + + // Try creating an object of any of the cmis:policy subtypes + IRI typesHREF = cmisClient.getTypesChildrenCollection(cmisClient.getWorkspace(cmisService)); + Map args = new HashMap(); + args.put("typeId", "cmis:policy"); + args.put("includePropertyDefinitions", "true"); + Feed types = fetch(typesHREF, args); + List entries = types.getEntries(); + assertNotSame(0, entries.size()); + for (Entry type : entries) { + CMISTypeDefinition entryType = type.getExtension(CMISConstants.TYPE_DEFINITION); + Assert.assertNotNull(entryType); + createObject(children.getHref(), getName(), entryType.getId(), 409); + } + + // Create a document to attempt to apply policies to + Entry document = createObject(children.getHref(), getName(), "cmis:document"); + + // retrieve policies feed on document (this should be empty) + Link polsLink = document.getLink(CMISConstants.REL_POLICIES); + assertNotNull(polsLink); + Feed polsBefore = fetch(polsLink.getHref(), null); + assertNotNull(polsBefore); + assertEquals(0, polsBefore.getEntries().size()); + + // Try applying a policy (expect a constraint exception) + String policyFile = localTemplates.load("PolicyTest.applyPolicy.atomentry.xml"); + policyFile = policyFile.replace("${OBJECTID}", "doesnotexist"); + Request req = new PostRequest(polsLink.getHref().toString(), policyFile, CMISConstants.MIMETYPE_ENTRY); + sendRequest(req, 409); + } +}