KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > AappTest


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.webservices.adminapi;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.TimeZone JavaDoc;
25 import junit.framework.TestCase;
26 import org.apache.commons.httpclient.HttpClient;
27 import org.apache.commons.httpclient.HttpMethod;
28 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
29 import org.apache.commons.httpclient.methods.PostMethod;
30 import org.apache.commons.httpclient.methods.DeleteMethod;
31 import org.apache.commons.httpclient.methods.GetMethod;
32 import org.apache.commons.httpclient.methods.PutMethod;
33 import org.apache.commons.httpclient.util.Base64;
34 import org.apache.roller.webservices.adminapi.sdk.MemberEntry;
35 import org.apache.roller.webservices.adminapi.sdk.MemberEntrySet;
36 import org.apache.roller.webservices.adminapi.sdk.MissingElementException;
37 import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
38 import org.apache.roller.webservices.adminapi.sdk.UserEntry;
39 import org.apache.roller.webservices.adminapi.sdk.UserEntrySet;
40 import org.apache.roller.webservices.adminapi.sdk.WeblogEntry;
41 import org.apache.roller.webservices.adminapi.sdk.WeblogEntrySet;
42 import org.jdom.JDOMException;
43
44 public class AappTest extends TestCase {
45     public static class HttpResponse {
46         private int status;
47         private InputStream JavaDoc responseBody;
48         
49         public HttpResponse(int status) {
50             this(status, null);
51         }
52         
53         public HttpResponse(int status, InputStream JavaDoc responseBody) {
54             this.status = status;
55             this.responseBody = responseBody;
56         }
57         
58         public int getStatus() {
59             return status;
60         }
61         
62         public InputStream JavaDoc getResponseBody() {
63             return responseBody;
64         }
65     }
66     
67     private static final Date JavaDoc sampleDate = new Date JavaDoc();
68     private static final String JavaDoc DEFAULT_ENDPOINT_URL = "http://localhost:8080/roller/aapp";
69     private static final String JavaDoc DEFAULT_USER = "jtb";
70     private static final String JavaDoc DEFAULT_PASSWORD = "iplanet";
71     
72     protected static String JavaDoc getEndpointUrl() {
73         String JavaDoc endpoint = System.getProperty("aapp.endpoint");
74         if (endpoint == null) {
75             endpoint = DEFAULT_ENDPOINT_URL;
76         }
77         
78         System.err.println("endpoint=" + endpoint);
79         return endpoint;
80     }
81
82     protected static String JavaDoc getUser() {
83         String JavaDoc user = System.getProperties().getProperty("aapp.user");
84         if (user == null) {
85             user = DEFAULT_USER;
86         }
87         
88         System.err.println("user=" + user);
89         return user;
90     }
91
92     protected static String JavaDoc getPassword() {
93         String JavaDoc password = System.getProperties().getProperty("aapp.password");
94         if (password == null) {
95             password = DEFAULT_PASSWORD;
96         }
97         
98         System.err.println("password=" + password);
99         return password;
100     }
101
102     protected static UserEntry getSampleUserEntry() {
103         UserEntry ue = new UserEntry("foo", getEndpointUrl());
104         ue.setEmailAddress("foo@bar.org");
105         ue.setFullName("Foo Bar");
106         ue.setLocale(Locale.getDefault());
107         ue.setTimezone(TimeZone.getDefault());
108         ue.setPassword("foo");
109         
110         return ue;
111     }
112
113     protected static MemberEntry getSampleMemberEntry() {
114         MemberEntry me = new MemberEntry("fooblog", "foo", getEndpointUrl());
115         me.setPermission(MemberEntry.Permissions.AUTHOR);
116         return me;
117     }
118     
119     protected static UserEntry updateSampleUserEntry(UserEntry ue) {
120         UserEntry ueUpdate = new UserEntry(ue.getName(), getEndpointUrl());
121         ueUpdate.setEmailAddress("billy@bob.org");
122         ueUpdate.setFullName("Billy Bob");
123         ueUpdate.setLocale(new Locale JavaDoc("ms", "MY"));
124         ueUpdate.setTimezone(TimeZone.getTimeZone("Asia/Kuala_Lumpur"));
125         ueUpdate.setPassword("billy");
126         
127         return ueUpdate;
128     }
129
130     protected static WeblogEntry updateSampleWeblogEntry(WeblogEntry we) {
131         WeblogEntry weUpdate = new WeblogEntry(we.getHandle(), getEndpointUrl());
132         weUpdate.setEmailAddress("billy@bob.org");
133         weUpdate.setName("Billy Bob Weblog Name");
134         weUpdate.setLocale(new Locale JavaDoc("ms", "MY"));
135         weUpdate.setTimezone(TimeZone.getTimeZone("Asia/Kuala_Lumpur"));
136         weUpdate.setDescription("Billy Bob Weblog Description");
137         weUpdate.setCreatingUser(we.getCreatingUser());
138         
139         return weUpdate;
140     }
141
142     protected static MemberEntry updateSampleMemberEntry(MemberEntry me) {
143         MemberEntry meUpdate = new MemberEntry(me.getHandle(), me.getName(), getEndpointUrl());
144         meUpdate.setPermission(MemberEntry.Permissions.LIMITED);
145         
146         return meUpdate;
147     }
148     
149     protected static UserEntrySet updateSampleUserEntrySet(UserEntrySet ues) {
150         UserEntry ue = (UserEntry)ues.getEntries()[0];
151         UserEntry ueUpdated = updateSampleUserEntry(ue);
152         UserEntrySet uesUpdated = new UserEntrySet(getEndpointUrl());
153         uesUpdated.setEntries(new UserEntry[] { ueUpdated });
154         
155         return uesUpdated;
156     }
157     
158     protected static WeblogEntrySet updateSampleWeblogEntrySet(WeblogEntrySet wes) {
159         WeblogEntry we = (WeblogEntry)wes.getEntries()[0];
160         WeblogEntry weUpdated = updateSampleWeblogEntry(we);
161         WeblogEntrySet wesUpdated = new WeblogEntrySet(getEndpointUrl());
162         wesUpdated.setEntries(new WeblogEntry[] { weUpdated });
163         
164         return wesUpdated;
165     }
166
167     protected static MemberEntrySet updateSampleMemberEntrySet(MemberEntrySet mes) {
168         MemberEntry me = (MemberEntry)mes.getEntries()[0];
169         MemberEntry meUpdated = updateSampleMemberEntry(me);
170         MemberEntrySet mesUpdated = new MemberEntrySet(getEndpointUrl());
171         mesUpdated.setEntries(new MemberEntry[] { meUpdated });
172         
173         return mesUpdated;
174     }
175
176     protected static WeblogEntry getSampleWeblogEntry() {
177         WeblogEntry we = new WeblogEntry("fooblog", getEndpointUrl());
178         we.setEmailAddress("foo@bar.org");
179         we.setCreatingUser("foo");
180         we.setDescription("Foo Weblog Description");
181         we.setLocale(Locale.getDefault());
182         we.setTimezone(TimeZone.getDefault());
183         we.setName("Foo Weblog Name");
184         
185         return we;
186     }
187     
188     protected static UserEntrySet getSampleUserEntrySet() {
189         UserEntry ue = getSampleUserEntry();
190         UserEntrySet ues = new UserEntrySet(getEndpointUrl());
191         ues.setEntries(new UserEntry[] { ue });
192         
193         return ues;
194     }
195     
196     protected static WeblogEntrySet getSampleWeblogEntrySet() {
197         WeblogEntry we = getSampleWeblogEntry();
198         WeblogEntrySet wes = new WeblogEntrySet(getEndpointUrl());
199         wes.setEntries(new WeblogEntry[] { we });
200         
201         return wes;
202     }
203     
204     protected static MemberEntrySet getSampleMemberEntrySet() {
205         MemberEntry me = getSampleMemberEntry();
206         MemberEntrySet mes = new MemberEntrySet(getEndpointUrl());
207         mes.setEntries(new MemberEntry[] { me });
208         
209         return mes;
210     }
211
212     protected UserEntrySet createSampleUser() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
213         UserEntrySet ues = getSampleUserEntrySet();
214         
215         String JavaDoc url = ues.getHref();
216         String JavaDoc user = getUser();
217         String JavaDoc password = getPassword();
218         
219         String JavaDoc body = ues.toString();
220         
221         HttpResponse res = post(url, user, password, body);
222         assertEquals(201, res.getStatus());
223         
224         UserEntrySet uesResponse = null;
225         InputStream JavaDoc responseBody = res.getResponseBody();
226         if (responseBody != null) {
227            uesResponse = new UserEntrySet(responseBody, getEndpointUrl());
228         }
229         
230         return uesResponse;
231     }
232
233     protected UserEntrySet updateSampleUser() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
234         UserEntrySet ues = updateSampleUserEntrySet(getSampleUserEntrySet());
235         
236         String JavaDoc url = ues.getHref();
237         String JavaDoc user = getUser();
238         String JavaDoc password = getPassword();
239         
240         String JavaDoc body = ues.toString();
241         
242         HttpResponse res = put(url, user, password, body);
243         assertEquals(200, res.getStatus());
244         
245         UserEntrySet uesResponse = null;
246         InputStream JavaDoc responseBody = res.getResponseBody();
247         if (responseBody != null) {
248            uesResponse = new UserEntrySet(responseBody, getEndpointUrl());
249         }
250         
251         return uesResponse;
252     }
253
254     protected WeblogEntrySet updateSampleWeblog() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
255         WeblogEntrySet wes = updateSampleWeblogEntrySet(getSampleWeblogEntrySet());
256         
257         String JavaDoc url = wes.getHref();
258         String JavaDoc user = getUser();
259         String JavaDoc password = getPassword();
260         
261         String JavaDoc body = wes.toString();
262         
263         HttpResponse res = put(url, user, password, body);
264         assertEquals(200, res.getStatus());
265         
266         WeblogEntrySet wesResponse = null;
267         InputStream JavaDoc responseBody = res.getResponseBody();
268         if (responseBody != null) {
269            wesResponse = new WeblogEntrySet(responseBody, getEndpointUrl());
270         }
271         
272         return wesResponse;
273     }
274
275     protected MemberEntrySet updateSampleMember() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
276         MemberEntrySet mes = updateSampleMemberEntrySet(getSampleMemberEntrySet());
277         
278         String JavaDoc url = mes.getHref();
279         String JavaDoc user = getUser();
280         String JavaDoc password = getPassword();
281         
282         String JavaDoc body = mes.toString();
283         
284         HttpResponse res = put(url, user, password, body);
285         assertEquals(200, res.getStatus());
286         
287         MemberEntrySet mesResponse = null;
288         InputStream JavaDoc responseBody = res.getResponseBody();
289         if (responseBody != null) {
290            mesResponse = new MemberEntrySet(responseBody, getEndpointUrl());
291         }
292         
293         return mesResponse;
294     }
295     
296     protected WeblogEntrySet createSampleWeblog() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
297         WeblogEntrySet wes = getSampleWeblogEntrySet();
298         
299         String JavaDoc url = wes.getHref();
300         String JavaDoc user = getUser();
301         String JavaDoc password = getPassword();
302         
303         String JavaDoc body = wes.toString();
304         
305         HttpResponse res = post(url, user, password, body);
306         assertEquals(201, res.getStatus());
307         
308         WeblogEntrySet wesResponse = null;
309         InputStream JavaDoc responseBody = res.getResponseBody();
310         if (responseBody != null) {
311            wesResponse = new WeblogEntrySet(responseBody, getEndpointUrl());
312         }
313         
314         return wesResponse;
315     }
316
317     protected MemberEntrySet createSampleMember() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
318         MemberEntrySet mes = getSampleMemberEntrySet();
319         
320         String JavaDoc url = mes.getHref();
321         String JavaDoc user = getUser();
322         String JavaDoc password = getPassword();
323         
324         String JavaDoc body = mes.toString();
325         
326         HttpResponse res = post(url, user, password, body);
327         assertEquals(201, res.getStatus());
328         
329         MemberEntrySet mesResponse = null;
330         InputStream JavaDoc responseBody = res.getResponseBody();
331         if (responseBody != null) {
332            mesResponse = new MemberEntrySet(responseBody, getEndpointUrl());
333         }
334         
335         return mesResponse;
336     }
337     
338     protected UserEntrySet deleteSampleUser() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
339         UserEntry ue = getSampleUserEntry();
340         
341         HttpResponse res = delete(ue.getHref(), getUser(), getPassword());
342         assertEquals(200, res.getStatus());
343         
344         UserEntrySet uesResponse = null;
345         InputStream JavaDoc responseBody = res.getResponseBody();
346         if (responseBody != null) {
347            uesResponse = new UserEntrySet(responseBody, getEndpointUrl());
348         }
349         
350         return uesResponse;
351     }
352
353     protected UserEntrySet fetchSampleUser() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
354         UserEntry ue = getSampleUserEntry();
355         
356         HttpResponse res = get(ue.getHref(), getUser(), getPassword());
357         assertEquals(200, res.getStatus());
358         
359         UserEntrySet uesResponse = null;
360         InputStream JavaDoc responseBody = res.getResponseBody();
361         if (responseBody != null) {
362            uesResponse = new UserEntrySet(responseBody, getEndpointUrl());
363         }
364         
365         return uesResponse;
366     }
367
368     protected WeblogEntrySet fetchSampleWeblog() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
369         WeblogEntry we = getSampleWeblogEntry();
370         
371         HttpResponse res = get(we.getHref(), getUser(), getPassword());
372         assertEquals(200, res.getStatus());
373         
374         WeblogEntrySet wesResponse = null;
375         InputStream JavaDoc responseBody = res.getResponseBody();
376         if (responseBody != null) {
377            wesResponse = new WeblogEntrySet(responseBody, getEndpointUrl());
378         }
379         
380         return wesResponse;
381     }
382
383     protected MemberEntrySet fetchSampleMember() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
384         MemberEntry me = getSampleMemberEntry();
385         
386         HttpResponse res = get(me.getHref(), getUser(), getPassword());
387         assertEquals(200, res.getStatus());
388         
389         MemberEntrySet mesResponse = null;
390         InputStream JavaDoc responseBody = res.getResponseBody();
391         if (responseBody != null) {
392            mesResponse = new MemberEntrySet(responseBody, getEndpointUrl());
393         }
394         
395         return mesResponse;
396     }
397     
398     protected WeblogEntrySet deleteSampleWeblog() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
399         WeblogEntry we = getSampleWeblogEntry();
400         
401         HttpResponse res = delete(we.getHref(), getUser(), getPassword());
402         assertEquals(200, res.getStatus());
403         
404         WeblogEntrySet wesResponse = null;
405         InputStream JavaDoc responseBody = res.getResponseBody();
406         if (responseBody != null) {
407            wesResponse = new WeblogEntrySet(responseBody, getEndpointUrl());
408         }
409         
410         return wesResponse;
411     }
412
413     protected MemberEntrySet deleteSampleMember() throws IOException JavaDoc, JDOMException, MissingElementException, UnexpectedRootElementException {
414         MemberEntry me = getSampleMemberEntry();
415         
416         HttpResponse res = delete(me.getHref(), getUser(), getPassword());
417         assertEquals(200, res.getStatus());
418         
419         MemberEntrySet mesResponse = null;
420         InputStream JavaDoc responseBody = res.getResponseBody();
421         if (responseBody != null) {
422            mesResponse = new MemberEntrySet(responseBody, getEndpointUrl());
423         }
424         
425         return mesResponse;
426     }
427     
428     protected static HttpResponse post(String JavaDoc url, String JavaDoc user, String JavaDoc password, String JavaDoc body) throws IOException JavaDoc {
429         HttpClient httpClient = new HttpClient();
430         EntityEnclosingMethod method = new PostMethod(url);
431         addAuthHeader(method, user, password);
432         
433         method.setRequestBody(body);
434         
435         String JavaDoc contentType = "application/xml; charset=utf-8";
436         method.setRequestHeader("Content-type", contentType);
437         
438         int status = httpClient.executeMethod(method);
439         InputStream JavaDoc responseBody = method.getResponseBodyAsStream();
440         
441         HttpResponse res = new HttpResponse(status, responseBody);
442         return res;
443     }
444
445     protected static HttpResponse put(String JavaDoc url, String JavaDoc user, String JavaDoc password, String JavaDoc body) throws IOException JavaDoc {
446         HttpClient httpClient = new HttpClient();
447         EntityEnclosingMethod method = new PutMethod(url);
448         addAuthHeader(method, user, password);
449         
450         method.setRequestBody(body);
451         
452         String JavaDoc contentType = "application/xml; charset=utf-8";
453         method.setRequestHeader("Content-type", contentType);
454         
455         int status = httpClient.executeMethod(method);
456         InputStream JavaDoc responseBody = method.getResponseBodyAsStream();
457         
458         HttpResponse res = new HttpResponse(status, responseBody);
459         return res;
460     }
461     
462     protected static HttpResponse get(String JavaDoc url, String JavaDoc user, String JavaDoc password) throws IOException JavaDoc {
463         HttpClient httpClient = new HttpClient();
464         HttpMethod method = new GetMethod(url);
465         addAuthHeader(method, user, password);
466         
467         String JavaDoc contentType = "application/xml; charset=utf-8";
468         method.setRequestHeader("Content-type", contentType);
469         
470         int status = httpClient.executeMethod(method);
471         InputStream JavaDoc responseBody = method.getResponseBodyAsStream();
472         
473         HttpResponse res = new HttpResponse(status, responseBody);
474         return res;
475     }
476     
477     protected static HttpResponse delete(String JavaDoc url, String JavaDoc user, String JavaDoc password) throws IOException JavaDoc {
478         HttpClient httpClient = new HttpClient();
479         HttpMethod method = new DeleteMethod(url);
480         addAuthHeader(method, user, password);
481                 
482         String JavaDoc contentType = "application/xml; charset=utf-8";
483         method.setRequestHeader("Content-type", contentType);
484         
485         int status = httpClient.executeMethod(method);
486         InputStream JavaDoc responseBody = method.getResponseBodyAsStream();
487         
488         HttpResponse res = new HttpResponse(status, responseBody);
489         return res;
490     }
491     
492     private static void addAuthHeader(HttpMethod method, String JavaDoc user, String JavaDoc password) {
493         String JavaDoc credentials = user + ":" + password;
494         method.setRequestHeader("Authorization", "Basic " + new String JavaDoc(Base64.encode(credentials.getBytes())));
495     }
496 }
497
Popular Tags