KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.InputStream JavaDoc;
20
21 import org.alfresco.webservice.action.Action;
22 import org.alfresco.webservice.classification.AppliedCategory;
23 import org.alfresco.webservice.classification.CategoriesResult;
24 import org.alfresco.webservice.classification.ClassificationServiceSoapBindingStub;
25 import org.alfresco.webservice.repository.UpdateResult;
26 import org.alfresco.webservice.types.CML;
27 import org.alfresco.webservice.types.CMLCreate;
28 import org.alfresco.webservice.types.Category;
29 import org.alfresco.webservice.types.ClassDefinition;
30 import org.alfresco.webservice.types.Classification;
31 import org.alfresco.webservice.types.ContentFormat;
32 import org.alfresco.webservice.types.NamedValue;
33 import org.alfresco.webservice.types.ParentReference;
34 import org.alfresco.webservice.types.Predicate;
35 import org.alfresco.webservice.types.Reference;
36 import org.alfresco.webservice.util.Constants;
37 import org.alfresco.webservice.util.ContentUtils;
38 import org.alfresco.webservice.util.WebServiceFactory;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 public class ClassificationServiceSystemTest extends BaseWebServiceSystemTest
43 {
44     private static Log logger = LogFactory
45             .getLog(ClassificationServiceSystemTest.class);
46
47     private ClassificationServiceSoapBindingStub classificationService;
48
49     private static boolean categoriesLoaded = false;
50     
51     @Override JavaDoc
52     protected void setUp() throws Exception JavaDoc
53     {
54         super.setUp();
55
56         this.classificationService = WebServiceFactory.getClassificationService();
57         
58         if (ClassificationServiceSystemTest.categoriesLoaded == false)
59         {
60             // Import categories into the new store
61
InputStream JavaDoc viewStream = getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.acp");
62             byte[] byteArray = ContentUtils.convertToByteArray(viewStream);
63             
64             // Create the node that will contain the category import file
65
ParentReference categoryParentRef = new ParentReference(BaseWebServiceSystemTest.store, BaseWebServiceSystemTest.rootReference.getUuid(), null, Constants.ASSOC_CHILDREN, "{test}testContent");
66             NamedValue[] categoryProperties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "categories.acp")};
67             CMLCreate createCategory = new CMLCreate("categoryImport", categoryParentRef, Constants.TYPE_CONTENT, categoryProperties);
68             CML cml2 = new CML();
69             cml2.setCreate(new CMLCreate[]{createCategory});
70             UpdateResult[] updateResult = this.repositoryService.update(cml2);
71             Reference categoryReference = updateResult[0].getDestination();
72             
73             // Upload the content to the node
74
this.contentService.write(
75                     categoryReference,
76                     Constants.PROP_CONTENT,
77                     byteArray,
78                     new ContentFormat("application/acp", "UTF-8"));
79             
80             // Create an action to import the categories
81
String JavaDoc rootNodeRef = store.getScheme().getValue() + "://" + store.getAddress() + "/" + rootReference.getUuid();
82             Action importAction = new Action();
83             importAction.setActionName("import");
84             NamedValue[] params = new NamedValue[]{
85                     new NamedValue("encoding", "UTF-8"),
86                     new NamedValue("destination", rootNodeRef)
87             };
88             importAction.setParameters(params);
89             
90             WebServiceFactory.getActionService().executeActions(
91                     new Predicate(new Reference[]{categoryReference}, store, null),
92                     new Action[]{importAction});
93             
94             ClassificationServiceSystemTest.categoriesLoaded = true;
95         }
96     }
97
98     /**
99      * Tests the getClassifications service method
100      *
101      * @throws Exception
102      */

103     public void testGetClassifications() throws Exception JavaDoc
104     {
105         Classification[] classifications = this.classificationService
106                 .getClassifications(BaseWebServiceSystemTest.store);
107
108         assertNotNull(classifications);
109         assertTrue((classifications.length != 0));
110         Classification classification = classifications[0];
111         assertNotNull(classification.getTitle());
112         assertNotNull(classification.getRootCategory());
113         assertNotNull(classification.getRootCategory().getId());
114         assertNotNull(classification.getRootCategory().getTitle());
115         
116         if (logger.isDebugEnabled() == true)
117         {
118             for (Classification item : classifications)
119             {
120                 logger.debug(
121                         "Classification '" +
122                         item.getTitle() +
123                         "' with root category '" +
124                         item.getRootCategory().getTitle() +
125                         "'");
126             }
127         }
128     }
129
130     /**
131      * Tests the getChildCategories service method
132      *
133      * @throws Exception
134      */

135     public void testGetChildCategories() throws Exception JavaDoc
136     {
137         Classification[] classifications = this.classificationService.getClassifications(BaseWebServiceSystemTest.store);
138         Reference parentCategory = classifications[0].getRootCategory().getId();
139         
140         Category[] categories = this.classificationService.getChildCategories(parentCategory);
141         assertNotNull(categories);
142         assertTrue((categories.length != 0));
143         Category item = categories[0];
144         assertNotNull(item.getId());
145         assertNotNull(item.getTitle());
146         
147         if (logger.isDebugEnabled() == true)
148         {
149             for (Category category : categories)
150             {
151                 logger.debug(
152                         "Sub-category '" +
153                         category.getTitle() +
154                         "'");
155             }
156         }
157     }
158
159     /**
160      * Tests the getCategories and setCategories service methods
161      *
162      * @throws Exception
163      */

164     public void testGetAndSetCategories() throws Exception JavaDoc
165     {
166         Classification[] classifications = this.classificationService.getClassifications(BaseWebServiceSystemTest.store);
167         String JavaDoc classification = classifications[0].getClassification();
168         Reference category = classifications[0].getRootCategory().getId();
169         
170         Reference reference = createContentAtRoot("TestContent" + System.currentTimeMillis(), "Any old content.");
171         Predicate predicate = convertToPredicate(reference);
172         
173         // First try and get the categories for a uncategoried node
174
CategoriesResult[] result1 = this.classificationService.getCategories(predicate);
175         assertNotNull(result1);
176         assertEquals(1, result1.length);
177         assertNull(result1[0].getCategories());
178         
179         AppliedCategory appliedCategory = new AppliedCategory();
180         appliedCategory.setCategories(new Reference[]{category});
181         appliedCategory.setClassification(classification);
182         
183         AppliedCategory[] appliedCategories = new AppliedCategory[]{appliedCategory};
184         
185         // Now classify the node
186
CategoriesResult[] result2 = this.classificationService.setCategories(predicate, appliedCategories);
187         assertNotNull(result2);
188         assertEquals(1, result2.length);
189         
190         // Now get the value back
191
CategoriesResult[] result3 = this.classificationService.getCategories(predicate);
192         assertNotNull(result3);
193         assertEquals(1, result3.length);
194         CategoriesResult categoryResult = result3[0];
195         assertEquals(reference.getUuid(), categoryResult.getNode().getUuid());
196         AppliedCategory[] appCats = categoryResult.getCategories();
197         assertNotNull(appCats);
198         assertEquals(1, appCats.length);
199         AppliedCategory appCat = appCats[0];
200         assertEquals(classification, appCat.getClassification());
201         Reference[] refs = appCat.getCategories();
202         assertNotNull(refs);
203         assertEquals(1, refs.length);
204         Reference ref = refs[0];
205         assertEquals(category.getUuid(), ref.getUuid());
206         
207         // TODO test multiple classifiations
208
// TODO test clearing the classifications
209
// TODO test updating the classifications
210
}
211
212
213     /**
214      * Tests the describeClassification service method
215      *
216      * @throws Exception
217      */

218     public void testDescribeClassification() throws Exception JavaDoc
219     {
220         Classification[] classifications = this.classificationService.getClassifications(BaseWebServiceSystemTest.store);
221         String JavaDoc classification = classifications[0].getClassification();
222         
223         ClassDefinition classDefinition = this.classificationService.describeClassification(classification);
224         
225         assertNotNull(classDefinition);
226         
227         // TODO test the result more rigiously
228
}
229 }
230
Popular Tags