>From 8560898f3bf2262bbb2c498f453a6a3bb65f9d28 Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Wed, 6 Nov 2013 16:46:19 -0500
Subject: [PATCH] Added JUnit tests upstream.

Include JUnit tests to the upstream. This will be the location
for all the new tests to come. These tests can be run on a beaker machine
as part of the QE tests or in eclipse to test an already existing test framework.

Tickets #657,722,723,724
---
 .classpath                                         |   1 +
 .../dogtag/dev_java_tests/.classpath               |  68 ++++---
 tests/dogtag/dev_java_tests/.project               |  17 ++
 .../.settings/org.eclipse.jdt.core.prefs           |  11 ++
 tests/dogtag/dev_java_tests/conf/deploy.cfg        |   9 +
 tests/dogtag/dev_java_tests/conf/test.cfg          |  76 ++++++++
 tests/dogtag/dev_java_tests/run_junit_tests.sh     |  42 +++++
 .../dogtag/dev_java_tests/src/BeakerTestSuite.java |  13 ++
 .../src/com/netscape/beakertests/CATestJunit.java  | 179 +++++++++++++++++++
 .../src/com/netscape/beakertests/PKIJUnitTest.java |  89 ++++++++++
 .../src/com/netscape/beakertests/PKITestSuite.java | 195 +++++++++++++++++++++
 .../src/com/netscape/beakertests/SampleTest1.java  |  13 ++
 12 files changed, 678 insertions(+), 35 deletions(-)
 copy .classpath => tests/dogtag/dev_java_tests/.classpath (54%)
 create mode 100644 tests/dogtag/dev_java_tests/.project
 create mode 100644 tests/dogtag/dev_java_tests/.settings/org.eclipse.jdt.core.prefs
 create mode 100644 tests/dogtag/dev_java_tests/conf/deploy.cfg
 create mode 100644 tests/dogtag/dev_java_tests/conf/test.cfg
 create mode 100644 tests/dogtag/dev_java_tests/run_junit_tests.sh
 create mode 100644 tests/dogtag/dev_java_tests/src/BeakerTestSuite.java
 create mode 100644 tests/dogtag/dev_java_tests/src/com/netscape/beakertests/CATestJunit.java
 create mode 100644 tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKIJUnitTest.java
 create mode 100644 tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKITestSuite.java
 create mode 100644 tests/dogtag/dev_java_tests/src/com/netscape/beakertests/SampleTest1.java

diff --git a/.classpath b/.classpath
index f34b318ca7bbdf936e224a3565506c43372b5a5e..a882e4b88b1339f7939eb1fdfd2f44253a6d2196 100644
--- a/.classpath
+++ b/.classpath
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/test/src"/>
+	<classpathentry kind="src" output="tests/dogtag/dev_java_tests/bin" path="tests/dogtag/dev_java_tests/src"/>
 	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/symkey/src"/>
 	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/util/src"/>
 	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/util/test"/>
diff --git a/.classpath b/tests/dogtag/dev_java_tests/.classpath
similarity index 54%
copy from .classpath
copy to tests/dogtag/dev_java_tests/.classpath
index f34b318ca7bbdf936e224a3565506c43372b5a5e..d40b7a3b1573e5e49cede399fb7fe97d6495e0f7 100644
--- a/.classpath
+++ b/tests/dogtag/dev_java_tests/.classpath
@@ -1,54 +1,52 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/test/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/symkey/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/util/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/util/test"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/common/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/common/functional/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/java-tools/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/server/tomcat/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/server/cms/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/server/cmscore/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/server/test"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/silent/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/ca/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/ca/functional/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/kra/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/kra/functional/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/ocsp/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/tks/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/tps-tomcat/src"/>
-	<classpathentry excluding="**/CMakeLists.txt" kind="src" path="base/migrate/80"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="lib" path="/usr/share/java/junit4.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-certsrv.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-ca.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-cms.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-cmsbundle.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-cmscore.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-cmsutil.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-console-theme.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-kra.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-nsutil.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-ocsp.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-silent.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-tks.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-tomcat.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-tools.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/pki/pki-tps.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/jss/jss4.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/httpcomponents/httpclient.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/httpcomponents/httpcore.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/jaxrs-api.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-atom-provider.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxb-provider.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxrs.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxrs-jandex.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jettison-provider.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/apache-commons-cli.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/apache-commons-codec.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/apache-commons-logging.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/commons-codec.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/commons-httpclient.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/idm-console-base.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/idm-console-base-1.1.7.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/idm-console-mcc.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/idm-console-nmclf.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/jakarta-commons-httpclient.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/junit4.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/ldapjdk.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/jaxb-api.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/httpcomponents/httpclient.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/httpcomponents/httpcore.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/resteasy/jaxrs-api.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxb-provider.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxrs.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jettison-provider.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/ldapjdk.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/apache-commons-lang.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/istack-commons-runtime.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/scannotation.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/servlet.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/velocity.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/xerces-j2.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/xml-commons-apis.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/istack-commons-runtime.jar"/>
-	<classpathentry kind="lib" path="/usr/lib64/jss/jss4.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/apache-commons-lang.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-atom-provider.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/tomcat/catalina.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/tomcat/tomcat-util.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/commons-io.jar"/>
-	<classpathentry kind="output" path="build/classes"/>
+	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/tests/dogtag/dev_java_tests/.project b/tests/dogtag/dev_java_tests/.project
new file mode 100644
index 0000000000000000000000000000000000000000..84a4ea49aa38eebad0425f479c117aefe67f71c3
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>PKIUnitTests</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/tests/dogtag/dev_java_tests/.settings/org.eclipse.jdt.core.prefs b/tests/dogtag/dev_java_tests/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000000000000000000000000000000000000..7341ab1683c4f1bd2f33bbd3d102ebf032569b57
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,11 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7
diff --git a/tests/dogtag/dev_java_tests/conf/deploy.cfg b/tests/dogtag/dev_java_tests/conf/deploy.cfg
new file mode 100644
index 0000000000000000000000000000000000000000..e6f1d5449d6fa422d11a858af22b35fc0532cbf6
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/conf/deploy.cfg
@@ -0,0 +1,9 @@
+[DEFAULT]
+pki_client_pkcs12_password=Secret123
+pki_ds_password=
+
+[CA]
+pki_admin_name=caadmin
+pki_admin_nickname=caadmin
+pki_admin_password=Secret123
+pki_admin_uid=caadmin
diff --git a/tests/dogtag/dev_java_tests/conf/test.cfg b/tests/dogtag/dev_java_tests/conf/test.cfg
new file mode 100644
index 0000000000000000000000000000000000000000..f3a8c9d27571623737a0709672407a22daa6d5f3
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/conf/test.cfg
@@ -0,0 +1,76 @@
+### The configuration file used when running the tests in eclipse.
+### For compatibility with QE beaker tests, the same parameter names are to be used.
+
+### Configuration entries specific to the local setup.
+### The comments above a parameter contain the corresponding name of the
+### entry in the deployment config file to be passed to pkispawn.
+### The deploment configuration to be used for creating the subsystems 
+### for running these tests is deploy.cfg,
+### present in the same folder as this file.
+
+host=localhost
+# Provide the details of the nssdb which contains the admin cert.
+nss_db_dir=/tmp/nssdb
+nss_db_password=XXXXXXXX
+# pki_admin_nickname
+admin_cert_nickname=caadmin
+# Default location = ~/.dogtag/pki-tomcat/ca_admin_cert.p12
+CA_ADMIN_CERT_LOCATION=
+# pki_client_pkcs12_password
+CA_CLIENT_PKCS12_PASSWORD=Secret123
+
+### Configuration entries used in QE tests.
+# pki_https_port
+CA_SECURE_PORT=8443
+LDAP_ROOTDN=
+LDAP_ROOTDNPWD=
+LDAP_BASEDN=
+LDAP_ADMINPW=
+CERTDB_DIR=
+CERTDB_DIR_PASSWORD=
+CA_LDAP_INSTANCE_NAME=
+CA_ADMIN_USER=
+CA_ADMIN_PASSWORD=
+CA_KEY_TYPE=
+CA_KEY_SIZE=
+CA_INSTANCE_ID=
+CA_DB_SUFFIX=
+CA_LDAP_PORT=
+CA_CLIENT_PKCS12_PASSWORD=
+CA_SECURITY_DOMAIN_PASSWORD=
+CA_UNSECURE_PORT=
+CA_AJP_PORT=
+CA_MANAGEMENT_PORT=
+CA_AGENT_CERT_NICKNAME=
+CA_AGENT_CERT_SUBJECT_NAME=
+CA_SIGNING_CERT_SUBJECT_NAME=
+CA_SUBSYSTEM_CERT_SUBJECT_NAME=
+CA_OCSP_SIGNING_CERT_SUBJECT_NAME=
+CA_AUDIT_SIGNING_CERT_SUBJECT_NAME=
+CA_SUBSYSTEM_NAME=
+CA_AGENT_KEY_SIZE=
+CA_AGENT_KEY_TYPE=
+CA_BACKUP_PASSWORD=
+CA_TOKEN_NAME=
+CA_TOKEN_PASSWORD=
+CA_KEY_ALGORITHM=SHA512withRSA
+CA_SIGNING_ALGORITHM=SHA512withRSA
+CA_SIGNING_SIGNING_ALGORITHM=SHA512withRSA
+CA_OCSP_SIGNING_ALGORITHM=SHA512withRSA
+CA_BACKUP_FILE_NAME="$CERTDB_DIR/$CA_INSTANCE_ID.p12"
+KRA_LDAP_INSTANCE_NAME="pki-kra1-ldap"
+KRA_INSTANCE_ID="pki-kra1"
+KRA_DB_SUFFIX="dc=pki-kra1"
+KRA_LDAP_PORT=1501
+KRA_ADMIN_PASSWORD="Secret123"
+KRA_ADMIN_USER="kraadmin"
+KRA_BACKUP_PASSWORD=redhat123
+KRA_CLIENT_PKCS12_PASSWORD=Secret123
+OCSP_LDAP_INSTANCE_NAME="pki-ocsp1-ldap"
+OCSP_INSTANCE_ID="pki-ocsp1"
+OCSP_DB_SUFFIX="dc=pki-ocsp1"
+OCSP_LDAP_PORT=1502
+OCSP_ADMIN_PASSWORD="Secret123"
+OCSP_ADMIN_USER="ocspadmin"
+OCSP_BACKUP_PASSWORD=redhat123
+OCSP_CLIENT_PKCS12_PASSWORD=Secret123
diff --git a/tests/dogtag/dev_java_tests/run_junit_tests.sh b/tests/dogtag/dev_java_tests/run_junit_tests.sh
new file mode 100644
index 0000000000000000000000000000000000000000..b00dfb5063945041f8f54977eb5cd3a6a46d4b50
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/run_junit_tests.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# Include rhts environment
+. /usr/bin/rhts-environment.sh
+. /usr/share/beakerlib/beakerlib.sh
+. /opt/rhqa_pki/rhcs-shared.sh
+. /opt/rhqa_pki/pki-cert-cli-lib.sh
+. /opt/rhqa_pki/env.sh
+
+run_dev_junit_tests() {
+
+    BIN_PATH="`pwd`/dev_java_tests/bin"
+    RUNNING_WITH_BEAKER="TRUE"
+    admin_cert_nickname="PKI Administrator for $CA_DOMAIN"
+    nss_db_password="Secret123"
+    chmod 777 $CA_ADMIN_CERT_LOCATION
+    tmpDir=`mktemp -d`
+    pushd $tmpDir
+    nss_db_dir="$tmpDir/nssdb"
+    mkdir $nss_db_dir
+    importP12File $CA_ADMIN_CERT_LOCATION $CA_CLIENT_PKCS12_PASSWORD $nss_db_dir $nss_db_password $admin_cert_nickname
+    install_and_trust_CA_cert $CA_SERVER_ROOT $nss_db_dir
+
+    host=`hostname`
+
+    export nss_db_dir nss_db_password admin_cert_nickname host CA_CLIENT_PKCS12_PASSWORD RUNNING_WITH_BEAKER
+
+    ### Add all the pki packages and other dependent pki packages to the class path
+    export CLASSPATH=/usr/share/java/junit4.jar:/usr/share/java/pki/*:/usr/lib64/jss/jss4.jar:/usr/share/java/httpcomponents/httpclient.jar:/usr/share/java/httpcomponents/httpcore.jar:/usr/share/java/resteasy/jaxrs-api.jar:/usr/share/java/resteasy/resteasy-atom-provider.jar:/usr/share/java/resteasy/resteasy-jaxb-provider.jar:/usr/share/java/resteasy/resteasy-jaxrs.jar:/usr/share/java/resteasy/resteasy-jaxrs-jandex.jar:/usr/share/java/resteasy/resteasy-jettison-provider.jar:/usr/share/java/apache-commons-cli.jar:/usr/share/java/apache-commons-codec.jar:/usr/share/java/apache-commons-logging.jar:/usr/share/java/commons-codec.jar:/usr/share/java/commons-httpclient.jar:/usr/share/java/idm-console-base-1.1.7.jar:/usr/share/java/idm-console-mcc.jar:/usr/share/java/idm-console-nmclf.jar:/usr/share/java/jakarta-commons-httpclient.jar:/usr/share/java/jaxb-api.jar:/usr/share/java/ldapjdk.jar:/usr/share/java/apache-commons-lang.jar:/usr/share/java/istack-commons-runtime.jar:/usr/share/java/scannotation.jar:/usr/share/java/servlet.jar:/usr/share/java/velocity.jar:/usr/share/java/xerces-j2.jar:/usr/share/java/xml-commons-apis.jar:/usr/share/java/tomcat/catalina.jar:/usr/share/java/tomcat/tomcat-util.jar:/usr/share/java/commons-io.jar:$BIN_PATH
+
+    cd $BIN_PATH
+
+    echo "running the Java tests"
+    java org.junit.runner.JUnitCore BeakerTestSuite > /dev/null 2>&1
+
+    chmod +x java-tests-script.sh
+
+    sh java-tests-script.sh
+
+    #rlJournalEnd
+
+}
diff --git a/tests/dogtag/dev_java_tests/src/BeakerTestSuite.java b/tests/dogtag/dev_java_tests/src/BeakerTestSuite.java
new file mode 100644
index 0000000000000000000000000000000000000000..238e2ee89cd87fa4913b6871a2444ee2c094e8c1
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/src/BeakerTestSuite.java
@@ -0,0 +1,13 @@
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite.SuiteClasses;
+
+import com.netscape.beakertests.CATestJunit;
+import com.netscape.beakertests.PKITestSuite;
+import com.netscape.beakertests.SampleTest1;
+
+@RunWith(PKITestSuite.class)
+@SuiteClasses({ CATestJunit.class, SampleTest1.class })
+public class BeakerTestSuite {
+    // Just a holder for all the test cases.
+
+}
diff --git a/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/CATestJunit.java b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/CATestJunit.java
new file mode 100644
index 0000000000000000000000000000000000000000..d1255ef1dc7bb9046e8abd0dbb0ed37254b6e4ae
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/CATestJunit.java
@@ -0,0 +1,179 @@
+package com.netscape.beakertests;
+
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mozilla.jss.CryptoManager;
+import org.mozilla.jss.crypto.AlreadyInitializedException;
+import org.mozilla.jss.crypto.CryptoToken;
+import org.mozilla.jss.util.Password;
+
+import com.netscape.certsrv.ca.CAClient;
+import com.netscape.certsrv.cert.CertClient;
+import com.netscape.certsrv.cert.CertData;
+import com.netscape.certsrv.cert.CertNotFoundException;
+import com.netscape.certsrv.cert.CertRequestInfo;
+import com.netscape.certsrv.client.ClientConfig;
+import com.netscape.certsrv.client.PKIClient;
+import com.netscape.certsrv.dbs.certdb.CertId;
+
+public class CATestJunit extends PKIJUnitTest {
+
+    CryptoManager manager = null;
+    CryptoToken token = null;
+    CAClient client;
+    CertClient certClient;
+
+    public CATestJunit() {
+        super();
+    }
+
+    @Before
+    public void initializeDB() {
+
+        String host = getParameter("host");
+        String port = getParameter("CA_SECURE_PORT");
+        String token_pwd = getParameter("nss_db_password");
+        String db_dir = getParameter("nss_db_dir");
+        String protocol = "https";
+        String clientCertNickname = getParameter("admin_cert_nickname");
+
+        log("Host " + host);
+        log("NSS DB_DIR " + db_dir);
+        log("NSS DB_PWD " + token_pwd);
+
+        // Initialize token
+        try {
+            CryptoManager.initialize(db_dir);
+        } catch (AlreadyInitializedException e) {
+            // it is ok if it is already initialized
+        } catch (Exception e) {
+            log(("INITIALIZATION ERROR: " + e.toString()));
+            System.exit(1);
+        }
+        // log into token
+        try {
+            manager = CryptoManager.getInstance();
+            token = manager.getInternalKeyStorageToken();
+            Password password = new Password(token_pwd.toCharArray());
+            try {
+                token.login(password);
+            } catch (Exception e) {
+                log("login Exception: " + e.toString());
+                if (!token.isLoggedIn()) {
+                    token.initPassword(password, password);
+                }
+            }
+        } catch (Exception e) {
+            log("Exception in logging into token:" + e.toString());
+        }
+        try {
+            ClientConfig config = new ClientConfig();
+            config.setServerURI(protocol + "://" + host + ":" + port + "/ca");
+            config.setCertNickname(clientCertNickname);
+            log("URI::: " + config.getServerURI().toString());
+            client = new CAClient(new PKIClient(config));
+            certClient = (CertClient)client.getClient("cert");
+        } catch (Exception e) {
+            e.printStackTrace();
+            return;
+        }
+    }
+
+    @Test
+    public void listCompleteCertRequests() {
+
+        Collection<CertRequestInfo> list = null;
+        try {
+            list = certClient.listRequests("complete", null, null, null, null, null).getRequests();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        printRequests(list);
+    }
+
+    @Test
+    public void getACert() {
+        // Get a CertInfo
+        int certIdToPrint = 1;
+        CertId id = new CertId(certIdToPrint);
+        CertData certData = null;
+        try {
+            certData = certClient.getCert(id);
+        } catch (CertNotFoundException e) {
+            e.printStackTrace();
+            log("Cert: " + certIdToPrint + " not found. \n" + e.toString());
+        }
+        printCertificate(certData);
+    }
+
+    @Test(expected = CertNotFoundException.class)
+    public void getABadCert() {
+        // Try an invalid Cert to print out
+        // Get a CertInfo
+        int certIdBadToPrint = 9999999;
+        CertId certIdBad = new CertId(certIdBadToPrint);
+        CertData certDataBad = null;
+        try {
+            certDataBad = certClient.getCert(certIdBad);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log("Cert: " + certIdBadToPrint + " not found. \n" + e.toString());
+            throw e;
+        }
+
+        printCertificate(certDataBad);
+    }
+
+    private void printRequests(Collection<CertRequestInfo> list) {
+        if (list == null) {
+            log("No requests found");
+            return;
+        }
+        for (CertRequestInfo info : list) {
+            printRequestInfo(info);
+        }
+    }
+
+    private void printRequestInfo(CertRequestInfo info) {
+        if (info == null) {
+            log("No RequestInfo: ");
+            return;
+        }
+
+        log("CertRequestURL: " + info.getRequestURL());
+        log("CertId: " + ((info.getCertId() != null) ? info.getCertId() : ""));
+        log("RequestType: " + info.getCertRequestType());
+        log("Status:        " + info.getRequestStatus());
+        log("Type:          " + info.getRequestType());
+        log("CertURL: "
+                + ((info.getCertURL() != null) ? info.getCertURL() : "") + "\n");
+    }
+
+    private void printCertificate(CertData info) {
+
+        if (info == null) {
+            log("No CertificateData: ");
+            return;
+        }
+
+        log("CertificateInfo: " + "\n");
+        log("-----------------");
+
+        log("CertSerialNo:  \n" + info.getSerialNumber() + "\n");
+        log("CertSubject:  \n" + info.getSubjectDN() + "\n");
+        log("CertIssuer: \n" + info.getIssuerDN() + "\n");
+        log("NotBefore:  \n" + info.getNotBefore() + "\n");
+        log("NotAfter: \n" + info.getNotAfter() + "\n");
+        log("CertBase64: \n" + info.getEncoded() + "\n");
+        log("CertPKCS7Chain: \n" + info.getPkcs7CertChain() + "\n");
+        log("CertPrettyPrint: \n" + info.getPrettyPrint());
+
+    }
+
+    /*
+     * private static void log(String string) { log(string); }
+     */
+}
diff --git a/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKIJUnitTest.java b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKIJUnitTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..0ce46c7522b5aefee976fde7c226ac2cd7ed46dd
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKIJUnitTest.java
@@ -0,0 +1,89 @@
+package com.netscape.beakertests;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * Super class for all the test classes.
+ * Provides common functionality for logging messages and
+ * providing asserts and getting the test environment parameters
+ * @author akoneru
+ *
+ */
+public class PKIJUnitTest {
+
+    public static String INFO = "rlLogInfo";
+    public static String DEBUG = "rlLogDebug";
+    public static String WARNING = "rlLogWarning";
+    public static String ERROR = "rlLogError";
+    public static String CRITICAL = "rlLogFatal";
+
+    private BeakerScript beakerScript;
+    String logLevel;
+    Properties properties;
+    private boolean run_with_beaker = false;
+
+    public PKIJUnitTest() {
+        String runWithBeaker = System.getenv("RUNNING_WITH_BEAKER");
+        if (runWithBeaker == null || (! runWithBeaker.toLowerCase().equals("true"))) {
+            properties = new Properties();
+            try {
+                properties.load(new BufferedReader(new FileReader("tests/dogtag/dev_java_tests/conf/test.cfg")));
+            } catch (IOException e) {
+                e.printStackTrace();
+                System.err.println("Cannot read the configuration file");
+                System.exit(-1);
+            }
+        } else {
+            run_with_beaker = true;
+            beakerScript = BeakerScript.getInstance();
+            logLevel = INFO;
+        }
+    }
+
+    public void setLogLevel(String logLevel) {
+        this.logLevel = logLevel;
+    }
+
+    public void log(String message) {
+        if (run_with_beaker) {
+            beakerScript.addBeakerCommand(new String[] { logLevel, message });
+            return;
+        }
+        System.out.println(message);
+    }
+
+    /**
+     * Use this method to add asserts to the beaker output.
+     *
+     * @param hasPassed
+     * @param comment
+     */
+    public void beakerAssert(boolean hasPassed, String comment) {
+        if (comment == null) {
+            comment = "";
+        }
+        if (hasPassed) {
+            beakerScript.addBeakerCommand(new String[] { "rlPass", comment });
+        } else {
+            beakerScript.addBeakerCommand(new String[] { "rlFail", comment });
+        }
+    }
+
+    /**
+     * All the configuration entries are set as environment variables when run in beaker.
+     * Similar key-value pairs are found in the configuration file when running the tests in eclipse
+     * on a local setup.
+     * @param key
+     * @return
+     */
+    public String getParameter(String key) {
+        if (run_with_beaker) {
+            return System.getenv(key);
+        }
+        return properties.getProperty(key);
+    }
+
+}
diff --git a/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKITestSuite.java b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKITestSuite.java
new file mode 100644
index 0000000000000000000000000000000000000000..7269769b8043e319bb5a3428f1cff0b4c647ae62
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/PKITestSuite.java
@@ -0,0 +1,195 @@
+package com.netscape.beakertests;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.PrintWriter;
+
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+import org.junit.runner.Runner;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunListener;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runners.Suite;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.RunnerBuilder;
+
+/**
+ * Custom Suite runner to provide functionality for when running in a beaker test
+ * machine.
+ * @author akoneru
+ *
+ */
+public class PKITestSuite extends Suite {
+
+    private BeakerScript beakerScript;
+    private BeakerResultReporter bRR;
+    private boolean run_with_beaker = false;
+
+    public PKITestSuite(Class<?> klass, RunnerBuilder builder)
+            throws InitializationError {
+        super(klass, builder);
+        String runWithBeaker = System.getenv("RUNNING_WITH_BEAKER");
+        if (runWithBeaker != null && runWithBeaker.toLowerCase().equals("true")) {
+            run_with_beaker = true;
+            beakerScript = BeakerScript.getInstance();
+            bRR = new BeakerResultReporter();
+        }
+    }
+
+    protected PKITestSuite(Class<?> klass, Class<?>[] suiteClasses)
+            throws InitializationError {
+        super(klass, suiteClasses);
+        String runWithBeaker = System.getenv("RUNNING_WITH_BEAKER");
+        if (runWithBeaker != null && runWithBeaker.toLowerCase().equals("true")) {
+            run_with_beaker = true;
+            beakerScript = BeakerScript.getInstance();
+            bRR = new BeakerResultReporter();
+        }
+    }
+
+    @Override
+    protected void runChild(Runner runner, RunNotifier notifier) {
+        super.runChild(runner, notifier);
+    }
+
+    @Override
+    public void run(RunNotifier notifier) {
+        if (run_with_beaker)
+            notifier.addListener(bRR);
+        super.run(notifier);
+        if (run_with_beaker)
+            writeAndExecuteBeakerCommands();
+    }
+
+    private void writeAndExecuteBeakerCommands() {
+        File file = new File("java-tests-script.sh");
+        PrintWriter pw = null;
+        try {
+            pw = new PrintWriter(file);
+            pw.write(beakerScript.getAllCommandsToRun());
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } finally {
+            if (pw != null)
+                pw.close();
+        }
+    }
+
+}
+
+/**
+ * Additional run listener to invoke corresponding beaker-lib system call
+ * for every JUnit test event.
+ * @author akoneru
+ *
+ */
+class BeakerResultReporter extends RunListener {
+
+    private boolean result = true;
+    BeakerScript beakerScript;
+
+    public BeakerResultReporter() {
+        beakerScript = BeakerScript.getInstance();
+    }
+
+    @Override
+    public void testRunStarted(Description description) throws Exception {
+        beakerScript.addBeakerCommand(new String[] { "rlPhaseStart",
+                description.getDisplayName() });
+        super.testRunStarted(description);
+    }
+
+    @Override
+    public void testRunFinished(Result result) throws Exception {
+        super.testRunFinished(result);
+        beakerScript.addBeakerCommand(new String[] { "rlPhaseEnd" });
+    }
+
+    @Override
+    public void testAssumptionFailure(Failure failure) {
+        super.testAssumptionFailure(failure);
+    }
+
+    @Override
+    public void testStarted(Description description) throws Exception {
+        super.testStarted(description);
+        beakerScript.addBeakerCommand(new String[] { "rlPhaseStartTest",
+                description.getDisplayName() });
+    }
+
+    @Override
+    public void testFailure(Failure failure) throws Exception {
+        super.testFailure(failure);
+        result = false;
+        beakerScript.addBeakerCommand(new String[] { "rlFail",
+                failure.getMessage() });
+    }
+
+    @Override
+    public void testFinished(Description description) throws Exception {
+        super.testFinished(description);
+        if (result) {
+            beakerScript
+                    .addBeakerCommand(new String[] { "rlPass",
+                            description.getDisplayName(),
+                            description.getMethodName() });
+        }
+        beakerScript.addBeakerCommand(new String[] { "rlPhaseEnd" });
+    }
+
+    @Override
+    public void testIgnored(Description description) throws Exception {
+        super.testIgnored(description);
+    }
+
+}
+
+/**
+ * Represents the beaker script to be executed after the tests are completed.
+ * @author akoneru
+ *
+ */
+final class BeakerScript {
+
+    private static BeakerScript beakerScript = new BeakerScript();
+
+    private String beakerLib;
+    private StringBuilder commandStore;
+
+    private BeakerScript() {
+        // TODO Auto-generated constructor stub
+        // Add a check for the availability of beaker lib.
+        String bashHeader = "#!/bin/bash";
+        beakerLib = ". /usr/share/beakerlib/beakerlib.sh";
+        commandStore = new StringBuilder(bashHeader);
+        commandStore.append("\n");
+        commandStore.append(beakerLib);
+        commandStore.append("\n");
+    }
+
+    public static BeakerScript getInstance() {
+        return beakerScript;
+    }
+
+    public void addBeakerCommand(String[] command) {
+        for (int i = 0; i < command.length; i++) {
+            commandStore.append(command[i]);
+            commandStore.append(" ");
+            if (i == 0 && command.length > 1) {
+                commandStore.append("\"");
+            }
+        }
+        if (command.length > 1)
+            commandStore.append("\"");
+        commandStore.append("\n");
+    }
+
+    public String getAllCommandsToRun() {
+        return commandStore.toString();
+    }
+
+    public void close() {
+        beakerScript = new BeakerScript();
+    }
+}
\ No newline at end of file
diff --git a/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/SampleTest1.java b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/SampleTest1.java
new file mode 100644
index 0000000000000000000000000000000000000000..9ccd39e9dfed53071f1f7f88ed4119a26efe3c72
--- /dev/null
+++ b/tests/dogtag/dev_java_tests/src/com/netscape/beakertests/SampleTest1.java
@@ -0,0 +1,13 @@
+package com.netscape.beakertests;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class SampleTest1 extends PKIJUnitTest{
+	@Test
+	public void sampleTest() {
+		assertEquals("a", "a");
+		assertNotNull("Should not be null", null);
+	}
+}
-- 
1.8.3.1

