This project has retired. For details please refer to its Attic page.
RepositoryServiceMutabilityTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.chemistry.opencmis.commons.data.ContentStream;
32  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
33  import org.apache.chemistry.opencmis.commons.data.Properties;
34  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
35  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
36  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
37  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
38  import org.apache.chemistry.opencmis.commons.enums.Updatability;
39  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
40  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
41  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
42  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
43  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIntegerDefinitionImpl;
44  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringDefinitionImpl;
45  import org.apache.chemistry.opencmis.commons.spi.Holder;
46  import org.apache.chemistry.opencmis.inmemory.server.InMemoryObjectServiceImpl;
47  import org.apache.chemistry.opencmis.inmemory.server.InMemoryRepositoryServiceImpl;
48  import org.apache.chemistry.opencmis.inmemory.server.InMemoryServiceFactoryImpl;
49  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
50  import org.apache.chemistry.opencmis.inmemory.types.InMemoryDocumentTypeDefinition;
51  import org.apache.chemistry.opencmis.inmemory.types.PropertyCreationHelper;
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  import org.junit.After;
55  import org.junit.Before;
56  import org.junit.Test;
57  
58  /**
59   * Temporary test class until CMIS 1.1 bindings are completed. Until then
60   * we use a special setup procedure to directly connect to the repository
61   * service implementation of InMemory.
62   * 
63   * @author Jens
64   */
65  public class RepositoryServiceMutabilityTest extends AbstractServiceTest {
66  
67      private static final Log log = LogFactory.getLog(RepositoryServiceTest.class);
68      private static final String REPOSITORY_ID = "UnitTestRepository";
69      private static final String TYPE_ID_MUTABILITY = "BookTypeAddedLater";
70      private static final String PROPERTY_ID_TITLE = "Title";
71      private static final String PROPERTY_ID_NUMBER = "Number";
72  
73      private InMemoryRepositoryServiceImpl repSvc;
74      private InMemoryObjectServiceImpl objSvc;
75  
76      @Override
77      @Before
78      public void setUp() {
79          super.setTypeCreatorClass(UnitTestTypeSystemCreator.class.getName());
80          super.setUp();
81          
82          Map<String, String> parameters = new HashMap<String, String>();
83  
84          // attach repository info to the session:
85          parameters.put(ConfigConstants.TYPE_CREATOR_CLASS, getTypeCreatorClass());
86          parameters.put(ConfigConstants.REPOSITORY_ID, REPOSITORY_ID);
87          
88          InMemoryServiceFactoryImpl factory = new InMemoryServiceFactoryImpl();
89          factory.init(parameters);
90          StoreManager storeManager = factory.getStoreManger();
91          repSvc = new InMemoryRepositoryServiceImpl(storeManager);
92          objSvc = new InMemoryObjectServiceImpl(storeManager);
93      }
94  
95      @Override
96      @After
97      public void tearDown() {
98          super.tearDown();
99      }
100 
101     // This test is just added because this test class uses a different setup to connect to the
102     // server as long as the server bindings do not support the type mutability extension of 
103     // CMIS 1.1. If this test fails then the setUp() fails! 
104     @Test
105     public void testRepositoryInfo() {
106         log.info("starting testRepositoryInfo() ...");
107         List<RepositoryInfo> repositories = repSvc.getRepositoryInfos(fTestCallContext, null);
108         assertNotNull(repositories);
109         assertFalse(repositories.isEmpty());
110 
111         log.info("geRepositoryInfo(), found " + repositories.size() + " repository/repositories).");
112 
113         for (RepositoryInfo repository : repositories) {
114             RepositoryInfo repository2 = repSvc.getRepositoryInfo(fTestCallContext, repository.getId(), null);
115             assertNotNull(repository2);
116             assertEquals(repository.getId(), repository2.getId());
117             log.info("found repository" + repository2.getId());
118         }
119 
120         log.info("... testRepositoryInfo() finished.");
121     }
122     
123 
124     @Test
125     public void testTypeMutabilityCreation() throws Exception {
126         log.info("");
127         log.info("starting testTypeMutabilityCreation() ...");
128         TypeDefinition typeDefRef = createTypeForAddingAtRuntime();
129         String repositoryId = getRepositoryId();
130         // add type.
131         repSvc.createTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDefRef), null);
132         TypeDefinition type = repSvc.getTypeDefinition(fTestCallContext, repositoryId, typeDefRef.getId(), null);
133         assertEquals(typeDefRef.getId(), type.getId());
134         assertEquals(typeDefRef.getDescription(), type.getDescription());
135         assertEquals(typeDefRef.getDisplayName(), type.getDisplayName());
136         assertEquals(typeDefRef.getLocalName(), type.getLocalName());
137         assertEquals(typeDefRef.getLocalNamespace(), type.getLocalNamespace());
138         RepositoryServiceTest.containsAllBasePropertyDefinitions(type);
139         log.info("... testTypeMutabilityCreation() finished.");
140     }
141     
142     @Test
143     public void testTypeMutabilityCreateDuplicate() throws Exception {
144         log.info("");
145         log.info("starting testTypeMutabilityCreateDuplicate() ...");
146         TypeDefinition typeDefRef = createTypeForAddingAtRuntime();
147         String repositoryId = getRepositoryId();
148         // add type.
149         repSvc.createTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDefRef), null);
150         // add type again should fail
151         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
152         // type should still exist then
153         TypeDefinition type = repSvc.getTypeDefinition(fTestCallContext, repositoryId, typeDefRef.getId(), null);
154         assertEquals(typeDefRef.getId(), type.getId());
155         log.info("... testTypeMutabilityCreateDuplicate() finished.");
156     }
157     
158     
159     @Test
160     public void testTypeMutabilityTypeNameConstraints() throws Exception {
161         log.info("starting testTypeMutabilityTypeNameConstraints() ...");
162         
163         String repositoryId = getRepositoryId();
164         
165         // test illegal type id
166         InMemoryDocumentTypeDefinition typeDefRef = createTypeForAddingAtRuntime();
167         typeDefRef.setId(typeDefRef.getId() + "!!!");
168         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
169 
170         // test illegal parent type id
171         typeDefRef = createTypeForAddingAtRuntime();
172         typeDefRef.setParentTypeId("NonExistingParentType");
173         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
174 
175         // test null type id
176         typeDefRef = createTypeForAddingAtRuntime();
177         typeDefRef.setId(null);
178         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
179         
180         // test null query name
181         typeDefRef = createTypeForAddingAtRuntime();
182         typeDefRef.setQueryName(null);
183         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
184 
185         // test illegal query name
186         typeDefRef = createTypeForAddingAtRuntime();
187         typeDefRef.setQueryName(typeDefRef.getQueryName() + "!!!");
188         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
189 
190         // test null local name
191         typeDefRef = createTypeForAddingAtRuntime();
192         typeDefRef.setLocalName(null);
193         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
194 
195         // test illegal local name
196         typeDefRef = createTypeForAddingAtRuntime();
197         typeDefRef.setLocalName(typeDefRef.getLocalName() + "!!!");
198         checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
199 
200         log.info("... testTypeMutabilityTypeNameConstraints() finished.");              
201     }
202     
203     @Test
204     public void testTypeMutabilityPropertyNameConstraints() throws Exception {
205         log.info("starting testTypeMutabilityPropertyNameConstraints() ...");
206         
207         String repositoryId = getRepositoryId();
208         
209         // test null property id
210         InMemoryDocumentTypeDefinition typeDef = createTypeForAddingAtRuntime();
211         PropertyStringDefinitionImpl pd = getPropertyDefinitionImpl(typeDef);
212         pd.setId(null);
213         checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
214         
215         // test illegal property id
216         typeDef = createTypeForAddingAtRuntime();
217         pd = getPropertyDefinitionImpl(typeDef);
218         pd.setQueryName(pd.getQueryName() + "!*!");
219         checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
220 
221         // test null property query name
222         typeDef = createTypeForAddingAtRuntime();
223         pd = getPropertyDefinitionImpl(typeDef);
224         pd.setQueryName(null);
225         checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
226 
227         // test illegal property query name
228         typeDef = createTypeForAddingAtRuntime();
229         pd = getPropertyDefinitionImpl(typeDef);
230         pd.setQueryName(pd.getQueryName() + "!!!");
231         checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
232 
233         // test null property local name
234         typeDef = createTypeForAddingAtRuntime();
235         pd = getPropertyDefinitionImpl(typeDef);
236         pd.setLocalName(null);
237         checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
238 
239         // test illegal property local name
240         typeDef = createTypeForAddingAtRuntime();
241         pd = getPropertyDefinitionImpl(typeDef);
242         pd.setLocalName(typeDef.getLocalName() + "!!!");
243         checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
244 
245         log.info("... testTypeMutabilityPropertyNameConstraints() finished.");              
246     }
247     
248     private void checkAddingType(String repositoryId, TypeDefinition typeDef, Class<? extends Exception> clazz) {
249         try { 
250             repSvc.createTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDef), null);
251             fail("Illegal type should throw a " + clazz.getName());
252         } catch (RuntimeException e) {
253             assertTrue("Illegal type name threw wrong exception type (should be a " + clazz.getName() + ")",
254                     clazz.isInstance(e));
255         }        
256     }
257     
258     @Test
259     public void testTypeMutabilityUpdate() throws Exception {
260         log.info("");
261         log.info("starting testTypeMutabilityUpdate() ...");
262         TypeDefinition typeDefRef = createTypeForAddingAtRuntime();
263         String repositoryId = getRepositoryId();
264         repSvc.createTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDefRef), null);
265         // update type.
266         try {
267             repSvc.updateTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDefRef), null);
268             fail("updating a type should throw exception.");
269         } catch (Exception e) {
270             assert(e instanceof CmisNotSupportedException);
271         }
272         log.info("... testTypeMutabilityUpdate() finished.");
273     }
274    
275     @Test
276     public void testTypeMutabilityDeletion() throws Exception {
277         log.info("");
278         log.info("starting testTypeMutabilityDeletion() ...");
279         TypeDefinition typeDefRef = createTypeForAddingAtRuntime();
280         String repositoryId = getRepositoryId();
281         repSvc.createTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDefRef), null);
282         
283         String docId = createDoc("Book1", getRootFolderId(REPOSITORY_ID), TYPE_ID_MUTABILITY);
284         
285         // try deleting type, should fail, because in use.
286         try {
287             repSvc.deleteTypeDefinition(repositoryId, TYPE_ID_MUTABILITY, null);
288             fail("deleting a type which is in use should throw exception.");
289         } catch (Exception e) {
290             assert(e instanceof CmisInvalidArgumentException);
291         }
292 
293         objSvc.deleteObject(fTestCallContext, fRepositoryId, docId, true, null);
294         
295         try {
296             repSvc.deleteTypeDefinition(repositoryId, TYPE_ID_MUTABILITY, null);
297         } catch (Exception e) {
298             fail("deleting a type which is in not in use should not throw exception! Exception is: " + e);
299         }
300         
301         try {
302             repSvc.getTypeDefinition(fTestCallContext, repositoryId, TYPE_ID_MUTABILITY, null);
303             fail("getting a type after it was deleted should fail.");
304         } catch (Exception e) {
305         }
306 
307         try {
308             repSvc.deleteTypeDefinition(repositoryId, BaseTypeId.CMIS_DOCUMENT.name(), null);
309             fail("deleting a CMIS base type throw exception.");
310         } catch (Exception e) {
311             assert(e instanceof CmisInvalidArgumentException);
312         }
313         try {
314             repSvc.deleteTypeDefinition(repositoryId, BaseTypeId.CMIS_FOLDER.name(), null);
315             fail("deleting a CMIS base type throw exception.");
316         } catch (Exception e) {
317             assert(e instanceof CmisInvalidArgumentException);
318         }
319 
320         log.info("... testTypeMutabilityDeletion() finished.");
321     }
322 
323     private String getRepositoryId() {
324         List<RepositoryInfo> repositories = repSvc.getRepositoryInfos(fTestCallContext, null);
325         RepositoryInfo repository = repositories.get(0);
326         assertNotNull(repository);
327         return repository.getId();
328     }
329 
330     private String getRootFolderId(String repositoryId) {
331         RepositoryInfo repository = repSvc.getRepositoryInfo(fTestCallContext, repositoryId, null);
332         assertNotNull(repository);
333         return repository.getRootFolderId();
334     }
335 
336     private PropertyStringDefinitionImpl getPropertyDefinitionImpl(TypeDefinition typeDef) {
337         @SuppressWarnings("unchecked")
338         PropertyStringDefinitionImpl pd = (PropertyStringDefinitionImpl) typeDef.getPropertyDefinitions().get(PROPERTY_ID_TITLE);
339         return pd;
340     }
341     
342     private InMemoryDocumentTypeDefinition createTypeForAddingAtRuntime() {
343         
344         InMemoryDocumentTypeDefinition cmisLaterType = new InMemoryDocumentTypeDefinition(TYPE_ID_MUTABILITY,
345                 "Type with two properties", InMemoryDocumentTypeDefinition.getRootDocumentType());
346 
347         Map<String, PropertyDefinition<?>> propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
348 
349         PropertyIntegerDefinitionImpl prop1 = PropertyCreationHelper.createIntegerDefinition(PROPERTY_ID_NUMBER,
350                 "Sample Int Property", Updatability.READWRITE);
351         propertyDefinitions.put(prop1.getId(), prop1);
352 
353         PropertyStringDefinitionImpl prop2 = PropertyCreationHelper.createStringDefinition(PROPERTY_ID_TITLE,
354                 "Sample String Property", Updatability.READWRITE);
355         propertyDefinitions.put(prop2.getId(), prop2);
356         
357         cmisLaterType.addCustomPropertyDefinitions(propertyDefinitions);
358         
359         return cmisLaterType;
360     }
361 
362     String createDoc(String name, String folderId, String typeId) {
363         ContentStream contentStream = null;
364         List<String> policies = null;
365         ExtensionsData extension = null;
366 
367         Properties props = createDocumentProperties(name, typeId);
368 
369         String id = objSvc.createDocument(fTestCallContext, fRepositoryId, props, folderId, contentStream,
370                 VersioningState.NONE, policies, null, null, extension);
371         return id;        
372     }
373 }