chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1463270 - in /chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src: main/java/org/apache/chemistry/opencmis/inmemory/ main/java/org/apache/chemistry/opencmis/inmemory/server/ main/java/org/apache/chemis...
Date Mon, 01 Apr 2013 19:56:25 GMT
Author: jens
Date: Mon Apr  1 19:56:25 2013
New Revision: 1463270

URL: http://svn.apache.org/r1463270
Log:
InMemory: more support for Policies

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/DataObjectCreator.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/BaseServiceValidatorImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryPolicyServiceImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryService.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/api/StoredObject.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/InMemoryServiceValidatorImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/ObjectStoreImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/StoredObjectImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/PolicyTest.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/DataObjectCreator.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/DataObjectCreator.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/DataObjectCreator.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/DataObjectCreator.java
Mon Apr  1 19:56:25 2013
@@ -177,9 +177,9 @@ public class DataObjectCreator {
     }
 
     public static PolicyIdList fillPolicyIds(StoredObject so) {
-        // TODO: to be completed if policies are implemented
         PolicyIdListImpl polIds = new PolicyIdListImpl();
-        // polIds.setPolicyIds(...);
+        List<String> pols = so.getAppliedPolicies();
+        polIds.setPolicyIds(pols);
         return polIds;
     }
 

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/BaseServiceValidatorImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/BaseServiceValidatorImpl.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/BaseServiceValidatorImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/BaseServiceValidatorImpl.java
Mon Apr  1 19:56:25 2013
@@ -35,6 +35,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.spi.Holder;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.CmisServiceValidator;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.ObjectStore;
+import org.apache.chemistry.opencmis.inmemory.storedobj.api.Policy;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
 
@@ -455,7 +456,11 @@ public class BaseServiceValidatorImpl im
     public StoredObject[] removePolicy(CallContext context, String repositoryId, String policyId,
String objectId,
             ExtensionsData extension) {
 
-        return checkParams(repositoryId, policyId, objectId);
+        StoredObject[] sos = checkParams(repositoryId, policyId, objectId);
+        StoredObject pol = sos[0];
+        if (!(pol instanceof Policy))
+            throw new CmisInvalidArgumentException("Id " + policyId + " is not a policy object.");
+        return sos;
     }
 
     public StoredObject getAppliedPolicies(CallContext context, String repositoryId, String
objectId,

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryPolicyServiceImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryPolicyServiceImpl.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryPolicyServiceImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryPolicyServiceImpl.java
Mon Apr  1 19:56:25 2013
@@ -18,28 +18,77 @@
  */
 package org.apache.chemistry.opencmis.inmemory.server;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
 import org.apache.chemistry.opencmis.commons.data.ObjectData;
+import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
+import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
+import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
+import org.apache.chemistry.opencmis.commons.server.CallContext;
+import org.apache.chemistry.opencmis.inmemory.storedobj.api.ObjectStore;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
+import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
+import org.apache.chemistry.opencmis.inmemory.types.PropertyCreationHelper;
+import org.apache.chemistry.opencmis.server.support.TypeManager;
 
-public class InMemoryPolicyServiceImpl extends AbstractServiceImpl {
+public class InMemoryPolicyServiceImpl extends InMemoryAbstractServiceImpl {
 
     protected InMemoryPolicyServiceImpl(StoreManager storeManager) {
         super(storeManager);
     }
 
-    public List<ObjectData> getAppliedPolicies(String repositoryId, String objectId,
String filter,
-            ExtensionsData extension) {
-        return null;
+    public List<ObjectData> getAppliedPolicies(CallContext context, String repositoryId,
String objectId,
+            String filter, ExtensionsData extension) {
+
+        StoredObject so = validator.getAppliedPolicies(context, repositoryId, objectId, extension);
+
+        if (so == null) {
+            throw new CmisObjectNotFoundException("Unknown object id: " + objectId);
+        }
+
+        String user = context.getUsername();
+        TypeManager tm = fStoreManager.getTypeManager(repositoryId);
+
+        List<String> polIds = so.getAppliedPolicies();
+        List<ObjectData> res = new ArrayList<ObjectData>();
+        if (null != polIds && polIds.size() > 0) {
+            ObjectStore os = super.fStoreManager.getObjectStore(repositoryId);
+            for (String polId : polIds) {
+                StoredObject pol = os.getObjectById(polId);
+                ObjectData od = PropertyCreationHelper.getObjectData(tm, pol, filter, user,
false,
+                        IncludeRelationships.NONE, null, false, false, null);
+                res.add(od);
+            }
+        }
+        return res;
     }
 
-    public void removePolicy(String repositoryId, String policyId, String objectId, ExtensionsData
extension) {
-        
+    public void removePolicy(CallContext context, String repositoryId, String policyId, String
objectId,
+            ExtensionsData extension) {
+
+        StoredObject so = validator.getAppliedPolicies(context, repositoryId, objectId, extension);
+
+        List<String> polIds = so.getAppliedPolicies();
+        if (!polIds.contains(policyId)) {
+            throw new CmisInvalidArgumentException("Policy id " + policyId
+                    + "cannot be removed, because it is not applied to object " + objectId);
+        }
+        so.removePolicy(policyId);
     }
 
-    public void applyPolicy(String repositoryId, String policyId, String objectId, ExtensionsData
extension) {
+    public void applyPolicy(CallContext context, String repositoryId, String policyId, String
objectId,
+            ExtensionsData extension) {
+        StoredObject[] sos = validator.applyPolicy(context, repositoryId, policyId, objectId,
extension);
+        StoredObject so = sos[1];
+        
+        List<String> polIds = so.getAppliedPolicies();
+        if (null != polIds && polIds.contains(policyId)) {
+            throw new CmisInvalidArgumentException("Policy id " + policyId
+                    + "cannot be added, because it is already applied to object " + objectId);
+        }
+        so.addAppliedPolicy(policyId);
     }
 
 }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryService.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryService.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryService.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryService.java
Mon Apr  1 19:56:25 2013
@@ -451,18 +451,18 @@ public class InMemoryService extends Abs
 
     @Override
     public void applyPolicy(String repositoryId, String policyId, String objectId, ExtensionsData
extension) {
-        fPolSvc.applyPolicy(repositoryId, policyId, objectId, extension);
+        fPolSvc.applyPolicy(getCallContext(), repositoryId, policyId, objectId, extension);
     }
 
     @Override
     public List<ObjectData> getAppliedPolicies(String repositoryId, String objectId,
String filter,
             ExtensionsData extension) {
-        return fPolSvc.getAppliedPolicies(repositoryId, objectId, filter, extension);
+        return fPolSvc.getAppliedPolicies(getCallContext(), repositoryId, objectId, filter,
extension);
     }
 
     @Override
     public void removePolicy(String repositoryId, String policyId, String objectId, ExtensionsData
extension) {
-        fPolSvc.removePolicy(repositoryId, policyId, objectId, extension);
+        fPolSvc.removePolicy(getCallContext(), repositoryId, policyId, objectId, extension);
     }
 
     // /////////////

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/api/StoredObject.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/api/StoredObject.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/api/StoredObject.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/api/StoredObject.java
Mon Apr  1 19:56:25 2013
@@ -338,6 +338,21 @@ public interface StoredObject {
      *      list of ids of policies applied to this object
      */
     public List<String> getAppliedPolicies();
-
+    
+    /**
+     * add an id of a policy to an object
+     * 
+     * @param policyId
+     *      id of policy to add
+     */
+    public void addAppliedPolicy(String policyId);
+    
+    /**
+     * remove an id of a policy from an object
+     * 
+     * @param policyId
+     *      id of policy to remove
+     */
+    public void removePolicy(String policyId);
 
 }
\ No newline at end of file

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/InMemoryServiceValidatorImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/InMemoryServiceValidatorImpl.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/InMemoryServiceValidatorImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/InMemoryServiceValidatorImpl.java
Mon Apr  1 19:56:25 2013
@@ -32,6 +32,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.spi.Holder;
 import org.apache.chemistry.opencmis.inmemory.server.BaseServiceValidatorImpl;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.ObjectStore;
+import org.apache.chemistry.opencmis.inmemory.storedobj.api.Policy;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
 import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
 
@@ -399,7 +400,8 @@ public class InMemoryServiceValidatorImp
         if (addAces != null || removeAces != null)
             throw new CmisConstraintException("ACLs on policies are not suported.");
 
-        StoredObject so = super.createPolicy(context, repositoryId, folderId, addAces, removeAces,
policyIds, extension);
+        StoredObject so = super
+                .createPolicy(context, repositoryId, folderId, addAces, removeAces, policyIds,
extension);
         checkAllAccess(repositoryId, context.getUsername(), so);
         return so;
     }
@@ -906,6 +908,13 @@ public class InMemoryServiceValidatorImp
             ExtensionsData extension) {
 
         StoredObject[] sos = super.applyPolicy(context, repositoryId, policyId, objectId,
extension);
+        if (!(sos[0] instanceof Policy)) {
+            throw new CmisInvalidArgumentException("applyPolicy failed, " + policyId + "
is not a policy id");
+        }
+        if (sos[1] instanceof Policy) {
+            throw new CmisInvalidArgumentException("applyPolicy failed, " + objectId
+                    + " is a policy id. Applying policies to policies is not supported.");
+        }
         checkAllAccess(repositoryId, context.getUsername(), sos[1]);
         return sos;
     }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/ObjectStoreImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/ObjectStoreImpl.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/ObjectStoreImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/ObjectStoreImpl.java
Mon Apr  1 19:56:25 2013
@@ -264,7 +264,8 @@ public class ObjectStoreImpl implements 
         }
         int aclId = getAclId(((FolderImpl) folder), addACEs, removeACEs);
         doc.setAclId(aclId);
-        doc.setAppliedPolicies(policies);
+        if (null != policies)
+            doc.setAppliedPolicies(policies);
         return doc;
     }
 
@@ -278,7 +279,8 @@ public class ObjectStoreImpl implements 
         if (null != folder) {
             ((FolderImpl)folder).addChildItem(item); // add document to folder and
         }
-        item.setAppliedPolicies(policies);
+        if (null != policies)
+            item.setAppliedPolicies(policies);
         int aclId = getAclId(((FolderImpl)folder), addACEs, removeACEs);
         item.setAclId(aclId);
         return item;
@@ -300,7 +302,8 @@ public class ObjectStoreImpl implements 
         version.setCustomProperties(propMap);
         int aclId = getAclId(((FolderImpl)folder), addACEs, removeACEs);
         doc.setAclId(aclId);
-        doc.setAppliedPolicies(policies);
+        if (null != policies)
+            doc.setAppliedPolicies(policies);
         doc.persist();
         return version;
     }
@@ -321,7 +324,8 @@ public class ObjectStoreImpl implements 
 
         int aclId = getAclId(((FolderImpl)parent), addACEs, removeACEs);
         folder.setAclId(aclId);
-        folder.setAppliedPolicies(policies);
+        if (null != policies)
+            folder.setAppliedPolicies(policies);
 
         return folder;
     }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/StoredObjectImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/StoredObjectImpl.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/StoredObjectImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/storedobj/impl/StoredObjectImpl.java
Mon Apr  1 19:56:25 2013
@@ -149,11 +149,36 @@ public class StoredObjectImpl implements
     }
     
     public List<String> getAppliedPolicies() {
-        return policyIds;
+        if (null == policyIds)
+            return null;
+        else
+            return Collections.unmodifiableList(policyIds);
     }
     
     public void setAppliedPolicies(List<String> newPolicies) {
-        policyIds = newPolicies;
+        if (null == newPolicies) {
+            policyIds = null;
+        } else {
+            if (null == policyIds) {
+                policyIds = new ArrayList<String>();
+            }
+            policyIds.addAll(newPolicies);
+        }
+    }
+    
+    public void addAppliedPolicy(String policyId) {
+        if (null == policyIds) {
+            policyIds = new ArrayList<String>();
+        }
+        if (!policyIds.contains(policyId)) {
+            policyIds.add(policyId);
+        }
+    }
+
+    public void removePolicy(String policyId) {
+        if (null != policyIds && policyIds.contains(policyId)) {
+            policyIds.remove(policyId);
+        }
     }
     
     // CMIS 1.1:

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/PolicyTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/PolicyTest.java?rev=1463270&r1=1463269&r2=1463270&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/PolicyTest.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/PolicyTest.java
Mon Apr  1 19:56:25 2013
@@ -18,27 +18,27 @@
  */
 package org.apache.chemistry.opencmis.inmemory;
 
-import static org.junit.Assert.*;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Map.Entry;
 
 import org.apache.chemistry.opencmis.commons.PropertyIds;
-import org.apache.chemistry.opencmis.commons.data.Acl;
 import org.apache.chemistry.opencmis.commons.data.ContentStream;
-import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
 import org.apache.chemistry.opencmis.commons.data.ObjectData;
 import org.apache.chemistry.opencmis.commons.data.PolicyIdList;
 import org.apache.chemistry.opencmis.commons.data.Properties;
 import org.apache.chemistry.opencmis.commons.data.PropertyData;
-import org.apache.chemistry.opencmis.commons.data.PropertyId;
 import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
 import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
+import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
+import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
 import org.apache.chemistry.opencmis.commons.spi.Holder;
 import org.apache.chemistry.opencmis.inmemory.ObjectServiceTest.ObjectTestTypeSystemCreator;
 import org.junit.After;
@@ -49,6 +49,7 @@ public class PolicyTest extends Abstract
     private static final String TEST_CUSTOM_VALUE = "SimpleAuditPolicy";
     private static final String TEST_POLICY_TEXT = "Test Policy Unit Test";
     private static final String TEST_POLICY_NAME = "TestPolicy";
+    private static final String TEST_POLICY_2_NAME = "TestPolicy2";
     private static final String MY_DOC_1 = "Document_1";
     private static final String VER_DOC_NAME = "VersionedDocument";
 
@@ -65,7 +66,7 @@ public class PolicyTest extends Abstract
     public void tearDown() {
         super.tearDown();
     }
-    
+
     @Test
     public void createGetPolicy() {
         assertNotNull(polId);
@@ -117,7 +118,8 @@ public class PolicyTest extends Abstract
         String polIdRes = polIds.getPolicyIds().get(0);
         assertEquals(polId, polIdRes);
 
-        String versionSeriesId = (String) od.getProperties().getProperties().get(PropertyIds.VERSION_SERIES_ID).getFirstValue();
+        String versionSeriesId = (String) od.getProperties().getProperties().get(PropertyIds.VERSION_SERIES_ID)
+                .getFirstValue();
         assertNotNull(versionSeriesId);
         od = fVerSvc.getObjectOfLatestVersion(fRepositoryId, docId, versionSeriesId, false,
null, false,
                 IncludeRelationships.NONE, null, true, false, null);
@@ -129,6 +131,123 @@ public class PolicyTest extends Abstract
         assertEquals(polId, polIdRes);
     }
 
+    @Test
+    public void testPolicyServiceGetAppliedPolicies() {
+        assertNotNull(polId);
+        String docId1 = createDocumentWithPolicy(polId);
+        List<ObjectData> pols = fPolSvc.getAppliedPolicies(fRepositoryId, docId1, null,
null);
+        assertEquals(1, pols.size());
+        ObjectData od = pols.get(0);
+        assertEquals(polId, od.getId());
+        assertNull(od.getPolicyIds());
+    }
+
+    @Test
+    public void testPolicyServiceApplyPolicies() {
+
+        assertNotNull(polId);
+        String docId = createDocumentWithoutPolicy("Document_2", polId);
+        List<ObjectData> pols = fPolSvc.getAppliedPolicies(fRepositoryId, docId, null,
null);
+        assertEquals(0, pols.size());
+
+        // apply a policy
+        fPolSvc.applyPolicy(fRepositoryId, polId, docId, null);
+        pols = fPolSvc.getAppliedPolicies(fRepositoryId, docId, null, null);
+        assertEquals(1, pols.size());
+        ObjectData od = pols.get(0);
+        assertEquals(polId, od.getId());
+        assertNull(od.getPolicyIds());
+
+        String polId2 = createPolicy2();
+        fPolSvc.applyPolicy(fRepositoryId, polId2, docId, null);
+        pols = fPolSvc.getAppliedPolicies(fRepositoryId, docId, null, null);
+        assertEquals(2, pols.size());
+        od = pols.get(0);
+        assertEquals(polId, od.getId());
+        assertNull(od.getPolicyIds());
+        od = pols.get(1);
+        assertEquals(polId2, od.getId());
+
+        // assign an unknown id as policy
+        docId = createDocumentWithoutPolicy("Document_3", polId);
+        try {
+            fPolSvc.applyPolicy(fRepositoryId, "UnknownId", docId, null);
+            fail("applyPolicy with unknown id should fail.");
+        } catch (CmisObjectNotFoundException e) {
+        } catch (Exception ex) {
+            fail("applyPolicy with unknown id should throw a CmisInvalidArgumentException,
but was a " + ex.getClass());
+        }
+
+        // apply policy with a doc id
+        try {
+            String docId2 = createDocumentWithoutPolicy(polId);
+            fPolSvc.applyPolicy(fRepositoryId, docId2, docId, null);
+            fail("applyPolicy with document id as policy should fail.");
+        } catch (CmisInvalidArgumentException e) {
+        } catch (Exception ex) {
+            fail("applyPolicy with unknown id should throw a CmisInvalidArgumentException,
but was a " + ex.getClass());
+        }
+
+        // apply a policy to a policy
+        try {
+            fPolSvc.applyPolicy(fRepositoryId, polId2, polId, null);
+            fail("applyPolicy to a policy id should fail.");
+        } catch (CmisInvalidArgumentException e) {
+        } catch (Exception ex) {
+            fail("applyPolicy with unknown id should throw a CmisInvalidArgumentException,
but was a " + ex.getClass());
+        }
+
+    }
+
+    @Test
+    public void testPolicyServiceRemovePolicies() {
+        assertNotNull(polId);
+        String docId = createDocumentWithPolicy(polId);
+        String polId2 = createPolicy2();
+        String docId2 = createDocumentWithoutPolicy("Document_3", polId);
+
+        fPolSvc.applyPolicy(fRepositoryId, polId2, docId, null);
+
+        fPolSvc.removePolicy(fRepositoryId, polId, docId, null);
+        List<ObjectData> pols = fPolSvc.getAppliedPolicies(fRepositoryId, docId, null,
null);
+        assertEquals(1, pols.size());
+        ObjectData od = pols.get(0);
+        assertEquals(polId2, od.getId());
+
+        fPolSvc.removePolicy(fRepositoryId, polId2, docId, null);
+        pols = fPolSvc.getAppliedPolicies(fRepositoryId, docId, null, null);
+        assertEquals(0, pols.size());
+
+        // try again should fail
+        try {
+            fPolSvc.removePolicy(fRepositoryId, polId2, docId, null);
+            fail("Removing a non-existing policy should fail.");
+        } catch (CmisInvalidArgumentException e) {
+
+        } catch (Exception e) {
+            fail("Removing a non-existing policy should raise a CmisObjectNotFoundException,
but was a " + e);
+        }
+        
+        // try removing a non existing id
+        try {
+            fPolSvc.removePolicy(fRepositoryId, polId2, docId, null);
+            fail("Removing a non-existing policy should fail.");
+        } catch (CmisInvalidArgumentException e) {
+        } catch (Exception e) {
+            fail("Removing a non-existing policy should raise a CmisInvalidArgumentException,
but was a " + e);
+        }
+        
+        // try removing a non policy id
+        try {
+            fPolSvc.removePolicy(fRepositoryId, docId2, docId, null);
+            fail("Removing a non-existing policy should fail.");
+        } catch (CmisInvalidArgumentException e) {
+        } catch (Exception e) {
+            fail("Removing a non-policy should raise a CmisInvalidArgumentException, but
was a " + e);
+        }
+        
+    }
+
     private void createPolicy() {
         List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
         properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, TEST_POLICY_NAME));
@@ -141,6 +260,16 @@ public class PolicyTest extends Abstract
         polId = fObjSvc.createPolicy(fRepositoryId, props, null, null, null, null, null);
     }
 
+    private String createPolicy2() {
+        List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
+        properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, TEST_POLICY_2_NAME));
+        properties.add(fFactory.createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_POLICY.value()));
+        properties.add(fFactory.createPropertyIdData(PropertyIds.POLICY_TEXT, "ReadAuditLogging"));
+        Properties props = fFactory.createPropertiesData(properties);
+
+        return fObjSvc.createPolicy(fRepositoryId, props, null, null, null, null, null);
+    }
+
     private String createDocumentWithPolicy(String policyId) {
         List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
         properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, MY_DOC_1));
@@ -153,6 +282,20 @@ public class PolicyTest extends Abstract
         return id;
     }
 
+    private String createDocumentWithoutPolicy(String policyId) {
+        return createDocumentWithoutPolicy(MY_DOC_1, policyId);
+    }
+
+    private String createDocumentWithoutPolicy(String name, String policyId) {
+        List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
+        properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, name));
+        properties.add(fFactory.createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_DOCUMENT.value()));
+        Properties props = fFactory.createPropertiesData(properties);
+        String id = fObjSvc.createDocument(fRepositoryId, props, fRootFolderId, null, VersioningState.NONE,
null, null,
+                null, null);
+        return id;
+    }
+
     private String createVersionedDocument() {
         String id = null;
         ContentStream contentStream = null;
@@ -164,8 +307,8 @@ public class PolicyTest extends Abstract
                 ObjectServiceTest.TEST_VERSION_DOCUMENT_TYPE_ID));
         Properties props = fFactory.createPropertiesData(properties);
 
-        id = fObjSvc.createDocument(fRepositoryId, props, fRootFolderId, contentStream, VersioningState.MAJOR,
policies,
-                null, null, null);
+        id = fObjSvc.createDocument(fRepositoryId, props, fRootFolderId, contentStream, VersioningState.MAJOR,
+                policies, null, null, null);
 
         return id;
     }



Mime
View raw message