KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > example > webservice > repository > 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.example.webservice.repository;
18
19 import javax.xml.rpc.ServiceException JavaDoc;
20
21 import junit.framework.AssertionFailedError;
22
23 import org.alfresco.example.webservice.BaseWebServiceSystemTest;
24 import org.alfresco.example.webservice.types.ClassDefinition;
25 import org.alfresco.example.webservice.types.NamedValue;
26 import org.alfresco.example.webservice.types.NodeDefinition;
27 import org.alfresco.example.webservice.types.Predicate;
28 import org.alfresco.example.webservice.types.PropertyDefinition;
29 import org.alfresco.example.webservice.types.Query;
30 import org.alfresco.example.webservice.types.QueryConfiguration;
31 import org.alfresco.example.webservice.types.QueryLanguageEnum;
32 import org.alfresco.example.webservice.types.Reference;
33 import org.alfresco.example.webservice.types.ResultSet;
34 import org.alfresco.example.webservice.types.ResultSetRow;
35 import org.alfresco.example.webservice.types.ResultSetRowNode;
36 import org.alfresco.example.webservice.types.Store;
37 import org.alfresco.example.webservice.types.StoreEnum;
38 import org.alfresco.service.cmr.repository.StoreRef;
39 import org.apache.axis.EngineConfiguration;
40 import org.apache.axis.configuration.FileProvider;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 public class RepositoryServiceSystemTest extends BaseWebServiceSystemTest
45 {
46    private static Log logger = LogFactory.getLog(RepositoryServiceSystemTest.class);
47    private static Store STORE = new Store(StoreEnum.workspace, "SpacesStore");
48    private static StoreRef STORE_REF = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
49
50    private RepositoryServiceSoapBindingStub repoService;
51
52    @Override JavaDoc
53    protected void setUp() throws Exception JavaDoc
54    {
55       super.setUp();
56
57       try
58       {
59          EngineConfiguration config = new FileProvider(getResourcesDir(), "client-deploy.wsdd");
60          this.repoService = (RepositoryServiceSoapBindingStub)new RepositoryServiceLocator(config).getRepositoryService();
61       }
62       catch (ServiceException JavaDoc jre)
63       {
64          if (jre.getLinkedCause() != null)
65          {
66             jre.getLinkedCause().printStackTrace();
67          }
68          
69          throw new AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
70       }
71       
72       assertNotNull("repoService is null", this.repoService);
73       
74       // Time out after a minute
75
this.repoService.setTimeout(60000);
76    }
77
78    /**
79     * Tests the getStores method
80     *
81     * @throws Exception
82     */

83    public void testGetStores() throws Exception JavaDoc
84    {
85       Store[] stores = this.repoService.getStores();
86       assertNotNull("stores array should not be null", stores);
87       logger.debug("store1 = " + stores[0].getScheme() + ":" + stores[0].getAddress());
88       logger.debug("store2 = " + stores[1].getScheme() + ":" + stores[1].getAddress());
89       logger.debug("store3 = " + stores[2].getScheme() + ":" + stores[2].getAddress());
90       assertTrue("There should be 3 stores", stores.length == 3);
91    }
92    
93    /**
94     * Tests the query service call
95     *
96     * @throws Exception
97     */

98    public void testQuery() throws Exception JavaDoc
99    {
100       //Query query = new Query(QueryLanguageEnum.lucene, "*");
101
Query query = new Query(QueryLanguageEnum.lucene, "( +@\\{http\\://www.alfresco.org/1.0\\}name:test*) OR TEXT:test*");
102       
103       QueryResult queryResult = this.repoService.query(STORE, query, false);
104       assertNotNull("queryResult should not be null", queryResult);
105       
106       ResultSet resultSet = queryResult.getResultSet();
107       assertNotNull("The result set should not be null", resultSet);
108       logger.debug("There are " + resultSet.getTotalRowCount() + " rows:");
109       
110       if (resultSet.getTotalRowCount() > 0)
111       {
112          ResultSetRow[] rows = resultSet.getRows();
113          for (int x = 0; x < rows.length; x++)
114          {
115             ResultSetRow row = rows[x];
116             NamedValue[] columns = row.getColumns();
117             for (int y = 0; y < columns.length; y++)
118             {
119                logger.debug("row " + x + ": " + row.getColumns(y).getName() + " = " + row.getColumns(y).getValue());
120             }
121          }
122       }
123       else
124       {
125          logger.debug("The query returned no results");
126          fail("The query returned no results");
127       }
128    }
129    
130    /**
131     * Tests the ability to retrieve the results of a query in batches
132     *
133     * @throws Exception
134     */

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

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

253    public void testQueryAssociated() throws Exception JavaDoc
254    {
255       try
256       {
257          this.repoService.queryAssociated(null, null);
258          fail("This method should have thrown a repository fault");
259       }
260       catch (RepositoryFault rf)
261       {
262          // expected to get this
263
}
264    }
265    
266    /**
267     * Tests the describe service method
268     *
269     * @throws Exception
270     */

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

360    public void testPredicateQuery() throws Exception JavaDoc
361    {
362       // define a query to add to the predicate (get everything that mentions 'test')
363
Query query = new Query(QueryLanguageEnum.lucene, "( +@\\{http\\://www.alfresco.org/1.0\\}name:test*) OR TEXT:test*");
364       
365       Predicate predicate = new Predicate();
366       predicate.setQuery(query);
367       predicate.setStore(STORE);
368       
369       // call the service and make sure we get some details back
370
NodeDefinition[] nodeDefs = this.repoService.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 = " + propDef.getDataType());
389          }
390       }
391    }
392    
393    /**
394     * Tests the use of a path within a reference
395     *
396     * @throws Exception
397     */

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

438    public void testUpdate() throws Exception JavaDoc
439    {
440       try
441       {
442          this.repoService.update(null);
443          fail("This method should have thrown a repository fault");
444       }
445       catch (RepositoryFault rf)
446       {
447          // expected to get this
448
}
449    }
450 }
451
Popular Tags