]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-245: First (primitive, stubbed) working version of the ID Service. Resolved...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 16 Jul 2009 00:42:23 +0000 (00:42 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 16 Jul 2009 00:42:23 +0000 (00:42 +0000)
services/id/service/pom.xml
services/id/service/src/main/java/org/collectionspace/services/IDResource.java
services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java
services/id/service/src/main/java/org/collectionspace/services/id/YearIDGenerator.java
services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java [new file with mode: 0644]
services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java
services/id/service/src/test/java/org/collectionspace/services/id/NumericIDPartTest.java
services/id/service/src/test/java/org/collectionspace/services/id/StringIDPartTest.java

index 3c90f2664f69eec74b4e9700da3e54c6ee00d2c1..8fb48674d21cf281d843e80d88c2a655cc5da63e 100644 (file)
       <version>1.1.5</version>
     </dependency>
 
-     <dependency>
+    <!-- Cobertura, a tool for identifying the extent of code coverage in test classes -->
+    <dependency>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>cobertura-maven-plugin</artifactId>
       <version>2.2</version>
     </dependency>
-    <dependency>
-      <groupId>net.sourceforge.cobertura</groupId>
-      <artifactId>cobertura</artifactId>
-      <version>1.9</version>
-    </dependency>
 
   </dependencies>
   
       </plugin>
     </plugins>
   </build>
-
-  <reports>
-    <report>maven-cobertura-plugin</report>
-  </reports>
   
 </project>
index 8aad61e293106d7cffc391fd159c4beac11fc0d9..55428e9565b137f36969d5255633cd43e0c860bc 100644 (file)
@@ -32,6 +32,7 @@ import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Marshaller;
@@ -44,16 +45,19 @@ import org.slf4j.LoggerFactory;
 
 // Set the base path component for URLs that access this service.
 @Path("/ids")
+
 // Identify the default MIME media types consumed and produced by this service.
-@Consumes("application/xml")
-@Produces("application/xml")
+@Consumes(MediaType.APPLICATION_XML)
+@Produces(MediaType.APPLICATION_XML)
+
 public class IDResource {
 
        final Logger logger = LoggerFactory.getLogger(IDResource.class);
 
-       // Richard's comment in the CollectionObject Resource class, from which
-       // this class was derived: This should be a DI wired by a container like
-       // Spring, Seam, or EJB3.
+       // Per Richard's comment in the CollectionObject Resource class, from which
+       // this class was derived: "This should be a DI wired by a container like
+       // Spring, Seam, or EJB3."
+       
        final static IDService service = new IDServiceJdbcImpl();
 
   //////////////////////////////////////////////////////////////////////
@@ -85,37 +89,52 @@ public class IDResource {
   @Produces("text/plain")
        public Response getNextID(@PathParam("csid") String csid) {
        
-               Response response = null;
+         verbose("> in getNextID");
+         
+         // Unless the 'response' variable is explicitly initialized here, the
+         // compiler gives the error: "variable response might not have been initialized."
+         Response response = null;
+         response = response.ok().build();
                String nextId = "";
        
                try {
                
+                 // Retrieve the next ID for the requested pattern,
+                 // and return it in the entity body of the response.
                        nextId = service.nextID(csid);
                        
+      if (nextId == null || nextId.equals("")) {
+        response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+          .entity("ID Service returned null or empty ID").type(MediaType.TEXT_PLAIN).build();
+        return response;
+      }
+                       
                        response = Response.status(Response.Status.OK)
-                               .entity(nextId).type("text/plain").build();
-
-               // @TODO: Return our XML-based error results format.
+                         .entity(nextId).type(MediaType.TEXT_PLAIN).build();
+               
+               // @TODO: Return an XML-based error results format with the
+               // responses below.
                
                // @TODO: An IllegalStateException often indicates an overflow
                // of an IDPart.  Consider whether returning a 400 Bad Request
-               // status code is warranted, or whether returning some other
+               // status code is still warranted, or whether returning some other
                // status would be more appropriate.
                } catch (IllegalStateException ise) {
-                       response = Response.status(Response.Status.BAD_REQUEST)
-                               .entity(ise.getMessage()).type("text/plain").build();
-                               
+                 response = Response.status(Response.Status.BAD_REQUEST)
+                         .entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build();
+
                } catch (IllegalArgumentException iae) {
                        response = Response.status(Response.Status.BAD_REQUEST)
-                               .entity(iae.getMessage()).type("text/plain").build();
-                               
+                         .entity(iae.getMessage()).type(MediaType.TEXT_PLAIN).build();
+         
+         // This is guard code that should never be reached.
                } catch (Exception e) {
-                       response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
-                               .entity(e.getMessage()).type("text/plain").build();
+                 response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+                         .entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
                }
                
-    return response;
-    
+               return response;
   }
 
   //////////////////////////////////////////////////////////////////////
index f9c5b85c4b5050736f9ac88c19c1a23f766f328d..71a1105089f8f29dbfee931463040dac57ef7dfd 100644 (file)
@@ -54,6 +54,8 @@
 package org.collectionspace.services;
 
 import org.collectionspace.services.IDService;
+// The following import statement has been left open-ended
+// to accommodate future ID generation components.
 import org.collectionspace.services.id.*;
 
 public class IDServiceJdbcImpl implements IDService {
@@ -70,16 +72,27 @@ public class IDServiceJdbcImpl implements IDService {
        // a candidate ID.
        public String nextID(String csid) throws
                IllegalArgumentException, IllegalStateException {
-
+               
                IDPattern pattern;
                String nextId = "";
 
+               if (csid == null || csid.equals("")) {
+                       throw new IllegalArgumentException(
+                               "Identifier for ID pattern must not be null or empty.");
+               }
+
                try {
                        pattern = getIDPattern(csid);
                } catch (IllegalArgumentException e ) {
                        throw e;
                }
                
+               // Guard code - should not be needed.
+               if (pattern == null) {
+                       throw new IllegalArgumentException(
+                               "Pattern with ID " + "\'" + csid + "\'" + " could not be found.");
+               }
+               
                // Get the next ID associated with the pattern,
                // setting its current ID to the just-generated ID.
                //
@@ -97,8 +110,8 @@ public class IDServiceJdbcImpl implements IDService {
        
        // Returns an IDPattern.
        public IDPattern getIDPattern(String csid) throws IllegalArgumentException {
-
-               IDPattern pattern = new IDPattern();
+       
+         IDPattern pattern;
 
     // @TODO: Replace hard-coded IDs and related logic - currently
     // used here for bootstrapping and initial testing - with actual
@@ -115,8 +128,9 @@ public class IDServiceJdbcImpl implements IDService {
        
        // Retrieve the pattern.  (In this example, we're
        // simply hard-coding its construction here.)
+               pattern = new IDPattern();
                        pattern.add(new StringIDPart("E"));
-                       pattern.add(new NumericIDPart());
+                       pattern.add(new NumericIDPart("0"));
                        
                        return pattern;
        
@@ -126,11 +140,12 @@ public class IDServiceJdbcImpl implements IDService {
 
        // Retrieve the pattern.(In this example, we're
        // simply hard-coding its construction here.)
+               pattern = new IDPattern();
                        pattern.add(new YearIDPart());
                        pattern.add(new StringIDPart("."));
-                       pattern.add(new NumericIDPart());
+                       pattern.add(new NumericIDPart("1"));
                        pattern.add(new StringIDPart("."));
-                       pattern.add(new NumericIDPart());
+                       pattern.add(new NumericIDPart("0"));
                        
                        return pattern;
 
@@ -140,5 +155,5 @@ public class IDServiceJdbcImpl implements IDService {
                }
                
        }
-       
+               
 }
index 8fe648cc9396e4bed2bd4df1d1b338d24613d7fa..49f42b2d2c7ad8fdd2909301bdabf044367ade59 100644 (file)
@@ -116,7 +116,7 @@ public class YearIDGenerator implements IDGenerator {
                return this.currentValue;
   }
 
-       public String getCurrentYear() {
+       public static String getCurrentYear() {
                Calendar cal = GregorianCalendar.getInstance();
     int y = cal.get(Calendar.YEAR);
                return Integer.toString(y);
diff --git a/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java b/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java
new file mode 100644 (file)
index 0000000..7cab76a
--- /dev/null
@@ -0,0 +1,71 @@
+/*     
+ * IDServiceJdbcImplTest
+ *
+ * Test class for the ID Service's JDBC implementation class, IDServiceJdbcImpl.
+ *
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+ *
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+ *
+ * Copyright © 2009 Regents of the University of California
+ *
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+ *
+ * You may obtain a copy of the ECL 2.0 License at
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+ *
+ * $LastChangedBy: aron $
+ * $LastChangedRevision: 302 $
+ * $LastChangedDate$
+ */
+
+package org.collectionspace.services.test;
+
+import org.collectionspace.services.IDService;
+import org.collectionspace.services.IDServiceJdbcImpl;
+import org.collectionspace.services.id.YearIDGenerator;
+
+//import org.collectionspace.services.id.Id;
+//import org.collectionspace.services.id.IdList;
+//import org.collectionspace.services.id.IdPattern;
+//import org.collectionspace.services.id.IdPatternList;
+
+import junit.framework.TestCase;
+import static org.junit.Assert.*;
+
+public class IDServiceJdbcImplTest extends TestCase {
+
+  String currentYear = YearIDGenerator.getCurrentYear();
+  String nextId;
+  IDService service;
+
+  protected void setUp() {
+  
+       nextId = "";
+         service = new IDServiceJdbcImpl();
+         
+  }
+
+       public void testNextIDValidPattern() {
+
+    assertEquals("E1", service.nextID("1"));           
+    assertEquals(currentYear + ".1.1", service.nextID("2"));
+    
+       }
+
+       public void testNextIDNotValidPattern() {
+       
+               try {
+      nextId = service.nextID("3");
+                       fail("Should have thrown IllegalArgumentException here");
+               } catch (IllegalArgumentException expected) {
+                       // This Exception should be thrown, and thus the test should pass.
+               }
+               
+       }
+       
+}
index 9a1120b6a7cb1c09258e65036b157e6c081eef2d..88059188e19c71355389dd5bde6f23387329c153 100644 (file)
@@ -60,6 +60,14 @@ public class IDPatternTest extends TestCase {
                pattern.add(new StringIDPart("-"));
                pattern.add(new AlphabeticIDPart("a")); 
                assertEquals("2009.1-a", pattern.getCurrentID());
+
+               pattern = new IDPattern();
+               pattern.add(new YearIDPart("2009"));
+               pattern.add(new StringIDPart("."));
+               pattern.add(new NumericIDPart("0"));
+               pattern.add(new StringIDPart("."));
+               pattern.add(new NumericIDPart("0"));
+               assertEquals("2009.0.0", pattern.getCurrentID());
                        
        }
 
@@ -160,6 +168,14 @@ public class IDPatternTest extends TestCase {
                pattern.add(new NumericIDPart("1005"));
                assertEquals("T1006", pattern.nextID());
                assertEquals("T1007", pattern.nextID());
+
+               pattern = new IDPattern();
+               pattern.add(new YearIDPart("2009"));
+               pattern.add(new StringIDPart("."));
+               pattern.add(new NumericIDPart("1"));
+               pattern.add(new StringIDPart("."));
+               pattern.add(new NumericIDPart("1"));
+               assertEquals("2009.1.2", pattern.nextID());
                        
        }
 
@@ -194,7 +210,7 @@ public class IDPatternTest extends TestCase {
                assertEquals("2009.3-c", pattern.nextID("2009.3-b"));
 
        }
-       
+
        public void testEmptyPartsListCurrentID() {
 
                pattern = new IDPattern();
index 374fd147420124338e3e9b30516289ee6c8b8911..5cb7089ce21109ebdd03359997a77dc3f52c71fd 100644 (file)
@@ -43,6 +43,9 @@ public class NumericIDPartTest extends TestCase {
                assertEquals("26", part.nextID());
                assertEquals("27", part.nextID());
                assertEquals("28", part.nextID());
+               
+               part = new NumericIDPart();
+               assertEquals("2", part.nextID());
                        
        }
 
index 54ef72b89586b4608eabb7718b759a7e5bfc3009..6b26571cd613cfe389b8e1acfd955c4c14f4dfaf 100644 (file)
@@ -33,8 +33,13 @@ public class StringIDPartTest extends TestCase {
        IDPart part;
 
        public void testNextID() {
+
+               part = new StringIDPart("E");           
+               assertEquals("E", part.nextID());       
+               
                part = new StringIDPart("XYZ");         
-               assertEquals("XYZ", part.nextID());                     
+               assertEquals("XYZ", part.nextID());             
+               
        }
 
        public void testresetID() {