1 package org.collectionspace.hello.test;
3 import org.junit.Assert;
6 import java.io.BufferedReader;
7 import java.io.InputStreamReader;
8 import java.io.OutputStream;
9 import java.net.HttpURLConnection;
13 * @version $Revision: 1 $
15 public class IdentifierServiceRawXmlTest {
18 public void testIdentifierResource() throws Exception {
19 verbose("create a new Identifier");
20 // Create a new object
21 String newIdentifier = "<ns2:identifier xmlns:ns2=\"http://collectionspace.org/hello\">" +
22 "<namespace>edu.stanford</namespace>" +
25 URL postUrl = new URL("http://localhost:8080/helloworld/cspace/identifiers");
26 HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
27 connection.setDoOutput(true);
28 connection.setInstanceFollowRedirects(false);
29 connection.setRequestMethod("POST");
30 connection.setRequestProperty("Content-Type", "application/xml");
31 OutputStream os = connection.getOutputStream();
32 os.write(newIdentifier.getBytes());
34 Assert.assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode());
35 String createdUrl = connection.getHeaderField("Location");
36 verbose("Location: " + createdUrl);
37 connection.disconnect();
41 verbose("get created Identifier");
42 URL getUrl = new URL(createdUrl);
43 connection = (HttpURLConnection) getUrl.openConnection();
44 connection.setRequestMethod("GET");
45 verbose("Content-Type: " + connection.getContentType());
47 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
49 String line = reader.readLine();
50 while (line != null) {
52 line = reader.readLine();
54 Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
55 connection.disconnect();
59 private void verbose(String msg) {
60 System.out.println("IdentifierServiceRawXmlTest : " + msg);