1 package org.collectionspace.hello.test;
3 import org.junit.Assert;
5 import org.apache.commons.httpclient.HttpMethodBase;
6 import org.apache.commons.httpclient.HttpClient;
7 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
9 import java.io.BufferedReader;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.net.HttpURLConnection;
16 * @version $Revision: 1 $
18 public class PersonServiceRawXmlTest {
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>";
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());
35 Assert.assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode());
36 String createdUrl = connection.getHeaderField("Location");
37 verbose("Location: " + createdUrl);
38 connection.disconnect();
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());
48 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
50 String line = reader.readLine();
51 while (line != null) {
53 line = reader.readLine();
55 Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
56 connection.disconnect();
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());
66 Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
67 connection.disconnect();
70 verbose("updated Person");
71 connection = (HttpURLConnection) getUrl.openConnection();
72 connection.setRequestMethod("GET");
74 verbose("Content-Type: " + connection.getContentType());
75 reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
77 line = reader.readLine();
78 while (line != null) {
80 line = reader.readLine();
82 Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
83 connection.disconnect();
86 private void verbose(String msg) {
87 System.out.println("PersonServiceRawXmlTest : " + msg);