From: Aron Roberts Date: Fri, 26 Jun 2009 19:27:44 +0000 (+0000) Subject: CSPACE-245: Stubbed out bare-bones client-side tests of a generic HTTP service, which... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=16ef4b4c0a78824b6dabfd508abea5b92ac51a06;p=tmp%2Fjakarta-migration.git CSPACE-245: Stubbed out bare-bones client-side tests of a generic HTTP service, which will subsequently be adapted to test the RESTful interface to the ID Service. --- diff --git a/services/id/service/pom.xml b/services/id/service/pom.xml index e16cf8893..396cb4b61 100644 --- a/services/id/service/pom.xml +++ b/services/id/service/pom.xml @@ -98,17 +98,17 @@ org.restlet org.restlet - 1.0.7 + 1.1.5 com.noelios.restlet com.noelios.restlet.ext.httpclient - 1.0.7 + 1.1.5 com.noelios.restlet com.noelios.restlet - 1.0.7 + 1.1.5 diff --git a/services/id/service/src/main/java/org/collectionspace/services/IDService.java b/services/id/service/src/main/java/org/collectionspace/services/IDService.java index d0b9a6077..92aa6c13c 100644 --- a/services/id/service/src/main/java/org/collectionspace/services/IDService.java +++ b/services/id/service/src/main/java/org/collectionspace/services/IDService.java @@ -20,12 +20,7 @@ package org.collectionspace.services; -import java.io.IOException; -import org.dom4j.Document; -import org.dom4j.DocumentException; - import org.collectionspace.services.id.*; -import org.collectionspace.services.id.IDServiceNuxeoImpl; public interface IDService { @@ -55,28 +50,5 @@ public interface IDService { // Update // Delete (possibly not permitted - deactivate instead?) - - -/* - // Create - Document postCollectionObject(CollectionObject co) - throws DocumentException, IOException; - - // Read single object - Document getCollectionObject(String csid) throws DocumentException, - IOException; - - // Read a list of objects - Document getCollectionObjectList() throws DocumentException, IOException; - - // Update - Document putCollectionObject(String csid, CollectionObject theUpdate) - throws DocumentException, IOException; - - // Delete - Document deleteCollectionObject(String csid) throws DocumentException, - IOException; - -*/ } diff --git a/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java b/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java index 9a69a486f..20ddaf179 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/IDServiceTest.java @@ -1,3 +1,31 @@ +/* + * IDServiceTest + * + * Test class for the ID Service. + * + * 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 + * + * @author $Author: aron $ + * @version $Revision: 267 $ + * $Date: 2009-06-19 19:03:38 -0700 (Fri, 19 Jun 2009) $ + */ + +// @TODO: Use a URL builder in core Java or the Restlet framework, +// rather than String objects. + +// @TODO: Consider using client-side RESTeasy, rather than Restlet, +// if there is a desire to reduce the number of dependencies. +// +// (Note also Sanjay's comment c. June 2009 re RESTEasy's client-side +// behavior around having to send authentication credentials in +// advance, rather than via a challenge - if that was understood correctly.) + package org.collectionspace.services.test; //import org.collectionspace.services.id.Id; @@ -5,6 +33,80 @@ package org.collectionspace.services.test; //import org.collectionspace.services.id.IdPattern; //import org.collectionspace.services.id.IdPatternList; -public class IDServiceTest { - //empty +import junit.framework.TestCase; +import static org.junit.Assert.*; + +import org.restlet.Client; +import org.restlet.data.Method; +import org.restlet.data.Protocol; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Status; + +public class IDServiceTest extends TestCase { + + final static String DEFAULT_REFERRER_URL = "http://collectionspace.org"; + final static String DEFAULT_SUCCESS_URL_STRING = "http://www.example.com/"; + final static String DEFAULT_FAILURE_URL_STRING = "http://www.example.com/nonexistent"; + + // Stub test to verify basic functionality. + public void testSuccessfulRequest() { + Response response = submitRequest(DEFAULT_SUCCESS_URL_STRING); + assertTrue(isSuccessResponse(response)); + } + + // Stub test to verify basic functionality. + public void testFailureRequest() { + Response response = submitRequest(DEFAULT_FAILURE_URL_STRING); + assertFalse(isSuccessResponse(response)); + } + + // Return a flag indicating whether a response from a + // service request represents a successful outcome. + public boolean isSuccessResponse(Response response) { + + if (response == null || response.getStatus() == null) { + return false; + } + + // Note: we can also test specifically for a 200 OK response via + // 'if (response.getStatus() == Status.SUCCESS_OK) ...' + if (response.getStatus().isSuccess()) { + return true; + } else { + return false; + } + + } + + // Submit a request to a service. + // + // @TODO: Remove hard-coding of HTTP protocol requests. + public Response submitRequest(String urlStr) { + + // Adapted from the Restlet 1.1 tutorial + // http://www.restlet.org/documentation/1.1/tutorial + // + // Note that if we later migrate to using the Restlet 2.0 + // framework, it uses a resource model on the client-side, + // via the ClientResource class: + // http://www.restlet.org/documentation/2.0/tutorial + + // @TODO: Validate the submitted URL here. + + // Prepare the request. + Request request = new Request(Method.GET, urlStr); + request.setReferrerRef(DEFAULT_REFERRER_URL); + + // Handle it using an HTTP client connector. + // + // @TODO: We may need to derive the protocol, + // such as HTTP v. HTTPS, from the submitted URL. + Client client = new Client(Protocol.HTTP); + Response response = client.handle(request); + + return response; + + } + }