KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > test > RepositoryServiceSystemTest


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.webservice.test;
18
19 import org.alfresco.webservice.repository.Association;
20 import org.alfresco.webservice.repository.AssociationDirectionEnum;
21 import org.alfresco.webservice.repository.QueryResult;
22 import org.alfresco.webservice.repository.RepositoryServiceLocator;
23 import org.alfresco.webservice.repository.UpdateResult;
24 import org.alfresco.webservice.types.CML;
25 import org.alfresco.webservice.types.CMLAddAspect;
26 import org.alfresco.webservice.types.CMLCreate;
27 import org.alfresco.webservice.types.ClassDefinition;
28 import org.alfresco.webservice.types.NamedValue;
29 import org.alfresco.webservice.types.Node;
30 import org.alfresco.webservice.types.NodeDefinition;
31 import org.alfresco.webservice.types.ParentReference;
32 import org.alfresco.webservice.types.Predicate;
33 import org.alfresco.webservice.types.PropertyDefinition;
34 import org.alfresco.webservice.types.Query;
35 import org.alfresco.webservice.types.QueryConfiguration;
36 import org.alfresco.webservice.types.QueryLanguageEnum;
37 import org.alfresco.webservice.types.Reference;
38 import org.alfresco.webservice.types.ResultSet;
39 import org.alfresco.webservice.types.ResultSetRow;
40 import org.alfresco.webservice.types.ResultSetRowNode;
41 import org.alfresco.webservice.types.Store;
42 import org.alfresco.webservice.util.Constants;
43 import org.alfresco.webservice.util.WebServiceFactory;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47 public class RepositoryServiceSystemTest extends BaseWebServiceSystemTest
48 {
49     private static Log logger = LogFactory
50             .getLog(RepositoryServiceSystemTest.class);
51
52     @Override JavaDoc
53     protected void setUp() throws Exception JavaDoc
54     {
55         super.setUp();
56     }
57
58     /**
59      * Tests the getStores method
60      *
61      * @throws Exception
62      */

63     public void testGetStores() throws Exception JavaDoc
64     {
65         Store[] stores = WebServiceFactory.getRepositoryService().getStores();
66         assertNotNull("Stores array should not be null", stores);
67         assertTrue("There should be at least 1 store", stores.length >= 1);
68     }
69
70     /**
71      * Tests the query service call
72      *
73      * @throws Exception
74      */

75     public void testQuery() throws Exception JavaDoc
76     {
77         //Query query = new Query(QueryLanguageEnum.lucene, "*");
78
Query query = new Query(QueryLanguageEnum.lucene,
79                 "( +@\\{http\\://www.alfresco.org/1.0\\}name:test*) OR TEXT:test*");
80
81         QueryResult queryResult = WebServiceFactory.getRepositoryService().query(BaseWebServiceSystemTest.store, query,
82                 false);
83         assertNotNull("queryResult should not be null", queryResult);
84
85         ResultSet resultSet = queryResult.getResultSet();
86         assertNotNull("The result set should not be null", resultSet);
87         logger.debug("There are " + resultSet.getTotalRowCount() + " rows:");
88
89         if (resultSet.getTotalRowCount() > 0)
90         {
91             ResultSetRow[] rows = resultSet.getRows();
92             for (int x = 0; x < rows.length; x++)
93             {
94                 ResultSetRow row = rows[x];
95                 NamedValue[] columns = row.getColumns();
96                 for (int y = 0; y < columns.length; y++)
97                 {
98                     logger.debug("row " + x + ": "
99                             + row.getColumns(y).getName() + " = "
100                             + row.getColumns(y).getValue());
101                 }
102             }
103         } else
104         {
105             logger.debug("The query returned no results");
106             fail("The query returned no results");
107         }
108     }
109
110     /**
111      * Tests the ability to retrieve the results of a query in batches
112      *
113      * @throws Exception
114      */

115     public void testQuerySession() throws Exception JavaDoc
116     {
117         // define a query that will return a lot of hits i.e. EVERYTHING
118
Query query = new Query(QueryLanguageEnum.lucene, "*");
119
120         // add the query configuration header to the call
121
int batchSize = 5;
122         QueryConfiguration queryCfg = new QueryConfiguration();
123         queryCfg.setFetchSize(batchSize);
124         WebServiceFactory.getRepositoryService().setHeader(new RepositoryServiceLocator()
125                 .getServiceName().getNamespaceURI(), "QueryHeader", queryCfg);
126
127         // get the first set of results back
128
QueryResult queryResult = WebServiceFactory.getRepositoryService().query(BaseWebServiceSystemTest.store, query,
129                 false);
130         assertNotNull("queryResult should not be null", queryResult);
131         String JavaDoc querySession = queryResult.getQuerySession();
132         String JavaDoc origQuerySession = querySession;
133         assertNotNull("querySession should not be null", querySession);
134
135         ResultSet resultSet = queryResult.getResultSet();
136         assertNotNull("The result set should not be null", resultSet);
137         logger.debug("There are " + resultSet.getTotalRowCount()
138                 + " rows in total");
139         logger.debug("There are " + resultSet.getRows().length
140                 + " rows in the first set");
141         assertEquals("The result set size should be " + batchSize, batchSize,
142                 resultSet.getRows().length);
143
144         // get the next batch of results
145
queryResult = WebServiceFactory.getRepositoryService().fetchMore(querySession);
146         assertNotNull("queryResult should not be null", queryResult);
147         querySession = queryResult.getQuerySession();
148         assertNotNull("querySession should not be null", querySession);
149
150         ResultSet resultSet2 = queryResult.getResultSet();
151         assertNotNull("The second result set should not be null", resultSet2);
152         logger.debug("There are " + resultSet2.getRows().length
153                 + " rows in the second set");
154         assertEquals("The result set size should be " + batchSize, batchSize,
155                 resultSet2.getRows().length);
156
157         // get the rest of the results to make sure it finishes properly
158
while (querySession != null)
159         {
160             queryResult = WebServiceFactory.getRepositoryService().fetchMore(querySession);
161             assertNotNull("queryResult returned in loop should not be null",
162                     queryResult);
163             querySession = queryResult.getQuerySession();
164             logger.debug("There were another "
165                     + queryResult.getResultSet().getRows().length
166                     + " rows returned");
167         }
168
169         // try and fetch some more results and we should get an error
170
try
171         {
172             queryResult = WebServiceFactory.getRepositoryService().fetchMore(origQuerySession);
173             fail("We should have seen an error as all the results have been returned");
174         } catch (Exception JavaDoc e)
175         {
176             // expected
177
}
178     }
179
180     /**
181      * Tests the queryParents service method
182      *
183      * @throws Exception
184      */

185     public void testQueryParents() throws Exception JavaDoc
186     {
187         // query for all the child nodes of the root
188
Reference node = BaseWebServiceSystemTest.rootReference;
189         String JavaDoc rootId = node.getUuid();
190
191         QueryResult rootChildren = WebServiceFactory.getRepositoryService().queryChildren(node);
192
193         assertNotNull("rootChildren should not be null", rootChildren);
194         ResultSet rootChildrenResults = rootChildren.getResultSet();
195         assertNotNull("rootChildrenResults should not be null",
196                 rootChildrenResults);
197         assertTrue("There should be at least one child of the root node",
198                 rootChildrenResults.getRows().length > 0);
199
200         // get hold of the id of the first child
201
ResultSetRow firstRow = rootChildrenResults.getRows(0);
202         assertNotNull("getColumns() should not return null", firstRow
203                 .getColumns());
204         String JavaDoc id = firstRow.getNode().getId();
205         logger.debug("Retrieving parents for first node found: " + id + "....");
206
207         node = new Reference();
208         node.setStore(BaseWebServiceSystemTest.store);
209         node.setUuid(id);
210         QueryResult parents = WebServiceFactory.getRepositoryService().queryParents(node);
211
212         assertNotNull("parents should not be null", parents);
213         ResultSet parentsResults = parents.getResultSet();
214         assertNotNull("parentsResults should not be null", parentsResults);
215         assertTrue("There should be at least one parent", parentsResults
216                 .getRows().length > 0);
217
218         // show the results
219
boolean rootFound = false;
220         ResultSetRow[] rows = parentsResults.getRows();
221         for (int x = 0; x < rows.length; x++)
222         {
223             ResultSetRow row = rows[x];
224             assertNotNull("getColumns() should not return null", row
225                     .getColumns());
226             ResultSetRowNode rowNode = row.getNode();
227             String JavaDoc nodeId = rowNode.getId();
228             logger.debug("parent node = " + nodeId + ", type = "
229                     + rowNode.getType());
230
231             if (nodeId.equals(rootId) == true)
232             {
233                 rootFound = true;
234             }
235         }
236
237         // make sure the root node was one of the parents
238
assertTrue("The root node was not found as one of the parents!!",
239                 rootFound);
240     }
241
242     /*
243      * Tests the queryAssociated service method
244      *
245      * @throws Exception
246      */

247     public void testQueryAssociated() throws Exception JavaDoc
248     {
249         Association association = new Association(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "translations"), AssociationDirectionEnum.target);
250         QueryResult result = WebServiceFactory.getRepositoryService().queryAssociated(BaseWebServiceSystemTest.contentReference, new Association[]{association});
251         assertNotNull(result);
252         assertNotNull(result.getResultSet());
253         assertNotNull(result.getResultSet().getRows());
254         assertEquals(1, result.getResultSet().getRows().length);
255     }
256
257     /**
258      * Tests the describe service method
259      *
260      * @throws Exception
261      */

262     public void testDescribe() throws Exception JavaDoc
263     {
264         // get hold of a node we know some info about so we can test the
265
// returned values (the Alfresco Tutorial PDF)
266
Query query = new Query(QueryLanguageEnum.lucene,
267                 "( +@\\{http\\://www.alfresco.org/1.0\\}name:test*) OR TEXT:test*");
268         QueryResult queryResult = this.repositoryService.query(BaseWebServiceSystemTest.store, query,
269                 false);
270         assertNotNull("queryResult should not be null", queryResult);
271         ResultSet resultSet = queryResult.getResultSet();
272         assertNotNull("The result set should not be null", resultSet);
273         assertTrue("There should be at least one result", resultSet
274                 .getTotalRowCount() > 0);
275         String JavaDoc id = resultSet.getRows(0).getNode().getId();
276         assertNotNull("Id of Alfresco Tutorial PDF should not be null", id);
277
278         // create a predicate object to to send to describe method
279
Reference ref = new Reference();
280         ref.setStore(BaseWebServiceSystemTest.store);
281         ref.setUuid(id);
282         Predicate predicate = new Predicate(new Reference[] { ref }, null, null);
283
284         // make the service call
285
NodeDefinition[] nodeDefs = WebServiceFactory.getRepositoryService().describe(predicate);
286         assertNotNull("nodeDefs should not be null", nodeDefs);
287         assertTrue("There should only be one result", nodeDefs.length == 1);
288
289         // get the result
290
NodeDefinition nodeDef = nodeDefs[0];
291         assertNotNull("The nodeDef should not be null", nodeDef);
292         ClassDefinition typeDef = nodeDef.getType();
293         assertNotNull("Type definition should not be null", typeDef);
294
295         assertEquals("Type name is incorrect",
296                 "{http://www.alfresco.org/model/content/1.0}content", typeDef
297                         .getName());
298         assertEquals("Superclass type name is incorrect",
299                 "{http://www.alfresco.org/model/content/1.0}cmobject", typeDef
300                         .getSuperClass());
301         assertEquals("Type title is incorrect", "Content", typeDef.getTitle());
302         assertEquals("Type description is incorrect", "Base Content Object", typeDef
303                 .getDescription());
304         assertFalse("Type is an aspect and it shouldn't be", typeDef
305                 .isIsAspect());
306         assertNull("There should not be any associations", typeDef
307                 .getAssociations());
308         assertNotNull("Properties should not be null", typeDef.getProperties());
309         assertEquals("There should be 2 properties", 2,
310                 typeDef.getProperties().length);
311
312         // check the name and type of each of the properties
313
assertEquals("Property1 name is incorrect",
314                 "{http://www.alfresco.org/model/content/1.0}content", typeDef
315                         .getProperties(0).getName());
316         assertEquals("Property1 type name is incorrect",
317                 "{http://www.alfresco.org/model/dictionary/1.0}content", typeDef
318                         .getProperties(0).getDataType());
319
320         assertEquals("Property5 name is incorrect",
321                 "{http://www.alfresco.org/model/content/1.0}name", typeDef
322                         .getProperties(1).getName());
323         assertEquals("Property5 type name is incorrect",
324                 "{http://www.alfresco.org/model/dictionary/1.0}text", typeDef
325                         .getProperties(1).getDataType());
326
327         // check the aspects
328
ClassDefinition[] aspects = nodeDef.getAspects();
329         assertNotNull("aspects should not be null", aspects);
330         assertEquals("There should be 2 aspects", 2, aspects.length);
331
332         // check the first aspect
333
ClassDefinition aspect1 = aspects[0];
334         assertEquals("Aspect1 name is incorrect",
335                 "{http://www.alfresco.org/model/system/1.0}referenceable",
336                 aspect1.getName());
337         assertTrue("Aspect1 should be an aspect", aspect1.isIsAspect());
338         assertNotNull("Aspect1 should have properties", aspect1.getProperties());
339         assertEquals("Aspect1 has wrong number of properties", 3, aspect1
340                 .getProperties().length);
341
342         // check the second aspect
343
ClassDefinition aspect2 = aspects[1];
344         assertEquals("Aspect2 name is incorrect",
345                 "{http://www.alfresco.org/model/content/1.0}auditable", aspect2
346                         .getName());
347         assertTrue("Aspect2 should be an aspect", aspect2.isIsAspect());
348         assertNotNull("Aspect2 should have properties", aspect2.getProperties());
349         assertEquals("Aspect2 has wrong number of properties", 5, aspect2
350                 .getProperties().length);
351     }
352
353     /**
354      * Tests passing a query in the predicate to return items to describe
355      *
356      * @throws Exception
357      */

358     public void testPredicateQuery() throws Exception JavaDoc
359     {
360         // define a query to add to the predicate (get everything that mentions
361
// 'test')
362
Query query = new Query(QueryLanguageEnum.lucene,
363                 "( +@\\{http\\://www.alfresco.org/1.0\\}name:test*) OR TEXT:test*");
364
365         Predicate predicate = new Predicate();
366         predicate.setQuery(query);
367         predicate.setStore(BaseWebServiceSystemTest.store);
368
369         // call the service and make sure we get some details back
370
NodeDefinition[] nodeDefs = WebServiceFactory.getRepositoryService().describe(predicate);
371         assertNotNull("nodeDefs should not be null", nodeDefs);
372         assertTrue("There should be at least one result", nodeDefs.length > 0);
373
374         NodeDefinition nodeDef = nodeDefs[0];
375         assertNotNull("The nodeDef should not be null", nodeDef);
376         ClassDefinition typeDef = nodeDef.getType();
377         assertNotNull("Type definition should not be null", typeDef);
378
379         logger.debug("type name = " + typeDef.getName());
380         logger.debug("is aspect = " + typeDef.isIsAspect());
381         PropertyDefinition[] propDefs = typeDef.getProperties();
382         if (propDefs != null)
383         {
384             logger.debug("There are " + propDefs.length + " properties:");
385             for (int x = 0; x < propDefs.length; x++)
386             {
387                 PropertyDefinition propDef = propDefs[x];
388                 logger.debug("name = " + propDef.getName() + " type = "
389                         + propDef.getDataType());
390             }
391         }
392     }
393
394     /**
395      * Tests the use of a path within a reference
396      *
397      * @throws Exception
398      */

399     public void testPathReference() throws Exception JavaDoc
400     {
401         // setup a predicate to find the test folder using an xpath
402
Reference ref = new Reference();
403         ref.setStore(BaseWebServiceSystemTest.store);
404         ref.setPath("//*[@cm:name = '" + FOLDER_NAME + "']");
405         Predicate predicate = new Predicate();
406         predicate.setNodes(new Reference[] { ref });
407
408         // call the service and make sure we get some details back
409
NodeDefinition[] nodeDefs = WebServiceFactory.getRepositoryService().describe(predicate);
410         assertNotNull("nodeDefs should not be null", nodeDefs);
411         assertTrue("There should be at least one result", nodeDefs.length > 0);
412
413         NodeDefinition nodeDef = nodeDefs[0];
414         assertNotNull("The nodeDef should not be null", nodeDef);
415         ClassDefinition typeDef = nodeDef.getType();
416         assertNotNull("Type definition should not be null", typeDef);
417
418         logger.debug("type name = " + typeDef.getName());
419         assertEquals("Type is incorrect",
420                 "{http://www.alfresco.org/model/content/1.0}folder", typeDef
421                         .getName());
422         logger.debug("is aspect = " + typeDef.isIsAspect());
423         assertFalse("Item should not be an aspect", typeDef.isIsAspect());
424         PropertyDefinition[] propDefs = typeDef.getProperties();
425         if (propDefs != null)
426         {
427             logger.debug("There are " + propDefs.length + " properties:");
428             for (int x = 0; x < propDefs.length; x++)
429             {
430                 PropertyDefinition propDef = propDefs[x];
431                 logger.debug("name = " + propDef.getName() + " type = "
432                         + propDef.getDataType());
433             }
434         }
435     }
436
437     /**
438      * Tests the update service method
439      *
440      * @throws Exception
441      */

442     public void testUpdate() throws Exception JavaDoc
443     {
444         CMLCreate create = new CMLCreate();
445         create.setId("id1");
446         create.setType(Constants.TYPE_CONTENT);
447
448         ParentReference parentReference = new ParentReference();
449         parentReference.setAssociationType(Constants.ASSOC_CHILDREN);
450         parentReference.setChildName(Constants.ASSOC_CHILDREN);
451         parentReference.setStore(BaseWebServiceSystemTest.store);
452         parentReference.setUuid(BaseWebServiceSystemTest.rootReference.getUuid());
453
454         create.setParent(parentReference);
455         create.setProperty(new NamedValue[] {
456                         new NamedValue(
457                                 Constants.PROP_NAME,
458                                 "name")});
459         
460         CMLAddAspect aspect = new CMLAddAspect();
461         aspect.setAspect(Constants.ASPECT_VERSIONABLE);
462         aspect.setWhere_id("id1");
463
464         CML cml = new CML();
465         cml.setCreate(new CMLCreate[]{create});
466         cml.setAddAspect(new CMLAddAspect[]{aspect});
467         
468         UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);
469         assertNotNull(results);
470         assertEquals(2, results.length);
471     }
472     
473     public void testGet()
474         throws Exception JavaDoc
475     {
476         Predicate predicate = new Predicate(null, BaseWebServiceSystemTest.store, null);
477         Node[] nodes = WebServiceFactory.getRepositoryService().get(predicate);
478         assertNotNull(nodes);
479         assertEquals(1, nodes.length);
480         
481         Node rootNode = nodes[0];
482         assertEquals(BaseWebServiceSystemTest.rootReference.getUuid(), rootNode.getReference().getUuid());
483         
484         logger.debug("Root node type = " + rootNode.getType());
485         String JavaDoc aspects = "";
486         for (String JavaDoc aspect : rootNode.getAspects())
487         {
488             aspects += aspect + ", ";
489         }
490         logger.debug("Root node aspects = " + aspects);
491         for (NamedValue prop : rootNode.getProperties())
492         {
493             logger.debug("Root node property " + prop.getName() + " = " + prop.getValue());
494         }
495         
496     }
497 }
498
Popular Tags