<name>services.id.client</name>
<dependencies>
- <!-- keep slf4j dependencies on the top -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- CollectionSpace -->
<dependency>
<groupId>org.collectionspace.services</groupId>
<artifactId>org.collectionspace.services.client</artifactId>
<version>${project.version}</version>
</dependency>
- <!-- External dependencies -->
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>5.6</version>
- </dependency>
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jaxrs</artifactId>
- <!-- filter out unwanted jars -->
- <exclusions>
- <exclusion>
- <groupId>tjws</groupId>
- <artifactId>webserver</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
<dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jaxb-provider</artifactId>
- </dependency>
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-multipart-provider</artifactId>
+ <groupId>org.collectionspace.services</groupId>
+ <artifactId>org.collectionspace.services.id.service</artifactId>
+ <version>1.8-SNAPSHOT</version>
+ <scope>test</scope>
+ <type>jar</type>
</dependency>
</dependencies>
package org.collectionspace.services.client;
+import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.ClientResponse;
/**
- * An AcquisitionClient.
-
- * @version $Revision:$
+ * IDClient.
+ *
+ * $LastChangedRevision: $
+ * $LastChangedDate: $
*/
public class IdClient extends AbstractServiceClientImpl<String, IdProxy> {
* @see org.collectionspace.services.client.BaseServiceClient#getServicePathComponent()
*/
@Override
- public String getServicePathComponent() {
+ public String getServicePathComponent() {
return "idgenerators";
}
- @Override
- public String getServiceName() {
- return null; //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3497
- }
+ @Override
+ public String getServiceName() {
+ return null; //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3497
+ }
- @Override
- public Class<IdProxy> getProxyClass() {
- return IdProxy.class;
- }
+ @Override
+ public Class<IdProxy> getProxyClass() {
+ return IdProxy.class;
+ }
- /*
+ /*
* Proxied service calls
*/
- public ClientResponse<String> readList() {
- return getProxy().readList();
+ // Operations on ID Generators
+
+ public ClientResponse<Response> create(String xmlPayload) {
+ return getProxy().create(xmlPayload);
}
public ClientResponse<String> read(String csid) {
return getProxy().read(csid);
}
+
+ public ClientResponse<String> readList() {
+ return getProxy().readList();
+ }
+
+ @Override
+ public ClientResponse<Response> delete(String csid) {
+ return getProxy().delete(csid);
+ }
+
+ // Operations on IDs
public ClientResponse<String> createId(String csid) {
return getProxy().createId(csid);
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
+import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.ClientResponse;
+/**
+ * IDProxy.
+ *
+ * $LastChangedRevision: $
+ * $LastChangedDate: $
+ */
+
@Path("/idgenerators/")
-@Produces({"application/xml"})
-@Consumes({"text/plain"})
+@Produces({"text/plain"})
public interface IdProxy extends CollectionSpaceProxy<String> {
-
+
+ // Operations on ID Generators
+
+ //(C)reate ID generator
+ @POST
+ @Consumes({"application/xml"})
+ @Produces({"*/*"})
+ ClientResponse<Response> create(String xmlPayload);
+
+ //(R)ead ID Generator
+ @GET
+ @Path("/{csid}")
+ @Produces({"application/xml"})
+ ClientResponse<String> read(@PathParam("csid") String csid);
+
+ // Read (L)ist of ID Generators
@GET
@Produces({"application/xml"})
ClientResponse<String> readList();
-
- //(C)reate
+
+ //(D)elete ID Generator
+ @DELETE
+ @Path("/{csid}")
+ @Override
+ ClientResponse<Response> delete(@PathParam("csid") String csid);
+
+ // Operations on IDs
+
+ //(C)reate ID
@POST
@Path("/{csid}/ids")
- @Produces({"text/plain"})
ClientResponse<String> createId(@PathParam("csid") String csid);
-
- //(R)ead
- @GET
- @Path("/{csid}")
- @Produces({"application/xml"})
- ClientResponse<String> read(@PathParam("csid") String csid);
+
}
*/
package org.collectionspace.services.client.test;
+import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.collectionspace.services.client.CollectionSpaceClient;
import org.collectionspace.services.client.IdClient;
+import org.collectionspace.services.common.document.BadRequestException;
+import org.collectionspace.services.common.document.DocumentNotFoundException;
+import org.collectionspace.services.id.IDGeneratorSerializer;
+import org.collectionspace.services.id.NumericIDGeneratorPart;
+import org.collectionspace.services.id.SettableIDGenerator;
+import org.collectionspace.services.id.StringIDGeneratorPart;
import org.collectionspace.services.jaxb.AbstractCommonList;
import org.jboss.resteasy.client.ClientResponse;
private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
String knownResourceId = "";
-
+ private List<String> allResourceIdsCreated = new ArrayList<String>();
/* (non-Javadoc)
* @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
// ---------------------------------------------------------------
// Success outcomes
+
+ @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
+ public void create(String testName) throws Exception {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug(testBanner(testName, CLASS_NAME));
+ };
+
+ // Perform setup.
+ testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
+
+ // Submit the request to the service and store the response.
+ IdClient client = new IdClient();
+
+ String xmlPayload = getSampleSerializedIdGenerator();
+ logger.debug("payload=\n" + xmlPayload);
+ ClientResponse<Response> res = client.create(xmlPayload);
+ int statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match
+ // the expected response(s)?
+ if(logger.isDebugEnabled()){
+ logger.debug(testName + ": status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+
+ String newID = extractId(res);
+
+ // Store the ID returned from the first resource created
+ // for additional tests below.
+ if (knownResourceId.isEmpty()){
+ knownResourceId = newID;
+ if (logger.isDebugEnabled()) {
+ logger.debug(testName + ": knownResourceId=" + knownResourceId);
+ }
+ }
+
+ // Store the IDs from every resource created by tests,
+ // so they can be deleted after tests have been run.
+ allResourceIdsCreated.add(newID);
+
+ }
- // Uncomment when getIDGeneratorCSID() no longer returns a hard-coded value.
-/*
@Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
- dependsOnMethods = {"readList", "read"})
+ dependsOnMethods = {"create"})
public void createId(String testName) throws Exception {
if (logger.isDebugEnabled()) {
invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
- String entity = res.getEntity();
- Assert.assertNotNull(entity);
+ String generatedId = res.getEntity();
+ Assert.assertNotNull(generatedId);
+ Assert.assertFalse(generatedId.isEmpty());
if (logger.isDebugEnabled()) {
- logger.debug("entity body=\r" + entity);
+ logger.debug("generated ID=" + generatedId);
+ }
+
+ // Create a second ID. Verify that it is different from the first.
+ // Assumes that the last part in the ID pattern generates values
+ // that will always differ at each generation.
+ res = client.createId(knownResourceId);
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+
+ String secondGeneratedId = res.getEntity();
+ Assert.assertNotNull(secondGeneratedId);
+ Assert.assertFalse(secondGeneratedId.isEmpty());
+ Assert.assertFalse(secondGeneratedId.equals(generatedId));
+ if (logger.isDebugEnabled()) {
+ logger.debug("second generated ID=" + secondGeneratedId);
}
+
}
- *
- */
// Failure outcomes
// None at present.
// Success outcomes
@Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
- dependsOnMethods = {"readList"})
+ dependsOnMethods = {"create"})
public void read(String testName) throws Exception {
if (logger.isDebugEnabled()) {
// Perform setup.
testSetup(STATUS_OK, ServiceRequestType.READ);
+
+ if(logger.isDebugEnabled()){
+ logger.debug("Reading ID Generator at CSID " + knownResourceId + " ...");
+ }
// Submit the request to the service and store the response.
IdClient client = new IdClient();
// Success outcomes
- @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
+ @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
+ dependsOnMethods = {"create"})
public void readList(String testName) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("entity body=\r" + entity);
}
- knownResourceId = getOneIDGeneratorCSID(entity);
}
// Failure outcomes
// None at present.
+
+ @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
+ dependsOnMethods = {"create", "createId", "read", "readList"})
+ public void delete(String testName) throws Exception {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug(testBanner(testName, CLASS_NAME));
+ }
+
+ // Perform setup.
+ testSetup(STATUS_OK, ServiceRequestType.DELETE);
+
+ // Submit the request to the service and store the response.
+ IdClient client = new IdClient();
+ ClientResponse<Response> res = client.delete(knownResourceId);
+ int statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match
+ // the expected response(s)?
+ if(logger.isDebugEnabled()){
+ logger.debug(testName + ": status = " + statusCode);
+ }
+ Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
+ Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
+ }
// ---------------------------------------------------------------
// Utility methods used by tests above
return new IdClient().getServicePathComponent();
}
- private String getOneIDGeneratorCSID(String entity) {
- // FIXME: Temporarliy uses a hard-coded, known ID.
- //
- // Instead get this from the entity body, for now via XPath,
- // and later, when we've declared an XSD for ID Generator payloads,
- // via the appropriate JAXB-generated method.
- return "4b984865-f93d-4481-b874-3dba863ec589";
- }
-
@Override
protected String getServiceName() {
throw new UnsupportedOperationException(); //FIXME: REM - See http://issues.collectionspace.org/browse/CSPACE-3498 }
}
+
+ /**
+ * Returns a serialized ID generator, based on the SPECTRUM entry number pattern,
+ * as a sample ID generator to be used in tests.
+ *
+ * This is a minimal ID Generator, containing only ID Generator Parts,
+ * and lacking a display name, description etc.
+ *
+ * @return a serialized ID Generator
+ * @throws BadRequestException
+ */
+ public String getSampleSerializedIdGenerator() throws BadRequestException {
+
+ SettableIDGenerator generator = new SettableIDGenerator();
+ generator.add(new StringIDGeneratorPart("E"));
+ generator.add(new NumericIDGeneratorPart("1"));
+ return IDGeneratorSerializer.serialize(generator);
+
+ }
}
<packaging>pom</packaging>
<modules>
+ <!--module>3rdparty</module-->
<!--module>jaxb</module-->
<module>service</module>
- <!--module>3rdparty</module-->
- <!-- Uncomment the following when/if we'll be creating a client library for this service. -->
<module>client</module>
</modules>
<name>services.id.service</name>
<dependencies>
- <!-- keep slf4j dependencies on the top -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </dependency>
+
<!-- Code common to all CollectionSpace services. -->
<dependency>
<groupId>org.collectionspace.services</groupId>
<scope>test</scope>
</dependency>
- <!-- TestNG, a testing framework. -->
- <!-- NOTE: Versions later than 5.6 - such as 5.8 - are available. -->
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>5.6</version>
- </dependency>
-
- <!-- javax Security, a Java framework for authentication and authorization. -->
- <!-- JAAS support: is javax security needed with JAAS's incorporation into JDK 1.4? -->
- <dependency>
- <groupId>javax.security</groupId>
- <artifactId>jaas</artifactId>
- <version>1.0.01</version>
- <scope>provided</scope>
- </dependency>
-
- <!-- Dom4j, a framework for processing XML -->
- <dependency>
- <groupId>dom4j</groupId>
- <artifactId>dom4j</artifactId>
- <version>1.6.1</version>
- <scope>provided</scope>
- </dependency>
-
- <!-- JBoss RESTEasy, a framework for providing and consuming RESTful services. -->
- <!-- JAX-RS support, a Java specification for RESTful services. -->
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jaxrs</artifactId>
- <exclusions>
- <exclusion>
- <groupId>tjws</groupId>
- <artifactId>webserver</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- JAXB support, a Java spec for binding objects to XML (& JSON ...) representations. -->
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-jaxb-provider</artifactId>
- </dependency>
- <!-- MIME Multipart messaging support. -->
- <dependency>
- <groupId>org.jboss.resteasy</groupId>
- <artifactId>resteasy-multipart-provider</artifactId>
- </dependency>
-
<!-- Cobertura, a tool for identifying the extent of code coverage in test classes
<dependency>
<groupId>org.codehaus.mojo</groupId>
<version>2.2</version>
</dependency> -->
- <!-- MySQL Connector, a JDBC driver for the MySQL DBMS -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <!--version>5.1.6</version-->
- <scope>test</scope>
- </dependency>
-
<!-- XStream, an XML serializer -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
*/
package org.collectionspace.services.id;
-import java.io.StringWriter;
import java.util.Map;
+import java.util.UUID;
import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.collectionspace.services.common.document.DocumentNotFoundException;
import org.dom4j.Document;
-import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
-import org.dom4j.io.OutputFormat;
-import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*/
// Set the base path component for URLs that access this service.
@Path("/idgenerators")
+@Produces(MediaType.TEXT_PLAIN)
public class IDResource {
final Logger logger = LoggerFactory.getLogger(IDResource.class);
final static IDService service = new IDServiceJdbcImpl();
-
// Query parameter names and values.
final static String QUERY_PARAM_LIST_FORMAT = "format";
final static String LIST_FORMAT_SUMMARY = "summary";
final static String LIST_FORMAT_FULL = "full";
final static String QUERY_PARAM_ID_GENERATOR_ROLE = "role";
-
// XML namespace for the ID Service.
final static String ID_SERVICE_NAMESPACE =
- "http://collectionspace.org/services/id";
+ "http://collectionspace.org/services/id";
final static String ID_SERVICE_NAMESPACE_PREFIX = "ns2";
-
// Names of elements for ID generator instances, lists and list items.
final static String ID_GENERATOR_NAME = "idgenerator";
final static String ID_GENERATOR_COMPONENTS_NAME = "idgenerator-components";
final static String ID_GENERATOR_LIST_NAME = "idgenerator-list";
final static String ID_GENERATOR_LIST_ITEM_NAME = "idgenerator-list-item";
-
-
// Base URL path for REST-based requests to the ID Service.
//
// @TODO Investigate whether this can be obtained from the
*/
@POST
@Path("/{csid}/ids")
- @Produces(MediaType.TEXT_PLAIN)
public Response newID(@PathParam("csid") String csid) {
// @TODO The JavaDoc description reflects an as-yet-to-be-carried out
// If the new ID is empty, return an error response.
if (newId == null || newId.trim().isEmpty()) {
response =
- Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity("ID Service returned null or empty ID")
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("ID Service returned null or empty ID").type(MediaType.TEXT_PLAIN).build();
return response;
}
// - HTTP Status code (to '201 Created')
// - Content-type header (to the relevant media type)
// - Entity body (to the new ID)
- response = Response.status(Response.Status.CREATED)
- .entity(newId)
- .type(MediaType.TEXT_PLAIN)
- .build();
+ response = Response.status(Response.Status.CREATED).entity(newId).type(MediaType.TEXT_PLAIN).build();
- // @TODO Return an XML-based error results format with the
- // responses below.
+ // @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 still warranted, or whether returning some other
- // status would be more appropriate.
+ // @TODO An IllegalStateException often indicates an overflow
+ // of an IDPart. Consider whether returning a 400 Bad Request
+ // status code is still warranted, or whether returning some other
+ // status would be more appropriate.
} catch (DocumentNotFoundException dnfe) {
- response = Response.status(Response.Status.NOT_FOUND)
- .entity(dnfe.getMessage()).type(MediaType.TEXT_PLAIN).build();
+ response = Response.status(Response.Status.NOT_FOUND).entity(dnfe.getMessage()).type(MediaType.TEXT_PLAIN).build();
} catch (BadRequestException bre) {
- response = Response.status(Response.Status.BAD_REQUEST)
- .entity(bre.getMessage()).type(MediaType.TEXT_PLAIN).build();
+ response = Response.status(Response.Status.BAD_REQUEST).entity(bre.getMessage()).type(MediaType.TEXT_PLAIN).build();
} catch (IllegalStateException ise) {
- response = Response.status(Response.Status.BAD_REQUEST)
- .entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build();
+ response = Response.status(Response.Status.BAD_REQUEST).entity(ise.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(MediaType.TEXT_PLAIN).build();
+ response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
}
return response;
@POST
@Path("")
@Consumes(MediaType.APPLICATION_XML)
- @Produces(MediaType.TEXT_PLAIN)
- public Response createIDGenerator() {
-
- // @TODO Implement this stubbed method
- // by replacing this placeholder code.
+ public Response createIDGenerator(String xmlPayload) {
ResponseBuilder builder = Response.ok();
Response response = builder.build();
- // If we successfully obtained a new ID from the specified
- // ID generator instance, return it in the response.
+ try {
- String id = "idGeneratorResourceIdGoesHere";
- // String id = service.createIDGenerator();
+ String csid = UUID.randomUUID().toString();
+ service.createIDGenerator(csid, xmlPayload);
- // Build the URI to be returned in the Location header.
- //
- // Gets the base URL path to this resource.
- UriBuilder path = UriBuilder.fromResource(IDResource.class);
- // @TODO Look into whether we can create the path using the
- // URI template in the @Path annotation to this method, rather
- // than the hard-coded analog to that template currently used.
- path.path("" + id);
-
- // Build the response, setting the:
- // - HTTP Status code (to '201 Created')
- // - Content-type header (to the relevant media type)
- // - Entity body (to the new ID)
- response =
- Response.created(path.build())
- .entity("")
- .type(MediaType.TEXT_PLAIN)
- .build();
+ // Build the URI to be returned in the Location header.
+ //
+ // Gets the base URL path to this resource.
+ UriBuilder path = UriBuilder.fromResource(IDResource.class);
+ // @TODO Look into whether we can create the path using the
+ // URI template in the @Path annotation to this method, rather
+ // than the hard-coded analog to that template currently used.
+ path.path("" + csid);
+
+ // Build the response, setting the:
+ // - HTTP Status code (to '201 Created')
+ // - Content-type header (to the relevant media type)
+ // - Entity body (to the new ID)
+ response =
+ Response.created(path.build()).entity("").type(MediaType.TEXT_PLAIN).build();
+
+ } catch (Exception e) {
+ response =
+ Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
+ }
return response;
}
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement(ID_GENERATOR_NAME);
Namespace namespace =
- new Namespace(ID_SERVICE_NAMESPACE_PREFIX, ID_SERVICE_NAMESPACE);
+ new Namespace(ID_SERVICE_NAMESPACE_PREFIX, ID_SERVICE_NAMESPACE);
doc.getRootElement().add(namespace);
// Make new elements for each of the components of this ID generator
resourceRepresentation = XmlTools.prettyPrintXML(doc);
response =
- Response.status(Response.Status.OK)
- .entity(resourceRepresentation)
- .type(MediaType.APPLICATION_XML)
- .build();
+ Response.status(Response.Status.OK).entity(resourceRepresentation).type(MediaType.APPLICATION_XML).build();
- // @TODO Return an XML-based error results format with the
- // responses below.
+ // @TODO Return an XML-based error results format with the
+ // responses below.
} catch (DocumentNotFoundException dnfe) {
response =
- Response.status(Response.Status.NOT_FOUND)
- .entity(dnfe.getMessage())
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.NOT_FOUND).entity(dnfe.getMessage()).type(MediaType.TEXT_PLAIN).build();
} catch (IllegalStateException ise) {
response =
- Response.status(Response.Status.BAD_REQUEST)
- .entity(ise.getMessage())
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.BAD_REQUEST).entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build();
- // This is guard code that should never be reached.
+ // This is guard code that should never be reached.
} catch (Exception e) {
response =
- Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(e.getMessage())
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
}
return response;
@Path("")
@Produces(MediaType.APPLICATION_XML)
public Response readIDGeneratorsList(
- @QueryParam(QUERY_PARAM_LIST_FORMAT) String listFormat,
- @QueryParam(QUERY_PARAM_ID_GENERATOR_ROLE) String role) {
+ @QueryParam(QUERY_PARAM_LIST_FORMAT) String listFormat,
+ @QueryParam(QUERY_PARAM_ID_GENERATOR_ROLE) String role) {
// @TODO The names and values of the query parameters above
// ("format"and "role") are arbitrary, as are the format of the
String resourceRepresentation = "";
try {
- Map<String,IDGeneratorInstance> generators =
+ Map<String, IDGeneratorInstance> generators =
service.readIDGeneratorsList();
// If no ID generator instances were found, return an empty list.
- if (generators == null || generators.size() == 0) {
+ if (generators == null || generators.isEmpty()) {
Document doc = baseListDocument();
resourceRepresentation = doc.asXML();
response =
- Response.status(Response.Status.OK)
- .entity(resourceRepresentation)
- .type(MediaType.APPLICATION_XML)
- .build();
+ Response.status(Response.Status.OK).entity(resourceRepresentation).type(MediaType.APPLICATION_XML).build();
return response;
}
// ID generator instances.
if (role == null || role.trim().isEmpty()) {
// Do nothing
- // Otherwise, return only ID generator instances
- // matching the requested role.
+ // Otherwise, return only ID generator instances
+ // matching the requested role.
} else {
// @TODO Implement this stubbed code, by
// iterating over generator instances and
resourceRepresentation = formattedSummaryList(generators);
} else if (listFormat.equalsIgnoreCase(LIST_FORMAT_FULL)) {
resourceRepresentation = formattedFullList(generators);
- // Return an error if the value of the query parameter
- // is unrecognized.
- //
- // @TODO Return an appropriate XML-based entity body upon error.
+ // Return an error if the value of the query parameter
+ // is unrecognized.
+ //
+ // @TODO Return an appropriate XML-based entity body upon error.
} else {
String msg =
- "Query parameter '" + listFormat + "' was not recognized.";
+ "Query parameter '" + listFormat + "' was not recognized.";
if (logger.isDebugEnabled()) {
logger.debug(msg);
}
response =
- Response.status(Response.Status.BAD_REQUEST)
- .entity("")
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.BAD_REQUEST).entity("").type(MediaType.TEXT_PLAIN).build();
}
response =
- Response.status(Response.Status.OK)
- .entity(resourceRepresentation)
- .type(MediaType.APPLICATION_XML)
- .build();
+ Response.status(Response.Status.OK).entity(resourceRepresentation).type(MediaType.APPLICATION_XML).build();
- // @TODO Return an XML-based error results format with the
- // responses below.
+ // @TODO Return an XML-based error results format with the
+ // responses below.
} catch (IllegalStateException ise) {
response =
- Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(ise.getMessage())
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ise.getMessage()).type(MediaType.TEXT_PLAIN).build();
- // This is guard code that should never be reached.
+ // This is guard code that should never be reached.
} catch (Exception e) {
response =
- Response.status(Response.Status.INTERNAL_SERVER_ERROR)
- .entity(e.getMessage())
- .type(MediaType.TEXT_PLAIN)
- .build();
+ Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
}
return response;
}
//////////////////////////////////////////////////////////////////////
+ /**
+ * Creates a new ID generator instance.
+ *
+ */
+ @DELETE
+ @Path("/{csid}")
+ public Response deleteIDGenerator(@PathParam("csid") String csid) {
+
+ ResponseBuilder builder = Response.ok();
+ Response response = builder.build();
+
+ try {
+ service.deleteIDGenerator(csid);
+ response = Response.ok().entity("").type(MediaType.TEXT_PLAIN).build();
+ } catch (Exception e) {
+ response =
+ Response.status(Response.Status.INTERNAL_SERVER_ERROR).
+ entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
+ }
+
+ return response;
+ }
+
+//////////////////////////////////////////////////////////////////////
/**
* Identifies whether the specified ID generator instance can
* generate and validate IDs in a specified role (aka type or context).
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement(ID_GENERATOR_LIST_NAME);
Namespace namespace =
- new Namespace(ID_SERVICE_NAMESPACE_PREFIX, ID_SERVICE_NAMESPACE);
+ new Namespace(ID_SERVICE_NAMESPACE_PREFIX, ID_SERVICE_NAMESPACE);
doc.getRootElement().add(namespace);
return doc;
* @return A summary list of ID generator instances.
*/
private String formattedSummaryList(
- Map<String,IDGeneratorInstance> generators) {
+ Map<String, IDGeneratorInstance> generators) {
Document doc = baseListDocument();
// Retrieve the CSIDs from each ID generator instance,
// and use these in summary information returned about
// each instance.
- for (String csid : generators.keySet())
- {
+ for (String csid : generators.keySet()) {
// Add a new element for each item in the list.
listitem =
- doc.getRootElement().addElement(ID_GENERATOR_LIST_ITEM_NAME);
+ doc.getRootElement().addElement(ID_GENERATOR_LIST_ITEM_NAME);
// Append display name information for this ID generator instance.
displayname = generators.get(csid).getDisplayName();
listitem = appendDisplayNameIDGeneratorInformation(listitem, displayname);
// Append summary information about this ID generator instance.
listitem = appendSummaryIDGeneratorInformation(listitem, csid);
- }
+ }
- return XmlTools.prettyPrintXML(doc);
+ return XmlTools.prettyPrintXML(doc);
}
//////////////////////////////////////////////////////////////////////
* @return A full list of ID generator instances.
*/
private String formattedFullList(
- Map<String,IDGeneratorInstance> generators) {
+ Map<String, IDGeneratorInstance> generators) {
Document doc = baseListDocument();
Element listitem = null;
String displayname = "";
- for (String csid : generators.keySet())
- {
+ for (String csid : generators.keySet()) {
// Add a new element for each item in the list.
listitem =
- doc.getRootElement().addElement(ID_GENERATOR_LIST_ITEM_NAME);
+ doc.getRootElement().addElement(ID_GENERATOR_LIST_ITEM_NAME);
// Append display name information for this ID generator instance.
displayname = generators.get(csid).getDisplayName();
listitem = appendDisplayNameIDGeneratorInformation(listitem, displayname);
// Append details of this ID generator instance.
Element instance = listitem.addElement(ID_GENERATOR_NAME);
listitem = appendDetailedIDGeneratorInformation(instance,
- generators.get(csid));
+ generators.get(csid));
}
return XmlTools.prettyPrintXML(doc);
}
-
+
//////////////////////////////////////////////////////////////////////
/**
* Appends a display name to an element representing
* @return The XML element representing an ID generator instance,
* with the display name appended.
*/
- private Element appendDisplayNameIDGeneratorInformation(Element instanceElement,
- String displaynameValue) {
+ private Element appendDisplayNameIDGeneratorInformation(Element instanceElement,
+ String displaynameValue) {
Element displayname = instanceElement.addElement("displayname");
displayname.addText(displaynameValue);
return instanceElement;
}
-
//////////////////////////////////////////////////////////////////////
/**
* Appends summary information to an element representing
* @return The XML element representing an ID generator instance,
* with summary information appended.
*/
- private Element appendSummaryIDGeneratorInformation(Element instanceElement,
- String csidValue) {
+ private Element appendSummaryIDGeneratorInformation(Element instanceElement,
+ String csidValue) {
Element uri = instanceElement.addElement("uri");
uri.addText(getRelativePath(csidValue));
* with detailed information appended.
*/
private Element appendDetailedIDGeneratorInformation(
- Element instanceElement, IDGeneratorInstance generatorInstance) {
+ Element instanceElement, IDGeneratorInstance generatorInstance) {
// Append description information.
Element description = instanceElement.addElement("description");
// or launch time; or generate or load this value once, at the
// time that an ID generator instance is created.
String lastgenerated = generatorInstance.getLastGeneratedID();
- if (lastgenerated != null & ! lastgenerated.trim().isEmpty()) {
+ if (lastgenerated != null & !lastgenerated.trim().isEmpty()) {
displayid.addText(lastgenerated);
} else {
SettableIDGenerator gen;
try {
- gen = IDGeneratorSerializer.deserialize(generatorInstance
- .getGeneratorState());
+ gen = IDGeneratorSerializer.deserialize(generatorInstance.getGeneratorState());
String current = gen.getCurrentID();
- if (current != null & ! current.trim().isEmpty()) {
+ if (current != null & !current.trim().isEmpty()) {
displayid.addText(current);
}
} catch (Exception e) {
// Do nothing here.
-
// @TODO
// Could potentially return an error message, akin to:
// displayid.addText("No ID available for display");
// Append components information.
Element generator =
- instanceElement.addElement(ID_GENERATOR_COMPONENTS_NAME);
+ instanceElement.addElement(ID_GENERATOR_COMPONENTS_NAME);
// Get an XML string representation of the ID generator's components.
String generatorStr = generatorInstance.getGeneratorState();
// Convert the XML string representation of the ID generator's
Document generatorDoc = XmlTools.textToXMLDocument(generatorStr);
Element generatorRoot = generatorDoc.getRootElement();
generator.add(generatorRoot.createCopy());
- // If an error occurs parsing the XML string representation,
- // the text of the components element will remain empty.
+ // If an error occurs parsing the XML string representation,
+ // the text of the components element will remain empty.
} catch (Exception e) {
if (logger.isDebugEnabled()) {
- logger.debug("Error parsing XML text: " + generatorStr);
+ logger.debug("Error parsing XML text: " + generatorStr);
}
}
return instanceElement;
}
-
//////////////////////////////////////////////////////////////////////
/**
* Returns a relative URI path to a resource
*/
private String getRelativePath(String csid) {
- // @TODO Verify that this is the correct relative path.
- // Do we need to check the path provided in the original request?
-
- if (csid !=null && ! csid.trim().isEmpty()) {
- return BASE_URL_PATH + "/" + csid;
- } else {
- return BASE_URL_PATH;
- }
- }
+ // @TODO Verify that this is the correct relative path.
+ // Do we need to check the path provided in the original request?
+ if (csid != null && !csid.trim().isEmpty()) {
+ return BASE_URL_PATH + "/" + csid;
+ } else {
+ return BASE_URL_PATH;
+ }
+ }
}
* You may obtain a copy of the ECL 2.0 License at
* https://source.collectionspace.org/collection-space/LICENSE.txt
*/
-
package org.collectionspace.services.id;
-// May at some point instead use
-// org.jboss.resteasy.spi.NotFoundException
import java.util.Map;
-import org.collectionspace.services.common.document.DocumentNotFoundException;
-import org.collectionspace.services.common.document.BadRequestException;
-
/**
* IDService
*
* Interface for the ID Service.
*
- * $LastChangedBy$
* $LastChangedRevision$
* $LastChangedDate$
*/
// ----------------------------------------
// IDs
// ----------------------------------------
-
// Create
-
// Read single object
-
// Generates and returns a new ID from the specified ID generator.
- public String createID(String csid) throws DocumentNotFoundException,
- BadRequestException, IllegalStateException;
-
+ public String createID(String csid) throws Exception;
+
// Returns the last-generated ID associated with the specified ID generator.
public String readLastID(String csid)
- throws DocumentNotFoundException, IllegalStateException;
+ throws Exception;
// Read a list of objects (aka read multiple)
-
// ----------------------------------------
// ID Generators
// ----------------------------------------
-
// Create
-
// Adds a new ID generator.
public void createIDGenerator(String csid, String serializedIDGenerator)
- throws BadRequestException, IllegalStateException;
-
+ throws Exception;
+
// Read single object
public IDGeneratorInstance readIDGenerator(String csid)
- throws DocumentNotFoundException, IllegalStateException;
-
+ throws Exception;
+
// Read a list of objects (aka read multiple)
// and return a list (map) of those objects and their identifiers.
- public Map<String,IDGeneratorInstance> readIDGeneratorsList()
- throws IllegalStateException;
+ public Map<String, IDGeneratorInstance> readIDGeneratorsList()
+ throws Exception;
// Update
- public void updateIDGenerator(String csid, String serializedIDGenerator)
- throws DocumentNotFoundException, BadRequestException,
- IllegalArgumentException, IllegalStateException;
-
- // Delete (possibly not permitted - deactivate instead?)
+ public void updateIDGenerator(String csid, String serializedIDGenerator)
+ throws Exception;
+ // Delete (possibly not permitted - deactivate instead?)
+ public void deleteIDGenerator(String csid)
+ throws Exception;
}
//
// We're currently overloading existing core and extension Java Exceptions
// in ways that are not consistent with their original semantic meaning.
-// @TODO Get the JDBC driver classname and database URL from configuration;
-// better yet, substitute JPA or Hibernate for JDBC for accessing
-// database-managed persistence.
// @TODO Remove any hard-coded dependencies on MySQL.
// @TODO Determine how to restrict access to ID-related tables by role.
// @TODO Retrieve IDGenerators from the database (via JDBC or
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
-
-// May at some point instead use
-// org.jboss.resteasy.spi.NotFoundException
import java.util.LinkedHashMap;
import java.util.Map;
+import javax.security.auth.login.LoginException;
import org.collectionspace.services.common.document.BadRequestException;
import org.collectionspace.services.common.document.DocumentNotFoundException;
+import org.collectionspace.services.common.storage.JDBCTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IDServiceJdbcImpl implements IDService {
final Logger logger = LoggerFactory.getLogger(IDServiceJdbcImpl.class);
- final String JDBC_DRIVER_CLASSNAME = "com.mysql.jdbc.Driver";
- final String DATABASE_NAME = "cspace";
- final String DATABASE_URL = "jdbc:mysql://localhost:3306/" + DATABASE_NAME;
- final String DATABASE_USERNAME = "test";
- final String DATABASE_PASSWORD = "test";
final String TABLE_NAME = "id_generator";
boolean jdbcDriverInstantiated = false;
boolean hasPreconditions = true;
/**
* Constructor (no-argument).
*/
- public void IDServiceJdbcImpl() throws IllegalStateException {
-
- // @TODO Decide when and how to fail at startup, or else to correct
- // failure conditions automatically, when preconditions are not met.
- //
- // Currently, errors at initialization are merely informative and
- // result in exceptions that can be persistently logged.
-
- try {
- init();
- } catch (IllegalStateException e) {
- throw e;
- }
-
- }
-
- // @TODO init() is currently UNTESTED as of 2009-08-11T13:00-0700.
- //////////////////////////////////////////////////////////////////////
- /**
- * Initializes the service.
- *
- * @throws IllegalStateException if one or more of the required preconditions
- * for the service is not present, or is not in its required state.
- */
- public void init() throws IllegalStateException {
-
- logger.debug("> in init");
-
- try {
- instantiateJdbcDriver(JDBC_DRIVER_CLASSNAME);
- } catch (IllegalStateException e) {
- throw e;
- }
-
- try {
- boolean hasTable = hasTable(TABLE_NAME);
- if (!hasTable) {
- String msg =
- "Required table "
- + "\'" + TABLE_NAME + "\'"
- + " could not be found in the database.";
- logger.warn(msg);
- throw new IllegalStateException(msg);
- }
- } catch (IllegalStateException e) {
- throw e;
- }
-
+ public void IDServiceJdbcImpl() {
}
// -----------------
* @throws IllegalStateException if a storage-related error occurred.
*/
@Override
- public String createID(String csid) throws DocumentNotFoundException,
- BadRequestException, IllegalArgumentException, IllegalStateException {
+ public String createID(String csid) throws Exception {
logger.debug("> in createID");
* @throws DocumentNotFoundException if the requested ID generator could not be found.
*/
public void updateLastID(String csid, String lastId)
- throws IllegalStateException, DocumentNotFoundException {
+ throws IllegalStateException, DocumentNotFoundException, LoginException, SQLException {
logger.debug("> in updateLastID");
* @throws IllegalStateException if a storage-related error occurred.
*/
@Override
- public String readLastID(String csid) throws DocumentNotFoundException,
- IllegalStateException {
+ public String readLastID(String csid) throws Exception {
logger.debug("> in readLastID");
throw new DocumentNotFoundException(
"ID generator " + "\'" + csid + "\'" + " could not be found.");
}
-
- lastId = rs.getString(1);
+ lastId = (rs.getString(1) != null ? rs.getString(1) : "");
logger.debug("> retrieved ID: " + lastId);
rs.close();
* @throws IllegalStateException if a storage-related error occurred.
*/
public void createIDGenerator(String csid, SettableIDGenerator generator)
- throws BadRequestException, IllegalStateException {
+ throws Exception {
logger.debug("> in createIDGenerator(String, SettableIDGenerator)");
*/
@Override
public void createIDGenerator(String csid, String serializedGenerator)
- throws BadRequestException, IllegalStateException {
+ throws Exception {
logger.debug("> in createIDGenerator(String, String)");
* @throws IllegalStateException if a storage-related error occurred.
*/
@Override
- public IDGeneratorInstance readIDGenerator(String csid) throws
- DocumentNotFoundException, IllegalStateException {
+ public IDGeneratorInstance readIDGenerator(String csid) throws Exception {
logger.debug("> in readIDGenerator");
}
instance = new IDGeneratorInstance();
- instance.setDisplayName(rs.getString(2));
- instance.setDescription(rs.getString(3));
- instance.setGeneratorState(rs.getString(4));
- instance.setLastGeneratedID(rs.getString(5));
+ instance.setDisplayName(rs.getString(2) != null ? rs.getString(2) : "");
+ instance.setDescription(rs.getString(3) != null ? rs.getString(3) : "");
+ instance.setGeneratorState(rs.getString(4) != null ? rs.getString(4) : "");
+ instance.setLastGeneratedID(rs.getString(5) != null ? rs.getString(5) : "");
rs.close();
*/
@Override
public Map<String, IDGeneratorInstance> readIDGeneratorsList()
- throws IllegalStateException {
+ throws Exception {
logger.debug("> in readIDGeneratorsList");
IDGeneratorInstance instance = null;
while (moreRows = rs.next()) {
instance = new IDGeneratorInstance();
- instance.setDisplayName(rs.getString(2));
- instance.setDescription(rs.getString(3));
- instance.setGeneratorState(rs.getString(4));
- instance.setLastGeneratedID(rs.getString(5));
+ instance.setDisplayName(rs.getString(2) != null ? rs.getString(2) : "[No display name]");
+ instance.setDescription(rs.getString(3) != null ? rs.getString(3) : "[No description]");
+ instance.setGeneratorState(rs.getString(4) != null ? rs.getString(4) : "[No generator state]");
+ instance.setLastGeneratedID(rs.getString(5) != null ? rs.getString(5) : "[No last generated ID]");
generators.put(rs.getString(1), instance);
}
* @throws IllegalStateException if a storage-related error occurred.
*/
public void updateIDGenerator(String csid, SettableIDGenerator generator)
- throws BadRequestException, DocumentNotFoundException,
- IllegalStateException {
+ throws Exception {
logger.debug("> in updateIDGenerator(String, SettableIDGenerator)");
*/
@Override
public void updateIDGenerator(String csid, String serializedGenerator)
- throws DocumentNotFoundException, BadRequestException,
- IllegalStateException {
+ throws Exception {
logger.debug("> in updateIDGenerator(String, String)");
*
* @throws IllegalStateException if a storage-related error occurred.
*/
+ @Override
public void deleteIDGenerator(String csid)
- throws DocumentNotFoundException, IllegalStateException {
+ throws Exception {
logger.debug("> in deleteIDGenerator");
// -------------------
// Database operations
// -------------------
- //////////////////////////////////////////////////////////////////////
- /**
- * Creates a new instance of the specified JDBC driver class.
- *
- * @param jdbcDriverClassname The name of a JDBC driver class.
- *
- * @throws IllegalStateException if a new instance of the specified
- * JDBC driver class cannot be created.
- */
- public void instantiateJdbcDriver(String jdbcDriverClassname)
- throws IllegalStateException {
-
- logger.debug("> in instantiateJdbcDriver(String)");
-
- try {
- Class.forName(jdbcDriverClassname).newInstance();
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException(
- "Error finding JDBC driver class '"
- + JDBC_DRIVER_CLASSNAME
- + "' to set up database connection.");
- } catch (InstantiationException e) {
- throw new IllegalStateException(
- "Error instantiating JDBC driver class '"
- + JDBC_DRIVER_CLASSNAME
- + "' to set up database connection.");
- } catch (IllegalAccessException e) {
- throw new IllegalStateException(
- "Error accessing JDBC driver class '"
- + JDBC_DRIVER_CLASSNAME
- + "' to set up database connection.");
- }
-
- jdbcDriverInstantiated = true;
-
- }
//////////////////////////////////////////////////////////////////////
/**
*
* @return A JDBC database Connection object.
*
+ * @throws LoginException
* @throws SQLException if a storage-related error occurred.
- *
- * @throws IllegalStateException if the attempt to instantiate the
- * JDBC driver fails.
*/
- public Connection getJdbcConnection() throws SQLException {
+ public Connection getJdbcConnection() throws LoginException, SQLException {
logger.debug("> in getJdbcConnection");
-
- if (! jdbcDriverInstantiated) {
- try {
- instantiateJdbcDriver(JDBC_DRIVER_CLASSNAME);
- } catch (IllegalStateException e) {
- throw e;
- }
- }
+
+ // Providing an empty repository name to getConnection() will cause the
+ // default repository name to be used.
+ final String EMPTY_REPOSITORY_NAME = "";
Connection conn = null;
try {
- conn = DriverManager.getConnection(DATABASE_URL,
- DATABASE_USERNAME, DATABASE_PASSWORD);
+ conn = JDBCTools.getConnection(EMPTY_REPOSITORY_NAME);
+ } catch (LoginException e) {
+ throw e;
} catch (SQLException e) {
throw e;
}
* @throws IllegalStateException if an error occurs while checking for the
* existence of the specified table.
*/
- public boolean hasTable(String tablename) throws IllegalStateException {
+ public boolean hasTable(String tablename) throws Exception {
logger.debug("> in hasTable");
// Retrieve a list of tables in the current database.
final String CATALOG_NAME = null;
- final String SCHEMA_NAME_PATTERN = null;
+ final String SCHEMA_NAME_PATTERN = "cspace";
final String[] TABLE_TYPES = null;
ResultSet tablesMatchingTableName =
conn.getMetaData().getTables(
+++ /dev/null
-/**
- * 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
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.collectionspace.services.id.test;
-
-import java.util.Map;
-import org.collectionspace.services.common.document.BadRequestException;
-import org.collectionspace.services.common.document.DocumentNotFoundException;
-
-import org.collectionspace.services.id.*;
-
-import org.testng.Assert;
-import org.testng.annotations.BeforeSuite;
-import org.testng.annotations.Test;
-
-/**
- * IDServiceJdbcImplTest
- *
- * Unit tests for the ID Service's JDBC implementation class, IDServiceJdbcImpl.
- *
- * $LastChangedRevision: 302 $
- * $LastChangedDate$
- */
-public class IDServiceJdbcImplTest {
-
- String csid;
- String nextId;
- String serializedGenerator;
- SettableIDGenerator generator;
- IDGeneratorInstance instance;
-
- IDServiceJdbcImpl jdbc = new IDServiceJdbcImpl();
- IDService service = jdbc;
-
- final static String TABLE_NAME = "id_generators";
- final static String DEFAULT_CSID = "TEST-1";
-
- final static String CURRENT_YEAR = YearIDGeneratorPart.getCurrentYear();
-
- // FIXME: Hard-coded driver name here should instead come from
- // external configuration.
- final String JDBC_DRIVER_CLASSNAME = "com.mysql.jdbc.Driver";
-
- @BeforeSuite
- public void init() {
- try {
- Class.forName(JDBC_DRIVER_CLASSNAME).newInstance();
- } catch (Exception e) {
- Assert.fail("Error locating required JDBC driver " + JDBC_DRIVER_CLASSNAME);
- }
- }
-
-
- @Test
- public void hasRequiredDatabaseTable() {
- Assert.assertTrue(
- jdbc.hasTable(TABLE_NAME),
- "Table '" + TABLE_NAME + "' must exist in database. " +
- "Please first run SQL setup script, " +
- "'create_id_generators_table.sql', in 'id' project.");
- }
-
- @Test(dependsOnMethods = {"hasRequiredDatabaseTable"})
- public void createIDGenerator() throws DocumentNotFoundException,
- BadRequestException, IllegalStateException {
- try {
- jdbc.deleteIDGenerator(DEFAULT_CSID);
- } catch (Exception e) {
- // Fail silently; this is guard code.
- }
- jdbc.createIDGenerator(DEFAULT_CSID, getSpectrumEntryNumberGenerator());
- }
-
-
- @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator"})
- public void readIDGenerator() throws DocumentNotFoundException,
- BadRequestException, IllegalStateException {
-
- instance = jdbc.readIDGenerator(DEFAULT_CSID);
- Assert.assertTrue(instance != null);
- serializedGenerator = instance.getGeneratorState();
- Assert.assertTrue(! serializedGenerator.equals(""));
- generator = IDGeneratorSerializer.deserialize(serializedGenerator);
- }
-
-
- @Test(dependsOnMethods =
- {"hasRequiredDatabaseTable", "createIDGenerator", "readIDGenerator"})
- public void readIDGeneratorsList() throws IllegalStateException {
-
- Map<String,IDGeneratorInstance> generators = jdbc.readIDGeneratorsList();
-
- // @TODO Replace this placeholder test, which just
- // verifies that no error occurred while retrieving the list,
- // with a more meaningful test.
- Assert.assertTrue(generators != null);
- Assert.assertTrue(generators.size() > 0);
- }
- @Test(dependsOnMethods =
- {"hasRequiredDatabaseTable", "createIDGenerator", "readIDGenerator",
- "readIDGeneratorsList"})
- public void readIDGeneratorsSummaryList() throws IllegalStateException {
-
- Map<String,IDGeneratorInstance> generators = jdbc.readIDGeneratorsList();
-
- // @TODO Replace this placeholder test, which just
- // verifies that no error occurred while retrieving the list,
- // with a more meaningful test.
- Assert.assertTrue(generators.size() > 0);
- }
-
-/*
- @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator",
- "readIDGenerator"})
- public void updateIDGenerator() throws DocumentNotFoundException,
- BadRequestException, IllegalStateException {
-
- final String NEW_DESCRIPTION = "new description";
-
- // Retrieve an existing generator, deserialize it,
- // update its contents, serialize it, and write it back.
- serializedGenerator = jdbc.readIDGenerator(DEFAULT_CSID);
- generator = IDGeneratorSerializer.deserialize(serializedGenerator);
- generator.setDescription(NEW_DESCRIPTION);
- serializedGenerator = IDGeneratorSerializer.serialize(generator);
-
- jdbc.updateIDGenerator(DEFAULT_CSID, serializedGenerator);
-
- serializedGenerator = jdbc.readIDGenerator(DEFAULT_CSID);
- generator = IDGeneratorSerializer.deserialize(serializedGenerator);
-
- Assert.assertEquals(generator.getDescription(), NEW_DESCRIPTION);
-
- }
-*/
-
- @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator",
- "readIDGenerator", "readIDGeneratorsList",
- "readIDGeneratorsSummaryList"})
- public void deleteIDGenerator() throws DocumentNotFoundException {
- jdbc.deleteIDGenerator(DEFAULT_CSID);
- }
-
- @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createIDGenerator",
- "readIDGenerator", "deleteIDGenerator"})
- public void createID() throws DocumentNotFoundException,
- BadRequestException, IllegalStateException {
-
- try {
- jdbc.deleteIDGenerator(DEFAULT_CSID);
- } catch (Exception e) {
- // This deletion attempt may properly fail silently
- // if no ID generator with the specified CSID currently
- // exists in the database.
- }
-
- jdbc.createIDGenerator(DEFAULT_CSID, getSpectrumEntryNumberGenerator());
-
- Assert.assertEquals(service.createID(DEFAULT_CSID), "E1");
- Assert.assertEquals(service.createID(DEFAULT_CSID), "E2");
- Assert.assertEquals(service.createID(DEFAULT_CSID), "E3");
-
- try {
- jdbc.deleteIDGenerator(DEFAULT_CSID);
- } catch (Exception e) {
- Assert.fail("Could not delete ID generator '" + DEFAULT_CSID + "'.");
- }
-
- jdbc.createIDGenerator(DEFAULT_CSID, getChinAccessionNumberGenerator());
-
- Assert.assertEquals(service.createID(DEFAULT_CSID), CURRENT_YEAR + ".1.1");
- Assert.assertEquals(service.createID(DEFAULT_CSID), CURRENT_YEAR + ".1.2");
- Assert.assertEquals(service.createID(DEFAULT_CSID), CURRENT_YEAR + ".1.3");
-
- try {
- jdbc.deleteIDGenerator(DEFAULT_CSID);
- } catch (Exception e) {
- Assert.fail("Could not delete ID generator '" + DEFAULT_CSID + "'.");
- }
-
- }
-
-
- // This test requires that:
- // 1. The ID Service is running and accessible to this test; and
- // 2. There is no ID generator retrievable through that service
- // with the identifier 'non-existent identifier'.
- @Test(dependsOnMethods = {"hasRequiredDatabaseTable", "createID"},
- expectedExceptions = DocumentNotFoundException.class)
- public void createIDNonExistentGenerator() throws DocumentNotFoundException,
- BadRequestException, IllegalArgumentException, IllegalStateException {
- nextId = service.createID("non-existent identifier");
- }
-
- // ---------------------------------------------------------------
- // Utility methods used by tests above
- // ---------------------------------------------------------------
-
- // @TODO Read test patterns from external configuration.
-
- public String getSpectrumEntryNumberGenerator() throws BadRequestException {
-
- generator = new SettableIDGenerator();
- generator.add(new StringIDGeneratorPart("E"));
- generator.add(new NumericIDGeneratorPart("1"));
-
- return IDGeneratorSerializer.serialize(generator);
-
- }
-
- public String getChinAccessionNumberGenerator() throws BadRequestException {
-
- generator = new SettableIDGenerator();
- generator.add(new YearIDGeneratorPart());
- generator.add(new StringIDGeneratorPart("."));
- generator.add(new NumericIDGeneratorPart("1"));
- generator.add(new StringIDGeneratorPart("."));
- generator.add(new NumericIDGeneratorPart("1"));
-
- return IDGeneratorSerializer.serialize(generator);
-
- }
-
-}