mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Unit tests for CMIS no-op policy implementation
- Factored out base class for Alfresco CMIS REST tests - Corrected getAppliedPolicies implementation git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19437 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -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<CMISTypeDefinition> policies;
|
||||
try
|
||||
{
|
||||
policies = cmisService.getAppliedPolicies(objectId, filter);
|
||||
policies = cmisService.getAppliedPolicies((String) cmisService.getProperty(source.getNodeRef(),
|
||||
CMISDictionaryModel.PROP_OBJECT_ID), filter);
|
||||
}
|
||||
catch (CMISServiceException e)
|
||||
{
|
||||
|
@@ -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 extends Element> T parse(Reader doc) {
|
||||
Document<T> 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<String> actual, String... expected)
|
||||
{
|
||||
Assert.assertTrue(actual.containsAll(Arrays.asList(expected)));
|
||||
}
|
||||
|
||||
private void assertDoesNotContain(Set<String> actual, String... unexpected)
|
||||
{
|
||||
Set<String> copy = new HashSet<String>(actual);
|
||||
copy.retainAll(Arrays.asList(unexpected));
|
||||
Assert.assertTrue(copy.isEmpty());
|
||||
}
|
||||
}
|
||||
|
158
source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.java
Normal file
158
source/java/org/alfresco/repo/cmis/rest/test/BaseCMISTest.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 extends Element> T parse(Reader doc)
|
||||
{
|
||||
Document<T> entryDoc = parser.parse(doc);
|
||||
return entryDoc.getRoot();
|
||||
}
|
||||
|
||||
protected <T extends Element> T fetch(IRI href, Map<String, String> 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<String> actual, String... expected)
|
||||
{
|
||||
Assert.assertTrue(actual.containsAll(Arrays.asList(expected)));
|
||||
}
|
||||
|
||||
protected void assertDoesNotContain(Set<String> actual, String... unexpected)
|
||||
{
|
||||
Set<String> copy = new HashSet<String>(actual);
|
||||
copy.retainAll(Arrays.asList(unexpected));
|
||||
Assert.assertTrue(copy.isEmpty());
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/"
|
||||
xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/">
|
||||
<id>urn:uuid:00000000-0000-0000-0000-000000000000</id>
|
||||
<title>${OBJECTID}</title>
|
||||
<updated>2010-01-01T00:00:00Z</updated>
|
||||
<author>admin</author>
|
||||
<link rel="alternate" href="" />
|
||||
<summary>${OBJECTID} (summary)</summary>
|
||||
<cmisra:object>
|
||||
<cmis:properties>
|
||||
<cmis:propertyId propertyDefinitionId="cmis:objectId">
|
||||
<cmis:value>${OBJECTID}</cmis:value>
|
||||
</cmis:propertyId>
|
||||
</cmis:properties>
|
||||
</cmisra:object>
|
||||
</entry>
|
78
source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.java
Normal file
78
source/java/org/alfresco/repo/cmis/rest/test/PolicyTest.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<String, String> args = new HashMap<String, String>();
|
||||
args.put("typeId", "cmis:policy");
|
||||
args.put("includePropertyDefinitions", "true");
|
||||
Feed types = fetch(typesHREF, args);
|
||||
List<Entry> 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user