Policy TestSuite Enabled
[policy/engine.git] / ECOMP-PAP-REST / src / test / java / org / openecomp / policy / pap / xacml / rest / components / PolicyDBDaoTest.java
index 81f33c9..6cb8e07 100644 (file)
@@ -36,41 +36,34 @@ import javax.persistence.Persistence;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.junit.After;
+import org.junit.Assert;
+//import org.apache.commons.logging.Log;
+//import org.apache.commons.logging.LogFactory;
+import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
-import org.openecomp.policy.pap.xacml.rest.adapters.PolicyRestAdapter;
-import org.openecomp.policy.pap.xacml.rest.components.ConfigPolicy;
-import org.openecomp.policy.pap.xacml.rest.components.Policy;
-import org.openecomp.policy.pap.xacml.rest.components.PolicyDBDao;
-import org.openecomp.policy.pap.xacml.rest.components.PolicyDBDaoTransaction;
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
 import org.openecomp.policy.pap.xacml.rest.components.PolicyDBDao.PolicyDBDaoTestClass;
 import org.openecomp.policy.rest.XACMLRestProperties;
+import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
 import org.openecomp.policy.rest.jpa.ActionBodyEntity;
 import org.openecomp.policy.rest.jpa.GroupEntity;
 import org.openecomp.policy.rest.jpa.PdpEntity;
 import org.openecomp.policy.rest.jpa.PolicyEntity;
 import org.openecomp.policy.rest.util.Webapps;
-
-import com.att.research.xacml.api.pap.PAPException;
-
 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
 import org.openecomp.policy.xacml.util.XACMLPolicyWriter;
-import com.att.research.xacml.util.XACMLProperties;
 
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-//import org.apache.commons.logging.Log;
-//import org.apache.commons.logging.LogFactory;
-import org.junit.Before;
-import org.junit.After;
-import org.junit.Ignore;
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.util.XACMLProperties;
 
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 
-import org.junit.Assert;
-import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
-import org.openecomp.policy.common.logging.flexlogger.Logger;
-
 @Ignore //only run locally as timing sometimes causes failures on Jenkins
 public class PolicyDBDaoTest {
 
@@ -96,7 +89,7 @@ public class PolicyDBDaoTest {
 
                em.getTransaction().commit();
                } catch(Exception e){
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        em.getTransaction().rollback();
                }
                em.close();
@@ -104,7 +97,7 @@ public class PolicyDBDaoTest {
                        dbd = PolicyDBDao.getPolicyDBDaoInstance(emf);
                        dbd2 = PolicyDBDao.getPolicyDBDaoInstance(emf);
                } catch (Exception e) {
-                       //e.printStackTrace();
+                       //logger.error("Exception Occured"+e);
                        Assert.fail();
                }
 
@@ -180,14 +173,14 @@ public class PolicyDBDaoTest {
 //                     Assert.fail();
 //             }
                String filePath = null;
-               String xmlFile = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Policy xmlns=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" PolicyId=\"urn:com:xacml:policy:id:eaa4bb64-59cf-4517-bb44-b2eeabd50b11\" Version=\"1\" RuleCombiningAlgId=\"urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides\">\n    <Description></Description>\n    <Target>\n        <AnyOf>\n            <AllOf>\n                <Match MatchId=\"org.openecomp.function.regex-match\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#integer\">99</AttributeValue>\n                    <AttributeDesignator Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" AttributeId=\"cpu\" DataType=\"http://www.w3.org/2001/XMLSchema#integer\" MustBePresent=\"false\"/>\n                </Match>\n            </AllOf>\n        </AnyOf>\n    </Target>\n    <Rule RuleId=\"urn:com:xacml:rule:id:3350bf37-43d0-4a94-a317-febec81150d8\" Effect=\"Permit\">\n        <Target/>\n        <ObligationExpressions>\n            <ObligationExpression ObligationId=\"test\" FulfillOn=\"Permit\">\n                <AttributeAssignmentExpression AttributeId=\"performer\" Category=\"urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">PDPAction</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"type\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">REST</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"url\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#anyURI\">http://localhost:8056/pcd</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"method\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">GET</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"body\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#anyURI\">$URLaction/com.Action_patbaction7.json</AttributeValue>\n                </AttributeAssignmentExpression>\n            </ObligationExpression>\n        </ObligationExpressions>\n    </Rule>\n</Policy>\n";
+               String xmlFile = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Policy xmlns=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" PolicyId=\"urn:com:xacml:policy:id:eaa4bb64-59cf-4517-bb44-b2eeabd50b11\" Version=\"1\" RuleCombiningAlgId=\"urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides\">\n    <Description></Description>\n    <Target>\n        <AnyOf>\n            <AllOf>\n                <Match MatchId=\"org.openecomp.labs.ecomp.function.regex-match\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#integer\">99</AttributeValue>\n                    <AttributeDesignator Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" AttributeId=\"cpu\" DataType=\"http://www.w3.org/2001/XMLSchema#integer\" MustBePresent=\"false\"/>\n                </Match>\n            </AllOf>\n        </AnyOf>\n    </Target>\n    <Rule RuleId=\"urn:com:xacml:rule:id:3350bf37-43d0-4a94-a317-febec81150d8\" Effect=\"Permit\">\n        <Target/>\n        <ObligationExpressions>\n            <ObligationExpression ObligationId=\"test\" FulfillOn=\"Permit\">\n                <AttributeAssignmentExpression AttributeId=\"performer\" Category=\"urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">PDPAction</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"type\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">REST</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"url\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#anyURI\">http://localhost:8056/pcd</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"method\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">GET</AttributeValue>\n                </AttributeAssignmentExpression>\n                <AttributeAssignmentExpression AttributeId=\"body\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\">\n                    <AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#anyURI\">$URLaction/com.Action_patbaction7.json</AttributeValue>\n                </AttributeAssignmentExpression>\n            </ObligationExpression>\n        </ObligationExpressions>\n    </Rule>\n</Policy>\n";
                String jsonFile = "{\"actionAttribute\":\"Memory\"}";
                
                try{
                        //policy file
                        InputStream in = new ByteArrayInputStream(xmlFile.getBytes());
                        String workspaceDir = "src/test/resources/junitTestCreatedDirectory/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE)+"/admin/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY);
-                       FileUtils.forceMkdir(new File(workspaceDir+"/org/openecomp"));
+                       FileUtils.forceMkdir(new File(workspaceDir+"/com/att"));
                        File outFile = new File(workspaceDir+"/org/openecomp/Action_mypol.xml");
                        OutputStream out = new FileOutputStream(outFile);
                        IOUtils.copy(in, out);
@@ -224,7 +217,7 @@ public class PolicyDBDaoTest {
                        try{
                                result = (PolicyEntity)getDataQuery.getSingleResult();
                        } catch(Exception e){
-                               e.printStackTrace();
+                               logger.error("Exception Occured"+e);
                                Assert.fail();
                        }
                        Assert.assertEquals(xmlFile, result.getPolicyData());
@@ -235,7 +228,7 @@ public class PolicyDBDaoTest {
                                transac = dbd.getNewTransaction();
                                transac.deletePolicy(filePath);
                        } catch(Exception e){
-                               e.printStackTrace();
+                               logger.error("Exception Occured"+e);
                                Assert.fail();
                        }
                        Assert.assertTrue(transac.isTransactionOpen());
@@ -253,7 +246,7 @@ public class PolicyDBDaoTest {
                                transac = dbd.getNewTransaction();
                                transac.deletePolicy(filePath);
                        } catch(Exception e){
-                               e.printStackTrace();
+                               logger.error("Exception Occured"+e);
                                Assert.fail();
                        }
                        transac.commitTransaction();
@@ -283,7 +276,7 @@ public class PolicyDBDaoTest {
                        }
                        willFail.close();
                        
-                       fakeFile = new File("directorythatdoesnotexist/"+workspaceDir+"org/openecomp/Action_mypol2.xml");
+                       fakeFile = new File("directorythatdoesnotexist/"+workspaceDir+"com/att/Action_mypol2.xml");
                        willFail = dbd.getNewTransaction();
                        try{
                        willFail.createPolicy(fakeFile.getAbsolutePath(), "user1");
@@ -319,7 +312,7 @@ public class PolicyDBDaoTest {
        @Test
        public void createFromPolicyObject(){
                String workspaceDir = "src/test/resources/junitTestCreatedDirectory/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE)+"/admin/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY);
-               File parentPath = new File(workspaceDir+"/org/openecomp");
+               File parentPath = new File(workspaceDir+"/com/att");
                File scope = new File(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE)+"/admin/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY));
                Policy policyObject = new ConfigPolicy();
                policyObject.policyAdapter = new PolicyRestAdapter();
@@ -350,7 +343,7 @@ public class PolicyDBDaoTest {
                try{
                result = (PolicyEntity)getDataQuery.getSingleResult();
                } catch(Exception e){
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                String expectedData;
@@ -367,7 +360,7 @@ public class PolicyDBDaoTest {
                        transaction = dbd.getNewTransaction();
                        transaction.deletePolicy(policyFile.getAbsolutePath());
                } catch(Exception e){
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                Assert.assertTrue(transaction.isTransactionOpen());
@@ -385,7 +378,7 @@ public class PolicyDBDaoTest {
                        transaction = dbd.getNewTransaction();
                        transaction.deletePolicy(policyFile.getAbsolutePath());
                } catch(Exception e){
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                //Assert.assertFalse(transaction.isTransactionOpen());
@@ -401,7 +394,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                EntityManager em = emf.createEntityManager();
@@ -423,7 +416,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                em = emf.createEntityManager();
@@ -443,7 +436,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                group = dbd.getNewTransaction();
@@ -452,7 +445,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                em = emf.createEntityManager();
@@ -474,7 +467,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                em = emf.createEntityManager();
@@ -495,7 +488,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                group = dbd.getNewTransaction();
@@ -504,7 +497,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                
@@ -514,7 +507,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                group = dbd.getNewTransaction();
@@ -523,7 +516,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                em = emf.createEntityManager();
@@ -543,7 +536,7 @@ public class PolicyDBDaoTest {
                        group.commitTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                em = emf.createEntityManager();
@@ -578,7 +571,7 @@ public class PolicyDBDaoTest {
                        group.rollbackTransaction();
                } catch(Exception e){
                        group.rollbackTransaction();
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                
@@ -592,7 +585,7 @@ public class PolicyDBDaoTest {
                        group.updateGroup(pdpGroup, "testuser");
                        group.commitTransaction();
                }catch (Exception e){
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        group.rollbackTransaction();
                        Assert.fail();
                }
@@ -635,8 +628,7 @@ public class PolicyDBDaoTest {
                        System.out.println("Decrypted password: "+decr);
                        Assert.assertEquals("testpassword", decr);
                } catch (Exception e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }
                
@@ -670,8 +662,7 @@ public class PolicyDBDaoTest {
                        }
                        Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                }
                if(logger.isDebugEnabled()){
                        Date date= new java.util.Date();
@@ -712,8 +703,7 @@ public class PolicyDBDaoTest {
                        }
                        Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                }
                if(logger.isDebugEnabled()){
                        Date date= new java.util.Date();
@@ -808,7 +798,7 @@ public class PolicyDBDaoTest {
                                                + "\n   TimeStamp = " + date.getTime()
                                                + "\n\n");
                        }
-                       e.printStackTrace();
+                       logger.error("Exception Occured"+e);
                        Assert.fail();
                }