From: Aron Roberts Date: Wed, 9 Jun 2010 05:54:32 +0000 (+0000) Subject: CSPACE-1936: Added initial, highly minimal set of client tests of the ID Service. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=3aabcae0b48ffeee30a9666c2f2a08e06e773a99;p=tmp%2Fjakarta-migration.git CSPACE-1936: Added initial, highly minimal set of client tests of the ID Service. --- diff --git a/services/id/client/src/main/java/org/collectionspace/services/client/IdClient.java b/services/id/client/src/main/java/org/collectionspace/services/client/IdClient.java new file mode 100644 index 000000000..9c5b097fa --- /dev/null +++ b/services/id/client/src/main/java/org/collectionspace/services/client/IdClient.java @@ -0,0 +1,74 @@ +package org.collectionspace.services.client; + +import javax.ws.rs.core.Response; + +import org.jboss.resteasy.client.ProxyFactory; +import org.jboss.resteasy.plugins.providers.RegisterBuiltin; +import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.spi.ResteasyProviderFactory; + +/** + * An AcquisitionClient. + + * @version $Revision:$ + */ +public class IdClient extends AbstractServiceClientImpl { + + /* (non-Javadoc) + * @see org.collectionspace.services.client.BaseServiceClient#getServicePathComponent() + */ + public String getServicePathComponent() { + return "idgenerators"; + } + // FIXME: Is the "instance" member still needed/used? + /** + * + */ +// private static final AcquisitionClient instance = new AcquisitionClient(); + /** + * + */ + private IdProxy idProxy; + + /** + * + * Default constructor for IntakeClient class. + * + */ + public IdClient() { + ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); + RegisterBuiltin.register(factory); + setProxy(); + } + + @Override + public CollectionSpaceProxy getProxy() { + return this.idProxy; + } + + /** + * allow to reset proxy as per security needs + */ + public void setProxy() { + if (useAuth()) { + idProxy = ProxyFactory.create(IdProxy.class, + getBaseURL(), getHttpClient()); + } else { + idProxy = ProxyFactory.create(IdProxy.class, + getBaseURL()); + } + } + + public ClientResponse readList() { + return idProxy.readList(); + } + + public ClientResponse read(String csid) { + return idProxy.read(csid); + } + + public ClientResponse createId(String csid) { + return idProxy.createId(csid); + } + +} diff --git a/services/id/client/src/main/java/org/collectionspace/services/client/IdProxy.java b/services/id/client/src/main/java/org/collectionspace/services/client/IdProxy.java new file mode 100644 index 000000000..99def0796 --- /dev/null +++ b/services/id/client/src/main/java/org/collectionspace/services/client/IdProxy.java @@ -0,0 +1,34 @@ +package org.collectionspace.services.client; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +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; + + +@Path("/idgenerators/") +@Produces({"application/xml"}) +@Consumes({"text/plain"}) +public interface IdProxy extends CollectionSpaceProxy { + + @GET + @Produces({"application/xml"}) + ClientResponse readList(); + + //(C)reate + @POST + @Path("/{csid}/ids") + @Produces({"text/plain"}) + ClientResponse createId(@PathParam("csid") String csid); + + //(R)ead + @GET + @Path("/{csid}") + @Produces({"application/xml"}) + ClientResponse read(@PathParam("csid") String csid); +} diff --git a/services/id/client/src/test/java/org/collectionspace/services/client/test/IdServiceTest.java b/services/id/client/src/test/java/org/collectionspace/services/client/test/IdServiceTest.java new file mode 100644 index 000000000..dba70a845 --- /dev/null +++ b/services/id/client/src/test/java/org/collectionspace/services/client/test/IdServiceTest.java @@ -0,0 +1,240 @@ +/** + * 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 (c) 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.client.test; + +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.jaxb.AbstractCommonList; + +import org.jboss.resteasy.client.ClientResponse; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * IdServiceTest, carries out tests against a + * deployed and running ID Service. + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class IdServiceTest extends BaseServiceTest { + + /** The logger. */ + private final String CLASS_NAME = IdServiceTest.class.getName(); + private final Logger logger = LoggerFactory.getLogger(CLASS_NAME); + + String knownResourceId = ""; + + + /* (non-Javadoc) + * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance() + */ + @Override + protected CollectionSpaceClient getClientInstance() { + return new IdClient(); + } + + /* (non-Javadoc) + * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse) + */ + @Override + protected AbstractCommonList getAbstractCommonList( + ClientResponse response) { + throw new UnsupportedOperationException( + "IdServiceTest.getAbstractCommonList method is not currently supported."); + } + + // --------------------------------------------------------------- + // CRUD tests : CREATE tests + // --------------------------------------------------------------- + + // Success outcomes + + // Uncomment when getIDGeneratorCSID() no longer returns a hard-coded value. + +/* + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + dependsOnMethods = {"readList", "read"}) + public void createId(String testName) throws Exception { + + if (logger.isDebugEnabled()) { + logger.debug(testBanner(testName, CLASS_NAME)); + }; + + // Perform setup, such as initializing the type of service request + // (e.g. CREATE, DELETE), its valid and expected status codes, and + // its associated HTTP method name (e.g. POST, DELETE). + testSetup(STATUS_CREATED, ServiceRequestType.CREATE); + + // Submit the request to the service and store the response. + IdClient client = new IdClient(); + ClientResponse res = client.createId(knownResourceId); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match + // the expected response(s)? + // + // Specifically: + // Does it fall within the set of valid status codes? + // Does it exactly match the expected status code? + 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 entity = res.getEntity(); + Assert.assertNotNull(entity); + if (logger.isDebugEnabled()) { + logger.debug("entity body=\r" + entity); + } + } + * + */ + + // Failure outcomes + // None at present. + + // --------------------------------------------------------------- + // CRUD tests : READ tests + // --------------------------------------------------------------- + + // Success outcomes + + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + dependsOnMethods = {"readList"}) + public void read(String testName) throws Exception { + + if (logger.isDebugEnabled()) { + logger.debug(testBanner(testName, CLASS_NAME)); + }; + + // Perform setup. + testSetup(STATUS_OK, ServiceRequestType.READ); + + // Submit the request to the service and store the response. + IdClient client = new IdClient(); + ClientResponse res = client.read(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); + + String entity = res.getEntity(); + Assert.assertNotNull(entity); + if (logger.isDebugEnabled()) { + logger.debug("entity body=\r" + entity); + } + } + + // Failure outcomes + // None at present. + + // --------------------------------------------------------------- + // CRUD tests : READ_LIST tests + // --------------------------------------------------------------- + // Success outcomes + + + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class) + public void readList(String testName) throws Exception { + + if (logger.isDebugEnabled()) { + logger.debug(testBanner(testName, CLASS_NAME)); + }; + + // Perform setup. + testSetup(STATUS_OK, ServiceRequestType.READ_LIST); + + // Submit the request to the service and store the response. + IdClient client = new IdClient(); + ClientResponse res = client.readList(); + 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); + + // 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 entity = res.getEntity(); + Assert.assertNotNull(entity); + if (logger.isDebugEnabled()) { + logger.debug("entity body=\r" + entity); + } + + knownResourceId = getOneIDGeneratorCSID(entity); + } + + // Failure outcomes + // None at present. + + // --------------------------------------------------------------- + // Utility methods used by tests above + // --------------------------------------------------------------- + /* (non-Javadoc) + * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent() + */ + @Override + public String getServicePathComponent() { + 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"; + } + +} + diff --git a/services/id/pom.xml b/services/id/pom.xml index 3e384c39d..fe8c9bebf 100644 --- a/services/id/pom.xml +++ b/services/id/pom.xml @@ -37,7 +37,7 @@ service - + client