From 5a84ab94466d02ca9409d266d2322fb14cc477f3 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 16 Jul 2009 00:42:23 +0000 Subject: [PATCH] CSPACE-245: First (primitive, stubbed) working version of the ID Service. Resolved "Failed processing arguments of public javax.ws.rs.core.Response" error by initializing Response via a ResponseBuilder. Added test for IDServiceJdbcImpl. --- services/id/service/pom.xml | 12 +--- .../collectionspace/services/IDResource.java | 57 ++++++++++----- .../services/IDServiceJdbcImpl.java | 29 ++++++-- .../services/id/YearIDGenerator.java | 2 +- .../services/IDServiceJdbcImplTest.java | 71 +++++++++++++++++++ .../services/id/IDPatternTest.java | 18 ++++- .../services/id/NumericIDPartTest.java | 3 + .../services/id/StringIDPartTest.java | 7 +- 8 files changed, 160 insertions(+), 39 deletions(-) create mode 100644 services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java diff --git a/services/id/service/pom.xml b/services/id/service/pom.xml index 3c90f2664..8fb48674d 100644 --- a/services/id/service/pom.xml +++ b/services/id/service/pom.xml @@ -144,16 +144,12 @@ 1.1.5 - + + org.codehaus.mojo cobertura-maven-plugin 2.2 - - net.sourceforge.cobertura - cobertura - 1.9 - @@ -182,9 +178,5 @@ - - - maven-cobertura-plugin - diff --git a/services/id/service/src/main/java/org/collectionspace/services/IDResource.java b/services/id/service/src/main/java/org/collectionspace/services/IDResource.java index 8aad61e29..55428e956 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/IDResource.java +++ b/services/id/service/src/main/java/org/collectionspace/services/IDResource.java @@ -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; + } ////////////////////////////////////////////////////////////////////// diff --git a/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java b/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java index f9c5b85c4..71a110508 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java +++ b/services/id/service/src/main/java/org/collectionspace/services/IDServiceJdbcImpl.java @@ -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 { } } - + } diff --git a/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGenerator.java b/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGenerator.java index 8fe648cc9..49f42b2d2 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGenerator.java +++ b/services/id/service/src/main/java/org/collectionspace/services/id/YearIDGenerator.java @@ -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 index 000000000..7cab76a0c --- /dev/null +++ b/services/id/service/src/test/java/org/collectionspace/services/IDServiceJdbcImplTest.java @@ -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. + } + + } + +} diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java index 9a1120b6a..88059188e 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/IDPatternTest.java @@ -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(); diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/NumericIDPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/NumericIDPartTest.java index 374fd1474..5cb7089ce 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/NumericIDPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/NumericIDPartTest.java @@ -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()); } diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/StringIDPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/StringIDPartTest.java index 54ef72b89..6b26571cd 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/StringIDPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/StringIDPartTest.java @@ -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() { -- 2.47.3