Java tutorial
/* * HSM Proxy Project. * Copyright (C) 2013 FedICT. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version * 3.0 as published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, see * http://www.gnu.org/licenses/. */ package test.unit.be.fedict.hsm.entity; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.List; import java.util.UUID; import javax.persistence.EntityExistsException; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.PersistenceException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Test; import be.fedict.hsm.entity.ApplicationEntity; import be.fedict.hsm.entity.ApplicationKeyEntity; import be.fedict.hsm.entity.ApplicationKeyId; import be.fedict.hsm.entity.AuditEntity; import be.fedict.hsm.entity.CredentialEntity; import be.fedict.hsm.entity.KeyStoreEntity; import be.fedict.hsm.entity.KeyStoreType; public class PersistenceTest { private static final Log LOG = LogFactory.getLog(PersistenceTest.class); private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; @Before public void setUp() throws Exception { this.entityManagerFactory = Persistence.createEntityManagerFactory("test"); this.entityManager = entityManagerFactory.createEntityManager(); } @Test public void testKeyStoreEntity() throws Exception { KeyStoreEntity keyStoreEntity = new KeyStoreEntity("name", KeyStoreType.PKCS12, "/home/fcorneli/test.p12", "test", 0); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); entityManager.persist(keyStoreEntity); entityTransaction.commit(); long id = keyStoreEntity.getId(); LOG.debug("keystore entity identifier: " + id); KeyStoreEntity resultEntity = this.entityManager.find(KeyStoreEntity.class, id); assertNotNull(resultEntity); LOG.debug("key store entity: " + resultEntity); List<KeyStoreEntity> keyStoreList = KeyStoreEntity.getList(this.entityManager); assertNotNull(keyStoreList); assertEquals(1, keyStoreList.size()); assertTrue(KeyStoreEntity.hasKeyStore(this.entityManager, "name")); } @Test public void testKeyStoreEntityUniqueName() throws Exception { String keyStoreName = UUID.randomUUID().toString(); KeyStoreEntity keyStoreEntity = new KeyStoreEntity(keyStoreName, KeyStoreType.PKCS12, "/home/fcorneli/test.p12", "test", 0); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); this.entityManager.persist(keyStoreEntity); entityTransaction.commit(); KeyStoreEntity keyStoreEntity2 = new KeyStoreEntity(keyStoreName, KeyStoreType.PKCS12, "/home/fcorneli/test.p12", "test", 0); entityTransaction.begin(); try { this.entityManager.persist(keyStoreEntity2); fail(); } catch (PersistenceException e) { entityTransaction.rollback(); } } @Test public void testUniqueApplicationName() throws Exception { ApplicationEntity applicationEntity = new ApplicationEntity("app"); ApplicationEntity applicationEntity2 = new ApplicationEntity("app"); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); this.entityManager.persist(applicationEntity); entityTransaction.commit(); entityTransaction.begin(); assertTrue(ApplicationEntity.hasApplication(this.entityManager, "app")); try { this.entityManager.persist(applicationEntity2); fail(); } catch (PersistenceException e) { entityTransaction.rollback(); } } @Test public void testApplicationCredential() throws Exception { ApplicationEntity applicationEntity = new ApplicationEntity("app"); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); this.entityManager.persist(applicationEntity); long appId = applicationEntity.getId(); entityTransaction.commit(); entityTransaction.begin(); applicationEntity = this.entityManager.find(ApplicationEntity.class, appId); CredentialEntity credentialEntity = new CredentialEntity("hash", "credential".getBytes(), applicationEntity); this.entityManager.persist(credentialEntity); entityTransaction.commit(); entityTransaction.begin(); List<CredentialEntity> credentialList = CredentialEntity.getCredentials(this.entityManager, applicationEntity); assertNotNull(credentialList); assertEquals(1, credentialList.size()); } @Test public void testApplicationKey() throws Exception { ApplicationEntity applicationEntity = new ApplicationEntity("app"); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); this.entityManager.persist(applicationEntity); entityTransaction.commit(); long appId = applicationEntity.getId(); entityTransaction.begin(); applicationEntity = this.entityManager.find(ApplicationEntity.class, appId); ApplicationKeyEntity applicationKeyEntity = new ApplicationKeyEntity(applicationEntity, "alias"); ApplicationKeyEntity applicationKeyEntity2 = new ApplicationKeyEntity(applicationEntity, "alias2"); this.entityManager.persist(applicationKeyEntity); this.entityManager.persist(applicationKeyEntity2); entityTransaction.commit(); this.entityManager.close(); this.entityManager = this.entityManagerFactory.createEntityManager(); entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); applicationKeyEntity = this.entityManager.find(ApplicationKeyEntity.class, new ApplicationKeyId(appId, "alias")); assertEquals("alias", applicationKeyEntity.getName()); assertEquals("app", applicationKeyEntity.getApplication().getName()); List<ApplicationKeyEntity> resultList = ApplicationKeyEntity.getApplicationKeys(this.entityManager, applicationEntity); assertEquals(2, resultList.size()); for (ApplicationKeyEntity result : resultList) { LOG.debug("key: " + result.getName()); } } @Test public void testApplicationKeyUniqueAlias() throws Exception { ApplicationEntity applicationEntity = new ApplicationEntity("app"); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); this.entityManager.persist(applicationEntity); entityTransaction.commit(); long appId = applicationEntity.getId(); entityTransaction.begin(); applicationEntity = this.entityManager.find(ApplicationEntity.class, appId); ApplicationKeyEntity applicationKeyEntity = new ApplicationKeyEntity(applicationEntity, "alias"); ApplicationKeyEntity applicationKeyEntity2 = new ApplicationKeyEntity(applicationEntity, "alias"); this.entityManager.persist(applicationKeyEntity); try { this.entityManager.persist(applicationKeyEntity2); fail(); } catch (EntityExistsException e) { entityTransaction.rollback(); } } @Test public void testAudit() throws Exception { AuditEntity auditEntity = new AuditEntity("applicationName", "applicationKeyAlias", "keyStoreName", "keyStoreAlias", "digestValue"); EntityTransaction entityTransaction = this.entityManager.getTransaction(); entityTransaction.begin(); this.entityManager.persist(auditEntity); entityTransaction.commit(); long auditId = auditEntity.getId(); entityTransaction.begin(); List<AuditEntity> result = AuditEntity.listAll(this.entityManager); entityTransaction.commit(); assertEquals(1, result.size()); assertEquals(auditId, result.get(0).getId()); } }