PolicyDbDao { public PolicyDbDaoTransaction getNewTransaction() { logger.debug("getNewTransaction() as getNewTransaction() called"); return new PolicyDbDaoTransactionInstance("init"); } @Autowired  PolicyDbDao(SessionFactory sessionFactory);  PolicyDbDao();  PolicyDbDao(String init); static PolicyDbDao getPolicyDbDaoInstance(); void setPapEngine(PAPPolicyEngine papEngine2); @SuppressWarnings("deprecation") void startTransactionSynced(Session session, int waitTime); PolicyDbDaoTransaction getNewTransaction(); PolicyDbDaoTransaction getNewAuditTransaction(); static boolean stringEquals(String one, String two); String[] getPapUrlUserPass(); String[] splitPapUrlUserPass(String url); StdPDPGroup synchronizeGroupPoliciesInFileSystem(StdPDPGroup pdpGroup, GroupEntity groupentity); String removeExtensionAndVersionFromPolicyName(String originalPolicyName); String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName); String getPdpPolicyName(String name, String scope); void auditLocalDatabase(PAPPolicyEngine papEngine2); StdPDPGroup auditLocalFileSystem(StdPDPGroup group); void synchronizeConfigDataInFileSystem(); void deleteAllGroupTables(); @SuppressWarnings("unchecked") void auditGroups(PAPPolicyEngine papEngine2); String getConfigFile(String filename, PolicyRestAdapter policy); String getConfigFile(String inputFilename, String configType); String[] getNameScopeAndVersionFromPdpPolicy(String fileName); static String createNewPdpGroupId(String name); static boolean isNullOrEmpty(String... strings); List<?> getOtherServers(); void setOtherServers(List<?> otherServers); PAPPolicyEngine getPapEngine(); static boolean isJunit(); static void setJunit(boolean isJunit); static PolicyDbDaoTestClass getPolicyDbDaoTestClass(); static final String JSON_CONFIG; static final String XML_CONFIG; static final String PROPERTIES_CONFIG; static final String OTHER_CONFIG; static final String AUDIT_USER; static final String CONFIG; static final String ACTION; static final String GROUP_ID; static final String DELETED; static final String GROUPENTITY_SELECT; static final String PDPENTITY_SELECT; static final String GROUP_NOT_FOUND; static final String FOUND_IN_DB_NOT_DEL; static final String MORE_THAN_ONE_PDP; static final String DELETED_STATUS_FOUND; static final String DUPLICATE_GROUPID; static final String PDP_ID; static final String QUERY_FAILED_FOR_GROUP; static final String QUERY_FAILED_GET_GROUP; static final String SCOPE; static final String POLICYDBDAO_VAR; static final String DUP_POLICYID; static final String FOUND_IN_DB; }
@Test public void createFromPolicyObject() { Policy policyObject = new ConfigPolicy(); policyObject.policyAdapter = new PolicyRestAdapter(); policyObject.policyAdapter.setConfigName("testpolicy1"); policyObject.policyAdapter.setPolicyDescription("my description"); policyObject.policyAdapter.setConfigBodyData("this is my test config file"); policyObject.policyAdapter.setPolicyName("SampleTest1206"); policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG); policyObject.policyAdapter.setPolicyType("Config"); policyObject.policyAdapter.setDomainDir("com"); policyObject.policyAdapter.setVersion("1"); policyObject.policyAdapter.setHighestVersion(1); PolicyType policyTypeObject = new PolicyType(); policyObject.policyAdapter.setPolicyData(policyTypeObject); ClassLoader classLoader = getClass().getClassLoader(); PolicyType policyConfig = new PolicyType(); policyConfig.setVersion("1"); policyConfig.setPolicyId(""); policyConfig.setTarget(new TargetType()); policyObject.policyAdapter.setData(policyConfig); try { policyObject.policyAdapter .setParentPath(IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml"))); } catch (Exception e2) { fail(); } PolicyDbDaoTransaction transaction = dbd.getNewTransaction(); try { transaction.createPolicy(policyObject, "testuser1"); transaction.commitTransaction(); } catch (Exception e) { transaction.rollbackTransaction(); Assert.fail(); } Session session = sessionFactory.openSession(); session.getTransaction().begin(); Query policyQuery = session.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:name AND p.scope=:scope"); policyQuery.setParameter("name", "Config_SampleTest1206.1.xml"); policyQuery.setParameter("scope", "com"); List<?> policyQueryList = policyQuery.list(); PolicyEntity result = null; try { result = (PolicyEntity) policyQueryList.get(0); } catch (Exception e) { logger.error("Exception Occured " + e); Assert.fail(); } session.getTransaction().commit(); session.close(); String expectedData; try { expectedData = IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml")); } catch (IOException e1) { expectedData = ""; } Assert.assertEquals(expectedData, result.getPolicyData()); result = null; Assert.assertFalse(transaction.isTransactionOpen()); }
@Ignore @Test public void threadingStabilityTest() { if (logger.isDebugEnabled()) { logger.debug("\n\n****************************" + "threadingStabilityTest() entry" + "******************************\n\n"); } PolicyDbDaoTransaction t = dbd.getNewTransaction(); Assert.assertTrue(t.isTransactionOpen()); try { int sleepTime = Integer.parseInt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_TRANS_TIMEOUT)) + 1000; if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n sleepTime = " + sleepTime + "\n TimeStamp = " + date.getTime() + "\n\n"); } Thread.sleep(sleepTime); } catch (InterruptedException e) { logger.error("Exception Occured" + e); } if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug( "\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Assert.assertFalse(t.isTransactionOpen() = " + t.isTransactionOpen() + ")" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Assert.assertFalse(t.isTransactionOpen()); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n a = dbd.getNewTransaction() " + "\n TimeStamp = " + date.getTime() + "\n\n"); } PolicyDbDaoTransaction a = dbd.getNewTransaction(); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug( "\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Assert.assertTrue(a.isTransactionOpen() = " + a.isTransactionOpen() + ")" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Assert.assertTrue(a.isTransactionOpen()); try { int sleepTime = Integer.parseInt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_TRANS_TIMEOUT)) + 1000; if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n sleepTime = " + sleepTime + "\n TimeStamp = " + date.getTime() + "\n\n"); } Thread.sleep(sleepTime); } catch (InterruptedException e) { logger.error("Exception Occured" + e); } if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n b = dbd.getNewTransaction() " + "\n TimeStamp = " + date.getTime() + "\n\n"); } PolicyDbDaoTransaction b = dbd.getNewTransaction(); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug( "\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Assert.assertFalse(a.isTransactionOpen() = " + a.isTransactionOpen() + ")" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Assert.assertFalse(a.isTransactionOpen()); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug( "\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Assert.assertTrue(b.isTransactionOpen() = " + b.isTransactionOpen() + ")" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Assert.assertTrue(b.isTransactionOpen()); b.close(); System.setProperty(XacmlRestProperties.PROP_PAP_TRANS_WAIT, "1000"); System.setProperty(XacmlRestProperties.PROP_PAP_TRANS_TIMEOUT, "5000"); PolicyDbDaoTransaction t1 = dbd.getNewTransaction(); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug( "\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Assert.assertTrue(t1.isTransactionOpen() = " + t1.isTransactionOpen() + ")" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Assert.assertTrue(t1.isTransactionOpen()); try { PolicyDbDaoTransaction t2 = dbd2.getNewTransaction(); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Thread.sleep(3000)" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Thread.sleep(3000); if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n Assert.assertTrue(t1.isTransactionOpen() = " + t1.isTransactionOpen() + ")" + "\n Assert.assertFalse(t2.isTransactionOpen() = " + t2.isTransactionOpen() + ")" + "\n TimeStamp = " + date.getTime() + "\n\n"); } Assert.fail("\n\nTransaction timeout of 1000 ms exceeded without a PersistenceException\n\n"); } catch (PersistenceException e) { if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() " + "\n SUCCESS! Transaction Wait Timeout worked!" + "\n Caught PersistenceException = " + e + "\n TimeStamp = " + date.getTime() + "\n\n"); } } catch (Exception e) { if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nPolicyDBDaoTest.threadingStabilityTest() FAILURE" + "\n Caught Exception = " + e + "\n TimeStamp = " + date.getTime() + "\n\n"); } logger.error("Exception Occured" + e); Assert.fail(); } if (logger.isDebugEnabled()) { Date date = new java.util.Date(); logger.debug("\n\nthreadingStabilityTest() exit" + "\n TimeStamp = " + date.getTime() + "\n\n"); } }