Modified: airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CommunityUserDAOTest.java URL: http://svn.apache.org/viewvc/airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CommunityUserDAOTest.java?rev=1479724&r1=1459823&r2=1479724&view=diff ============================================================================== --- airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CommunityUserDAOTest.java (original) +++ airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CommunityUserDAOTest.java Mon May 6 23:34:43 2013 @@ -1,9 +1,9 @@ -package org.apache.airavata.credential.store.impl.db; +package org.apache.airavata.credential.store.store.impl.db; import org.apache.airavata.common.utils.DBUtil; import org.apache.airavata.common.utils.DatabaseTestCases; import org.apache.airavata.common.utils.DerbyUtil; -import org.apache.airavata.credential.store.CommunityUser; +import org.apache.airavata.credential.store.credential.CommunityUser; import org.junit.*; import java.sql.Connection; @@ -27,9 +27,17 @@ public class CommunityUserDAOTest extend " (\n" + " GATEWAY_NAME VARCHAR(256) NOT NULL,\n" + " COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,\n" + + " TOKEN_ID VARCHAR(256) NOT NULL,\n" + " COMMUNITY_USER_EMAIL VARCHAR(256) NOT NULL,\n" + - " PRIMARY KEY (GATEWAY_NAME, COMMUNITY_USER_NAME)\n" + + " PRIMARY KEY (GATEWAY_NAME, COMMUNITY_USER_NAME, TOKEN_ID)\n" + " )"; + + String dropTable = "drop table COMMUNITY_USER"; + + try { + executeSQL(dropTable); + } catch (Exception e){} + executeSQL(createTable); } @@ -42,71 +50,135 @@ public class CommunityUserDAOTest extend @Before public void setUp() throws Exception { - communityUserDAO = new CommunityUserDAO(getDbUtil()); + communityUserDAO = new CommunityUserDAO(); Connection connection = getDbUtil().getConnection(); - DBUtil.truncate("community_user", connection); - connection.close(); + try { + DBUtil.truncate("community_user", connection); + } finally { + connection.close(); + } + } @Test public void testAddCommunityUser() throws Exception { - CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); - communityUserDAO.addCommunityUser(communityUser); + Connection connection = getConnection(); - communityUser = new CommunityUser("gw1", "ogce2","[email protected]"); - communityUserDAO.addCommunityUser(communityUser); + try { + + CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); + communityUserDAO.addCommunityUser(communityUser, "Token1", connection); + + communityUser = new CommunityUser("gw1", "ogce2","[email protected]"); + communityUserDAO.addCommunityUser(communityUser, "Token2", connection); + + CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce", connection); + Assert.assertNotNull(user); + Assert.assertEquals("[email protected]", user.getUserEmail()); + + user = communityUserDAO.getCommunityUser("gw1", "ogce2", connection); + Assert.assertNotNull(user); + Assert.assertEquals("[email protected]", user.getUserEmail()); + + user = communityUserDAO.getCommunityUserByToken("gw1", "Token1", connection); + Assert.assertNotNull(user); + Assert.assertEquals("ogce", user.getUserName()); + Assert.assertEquals("[email protected]", user.getUserEmail()); + + user = communityUserDAO.getCommunityUserByToken("gw1", "Token2", connection); + Assert.assertNotNull(user); + Assert.assertEquals("ogce2", user.getUserName()); + Assert.assertEquals("[email protected]", user.getUserEmail()); + + } finally { + connection.close(); + } - CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce"); - Assert.assertNotNull(user); - Assert.assertEquals("[email protected]", user.getUserEmail()); - - user = communityUserDAO.getCommunityUser("gw1", "ogce2"); - Assert.assertNotNull(user); - Assert.assertEquals("[email protected]", user.getUserEmail()); } @Test public void testDeleteCommunityUser() throws Exception { - CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); - communityUserDAO.addCommunityUser(communityUser); + Connection connection = getConnection(); + + try { + CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); + communityUserDAO.addCommunityUser(communityUser, "Token1", connection); - CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce"); - Assert.assertNotNull(user); + CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce", connection); + Assert.assertNotNull(user); - communityUser = new CommunityUser("gw1", "ogce","[email protected]"); - communityUserDAO.deleteCommunityUser(communityUser); + communityUser = new CommunityUser("gw1", "ogce","[email protected]"); + communityUserDAO.deleteCommunityUser(communityUser, connection); + + user = communityUserDAO.getCommunityUser("gw1", "ogce", connection); + Assert.assertNull(user); + + } finally { + connection.close(); + } + } + + @Test + public void testDeleteCommunityUserByToken() throws Exception { + + Connection connection = getConnection(); + + try { + CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); + communityUserDAO.addCommunityUser(communityUser, "Token1", connection); + + CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce", connection); + Assert.assertNotNull(user); + + communityUser = new CommunityUser("gw1", "ogce","[email protected]"); + communityUserDAO.deleteCommunityUserByToken(communityUser, "Token1", connection); + + user = communityUserDAO.getCommunityUser("gw1", "ogce", connection); + Assert.assertNull(user); + + } finally { + connection.close(); + } - user = communityUserDAO.getCommunityUser("gw1", "ogce"); - Assert.assertNull(user); } @Test public void testGetCommunityUsers() throws Exception { - CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); - communityUserDAO.addCommunityUser(communityUser); + Connection connection = getConnection(); + + try { + CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); + communityUserDAO.addCommunityUser(communityUser, "Token1", connection); + + CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce", connection); + Assert.assertNotNull(user); + Assert.assertEquals("[email protected]", user.getUserEmail()); + + } finally { + connection.close(); + } - CommunityUser user = communityUserDAO.getCommunityUser("gw1", "ogce"); - Assert.assertNotNull(user); - Assert.assertEquals("[email protected]", user.getUserEmail()); } @Test public void testGetCommunityUsersForGateway() throws Exception { + Connection connection = getConnection(); + CommunityUser communityUser = new CommunityUser("gw1", "ogce","[email protected]"); - communityUserDAO.addCommunityUser(communityUser); + communityUserDAO.addCommunityUser(communityUser, "Token1", connection); communityUser = new CommunityUser("gw1", "ogce2","[email protected]"); - communityUserDAO.addCommunityUser(communityUser); + communityUserDAO.addCommunityUser(communityUser, "Token2", connection); - List<CommunityUser> users = communityUserDAO.getCommunityUsers("gw1"); + List<CommunityUser> users = communityUserDAO.getCommunityUsers("gw1", connection); Assert.assertNotNull(users); Assert.assertEquals(2, users.size());
Copied: airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CredentialsDAOTest.java (from r1459824, airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/impl/db/CredentialsDAOTest.java) URL: http://svn.apache.org/viewvc/airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CredentialsDAOTest.java?p2=airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CredentialsDAOTest.java&p1=airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/impl/db/CredentialsDAOTest.java&r1=1459824&r2=1479724&rev=1479724&view=diff ============================================================================== --- airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/impl/db/CredentialsDAOTest.java (original) +++ airavata/trunk/modules/credential-store/src/test/java/org/apache/airavata/credential/store/store/impl/db/CredentialsDAOTest.java Mon May 6 23:34:43 2013 @@ -1,26 +1,25 @@ -package org.apache.airavata.credential.store.impl.db; +package org.apache.airavata.credential.store.store.impl.db; import junit.framework.Assert; import org.apache.airavata.common.utils.DBUtil; import org.apache.airavata.common.utils.DatabaseTestCases; import org.apache.airavata.common.utils.DerbyUtil; -import org.apache.airavata.credential.store.CertificateCredential; -import org.apache.airavata.credential.store.CommunityUser; -import org.bouncycastle.jce.X509Principal; -import org.bouncycastle.x509.X509V3CertificateGenerator; +import org.apache.airavata.credential.store.credential.CommunityUser; +import org.apache.airavata.credential.store.credential.Credential; +import org.apache.airavata.credential.store.credential.impl.certificate.CertificateCredential; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; -import java.math.BigInteger; import java.security.*; import java.security.cert.X509Certificate; import java.sql.Connection; import java.util.Arrays; -import java.util.Date; import java.util.List; /** @@ -28,18 +27,20 @@ import java.util.List; */ public class CredentialsDAOTest extends DatabaseTestCases { + private static final Logger logger = LoggerFactory.getLogger(CredentialsDAOTest.class); + private CredentialsDAO credentialsDAO; private X509Certificate x509Certificate; private PrivateKey privateKey; @BeforeClass - public static void setUpDatabase() throws Exception{ + public static void setUpDatabase() throws Exception { DerbyUtil.startDerbyInServerMode(getHostAddress(), getPort(), getUserName(), getPassword()); waitTillServerStarts(); - String createTable = "CREATE TABLE CREDENTIALS\n" + + /*String createTable = "CREATE TABLE CREDENTIALS\n" + "(\n" + " GATEWAY_NAME VARCHAR(256) NOT NULL,\n" + " COMMUNITY_USER_NAME VARCHAR(256) NOT NULL,\n" + @@ -51,7 +52,24 @@ public class CredentialsDAOTest extends " REQUESTING_PORTAL_USER_NAME VARCHAR(256) NOT NULL,\n" + " REQUESTED_TIME TIMESTAMP DEFAULT '0000-00-00 00:00:00',\n" + " PRIMARY KEY (GATEWAY_NAME, COMMUNITY_USER_NAME)\n" + + ")"; */ + + String createTable = "CREATE TABLE CREDENTIALS\n" + + "(\n" + + " GATEWAY_ID VARCHAR(256) NOT NULL,\n" + + " TOKEN_ID VARCHAR(256) NOT NULL,\n" + // Actual token used to identify the credential + " CREDENTIAL BLOB NOT NULL,\n" + + " PORTAL_USER_ID VARCHAR(256) NOT NULL,\n" + + " TIME_PERSISTED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n" + + " PRIMARY KEY (GATEWAY_ID, TOKEN_ID)\n" + ")"; + + String dropTable = "drop table CREDENTIALS"; + + try { + executeSQL(dropTable); + } catch (Exception e) {} + executeSQL(createTable); } @@ -64,35 +82,46 @@ public class CredentialsDAOTest extends @Before public void setUp() throws Exception { - credentialsDAO = new CredentialsDAO(getDbUtil()); + credentialsDAO = new CredentialsDAO(); // Cleanup tables; Connection connection = getDbUtil().getConnection(); - DBUtil.truncate("credentials", connection); - DBUtil.truncate("community_user", connection); - connection.close(); + try { + DBUtil.truncate("credentials", connection); + } finally { + connection.close(); + } initializeKeys(); } + private void initializeKeys() throws Exception { KeyStore ks = KeyStore.getInstance("JKS"); char[] password = "password".toCharArray(); String baseDirectory = System.getProperty("credential.module.directory"); - String keyStorePath = "src" + File.separator + "test" + File.separator + "resources" + File.separator + String keyStorePath = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "keystore.jks"; if (baseDirectory != null) { keyStorePath = baseDirectory + File.separator + keyStorePath; + } else { + keyStorePath = "modules" + File.separator + "credential-store" + File.separator + keyStorePath; + } + + File keyStoreFile = new File(keyStorePath); + if (!keyStoreFile.exists()) { + logger.error("Unable to read keystore file " + keyStoreFile); + throw new RuntimeException("Unable to read keystore file " + keyStoreFile); + } java.io.FileInputStream fis = null; try { - fis = - new java.io.FileInputStream(keyStorePath); + fis = new java.io.FileInputStream(keyStorePath); ks.load(fis, password); } finally { if (fis != null) { @@ -107,6 +136,7 @@ public class CredentialsDAOTest extends } + @Test public void testKeyReading() throws Exception { initializeKeys(); @@ -123,7 +153,22 @@ public class CredentialsDAOTest extends private void addTestCredentials() throws Exception { + Connection connection = getConnection(); + + try { + CertificateCredential certificateCredential = getTestCredentialObject(); + credentialsDAO.addCredentials(certificateCredential.getCommunityUser().getGatewayName(), + certificateCredential, connection); + + } finally { + connection.close(); + } + } + + public CertificateCredential getTestCredentialObject() { + CertificateCredential certificateCredential = new CertificateCredential(); + certificateCredential.setToken("tom"); certificateCredential.setCertificate(x509Certificate); certificateCredential.setPrivateKey(privateKey); certificateCredential.setCommunityUser(getCommunityUser("gw1", "tom")); @@ -132,13 +177,29 @@ public class CredentialsDAOTest extends certificateCredential.setNotBefore("13 OCT 2012 5:34:23"); certificateCredential.setNotAfter("14 OCT 2012 5:34:23"); - credentialsDAO.addCredentials(certificateCredential); + return certificateCredential; + } @Test public void testSerialization() throws IOException, ClassNotFoundException { - byte[] array = CredentialsDAO.convertObjectToByteArray(privateKey); - PrivateKey newKey = (PrivateKey) CredentialsDAO.convertByteArrayToObject(array); + + CertificateCredential certificateCredential = getTestCredentialObject(); + + byte[] array = CredentialsDAO.convertObjectToByteArray(certificateCredential); + CertificateCredential readCertificateCredential = (CertificateCredential) CredentialsDAO.convertByteArrayToObject(array); + + Assert.assertEquals(certificateCredential.getCertificate(), readCertificateCredential.getCertificate()); + Assert.assertEquals(certificateCredential.getCertificateRequestedTime(), readCertificateCredential.getCertificateRequestedTime()); + Assert.assertEquals(certificateCredential.getCommunityUser().getGatewayName(), readCertificateCredential.getCommunityUser().getGatewayName()); + Assert.assertEquals(certificateCredential.getCommunityUser().getUserEmail(), readCertificateCredential.getCommunityUser().getUserEmail()); + Assert.assertEquals(certificateCredential.getCommunityUser().getUserName(), readCertificateCredential.getCommunityUser().getUserName()); + Assert.assertEquals(certificateCredential.getLifeTime(), readCertificateCredential.getLifeTime()); + Assert.assertEquals(certificateCredential.getNotAfter(), readCertificateCredential.getNotAfter()); + Assert.assertEquals(certificateCredential.getNotBefore(), readCertificateCredential.getNotBefore()); + Assert.assertEquals(certificateCredential.getPortalUserName(), readCertificateCredential.getPortalUserName()); + + PrivateKey newKey = readCertificateCredential.getPrivateKey(); Assert.assertNotNull(newKey); Assert.assertEquals(privateKey.getClass(), newKey.getClass()); @@ -153,13 +214,18 @@ public class CredentialsDAOTest extends addTestCredentials(); - CertificateCredential certificateCredential - = credentialsDAO.getCredential("gw1", "tom"); - Assert.assertNotNull(certificateCredential); - Assert.assertEquals("jerry", certificateCredential.getPortalUserName()); - Assert.assertEquals(x509Certificate, certificateCredential.getCertificate()); - Assert.assertEquals(privateKey, certificateCredential.getPrivateKey()); + Connection connection = getConnection(); + try { + CertificateCredential certificateCredential + = (CertificateCredential)credentialsDAO.getCredential("gw1", "tom", connection); + Assert.assertNotNull(certificateCredential); + Assert.assertEquals("jerry", certificateCredential.getPortalUserName()); + Assert.assertEquals(x509Certificate, certificateCredential.getCertificate()); + Assert.assertEquals(privateKey.getFormat(), certificateCredential.getPrivateKey().getFormat()); + } finally { + connection.close(); + } } @Test @@ -167,14 +233,21 @@ public class CredentialsDAOTest extends addTestCredentials(); - CertificateCredential certificateCredential - = credentialsDAO.getCredential("gw1", "tom"); - Assert.assertNotNull(certificateCredential); + Connection connection = getConnection(); + + try { + CertificateCredential certificateCredential + = (CertificateCredential)credentialsDAO.getCredential("gw1", "tom", connection); + Assert.assertNotNull(certificateCredential); + + credentialsDAO.deleteCredentials("gw1", "tom", connection); - credentialsDAO.deleteCredentials("gw1", "tom"); + certificateCredential = (CertificateCredential)credentialsDAO.getCredential("gw1", "tom", connection); + Assert.assertNull(certificateCredential); - certificateCredential = credentialsDAO.getCredential("gw1", "tom"); - Assert.assertNull(certificateCredential); + } finally { + connection.close(); + } } @Test @@ -182,23 +255,32 @@ public class CredentialsDAOTest extends addTestCredentials(); - CertificateCredential certificateCredential = new CertificateCredential(); - certificateCredential.setCommunityUser(getCommunityUser("gw1", "tom")); - certificateCredential.setCertificate(x509Certificate); - certificateCredential.setPrivateKey(privateKey); - certificateCredential.setPortalUserName("test2"); - certificateCredential.setLifeTime(50); - certificateCredential.setNotBefore("15 OCT 2012 5:34:23"); - certificateCredential.setNotAfter("16 OCT 2012 5:34:23"); - - credentialsDAO.updateCredentials(certificateCredential); - - certificateCredential = credentialsDAO.getCredential("gw1", "tom"); - - Assert.assertEquals("CN=Airavata Project, OU=IU, O=Indiana University, L=Bloomington, ST=IN, C=US", - certificateCredential.getCertificate().getIssuerDN().toString()); - Assert.assertNotNull(certificateCredential.getPrivateKey()); - Assert.assertEquals("test2", certificateCredential.getPortalUserName()); + Connection connection = getConnection(); + + try { + CommunityUser communityUser = getCommunityUser("gw1", "tom"); + CertificateCredential certificateCredential = new CertificateCredential(); + certificateCredential.setToken("tom"); + certificateCredential.setCommunityUser(communityUser); + certificateCredential.setCertificate(x509Certificate); + //certificateCredential.setPrivateKey(privateKey); + certificateCredential.setPortalUserName("test2"); + certificateCredential.setLifeTime(50); + certificateCredential.setNotBefore("15 OCT 2012 5:34:23"); + certificateCredential.setNotAfter("16 OCT 2012 5:34:23"); + + credentialsDAO.updateCredentials(communityUser.getGatewayName(), certificateCredential, connection); + + certificateCredential = (CertificateCredential)credentialsDAO.getCredential("gw1", "tom", connection); + + Assert.assertEquals("CN=Airavata Project, OU=IU, O=Indiana University, L=Bloomington, ST=IN, C=US", + certificateCredential.getCertificate().getIssuerDN().toString()); + //Assert.assertNotNull(certificateCredential.getPrivateKey()); + Assert.assertEquals("test2", certificateCredential.getPortalUserName()); + + } finally { + connection.close(); + } } @@ -207,11 +289,18 @@ public class CredentialsDAOTest extends addTestCredentials(); - CertificateCredential certificateCredential = credentialsDAO.getCredential("gw1", "tom"); + Connection connection = getConnection(); + + try { + + CertificateCredential certificateCredential = (CertificateCredential)credentialsDAO.getCredential("gw1", "tom", connection); + Assert.assertEquals("CN=Airavata Project, OU=IU, O=Indiana University, L=Bloomington, ST=IN, C=US", + certificateCredential.getCertificate().getIssuerDN().toString()); + // Assert.assertNotNull(certificateCredential.getPrivateKey()); - Assert.assertEquals("CN=Airavata Project, OU=IU, O=Indiana University, L=Bloomington, ST=IN, C=US", - certificateCredential.getCertificate().getIssuerDN().toString()); - Assert.assertNotNull(certificateCredential.getPrivateKey()); + } finally { + connection.close(); + } } @Test @@ -219,8 +308,15 @@ public class CredentialsDAOTest extends addTestCredentials(); - List<CertificateCredential> list = credentialsDAO.getCredentials("gw1"); + Connection connection = getConnection(); + + try { + List<Credential> list = credentialsDAO.getCredentials("gw1", connection); + + Assert.assertEquals(1, list.size()); + } finally { + connection.close(); + } - Assert.assertEquals(1, list.size()); } }
