]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-324 added initial set of tests (support for various http methods, check for...
authorSanjay Dalal <sanjay.dalal@berkeley.edu>
Fri, 31 Jul 2009 20:51:24 +0000 (20:51 +0000)
committerSanjay Dalal <sanjay.dalal@berkeley.edu>
Fri, 31 Jul 2009 20:51:24 +0000 (20:51 +0000)
services/authentication/client/src/test/java/org/collectionspace/services/authentication/client/AuthenticationServiceTest.java
services/client/pom.xml
services/client/src/main/java/org/collectionspace/services/client/BaseServiceClient.java
services/client/src/main/java/org/collectionspace/services/client/CollectionSpaceClient.java
services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java [new file with mode: 0644]
services/client/src/test/java/org/collectionspace/services/client/test/ServiceTest.java [new file with mode: 0644]
services/client/src/test/resources/log4j.properties [new file with mode: 0644]

index bd49c3da8b5f94dc64c38840cdb8b30862b43c5a..efab061d8932ffc8365135ab867176c25692b688 100644 (file)
@@ -50,12 +50,13 @@ public class AuthenticationServiceTest {
 
     @Test
     public void auth_createCollectionObject() {
-        if(!isServerSecure()){
-            return;
-        }
         String identifier = this.createIdentifier();
         CollectionObject collectionObject = createCollectionObject(identifier);
         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
         collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
@@ -77,12 +78,13 @@ public class AuthenticationServiceTest {
 
     @Test(dependsOnMethods = {"auth_createCollectionObject"})
     public void auth_createCollectionObjectWithoutUser() {
-        if(!isServerSecure()){
-            return;
-        }
         String identifier = this.createIdentifier();
         CollectionObject collectionObject = createCollectionObject(identifier);
         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
         collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
@@ -101,13 +103,13 @@ public class AuthenticationServiceTest {
 
     @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutUser"})
     public void auth_createCollectionObjectWithoutPassword() {
-        if(!isServerSecure()){
-            logger.warn("set -Dcspace.server.secure=true to run security tests");
-            return;
-        }
         String identifier = this.createIdentifier();
         CollectionObject collectionObject = createCollectionObject(identifier);
         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
         collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
@@ -126,13 +128,13 @@ public class AuthenticationServiceTest {
 
     @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
     public void auth_createCollectionObjectWithIncorrectPassword() {
-        if(!isServerSecure()){
-            logger.warn("set -Dcspace.server.secure=true to run security tests");
-            return;
-        }
         String identifier = this.createIdentifier();
         CollectionObject collectionObject = createCollectionObject(identifier);
         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
         collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
@@ -151,13 +153,13 @@ public class AuthenticationServiceTest {
 
     @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
     public void auth_createCollectionObjectWithoutUserPassword() {
-        if(!isServerSecure()){
-            logger.warn("set -Dcspace.server.secure=true to run security tests");
-            return;
-        }
         String identifier = this.createIdentifier();
         CollectionObject collectionObject = createCollectionObject(identifier);
         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
         collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
@@ -176,13 +178,13 @@ public class AuthenticationServiceTest {
 
     @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
     public void auth_createCollectionObjectWithIncorrectUserPassword() {
-        if(!isServerSecure()){
-            logger.warn("set -Dcspace.server.secure=true to run security tests");
-            return;
-        }
         String identifier = this.createIdentifier();
         CollectionObject collectionObject = createCollectionObject(identifier);
         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "foo");
         collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
@@ -199,15 +201,14 @@ public class AuthenticationServiceTest {
                 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
     }
 
-
     @Test(dependsOnMethods = {"auth_createCollectionObjectWithIncorrectUserPassword"})
     public void auth_deleteCollectionObject() {
-        if(!isServerSecure()){
+        CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
+        collectionObjectClient = new CollectionObjectClient();
+        if(!collectionObjectClient.isServerSecure()){
             logger.warn("set -Dcspace.server.secure=true to run security tests");
             return;
         }
-        CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
-        collectionObjectClient = new CollectionObjectClient();
         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
         collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
@@ -277,8 +278,4 @@ public class AuthenticationServiceTest {
         long identifier = System.currentTimeMillis();
         return Long.toString(identifier);
     }
-
-    private boolean isServerSecure() {
-        return Boolean.getBoolean("cspace.server.secure");
-    }
 }
index 92d13f6f4d39a7b6e87289919f293b5f4ea021cd..72920e59f530285534302f3eb96e6849f9ccd88d 100644 (file)
     <name>services.client</name>\r
 \r
     <dependencies>\r
+        <dependency>\r
+            <groupId>org.slf4j</groupId>\r
+            <artifactId>slf4j-api</artifactId>\r
+        </dependency>\r
+        <dependency>\r
+            <groupId>org.slf4j</groupId>\r
+            <artifactId>slf4j-log4j12</artifactId>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.jboss.resteasy</groupId>\r
             <artifactId>resteasy-jaxrs</artifactId>\r
             <version>1.0.2.GA</version>\r
         </dependency>\r
         <dependency>\r
-            <groupId>junit</groupId>\r
-            <artifactId>junit</artifactId>\r
-            <version>4.1</version>\r
-            <scope>test</scope>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>org.slf4j</groupId>\r
-            <artifactId>slf4j-api</artifactId>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>org.slf4j</groupId>\r
-            <artifactId>slf4j-log4j12</artifactId>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>org.slf4j</groupId>\r
-            <artifactId>slf4j-simple</artifactId>\r
+            <groupId>org.testng</groupId>\r
+            <artifactId>testng</artifactId>\r
+            <version>5.6</version>\r
             <scope>test</scope>\r
-            <version>1.5.2</version>\r
         </dependency>\r
-      <!-- javax -->\r
-\r
 \r
+      <!-- javax -->\r
         <dependency>\r
             <groupId>javax.security</groupId>\r
             <artifactId>jaas</artifactId>\r
     <build>\r
         <finalName>collectionspace-services-client</finalName>\r
         <plugins>\r
+            <plugin>\r
+                <groupId>org.apache.maven.plugins</groupId>\r
+                <artifactId>maven-surefire-plugin</artifactId>\r
+                <configuration>\r
+                    <systemProperties>\r
+                        <property>\r
+                            <name>log4j.configuration</name>\r
+                            <value>file:target/test-classes/log4j.properties</value>\r
+                        </property>\r
+                    </systemProperties>\r
+                </configuration>\r
+            </plugin>\r
             <plugin>\r
                 <groupId>org.apache.maven.plugins</groupId>\r
                 <artifactId>maven-compiler-plugin</artifactId>\r
index 91aaee8528d95fb0a41b8f71fad2befee6199a2e..8579d5bd646a2a7f71f78b304c9d9d811ae7c558 100644 (file)
@@ -175,4 +175,9 @@ public abstract class BaseServiceClient implements CollectionSpaceClient {
             }\r
         }\r
     }\r
+\r
+    @Override\r
+    public boolean isServerSecure() {\r
+        return Boolean.getBoolean("cspace.server.secure");\r
+    }\r
 }\r
index 371803ff6637d11888cd3e57649be47a58504937..6d08d1fb8fa0a8eabb04318cad1cda0a4950c20d 100644 (file)
@@ -59,4 +59,10 @@ public interface CollectionSpaceClient {
 
     boolean useSSL();
 
+    /**
+     * checks System property cspace.server.secure
+     * @return
+     */
+    boolean isServerSecure();
+
 }
diff --git a/services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java b/services/client/src/main/java/org/collectionspace/services/client/TestServiceClient.java
new file mode 100644 (file)
index 0000000..a80d6db
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+ * 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.client;
+
+
+
+/**
+ * A CollectionObjectClient.
+
+ * @version $Revision:$
+ */
+public class TestServiceClient extends BaseServiceClient {
+
+
+    /**
+     *
+     * Default constructor for CollectionObjectClient class.
+     *
+     */
+    public TestServiceClient() {
+    }
+
+    
+}
diff --git a/services/client/src/test/java/org/collectionspace/services/client/test/ServiceTest.java b/services/client/src/test/java/org/collectionspace/services/client/test/ServiceTest.java
new file mode 100644 (file)
index 0000000..98b8b60
--- /dev/null
@@ -0,0 +1,204 @@
+/**
+ * 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.client.test;
+
+import java.io.IOException;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.OptionsMethod;
+import org.apache.commons.httpclient.methods.TraceMethod;
+import org.collectionspace.services.client.TestServiceClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * A ServiceTest.
+
+ * @version $Revision:$
+ */
+public class ServiceTest {
+
+    final Logger logger = LoggerFactory.getLogger(ServiceTest.class);
+    private HttpClient httpClient = new HttpClient();
+    private TestServiceClient serviceClient = new TestServiceClient();
+
+    @Test
+    public void servicesExist() {
+        //use ID service that should always be present in a working service layer
+        String url = serviceClient.getBaseURL() + "ids";
+        OptionsMethod method = new OptionsMethod(url);
+        try{
+            int statusCode = httpClient.executeMethod(method);
+            if(logger.isDebugEnabled()){
+                logger.debug("servicesExist url=" + url + " status=" + statusCode);
+            }
+            Assert.assertEquals(statusCode, HttpStatus.SC_OK,
+                    "expected " + HttpStatus.SC_OK);
+        }catch(HttpException e){
+            logger.error("Fatal protocol violation: ", e);
+        }catch(IOException e){
+            logger.error("Fatal transport error", e);
+        }catch(Exception e){
+            logger.error("unknown exception ", e);
+        }finally{
+            // Release the connection.
+            method.releaseConnection();
+        }
+    }
+
+    @Test
+    public void methodNotAllowed() {
+        //get is not allowed on id service
+        String url = serviceClient.getBaseURL() + "ids";
+        GetMethod method = new GetMethod(url);
+        try{
+            int statusCode = httpClient.executeMethod(method);
+            if(logger.isDebugEnabled()){
+                logger.debug(" methodNotAllowed url=" + url + " status=" + statusCode);
+            }
+            Assert.assertEquals(statusCode, HttpStatus.SC_METHOD_NOT_ALLOWED,
+                    "expected " + HttpStatus.SC_METHOD_NOT_ALLOWED);
+        }catch(HttpException e){
+            logger.error("Fatal protocol violation: ", e);
+        }catch(IOException e){
+            logger.error("Fatal transport error", e);
+        }catch(Exception e){
+            logger.error("unknown exception ", e);
+        }finally{
+            // Release the connection.
+            method.releaseConnection();
+        }
+    }
+
+    @Test
+    public void noService() {
+
+        String url = serviceClient.getBaseURL() + "fake-service";
+        GetMethod method = new GetMethod(url);
+        try{
+            int statusCode = httpClient.executeMethod(method);
+            if(logger.isDebugEnabled()){
+                logger.debug("noService url=" + url + " status=" + statusCode);
+            }
+            Assert.assertEquals(statusCode, HttpStatus.SC_NOT_FOUND,
+                    "expected " + HttpStatus.SC_NOT_FOUND);
+        }catch(HttpException e){
+            logger.error("Fatal protocol violation: ", e);
+        }catch(IOException e){
+            logger.error("Fatal transport error", e);
+        }catch(Exception e){
+            logger.error("unknown exception ", e);
+        }finally{
+            // Release the connection.
+            method.releaseConnection();
+        }
+    }
+
+    @Test
+    public void serviceSecure() {
+        if(!serviceClient.isServerSecure()){
+            logger.warn("set -Dcspace.server.secure=true to run security tests");
+            return;
+        }
+        String url = serviceClient.getBaseURL() + "collectionobjects";
+        GetMethod method = new GetMethod(url);
+        try{
+            int statusCode = httpClient.executeMethod(method);
+            if(logger.isDebugEnabled()){
+                logger.debug("serviceSecure url=" + url + " status=" + statusCode);
+            }
+            Assert.assertEquals(statusCode, HttpStatus.SC_UNAUTHORIZED,
+                    "expected " + HttpStatus.SC_UNAUTHORIZED);
+        }catch(HttpException e){
+            logger.error("Fatal protocol violation: ", e);
+        }catch(IOException e){
+            logger.error("Fatal transport error", e);
+        }catch(Exception e){
+            logger.error("unknown exception ", e);
+        }finally{
+            // Release the connection.
+            method.releaseConnection();
+        }
+    }
+
+    @Test
+    public void traceSupported() {
+        String url = serviceClient.getBaseURL() + "collectionobjects";
+        TraceMethod method = new TraceMethod(url);
+        try{
+            int statusCode = httpClient.executeMethod(method);
+
+            if(logger.isDebugEnabled()){
+                logger.debug("traceSupported url=" + url + " status=" + statusCode);
+                logger.debug("traceSupported response=" + new String(method.getResponseBody()));
+                for(Header h : method.getResponseHeaders()){
+                    logger.debug("traceSupported header name=" + h.getName() + " value=" + h.getValue());
+                }
+            }
+            Assert.assertEquals(statusCode, HttpStatus.SC_METHOD_NOT_ALLOWED,
+                    "expected " + HttpStatus.SC_METHOD_NOT_ALLOWED);
+        }catch(HttpException e){
+            logger.error("Fatal protocol violation: ", e);
+        }catch(IOException e){
+            logger.error("Fatal transport error", e);
+        }catch(Exception e){
+            logger.error("unknown exception ", e);
+        }finally{
+            // Release the connection.
+            method.releaseConnection();
+        }
+    }
+
+    @Test
+    public void headSupported() {
+        String url = serviceClient.getBaseURL() + "intakes";
+        HeadMethod method = new HeadMethod(url);
+        try{
+            int statusCode = httpClient.executeMethod(method);
+            Assert.assertEquals(method.getResponseBody(), null, "expected null");
+            if(logger.isDebugEnabled()){
+                logger.debug("headSupported url=" + url + " status=" + statusCode);
+                for(Header h : method.getResponseHeaders()){
+                    logger.debug("headSupported header name=" + h.getName() + " value=" + h.getValue());
+                }
+            }
+            Assert.assertEquals(statusCode, HttpStatus.SC_OK,
+                    "expected " + HttpStatus.SC_OK);
+        }catch(HttpException e){
+            logger.error("Fatal protocol violation: ", e);
+        }catch(IOException e){
+            logger.error("Fatal transport error", e);
+        }catch(Exception e){
+            logger.error("unknown exception ", e);
+        }finally{
+            // Release the connection.
+            method.releaseConnection();
+        }
+    }
+}
diff --git a/services/client/src/test/resources/log4j.properties b/services/client/src/test/resources/log4j.properties
new file mode 100644 (file)
index 0000000..18c5103
--- /dev/null
@@ -0,0 +1,23 @@
+log4j.rootLogger=debug, stdout, R\r
+\r
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender\r
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\r
+\r
+# Pattern to output the caller's file name and line number.\r
+log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+log4j.appender.R=org.apache.log4j.RollingFileAppender\r
+log4j.appender.R.File=target/test-client.log\r
+\r
+log4j.appender.R.MaxFileSize=100KB\r
+# Keep one backup file\r
+log4j.appender.R.MaxBackupIndex=1\r
+\r
+log4j.appender.R.layout=org.apache.log4j.PatternLayout\r
+log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n\r
+\r
+#packages\r
+log4j.logger.org.collectionspace=DEBUG\r
+log4j.logger.org.apache=INFO\r
+log4j.logger.httpclient=INFO\r
+log4j.logger.org.jboss.resteasy=INFO\r