]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b455b2f01a769650be8f9629072658779a1a4b43
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.test;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.apache.commons.httpclient.HttpMethodBase;
6 import org.apache.commons.httpclient.HttpClient;
7 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
8
9 import java.io.BufferedReader;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.net.HttpURLConnection;
13 import java.net.URL;
14
15 /**
16  * @version $Revision: 1 $
17  */
18 public class PersonServiceRawXmlTest {
19
20     @Test
21     public void testPersonResource() throws Exception {
22         verbose("create a new Person");
23         // Create a new object
24         String newPerson = "<ns2:person xmlns:ns2=\"http://collectionspace.org/hello\">" + "<firstName>John</firstName>" + "<lastName>Doe</lastName>" + "<street>2195 Hearst Ave</street>" + "<city>Berkeley</city>" + "<state>CA</state>" + "<zip>94504</zip>" + "<country>USA</country>" + "</ns2:person>";
25
26         URL postUrl = new URL("http://localhost:8080/helloworld/cspace/persons");
27         HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
28         connection.setDoOutput(true);
29         connection.setInstanceFollowRedirects(false);
30         connection.setRequestMethod("POST");
31         connection.setRequestProperty("Content-Type", "application/xml");
32         OutputStream os = connection.getOutputStream();
33         os.write(newPerson.getBytes());
34         os.flush();
35         Assert.assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode());
36         String createdUrl = connection.getHeaderField("Location");
37         verbose("Location: " + createdUrl);
38         connection.disconnect();
39
40
41         // Get the new object
42         verbose("get created Person");
43         URL getUrl = new URL(createdUrl);
44         connection = (HttpURLConnection) getUrl.openConnection();
45         connection.setRequestMethod("GET");
46         verbose("Content-Type: " + connection.getContentType());
47
48         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
49
50         String line = reader.readLine();
51         while (line != null) {
52             verbose(line);
53             line = reader.readLine();
54         }
55         Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
56         connection.disconnect();
57
58         String updatePerson = "<ns2:person xmlns:ns2=\"http://collectionspace.org/hello\">" + "<firstName>Jane</firstName>" + "<lastName>Doe</lastName>" + "<street>1 University Ave</street>" + "<city>Berkeley</city>" + "<state>CA</state>" + "<zip>94504</zip>" + "<country>USA</country>" + "</ns2:person>";
59         connection = (HttpURLConnection) getUrl.openConnection();
60         connection.setDoOutput(true);
61         connection.setRequestMethod("PUT");
62         connection.setRequestProperty("Content-Type", "application/xml");
63         os = connection.getOutputStream();
64         os.write(updatePerson.getBytes());
65         os.flush();
66         Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
67         connection.disconnect();
68
69         // Show the update
70         verbose("updated Person");
71         connection = (HttpURLConnection) getUrl.openConnection();
72         connection.setRequestMethod("GET");
73
74         verbose("Content-Type: " + connection.getContentType());
75         reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
76
77         line = reader.readLine();
78         while (line != null) {
79             verbose(line);
80             line = reader.readLine();
81         }
82         Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
83         connection.disconnect();
84     }
85
86     private void verbose(String msg) {
87         System.out.println("PersonServiceRawXmlTest : " + msg);
88     }
89 }