KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import org.alfresco.webservice.authoring.AuthoringServiceSoapBindingStub;
21 import org.alfresco.webservice.authoring.CancelCheckoutResult;
22 import org.alfresco.webservice.authoring.CheckinResult;
23 import org.alfresco.webservice.authoring.CheckoutResult;
24 import org.alfresco.webservice.authoring.LockStatus;
25 import org.alfresco.webservice.authoring.LockTypeEnum;
26 import org.alfresco.webservice.authoring.VersionResult;
27 import org.alfresco.webservice.content.Content;
28 import org.alfresco.webservice.types.ContentFormat;
29 import org.alfresco.webservice.types.NamedValue;
30 import org.alfresco.webservice.types.ParentReference;
31 import org.alfresco.webservice.types.Predicate;
32 import org.alfresco.webservice.types.Reference;
33 import org.alfresco.webservice.types.Version;
34 import org.alfresco.webservice.types.VersionHistory;
35 import org.alfresco.webservice.util.Constants;
36 import org.alfresco.webservice.util.ContentUtils;
37 import org.alfresco.webservice.util.WebServiceFactory;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 public class AuthoringServiceSystemTest extends BaseWebServiceSystemTest
42 {
43     @SuppressWarnings JavaDoc("unused")
44     private static Log logger = LogFactory.getLog(AuthoringServiceSystemTest.class);
45
46     private static final String JavaDoc INITIAL_VERSION_CONTENT = "Content of the initial version";
47     private static final String JavaDoc SECOND_VERSION_CONTENT = "The content for the second version is completely different";
48     
49     private static final String JavaDoc VALUE_DESCRIPTION = "description";
50
51     private AuthoringServiceSoapBindingStub authoringService;
52
53     public AuthoringServiceSystemTest()
54     {
55         this.authoringService = WebServiceFactory.getAuthoringService();
56     }
57     
58     /**
59      * Tests the checkout service method
60      *
61      * @throws Exception
62      */

63     public void testCheckout() throws Exception JavaDoc
64     {
65         doCheckOut();
66         
67         // TODO test multiple checkout
68
}
69     
70     /**
71      * Reusable method to do a standard checkout
72      *
73      * @return
74      * @throws Exception
75      */

76     private Reference doCheckOut() throws Exception JavaDoc
77     {
78         // Use the helper to create the verionable node
79
Reference reference = createContentAtRoot("version_test.txt", INITIAL_VERSION_CONTENT);
80         Predicate predicate = convertToPredicate(reference);
81
82         // Check the content out (to the same detination)
83
CheckoutResult result = this.authoringService.checkout(predicate, null);
84         assertNotNull(result);
85         assertEquals(1, result.getOriginals().length);
86         assertEquals(1, result.getWorkingCopies().length);
87         
88         // TODO need to check that the working copy and the origional are in the correct states ...
89

90         return result.getWorkingCopies()[0];
91     }
92
93     /**
94      * Tests the checkout service method passing a destination for the working
95      * copy
96      *
97      * @throws Exception
98      */

99     public void testCheckoutWithDestination() throws Exception JavaDoc
100     {
101         Reference reference = createContentAtRoot("version_test.txt", INITIAL_VERSION_CONTENT);
102         Predicate predicate = convertToPredicate(reference);
103         ParentReference parentReference = getFolderParentReference("{test}workingCopy");
104         
105         // Checkout the content to the folder
106
CheckoutResult result = this.authoringService.checkout(predicate, parentReference);
107         assertNotNull(result);
108         assertEquals(1, result.getOriginals().length);
109         assertEquals(1, result.getWorkingCopies().length);
110         
111         // TODO need to check that the working copy and the origional are in the correct states
112
}
113
114     /**
115      * Tests the checkin service method
116      *
117      * @throws Exception
118      */

119     public void testCheckin() throws Exception JavaDoc
120     {
121         // First we need to check a document out
122
Reference workingCopy = doCheckOut();
123         
124         // Check in but keep checked out
125
Predicate predicate = convertToPredicate(workingCopy);
126         NamedValue[] comments = getVersionComments();
127         CheckinResult checkinResult = this.authoringService.checkin(predicate, comments, true);
128         
129         // Check the result
130
assertNotNull(checkinResult);
131         assertEquals(1, checkinResult.getCheckedIn().length);
132         assertEquals(1, checkinResult.getWorkingCopies().length);
133         // TODO check that state of the orig and working copies
134

135         // Checkin but don't keep checked out
136
Predicate predicate2 = convertToPredicate(checkinResult.getWorkingCopies()[0]);
137         CheckinResult checkinResult2 = this.authoringService.checkin(predicate2, comments, false);
138         
139         // Check the result
140
assertNotNull(checkinResult2);
141         assertEquals(1, checkinResult2.getCheckedIn().length);
142         assertNull(checkinResult2.getWorkingCopies());
143         // TODO check the above behaviour ...
144
// TODO check that the state of the org and working copies
145

146         // TODO check multiple checkin
147
}
148
149     /**
150      * Helper method to get a list of version comments
151      *
152      * @return
153      */

154     private NamedValue[] getVersionComments()
155     {
156         NamedValue[] comments = new NamedValue[1];
157         comments[0] = new NamedValue("description", VALUE_DESCRIPTION);
158         return comments;
159     }
160
161     /**
162      * Tests the checkinExternal service method
163      *
164      * @throws Exception
165      */

166     public void testCheckinExternal() throws Exception JavaDoc
167     {
168         // First we need to check a document out
169
Reference workingCopy = doCheckOut();
170         
171         // Check in with external content
172
NamedValue[] comments = getVersionComments();
173         ContentFormat contentFormat = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8");
174         Reference origionalNode = this.authoringService.checkinExternal(workingCopy, comments, false, contentFormat, SECOND_VERSION_CONTENT.getBytes());
175         
176         // Check the origianl Node
177
assertNotNull(origionalNode);
178         Content[] contents = this.contentService.read(new Predicate(new Reference[]{origionalNode}, BaseWebServiceSystemTest.store, null), Constants.PROP_CONTENT.toString());
179         Content readResult = contents[0];
180         assertNotNull(readResult);
181         String JavaDoc checkedInContent = ContentUtils.getContentAsString(readResult);
182         assertNotNull(checkedInContent);
183         assertEquals(SECOND_VERSION_CONTENT, checkedInContent);
184     }
185
186     /**
187      * Tests the cancelCheckout service method
188      *
189      * @throws Exception
190      */

191     public void testCancelCheckout() throws Exception JavaDoc
192     {
193         // Check out a node
194
Reference workingCopy = doCheckOut();
195         
196         // Cancel the check out
197
Predicate predicate = convertToPredicate(workingCopy);
198         CancelCheckoutResult result = this.authoringService.cancelCheckout(predicate);
199         
200         // Check the result
201
assertNotNull(result);
202         assertEquals(1, result.getOriginals().length);
203         // TODO check that state of the orig and that the working copy has been deleted
204

205         // TODO I don't think that the working copies should be returned in the result since they have been deleted !!
206
}
207
208     /**
209      * Tests the lock service methods, lock, unlock and getStaus
210      *
211      * @throws Exception
212      */

213     public void testLockUnLockGetStatus() throws Exception JavaDoc
214     {
215         Reference reference = createContentAtRoot("lock_test1.txt", INITIAL_VERSION_CONTENT);
216         Predicate predicate = convertToPredicate(reference);
217         
218         // Get the status
219
checkLockStatus(predicate, null, null);
220         
221         // Lock with a write lock
222
Reference[] lockedRefs = this.authoringService.lock(predicate, false, LockTypeEnum.write);
223         assertNotNull(lockedRefs);
224         assertEquals(1, lockedRefs.length);
225         // TODO check in more detail
226

227         // Get the status
228
checkLockStatus(predicate, USERNAME, LockTypeEnum.write);
229         
230         // Unlock (bad)
231
// try
232
// {
233
// this.authoringService.unlock(predicate, "bad", false);
234
// fail("This should have thrown an exception.");
235
// }
236
// catch (Throwable exception)
237
// {
238
// // Good .. we where expceting this
239
// }
240

241         // Unlock (good)
242
Reference[] unlocked = this.authoringService.unlock(predicate, false);
243         assertNotNull(unlocked);
244         assertEquals(1, unlocked.length);
245         
246         // Get the status
247
checkLockStatus(predicate, null, null);
248         
249         // Read lock
250
Reference[] lockedRefs2 = this.authoringService.lock(predicate, false, LockTypeEnum.read);
251         
252         assertNotNull(lockedRefs2);
253         assertEquals(1, lockedRefs2.length);
254         // TODO check in more detail
255

256         // Get the status
257
checkLockStatus(predicate, USERNAME, LockTypeEnum.read);
258     }
259     
260     private void checkLockStatus(Predicate predicate, String JavaDoc lockOwner, LockTypeEnum lockType)
261         throws Exception JavaDoc
262     {
263         LockStatus[] lockStatus1 = this.authoringService.getLockStatus(predicate);
264         assertNotNull(lockStatus1);
265         assertEquals(1, lockStatus1.length);
266         LockStatus ls1 = lockStatus1[0];
267         assertNotNull(ls1);
268         assertEquals(lockOwner, ls1.getLockOwner());
269         assertEquals(lockType, ls1.getLockType());
270     }
271
272     /**
273      * Tests the createVersion service method
274      *
275      * @throws Exception
276      */

277     public void testVersionMethods() throws Exception JavaDoc
278     {
279         Reference reference = createContentAtRoot("create_version_test.txt", INITIAL_VERSION_CONTENT);
280         Predicate predicate = convertToPredicate(reference);
281         
282         // Get the version history (before its been versioned)
283
VersionHistory emptyVersionHistory = this.authoringService.getVersionHistory(reference);
284         assertNotNull(emptyVersionHistory);
285         assertNull(emptyVersionHistory.getVersions());
286         
287         // Create the version
288
VersionResult result = this.authoringService.createVersion(predicate, getVersionComments(), false);
289         assertNotNull(result);
290         assertEquals(1, result.getNodes().length);
291         assertEquals(1, result.getVersions().length);
292         Version version = result.getVersions()[0];
293         assertEquals("1.0", version.getLabel());
294         // TODO check commentaries
295
// TODO check creator
296

297         // Get the version history
298
VersionHistory versionHistory = this.authoringService.getVersionHistory(reference);
299         assertNotNull(versionHistory);
300         assertEquals(2, versionHistory.getVersions().length);
301         // TODO some more tests ...
302

303         // Update the content
304
this.contentService.write(reference, Constants.PROP_CONTENT, SECOND_VERSION_CONTENT.getBytes(), null);
305         
306         // Create another version
307
VersionResult versionResult2 = this.authoringService.createVersion(predicate, getVersionComments(), false);
308         assertNotNull(versionResult2);
309         assertEquals(1, versionResult2.getNodes().length);
310         assertEquals(1, versionResult2.getVersions().length);
311         Version version2 = versionResult2.getVersions()[0];
312         assertEquals("1.3", version2.getLabel());
313         // TODO check commentaries
314
// TODO check creator
315

316         // Check the version history
317
VersionHistory versionHistory2 = this.authoringService.getVersionHistory(reference);
318         assertNotNull(versionHistory2);
319         assertEquals(4, versionHistory2.getVersions().length);
320         // TODO some more tests ...
321

322         // Confirm the current content of the node
323
Content[] contents = this.contentService.read(new Predicate(new Reference[]{reference}, BaseWebServiceSystemTest.store, null), Constants.PROP_CONTENT.toString());
324         Content readResult1 = contents[0];
325         String JavaDoc content1 = ContentUtils.getContentAsString(readResult1);
326         assertEquals(SECOND_VERSION_CONTENT, content1);
327         
328         // Revert the node to the first version
329
this.authoringService.revertVersion(reference, "1.0");
330         
331         // Confirm that the state of the node has been reverted
332
Content[] contents2 = this.contentService.read(new Predicate(new Reference[]{reference}, BaseWebServiceSystemTest.store, null), Constants.PROP_CONTENT.toString());
333         Content readResult2 = contents2[0];
334         String JavaDoc content2 = ContentUtils.getContentAsString(readResult2);
335         assertEquals(INITIAL_VERSION_CONTENT, content2);
336         
337         // Now delete the version history
338
VersionHistory deletedVersionHistory = this.authoringService.deleteAllVersions(reference);
339         assertNotNull(deletedVersionHistory);
340         assertNull(deletedVersionHistory.getVersions());
341         
342         // Check the version history
343
VersionHistory versionHistory3 = this.authoringService.getVersionHistory(reference);
344         assertNotNull(versionHistory3);
345         assertNull(versionHistory3.getVersions());
346     }
347 }
348
Popular Tags