This project has retired. For details please refer to its
Attic page.
RepositoryServiceMutabilityTest xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
60
61
62
63
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
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
102
103
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
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
149 repSvc.createTypeDefinition(repositoryId, new Holder<TypeDefinition>(typeDefRef), null);
150
151 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
152
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
166 InMemoryDocumentTypeDefinition typeDefRef = createTypeForAddingAtRuntime();
167 typeDefRef.setId(typeDefRef.getId() + "!!!");
168 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
169
170
171 typeDefRef = createTypeForAddingAtRuntime();
172 typeDefRef.setParentTypeId("NonExistingParentType");
173 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
174
175
176 typeDefRef = createTypeForAddingAtRuntime();
177 typeDefRef.setId(null);
178 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
179
180
181 typeDefRef = createTypeForAddingAtRuntime();
182 typeDefRef.setQueryName(null);
183 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
184
185
186 typeDefRef = createTypeForAddingAtRuntime();
187 typeDefRef.setQueryName(typeDefRef.getQueryName() + "!!!");
188 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
189
190
191 typeDefRef = createTypeForAddingAtRuntime();
192 typeDefRef.setLocalName(null);
193 checkAddingType(repositoryId, typeDefRef, CmisInvalidArgumentException.class);
194
195
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
210 InMemoryDocumentTypeDefinition typeDef = createTypeForAddingAtRuntime();
211 PropertyStringDefinitionImpl pd = getPropertyDefinitionImpl(typeDef);
212 pd.setId(null);
213 checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
214
215
216 typeDef = createTypeForAddingAtRuntime();
217 pd = getPropertyDefinitionImpl(typeDef);
218 pd.setQueryName(pd.getQueryName() + "!*!");
219 checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
220
221
222 typeDef = createTypeForAddingAtRuntime();
223 pd = getPropertyDefinitionImpl(typeDef);
224 pd.setQueryName(null);
225 checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
226
227
228 typeDef = createTypeForAddingAtRuntime();
229 pd = getPropertyDefinitionImpl(typeDef);
230 pd.setQueryName(pd.getQueryName() + "!!!");
231 checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
232
233
234 typeDef = createTypeForAddingAtRuntime();
235 pd = getPropertyDefinitionImpl(typeDef);
236 pd.setLocalName(null);
237 checkAddingType(repositoryId, typeDef, CmisInvalidArgumentException.class);
238
239
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
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
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 }