]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-18: CollectionObject service is now running in JBoss, and raw XML tests to...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 26 Mar 2009 17:28:15 +0000 (17:28 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 26 Mar 2009 17:28:15 +0000 (17:28 +0000)
Note that this cannot be verified to be working as of 2009-03-26 10:27 due to a JBoss WAR file issue.

sandbox/aron/HelloWorld-CollectionObject/HelloWorldJaxb/src/main/resources/hello.xsd
sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/main/java/org/collectionspace/hello/services/CollectionObjectResource.java [new file with mode: 0644]
sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/main/java/org/collectionspace/hello/services/HelloworldApplication.java
sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/test/java/org/collectionspace/hello/test/CollectionObjectServiceRawXmlTest.java [new file with mode: 0644]

index 8943ef96361b04e25770b161ca08c188bf49c0f6..f9a7e360c67444c087c03aef667017a6e678288a 100644 (file)
   version="0.1"
 >
 
-<!-- avoid XmlRootElement nightnmare, see http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html-->
-    <xs:element name="person">
-        <xs:complexType>
+<!-- 
+  Avoid XmlRootElement nightmare, see
+  http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html
+-->
 
-            <xs:sequence>
-                <xs:element name="firstName" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="lastName" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="street" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="city" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="state" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="zip" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="country" type="xs:string"
-                                               minOccurs="1" />
-            </xs:sequence>
-            <xs:attribute name="id" type="xs:long" />
-            <xs:attribute name="version" type="xs:int" />
+  <xs:element name="collectionObject">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element ref="serviceMetadata"/>
+        <xs:element ref="defaultCollectionObject"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+  <xs:element name="serviceMetadata">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element ref="collectionSpaceId"/>
+        <xs:element minOccurs="0" ref="created"/>
+        <xs:element minOccurs="0" ref="createdBy"/>
+        <xs:element minOccurs="0" ref="lastModified"/>
+        <xs:element minOccurs="0" ref="lastModifiedBy"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+  <xs:element name="collectionSpaceId" type="xs:string"/>
+  <xs:element name="created">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:dateTime">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="createdBy">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="lastModified">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:dateTime">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="lastModifiedBy">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="defaultCollectionObject">
+    <xs:complexType>
+      <xs:all>
+        <xs:element ref="objectNumber"/>
+        <xs:element ref="otherNumber"/>
+        <xs:element ref="briefDescription"/>
+        <xs:element ref="comments"/>
+        <xs:element ref="distinguishingFeatures"/>
+        <xs:element ref="objectName"/>
+        <xs:element ref="responsibleDepartment"/>
+        <xs:element ref="title"/>
+      </xs:all>
+    </xs:complexType>
+  </xs:element>
+  <xs:element name="objectNumber">
+    <xs:simpleType>
+      <xs:restriction base="xs:normalizedString">
+        <xs:minLength value="1"/>
+      </xs:restriction>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="otherNumber">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="briefDescription">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="comments">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="distinguishingFeatures">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="objectName">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="responsibleDepartment">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
+  <xs:element name="title">
+    <xs:simpleType>
+      <xs:union memberTypes="xs:string">
+        <xs:simpleType>
+          <xs:restriction base="xs:token">
+            <xs:length value="0"/>
+          </xs:restriction>
+        </xs:simpleType>
+      </xs:union>
+    </xs:simpleType>
+  </xs:element>
 
-        </xs:complexType>
-    </xs:element>
 
-    
-    <!-- person short is returned by getPersons method -->
-    <xs:element name="persons">
-        <xs:complexType>
-            <xs:sequence>
+  <xs:element name="collectionObjectList">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element minOccurs="0" maxOccurs="unbounded" ref="collectionObjectListItem"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+  <!--
+    Note CamelCase here; Sanjay uses the style "collection-object-list-item", and
+    we can and should revert to that if needed.
+  -->
+  <xs:element name="collectionObjectListItem">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element ref="collectionSpaceId"/>
+        <xs:element ref="objectNumber"/>
+        <xs:element ref="objectName"/>
+        <xs:element ref="uri"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+  <xs:element name="uri" type="xs:anyURI"/>
 
-                <!-- person list item is returned by getPersons method -->
-                <xs:element name="person-list-item" maxOccurs="unbounded">
-                    <xs:complexType>
-                        <xs:sequence>
-                            <xs:element name="firstName" type="xs:string"
-                                               minOccurs="1" />
-                            <xs:element name="lastName" type="xs:string"
-                                               minOccurs="1" />
-                            <!-- uri to retrive person details -->
-                            <xs:element name="uri" type="xs:anyURI"
-                                               minOccurs="1" />
-                            <xs:element name="id" type="xs:long"
-                                               minOccurs="1" />
-                        </xs:sequence>
-                    </xs:complexType>
-                </xs:element>
-            </xs:sequence>
-        </xs:complexType>
-    </xs:element>
 
-    <xs:element name="identifier">
-        <xs:complexType>
+  <xs:element name="person">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="firstName" type="xs:string" minOccurs="1" />
+        <xs:element name="lastName" type="xs:string" minOccurs="1" />
+        <xs:element name="street" type="xs:string" minOccurs="1" />
+        <xs:element name="city" type="xs:string" minOccurs="1" />
+        <xs:element name="state" type="xs:string" minOccurs="1" />
+        <xs:element name="zip" type="xs:string" minOccurs="1" />
+        <xs:element name="country" type="xs:string" minOccurs="1" />
+      </xs:sequence>
+      <xs:attribute name="id" type="xs:long" />
+      <xs:attribute name="version" type="xs:int" />
+    </xs:complexType>
+  </xs:element>
 
+  
+  <!-- person short is returned by getPersons method -->
+  <xs:element name="persons">
+    <xs:complexType>
+      <xs:sequence>
+        <!-- person list item is returned by getPersons method -->
+        <xs:element name="person-list-item" maxOccurs="unbounded">
+          <xs:complexType>
             <xs:sequence>
-                <xs:element name="namespace" type="xs:string"
-                                               minOccurs="1" />
-                <xs:element name="value" type="xs:string"
-                                               minOccurs="0" />
+              <xs:element name="firstName" type="xs:string" minOccurs="1" />
+              <xs:element name="lastName" type="xs:string" minOccurs="1" />
+              <!-- uri to retrive person details -->
+              <xs:element name="uri" type="xs:anyURI" minOccurs="1" />
+              <xs:element name="id" type="xs:long" minOccurs="1" />
             </xs:sequence>
-            <xs:attribute name="id" type="xs:long" />
-            <xs:attribute name="version" type="xs:int" />
+          </xs:complexType>
+        </xs:element>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
 
-        </xs:complexType>
-    </xs:element>
+  <xs:element name="identifier">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="namespace" type="xs:string" minOccurs="1" />
+        <xs:element name="value" type="xs:string" minOccurs="0" />
+      </xs:sequence>
+      <xs:attribute name="id" type="xs:long" />
+      <xs:attribute name="version" type="xs:int" />
+    </xs:complexType>
+  </xs:element>
+  
+  
 </xs:schema>
 
diff --git a/sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/main/java/org/collectionspace/hello/services/CollectionObjectResource.java b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/main/java/org/collectionspace/hello/services/CollectionObjectResource.java
new file mode 100644 (file)
index 0000000..1197cc7
--- /dev/null
@@ -0,0 +1,262 @@
+package org.collectionspace.hello.services;
+
+import java.net.URI;
+import java.util.List;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import org.collectionspace.hello.CollectionObject;
+import org.collectionspace.hello.CollectionObjectList;
+import org.collectionspace.hello.CollectionObjectListItem;
+import org.collectionspace.hello.DefaultCollectionObject;
+import org.collectionspace.hello.ServiceMetadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/collectionobjects")
+@Consumes("application/xml")
+@Produces("application/xml")
+public class CollectionObjectResource {
+
+    final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class);
+    private Map<String, CollectionObject> CollectionObjectDB =
+      new ConcurrentHashMap<String, CollectionObject>();
+    private AtomicLong idCounter = new AtomicLong();
+
+    public CollectionObjectResource() {
+    }
+
+    @POST
+    public Response createCollectionObject(CollectionObject c) {
+      if (c == null) {
+        Response response = Response.status(Response.Status.BAD_REQUEST).entity(
+          "Add failed, the CollectionObject provided was empty.").type("text/plain").build();
+        throw new WebApplicationException(response);
+      }
+      Long id = idCounter.incrementAndGet();
+      // c.getServiceMetadata().setCollectionSpaceId(id.toString());
+      c.setServiceMetadata( new ServiceMetadata() );
+      c.getServiceMetadata().setCollectionSpaceId("100");
+      // c.setVersion(1);
+      CollectionObjectDB.put(c.getServiceMetadata().getCollectionSpaceId(), c);
+      verbose("created CollectionObject", c);
+      UriBuilder path = UriBuilder.fromResource(CollectionObjectResource.class);
+      path.path("" + c.getServiceMetadata().getCollectionSpaceId());
+      Response response = Response.created(path.build()).build();
+      return response;
+    }
+
+    @GET
+    @Path("{id}")
+    public CollectionObject getCollectionObject(@PathParam("id") String id) {
+      CollectionObject c = CollectionObjectDB.get(id);
+      if (c == null) {
+        Response response = Response.status(Response.Status.NOT_FOUND).entity(
+          "Get failed, the requested CollectionObject ID:" + id + ": was not found.").type("text/plain").build();
+        throw new WebApplicationException(response);
+      }
+      verbose("get CollectionObject", c);
+      return c;
+    }
+
+    @PUT
+    @Path("{id}")
+    public CollectionObject updateCollectionObject(@PathParam("id") String id, CollectionObject update) {
+      CollectionObject current = CollectionObjectDB.get(id);
+      if (current == null) {
+        Response response = Response.status(Response.Status.NOT_FOUND).entity(
+          "Update failed, the CollectionObject ID:" + id + ": was not found.").type("text/plain").build();
+        throw new WebApplicationException(response);
+      }
+      verbose("update CollectionObject input", update);
+      //todo: intelligent merge needed
+      // current.getServiceMetadata().setLastUpdated( [current date/time here] );
+      current.getDefaultCollectionObject().setObjectNumber(
+        update.getDefaultCollectionObject().getObjectNumber());
+      current.getDefaultCollectionObject().setOtherNumber(
+        update.getDefaultCollectionObject().getOtherNumber());
+      current.getDefaultCollectionObject().setBriefDescription(
+        update.getDefaultCollectionObject().getBriefDescription());
+      current.getDefaultCollectionObject().setComments(
+        update.getDefaultCollectionObject().getComments());
+      current.getDefaultCollectionObject().setDistinguishingFeatures(
+        update.getDefaultCollectionObject().getDistinguishingFeatures());
+      current.getDefaultCollectionObject().setObjectName(
+        update.getDefaultCollectionObject().getObjectName());
+      current.getDefaultCollectionObject().setResponsibleDepartment(
+        update.getDefaultCollectionObject().getResponsibleDepartment());
+        verbose("update CollectionObject output", current);
+      return current;
+    }
+
+    // Get a list
+    @GET
+    public CollectionObjectList getCollectionObjectList(@Context UriInfo ui) {
+      CollectionObjectList CollectionObjectList = new CollectionObjectList();
+      // The auto-generated method called here has a potentially misleading name; it returns a List.
+      List<CollectionObjectListItem> list =
+        CollectionObjectList.getCollectionObjectListItem();
+      // builder starts with current URI and has appended path of getCollectionObject method
+      UriBuilder ub = ui.getAbsolutePathBuilder().path(this.getClass(), "getCollectionObject");
+      for (CollectionObject c : CollectionObjectDB.values()) {
+        CollectionObjectListItem cli = new CollectionObjectListItem();
+        cli.setCollectionSpaceId(c.getServiceMetadata().getCollectionSpaceId());
+        cli.setObjectNumber(c.getDefaultCollectionObject().getObjectNumber());
+        cli.setObjectName(c.getDefaultCollectionObject().getObjectName());
+        // builder has {id} variable that must be filled in for each customer
+        URI uri = ub.build(c.getServiceMetadata().getCollectionSpaceId());
+        cli.setUri(uri.toString());
+        list.add(cli);
+      }
+      return CollectionObjectList;
+    }
+
+    @DELETE
+    @Path("{id}")
+    public void deleteCollectionObject(@PathParam("id") String id) {
+      CollectionObject removed = CollectionObjectDB.remove(id);
+      if (removed == null) {
+        Response response = Response.status(Response.Status.NOT_FOUND).entity(
+          "Delete failed, the CollectionObject ID:" + id + ": was not found.").type("text/plain").build();
+        throw new WebApplicationException(response);
+      }
+      verbose("deleted CollectionObject", removed);
+    }
+
+    private void verbose(String msg, CollectionObject c) {
+      try {
+        System.out.println("CollectionObjectResource : " + msg);
+        JAXBContext jc = JAXBContext.newInstance(CollectionObject.class);
+        Marshaller m = jc.createMarshaller();
+        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+        m.marshal(c, System.out);
+      } catch (Exception e) {
+        e.printStackTrace();
+      }
+    }
+
+//    @POST
+//    @Consumes("application/xml")
+//    public Response createCollectionObject(InputStream is) {
+//        CollectionObject c = readCollectionObject(is);
+//        c.setId(idCounter.incrementAndGet());
+//        c.setVersion(1);
+//        CollectionObjectDB.put(c.getId(), c);
+//        try {
+//            System.out.println("Created CollectionObject " + c.getId());
+//            outputCollectionObject(System.out, c);
+//        } catch (IOException ioe) {
+//            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
+//        }
+//        return Response.created(URI.create("/CollectionObjects/" + c.getId())).build();
+//
+//    }
+//
+//    @GET
+//    @Path("{id}")
+//    @Produces("application/xml")
+//    public StreamingOutput getCollectionObject(@PathParam("id") int id) {
+//        final CollectionObject c = CollectionObjectDB.get(id);
+//        if (c == null) {
+//            throw new WebApplicationException(Response.Status.NOT_FOUND);
+//        }
+//        return new StreamingOutput() {
+//
+//            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
+//                outputCollectionObject(outputStream, c);
+//            }
+//        };
+//    }
+//
+//    @PUT
+//    @Path("{id}")
+//    @Consumes("application/xml")
+//    @Produces("application/xml")
+//    public StreamingOutput updateCollectionObject(@PathParam("id") int id, InputStream is) {
+//        CollectionObject update = readCollectionObject(is);
+//        CollectionObject current = CollectionObjectDB.get(id);
+//        if (current == null) {
+//            throw new WebApplicationException(Response.Status.NOT_FOUND);
+//        }
+//
+//        current.setFirstName(update.getFirstName());
+//        current.setLastName(update.getLastName());
+//        current.setStreet(update.getStreet());
+//        current.setState(update.getState());
+//        current.setZip(update.getZip());
+//        current.setCountry(update.getCountry());
+//        current.setVersion(current.getVersion() + 1);
+//        final CollectionObject scurrent = current;
+//        return new StreamingOutput() {
+//
+//            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
+//                outputCollectionObject(outputStream, scurrent);
+//            }
+//        };
+//    }
+//
+//    protected void outputCollectionObject(OutputStream os, CollectionObject c) throws IOException {
+//        PrintStream writer = new PrintStream(os);
+//        writer.println("<CollectionObject id=\"" + c.getId() + "\" version=\"" + c.getVersion() + "\">");
+//        writer.println("   <first-name>" + c.getFirstName() + "</first-name>");
+//        writer.println("   <last-name>" + c.getLastName() + "</last-name>");
+//        writer.println("   <street>" + c.getStreet() + "</street>");
+//        writer.println("   <city>" + c.getCity() + "</city>");
+//        writer.println("   <state>" + c.getState() + "</state>");
+//        writer.println("   <zip>" + c.getZip() + "</zip>");
+//        writer.println("   <country>" + c.getCountry() + "</country>");
+//        writer.println("</CollectionObject>");
+//    }
+//
+//    protected CollectionObject readCollectionObject(InputStream is) {
+//        try {
+//            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+//            Document doc = builder.parse(is);
+//            Element root = doc.getDocumentElement();
+//            CollectionObject c = new CollectionObject();
+//            if (root.getAttribute("id") != null && !root.getAttribute("id").trim().equals("")) {
+//                c.setId(Integer.valueOf(root.getAttribute("id")));
+//            }
+//            if (root.getAttribute("version") != null && !root.getAttribute("version").trim().equals("")) {
+//                c.setVersion(Integer.valueOf(root.getAttribute("version")));
+//            }
+//            NodeList nodes = root.getChildNodes();
+//            for (int i = 0; i < nodes.getLength(); i++) {
+//                Element element = (Element) nodes.item(i);
+//                if (element.getTagName().equals("first-name")) {
+//                    c.setFirstName(element.getTextContent());
+//                } else if (element.getTagName().equals("last-name")) {
+//                    c.setLastName(element.getTextContent());
+//                } else if (element.getTagName().equals("street")) {
+//                    c.setStreet(element.getTextContent());
+//                } else if (element.getTagName().equals("city")) {
+//                    c.setCity(element.getTextContent());
+//                } else if (element.getTagName().equals("state")) {
+//                    c.setState(element.getTextContent());
+//                } else if (element.getTagName().equals("zip")) {
+//                    c.setZip(element.getTextContent());
+//                } else if (element.getTagName().equals("country")) {
+//                    c.setCountry(element.getTextContent());
+//                }
+//            }
+//            return c;
+//        } catch (Exception e) {
+//            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+//        }
+//    }
+}
index b7b7acdc0c421359fca89811a547aacaa81427d4..572066a3bd0dc8e6e9683b212b817263d25e799c 100644 (file)
@@ -10,6 +10,7 @@ public class HelloworldApplication extends Application {
     private Set<Class<?>> empty = new HashSet<Class<?>>();
 
     public HelloworldApplication() {
+        singletons.add(new CollectionObjectResource());
         singletons.add(new PersonResource());
         singletons.add(new IdentifierResource());
     }
diff --git a/sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/test/java/org/collectionspace/hello/test/CollectionObjectServiceRawXmlTest.java b/sandbox/aron/HelloWorld-CollectionObject/HelloWorldService/src/test/java/org/collectionspace/hello/test/CollectionObjectServiceRawXmlTest.java
new file mode 100644 (file)
index 0000000..174994a
--- /dev/null
@@ -0,0 +1,107 @@
+package org.collectionspace.hello.test;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * @version $Revision: 1 $
+ */
+public class CollectionObjectServiceRawXmlTest {
+
+    @Test
+    public void testCollectionObjectResource() throws Exception {
+        verbose("create a new CollectionObject");
+        // Create a new object
+        String newCollectionObject = 
+          "<collectionObject xmlns=\"http://collectionspace.org/hello\">" +
+            "<serviceMetadata />" +
+            "<defaultCollectionObject>" +
+              "<objectNumber>1984.021.0049</objectNumber>" +
+              "<objectName>Radio News, vol. 10, no. 2, August 1928</objectName>" +
+            "</defaultCollectionObject>" +
+          "</collectionObject>";
+        verbose("new object: " + newCollectionObject);
+        URL postUrl = new URL("http://localhost:8080/helloworld/cspace/collectionobjects");
+        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
+        connection.setDoOutput(true);
+        connection.setInstanceFollowRedirects(false);
+        connection.setRequestMethod("POST");
+        connection.setRequestProperty("Content-Type", "application/xml");
+        OutputStream os = connection.getOutputStream();
+        os.write(newCollectionObject.getBytes());
+        os.flush();
+        verbose("response: " + connection.getResponseMessage());
+        Assert.assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode());
+        String createdUrl = connection.getHeaderField("Location");
+        verbose("Location: " + createdUrl);
+        connection.disconnect();
+
+
+        // Get the new object
+        verbose("get created CollectionObject");
+        URL getUrl = new URL(createdUrl);
+        connection = (HttpURLConnection) getUrl.openConnection();
+        connection.setRequestMethod("GET");
+        verbose("Content-Type: " + connection.getContentType());
+
+        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+
+        String line = reader.readLine();
+        while (line != null) {
+            verbose(line);
+            line = reader.readLine();
+        }
+        Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
+        connection.disconnect();
+
+        String updateCollectionObject = 
+          "<collectionObject xmlns=\"http://collectionspace.org/hello\">" +
+            "<serviceMetadata />" +
+            "<defaultCollectionObject>" +
+              "<objectNumber>1997.005.0437</objectNumber>" +
+              "<objectName>Toy, Gotham City Police Helicopter, 1992</objectName>" +
+            "</defaultCollectionObject>" +
+          "</collectionObject>";
+    
+        connection = (HttpURLConnection) getUrl.openConnection();
+        connection.setDoOutput(true);
+        connection.setRequestMethod("PUT");
+        connection.setRequestProperty("Content-Type", "application/xml");
+        os = connection.getOutputStream();
+        os.write(updateCollectionObject.getBytes());
+        os.flush();
+        verbose("response: " + connection.getResponseMessage());
+        Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
+        connection.disconnect();
+
+        // Show the update
+        verbose("updated CollectionObject");
+        connection = (HttpURLConnection) getUrl.openConnection();
+        connection.setRequestMethod("GET");
+
+        verbose("Content-Type: " + connection.getContentType());
+        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+
+        line = reader.readLine();
+        while (line != null) {
+            verbose(line);
+            line = reader.readLine();
+        }
+        Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
+        connection.disconnect();
+
+    }
+
+    private void verbose(String msg) {
+        System.out.println("CollectionObjectServiceRawXmlTest : " + msg);
+    }
+}