>From 2f95d262d4dc31807fffe29048f9d3050c883283 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Fri, 20 Jan 2012 09:32:39 -0500
Subject: [PATCH 14/14] Fix test client errors

Added ClientResponse annotation to SystemCertificateResource.
Added Consumes annotation to KeyResource, KeyRequestResource
Added checks for empty search results to test client, as well as stripping header, trailer
from transport cert.
---
 .../servlet/admin/SystemCertificateResource.java   |    6 ++-
 .../cms/servlet/base/CMSResourceService.java       |    4 +-
 .../com/netscape/cms/servlet/key/KeyResource.java  |    1 +
 .../cms/servlet/key/KeysResourceService.java       |    2 +-
 .../cms/servlet/request/KeyRequestResource.java    |    2 +
 .../netscape/cms/servlet/test/DRMRestClient.java   |    9 ++--
 .../src/com/netscape/cms/servlet/test/DRMTest.java |   56 +++++++++++++-------
 7 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResource.java b/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResource.java
index 6c4c0bc5461bccc48e0ec2395d7464f79b292219..d4cfcd296b34837b30c25362de2450a2970d2b69 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResource.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResource.java
@@ -6,6 +6,10 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import org.jboss.resteasy.annotations.ClientResponseType;
+
+import com.netscape.cms.servlet.cert.model.CertificateData;
+
 @Path("/config/cert")
 public interface SystemCertificateResource {
 
@@ -14,7 +18,7 @@ public interface SystemCertificateResource {
      */
     @GET
     @Path("/transport")
-    //@ClientResponseType(CertificateData.class)
+    @ClientResponseType(entityType=CertificateData.class)
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
     public Response getTransportCert();
 
diff --git a/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java
index 74d847d0a42b9bc52839800c1a2aae54c832d4d5..acddba559c2ac0b9152ca2f43f88cec20fde0358 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java
@@ -35,8 +35,8 @@ import com.netscape.cms.servlet.cert.model.CertificateData;
  * 
  */
 public class CMSResourceService {
-    protected static final String HEADER = "-----BEGIN NEW CERTIFICATE REQUEST-----";
-    protected static final String TRAILER = "-----END NEW CERTIFICATE REQUEST-----";
+    public static final String HEADER = "-----BEGIN NEW CERTIFICATE REQUEST-----";
+    public static final String TRAILER = "-----END NEW CERTIFICATE REQUEST-----";
 
     // caching parameters
     protected static final int DEFAULT_LONG_CACHE_LIFETIME = 1000;
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeyResource.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeyResource.java
index c6ddda6982afc78a9291f2868385c8f2e3924478..a47c46d86a9a12b8ed39efd650702f5642b3c42b 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeyResource.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeyResource.java
@@ -28,5 +28,6 @@ public interface KeyResource {
     @POST
     @Path("retrieve")
     @Produces(MediaType.TEXT_XML)
+    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED})
     public KeyData retrieveKey(MultivaluedMap<String, String> form);
 }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java
index b5032fa86c93c2b0fd5053df38f9f7b255748ed6..471abc161ce072e115d232a5d6a3aaf40989364b 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java
@@ -76,7 +76,7 @@ public class KeysResourceService extends CMSResourceService implements KeysResou
         }
         
         if (clientID != null) {
-            filter += "(clientID=" + clientID + ")";
+            filter += "(clientID=\'" + clientID + "\')";
             matches ++;
         }
         
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResource.java b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResource.java
index 0a476c49c09d6862d9e7601f8d70a48d29dd67fa..146b03d89f19bfdc71de7e2e4927770d900843dd 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResource.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResource.java
@@ -27,6 +27,7 @@ public interface KeyRequestResource {
     @POST
     @Path("archive")
     @Produces({ MediaType.TEXT_XML })
+    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED})
     public KeyRequestInfo archiveKey(MultivaluedMap<String, String> form);
 
     @POST
@@ -39,6 +40,7 @@ public interface KeyRequestResource {
     @POST
     @Path("recover")
     @Produces({ MediaType.TEXT_XML })
+    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED})
     public KeyRequestInfo recoverKey(MultivaluedMap<String, String> form);
 
     @POST
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
index 412df39a9d903f726be9c6c6fb858188f7688de3..06d5d961fe458ff899883811193c00201c7207b8 100644
--- a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
@@ -3,10 +3,8 @@ package com.netscape.cms.servlet.test;
 import java.util.Collection;
 import java.util.Iterator;
 
-import javax.ws.rs.core.Response;
-
+import org.jboss.resteasy.client.ClientResponse;
 import org.jboss.resteasy.client.ProxyFactory;
-
 import com.netscape.cms.servlet.admin.SystemCertificateResource;
 import com.netscape.cms.servlet.cert.model.CertificateData;
 import com.netscape.cms.servlet.key.KeyResource;
@@ -38,8 +36,9 @@ public class DRMRestClient {
     }
     
     public String getTransportCert() {
-        Response response = systemCertClient.getTransportCert();
-        CertificateData certData = (CertificateData) response.getEntity();
+        @SuppressWarnings("unchecked")
+        ClientResponse<CertificateData> response = (ClientResponse<CertificateData>) systemCertClient.getTransportCert();
+        CertificateData certData = response.getEntity();
         String transportCert = certData.getB64();
         return transportCert;
     }
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
index dbe7e7d79197915c285387c4b1360251ce0faae4..a8560dc2e0281d597c1ff08cd4c8bce0634d6b8d 100644
--- a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
@@ -59,6 +59,8 @@ import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
+
+import com.netscape.cms.servlet.base.CMSResourceService;
 import com.netscape.cms.servlet.key.model.KeyData;
 import com.netscape.cms.servlet.key.model.KeyDataInfo;
 import com.netscape.cms.servlet.request.model.KeyRequestInfo;
@@ -76,12 +78,14 @@ public class DRMTest {
         String host = null;
         String port = null;
         String token_pwd = null;
+        String db_dir = "./";
 
         // parse command line arguments
         Options options = new Options();
         options.addOption("h", true, "Hostname of the DRM");
         options.addOption("p", true, "Port of the DRM");
         options.addOption("w", true, "Token password");
+        options.addOption("d", true, "Directory for tokendb");
 
         try {
             CommandLineParser parser = new PosixParser();
@@ -108,6 +112,10 @@ public class DRMTest {
                 usage(options);
             }
 
+            if (cmd.hasOption("d")) {
+                db_dir = cmd.getOptionValue("d");
+            }
+
         } catch (ParseException e) {
             System.err.println("Error in parsing command line options: " + e.getMessage());
             usage(options);
@@ -120,35 +128,34 @@ public class DRMTest {
         CryptoManager manager = null;
         CryptoToken token = null;
         KeyGenerator kg1 = null;
-        String db_dir = "./";
-        
+
         // used for wrapping to send data to DRM
         String transportCert = null;
-        
+
         // Data to be archived
         SymmetricKey vek = null;
         String passphrase = null;
-        
+
         // Session keys and passphrases for recovery
         SymmetricKey recoveryKey = null;
         byte[] wrappedRecoveryKey = null;
         String recoveryPassphrase = null;
         byte[] wrappedRecoveryPassphrase = null;
-        
+
         // retrieved data (should match archived data)
         String wrappedRecoveredKey = null;
         String recoveredKey = null;
-        
+
         // various ids used in recovery/archival operations
         String keyId = null;
         String clientId = null;
         String recoveryRequestId = null;
-        
+
         // Variables for data structures from calls
         KeyRequestInfo requestInfo = null;
         KeyData keyData = null;
         KeyDataInfo keyInfo = null;
-         
+
         // Initialize token
         try {
             CryptoManager.initialize(db_dir);
@@ -175,31 +182,42 @@ public class DRMTest {
         } catch (Exception e) {
             log("Exception in logging into token:" + e.toString());
         }
-        
+
         // Set base URI and get client
-        String baseUri = "http://" + host + ":" + port + "/pki";
+        String baseUri = "http://" + host + ":" + port + "/kra/pki";
         DRMRestClient client = new DRMRestClient(baseUri);
 
         // Test 1: Get transport certificate from DRM
         transportCert = client.getTransportCert();
+        transportCert = transportCert.substring(CMSResourceService.HEADER.length(),
+                                                transportCert.indexOf(CMSResourceService.TRAILER));
+
         log("Transport Cert retrieved from DRM: " + transportCert);
 
         // Test 2: Get list of completed key archival requests
         log("\n\nList of completed archival requests");
-        Collection<KeyRequestInfo> list = client.listRequests("complete", "enrolment");
-        Iterator<KeyRequestInfo> iter = list.iterator();
-        while (iter.hasNext()) {
-            KeyRequestInfo info = iter.next();
-            printRequestInfo(info);
+        Collection<KeyRequestInfo> list = client.listRequests("complete", "enrollment");
+        if (list == null) {
+            log("No requests found");
+        } else {
+            Iterator<KeyRequestInfo> iter = list.iterator();
+            while (iter.hasNext()) {
+                KeyRequestInfo info = iter.next();
+                printRequestInfo(info);
+            }
         }
 
         // Test 3: Get list of key recovery requests
         log("\n\nList of completed recovery requests");
         Collection<KeyRequestInfo> list2 = client.listRequests("complete", "recovery");
-        Iterator<KeyRequestInfo> iter2 = list2.iterator();
-        while (iter2.hasNext()) {
-            KeyRequestInfo info = iter2.next();
-            printRequestInfo(info);
+        if (list2 == null) {
+            log("No requests found");
+        } else {
+            Iterator<KeyRequestInfo> iter2 = list2.iterator();
+            while (iter2.hasNext()) {
+                KeyRequestInfo info = iter2.next();
+                printRequestInfo(info);
+            }
         }
 
         // Test 4: Generate and archive a symmetric key
-- 
1.7.6.4

