KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > CMLUtilTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webservice;
18
19 import java.util.List JavaDoc;
20
21 import org.alfresco.model.ContentModel;
22 import org.alfresco.repo.content.MimetypeMap;
23 import org.alfresco.repo.security.authentication.AuthenticationComponent;
24 import org.alfresco.repo.webservice.repository.UpdateResult;
25 import org.alfresco.repo.webservice.types.CML;
26 import org.alfresco.repo.webservice.types.CMLAddAspect;
27 import org.alfresco.repo.webservice.types.CMLAddChild;
28 import org.alfresco.repo.webservice.types.CMLCopy;
29 import org.alfresco.repo.webservice.types.CMLCreate;
30 import org.alfresco.repo.webservice.types.CMLCreateAssociation;
31 import org.alfresco.repo.webservice.types.CMLDelete;
32 import org.alfresco.repo.webservice.types.CMLMove;
33 import org.alfresco.repo.webservice.types.CMLRemoveAspect;
34 import org.alfresco.repo.webservice.types.CMLRemoveAssociation;
35 import org.alfresco.repo.webservice.types.CMLRemoveChild;
36 import org.alfresco.repo.webservice.types.CMLUpdate;
37 import org.alfresco.repo.webservice.types.NamedValue;
38 import org.alfresco.repo.webservice.types.ParentReference;
39 import org.alfresco.repo.webservice.types.Predicate;
40 import org.alfresco.repo.webservice.types.Reference;
41 import org.alfresco.service.cmr.repository.AssociationRef;
42 import org.alfresco.service.cmr.repository.ChildAssociationRef;
43 import org.alfresco.service.cmr.repository.ContentData;
44 import org.alfresco.service.cmr.repository.NodeRef;
45 import org.alfresco.service.cmr.repository.NodeService;
46 import org.alfresco.service.cmr.repository.StoreRef;
47 import org.alfresco.service.cmr.search.SearchService;
48 import org.alfresco.service.namespace.NamespaceService;
49 import org.alfresco.service.namespace.QName;
50 import org.alfresco.util.BaseSpringTest;
51 import org.alfresco.util.PropertyMap;
52
53 /**
54  * @author Roy Wetherall
55  */

56 public class CMLUtilTest extends BaseSpringTest
57 {
58     private static final ContentData CONTENT_DATA_TEXT_UTF8 = new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8");
59     private static final ContentData CONTENT_DATA_HTML_UTF16 = new ContentData(null, MimetypeMap.MIMETYPE_HTML, 0L, "UTF-16");
60     
61     private CMLUtil cmlUtil;
62     private NodeService nodeService;
63     private StoreRef testStoreRef;
64     private NodeRef rootNodeRef;
65     private NodeRef nodeRef;
66     private NamespaceService namespaceService;
67     private SearchService searchService;
68     private NodeRef folderNodeRef;
69     private AuthenticationComponent authenticationComponent;
70
71     @Override JavaDoc
72     protected String JavaDoc[] getConfigLocations()
73     {
74         return new String JavaDoc[]{"classpath:org/alfresco/repo/webservice/cml-test.xml"};
75     }
76     
77     @Override JavaDoc
78     protected void onSetUpInTransaction() throws Exception JavaDoc
79     {
80         this.cmlUtil = (CMLUtil)this.applicationContext.getBean("CMLUtil");
81         this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
82         this.searchService = (SearchService)this.applicationContext.getBean("searchService");
83         this.namespaceService = (NamespaceService)this.applicationContext.getBean("namespaceService");
84         this.authenticationComponent = (AuthenticationComponent) this.applicationContext.getBean("authenticationComponent");
85         
86         this.authenticationComponent.setSystemUserAsCurrentUser();
87         
88         // Create the store and get the root node
89
this.testStoreRef = this.nodeService.createStore(
90                 StoreRef.PROTOCOL_WORKSPACE, "Test_"
91                         + System.currentTimeMillis());
92         this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
93         
94         // Create the node used for tests
95
PropertyMap contentProps = new PropertyMap();
96         contentProps.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT_UTF8);
97         this.nodeRef = this.nodeService.createNode(
98                 this.rootNodeRef,
99                 ContentModel.ASSOC_CHILDREN,
100                 ContentModel.ASSOC_CHILDREN,
101                 ContentModel.TYPE_CONTENT,
102                 contentProps).getChildRef();
103         
104         this.folderNodeRef = this.nodeService.createNode(
105                 this.rootNodeRef,
106                 ContentModel.ASSOC_CHILDREN,
107                 ContentModel.ASSOC_CHILDREN,
108                 ContentModel.TYPE_FOLDER).getChildRef();
109     }
110     
111     @Override JavaDoc
112     protected void onTearDownInTransaction() throws Exception JavaDoc
113     {
114         authenticationComponent.clearCurrentSecurityContext();
115         super.onTearDownInTransaction();
116     }
117     
118     public void testMoreThanOneStatement()
119     {
120         
121     }
122     
123     public void testCreate()
124     {
125         CMLCreate[] creates = new CMLCreate[]{createCMLCreate(ContentModel.TYPE_CONTENT, "id1")};
126         
127         CML cml = new CML();
128         cml.setCreate(creates);
129         
130         UpdateResult[] result = this.cmlUtil.executeCML(cml);
131         assertNotNull(result);
132         assertEquals(1, result.length);
133         
134         UpdateResult updateResult = result[0];
135         assertEquals("create", updateResult.getStatement());
136         assertNull(updateResult.getSource());
137         assertNotNull(updateResult.getDestination());
138         NodeRef createdNodeRef = Utils.convertToNodeRef(updateResult.getDestination(), this.nodeService, this.searchService, this.namespaceService);
139         assertNotNull(createdNodeRef);
140         
141         assertEquals(ContentModel.TYPE_CONTENT, this.nodeService.getType(createdNodeRef));
142         assertEquals("name", this.nodeService.getProperty(createdNodeRef, ContentModel.PROP_NAME));
143         
144         //System.out.println(NodeStoreInspector.dumpNodeStore(this.nodeService, this.testStoreRef));
145
}
146     
147     public void testAddRemoveAspect()
148     {
149         CMLAddAspect addAspect = new CMLAddAspect();
150         addAspect.setAspect(ContentModel.ASPECT_VERSIONABLE.toString());
151         addAspect.setWhere(createPredicate(this.nodeRef));
152         
153         CML cml = new CML();
154         cml.setAddAspect(new CMLAddAspect[]{addAspect});
155         
156         UpdateResult[] result = this.cmlUtil.executeCML(cml);
157         assertNotNull(result);
158         assertEquals(1, result.length);
159         
160         UpdateResult updateResult = result[0];
161         assertEquals("addAspect", updateResult.getStatement());
162         assertNotNull(updateResult.getSource());
163         assertNotNull(updateResult.getDestination());
164         
165         assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
166         
167         // TODO should test with properties set as well
168

169         CMLRemoveAspect removeAspect = new CMLRemoveAspect();
170         removeAspect.setAspect(ContentModel.ASPECT_VERSIONABLE.toString());
171         removeAspect.setWhere(createPredicate(this.nodeRef));
172         
173         CML cml2 = new CML();
174         cml2.setRemoveAspect(new CMLRemoveAspect[]{removeAspect});
175         
176         UpdateResult[] results2 = this.cmlUtil.executeCML(cml2);
177         assertNotNull(results2);
178         assertEquals(1, results2.length);
179         
180         UpdateResult result2 = results2[0];
181         assertEquals("removeAspect", result2.getStatement());
182         assertNotNull(result2.getDestination());
183         assertNotNull(result2.getSource());
184         
185         assertFalse(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE));
186         
187     }
188     
189     public void testUpdate()
190     {
191         CMLUpdate update = new CMLUpdate();
192         update.setWhere(createPredicate(this.nodeRef));
193         update.setProperty(new NamedValue[]
194         {
195                 new NamedValue(ContentModel.PROP_NAME.toString(), "updatedName"),
196                 new NamedValue(ContentModel.PROP_CONTENT.toString(), CONTENT_DATA_HTML_UTF16.toString())
197         });
198         
199         CML cml = new CML();
200         cml.setUpdate(new CMLUpdate[]{update});
201         
202         UpdateResult[] result = this.cmlUtil.executeCML(cml);
203         assertNotNull(result);
204         assertEquals(1, result.length);
205         
206         UpdateResult updateResult = result[0];
207         assertEquals("update", updateResult.getStatement());
208         assertNotNull(updateResult.getSource());
209         assertNotNull(updateResult.getDestination());
210         
211         assertEquals("updatedName", this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_NAME));
212         assertEquals(CONTENT_DATA_HTML_UTF16, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_CONTENT));
213     }
214     
215     public void testDelete()
216     {
217         CMLDelete delete = new CMLDelete();
218         delete.setWhere(createPredicate(this.nodeRef));
219         
220         CML cml = new CML();
221         cml.setDelete(new CMLDelete[]{delete});
222         
223         UpdateResult[] result = this.cmlUtil.executeCML(cml);
224         assertNotNull(result);
225         assertEquals(1, result.length);
226         
227         UpdateResult updateResult = result[0];
228         assertEquals("delete", updateResult.getStatement());
229         assertNotNull(updateResult.getSource());
230         assertNull(updateResult.getDestination());
231         
232         // Check that the node no longer exists
233
assertFalse(this.nodeService.exists(this.nodeRef));
234     }
235     
236     public void testMove()
237     {
238         CMLMove move = new CMLMove();
239         move.setTo(createParentReference(this.folderNodeRef, ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS));
240         move.setWhere(createPredicate(this.nodeRef));
241         
242         CML cml = new CML();
243         cml.setMove(new CMLMove[]{move});
244         
245         UpdateResult[] result = this.cmlUtil.executeCML(cml);
246         assertNotNull(result);
247         assertEquals(1, result.length);
248         
249         UpdateResult updateResult = result[0];
250         assertEquals("move", updateResult.getStatement());
251         assertNotNull(updateResult.getSource());
252         assertNotNull(updateResult.getDestination());
253         
254         List JavaDoc<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(this.folderNodeRef);
255         assertNotNull(assocs);
256         assertEquals(1, assocs.size());
257         ChildAssociationRef assoc = assocs.get(0);
258         assertEquals(assoc.getChildRef(), Utils.convertToNodeRef(
259                                                 updateResult.getDestination(),
260                                                 this.nodeService,
261                                                 this.searchService,
262                                                 this.namespaceService));
263     }
264     
265     public void testCopy()
266     {
267         CMLCopy copy = new CMLCopy();
268         copy.setTo(createParentReference(this.folderNodeRef, ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS));
269         copy.setWhere(createPredicate(this.nodeRef));
270         
271         CML cml = new CML();
272         cml.setCopy(new CMLCopy[]{copy});
273         
274         UpdateResult[] result = this.cmlUtil.executeCML(cml);
275         assertNotNull(result);
276         assertEquals(1, result.length);
277         
278         UpdateResult updateResult = result[0];
279         assertEquals("copy", updateResult.getStatement());
280         assertNotNull(updateResult.getSource());
281         assertNotNull(updateResult.getDestination());
282         
283         List JavaDoc<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(this.folderNodeRef);
284         assertNotNull(assocs);
285         assertEquals(1, assocs.size());
286         ChildAssociationRef assoc = assocs.get(0);
287         assertEquals(assoc.getChildRef(), Utils.convertToNodeRef(
288                                                 updateResult.getDestination(),
289                                                 this.nodeService,
290                                                 this.searchService,
291                                                 this.namespaceService));
292     }
293     
294     public void testAddChild()
295     {
296         CMLAddChild addChild = new CMLAddChild();
297         addChild.setTo(createParentReference(this.folderNodeRef, ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS));
298         addChild.setWhere(createPredicate(this.nodeRef));
299         
300         CML cml = new CML();
301         cml.setAddChild(new CMLAddChild[]{addChild});
302         
303         UpdateResult[] result = this.cmlUtil.executeCML(cml);
304         assertNotNull(result);
305         assertEquals(1, result.length);
306         
307         UpdateResult updateResult = result[0];
308         assertEquals("addChild", updateResult.getStatement());
309         assertNotNull(updateResult.getSource());
310         assertNotNull(updateResult.getDestination());
311         
312         List JavaDoc<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(this.folderNodeRef);
313         assertNotNull(assocs);
314         assertEquals(1, assocs.size());
315         ChildAssociationRef assoc = assocs.get(0);
316         assertEquals(assoc.getChildRef(), Utils.convertToNodeRef(
317                                                 updateResult.getDestination(),
318                                                 this.nodeService,
319                                                 this.searchService,
320                                                 this.namespaceService));
321     }
322     
323     public void testRemoveChild()
324     {
325         // Add the node as a child of the folder
326
this.nodeService.addChild(this.folderNodeRef, this.nodeRef, ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS);
327         
328         CMLRemoveChild removeChild = new CMLRemoveChild();
329         removeChild.setFrom(Utils.convertToReference(this.folderNodeRef));
330         removeChild.setWhere(createPredicate(this.nodeRef));
331         
332         CML cml = new CML();
333         cml.setRemoveChild(new CMLRemoveChild[]{removeChild});
334         
335         UpdateResult[] result = this.cmlUtil.executeCML(cml);
336         assertNotNull(result);
337         assertEquals(1, result.length);
338         
339         UpdateResult updateResult = result[0];
340         assertEquals("removeChild", updateResult.getStatement());
341         assertNotNull(updateResult.getSource());
342         assertNull(updateResult.getDestination());
343         
344         List JavaDoc<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(this.folderNodeRef);
345         assertEquals(0, assocs.size());
346     }
347     
348     public void testCreateAssociation()
349     {
350         CMLCreateAssociation createAssoc = new CMLCreateAssociation();
351         createAssoc.setAssociation(ContentModel.ASSOC_CONTAINS.toString());
352         createAssoc.setFrom(createPredicate(this.folderNodeRef));
353         createAssoc.setTo(createPredicate(this.nodeRef));
354         
355         CML cml = new CML();
356         cml.setCreateAssociation(new CMLCreateAssociation[]{createAssoc});
357         
358         UpdateResult[] result = this.cmlUtil.executeCML(cml);
359         assertNotNull(result);
360         assertEquals(1, result.length);
361         
362         UpdateResult updateResult = result[0];
363         assertEquals("createAssociation", updateResult.getStatement());
364         assertNotNull(updateResult.getSource());
365         assertNotNull(updateResult.getDestination());
366         
367         List JavaDoc<AssociationRef> assocs = this.nodeService.getTargetAssocs(this.folderNodeRef, ContentModel.ASSOC_CONTAINS);
368         assertNotNull(assocs);
369         assertEquals(1, assocs.size());
370         AssociationRef assoc = assocs.get(0);
371         assertEquals(assoc.getTargetRef(), Utils.convertToNodeRef(
372                                                 updateResult.getDestination(),
373                                                 this.nodeService,
374                                                 this.searchService,
375                                                 this.namespaceService));
376     }
377     
378     public void testRemoveAssociation()
379     {
380         this.nodeService.createAssociation(this.folderNodeRef, this.nodeRef, ContentModel.ASSOC_CONTAINS);
381         
382         CMLRemoveAssociation removeAssociation = new CMLRemoveAssociation();
383         removeAssociation.setAssociation(ContentModel.ASSOC_CONTAINS.toString());
384         removeAssociation.setFrom(createPredicate(this.folderNodeRef));
385         removeAssociation.setTo(createPredicate(this.nodeRef));
386         
387         CML cml = new CML();
388         cml.setRemoveAssociation(new CMLRemoveAssociation[]{removeAssociation});
389         
390         UpdateResult[] result = this.cmlUtil.executeCML(cml);
391         assertNotNull(result);
392         assertEquals(1, result.length);
393         
394         UpdateResult updateResult = result[0];
395         assertEquals("removeAssociation", updateResult.getStatement());
396         assertNotNull(updateResult.getSource());
397         assertNotNull(updateResult.getDestination());
398         
399         List JavaDoc<AssociationRef> assocs = this.nodeService.getTargetAssocs(this.folderNodeRef, ContentModel.ASSOC_CONTAINS);
400         assertNotNull(assocs);
401         assertEquals(0, assocs.size());
402     }
403     
404     private ParentReference createParentReference(NodeRef nodeRef, QName assocType, QName assocName)
405     {
406         ParentReference parentReference = new ParentReference();
407         parentReference.setAssociationType(assocType.toString());
408         parentReference.setChildName(assocName.toString());
409         parentReference.setStore(Utils.convertToStore(nodeRef.getStoreRef()));
410         parentReference.setUuid(nodeRef.getId());
411         return parentReference;
412     }
413     
414     private Predicate createPredicate(NodeRef nodeRef)
415     {
416         Predicate predicate = new Predicate();
417         predicate.setStore(Utils.convertToStore(nodeRef.getStoreRef()));
418         predicate.setNodes(new Reference[]{Utils.convertToReference(nodeRef)});
419         return predicate;
420     }
421
422     private CMLCreate createCMLCreate(QName type, String JavaDoc id)
423     {
424         CMLCreate create = new CMLCreate();
425         create.setId("id1");
426         create.setType(ContentModel.TYPE_CONTENT.toString());
427         
428         ParentReference parentReference = new ParentReference();
429         parentReference.setAssociationType(ContentModel.ASSOC_CHILDREN.toString());
430         parentReference.setChildName(ContentModel.ASSOC_CHILDREN.toString());
431         parentReference.setStore(Utils.convertToStore(this.testStoreRef));
432         parentReference.setUuid(this.rootNodeRef.getId());
433         
434         create.setParent(parentReference);
435         create.setProperty(getContentNamedValues());
436         
437         return create;
438     }
439     
440     private NamedValue[] getContentNamedValues()
441     {
442         return new NamedValue[]
443           {
444                 new NamedValue(ContentModel.PROP_NAME.toString(), "name"),
445                 new NamedValue(ContentModel.PROP_CONTENT.toString(), CONTENT_DATA_TEXT_UTF8.toString())
446           };
447     }
448 }
449
Popular Tags