- @Test
- public void transactionTests(){
-
-
-// try{
-// transac.commitTransaction();
-// Assert.fail();
-// } catch(IllegalStateException e){
-// //worked
-// } catch(Exception e2){
-// 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.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+"/com/att"));
- File outFile = new File(workspaceDir+"/org/openecomp/Action_mypol.xml");
- OutputStream out = new FileOutputStream(outFile);
- IOUtils.copy(in, out);
- filePath = outFile.getAbsolutePath();
- out.close();
-
- //action body file
- InputStream actionIn = new ByteArrayInputStream(jsonFile.getBytes());
- String webappDir = "src/test/resources/junitTestCreatedDirectory/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE);
- XACMLProperties.setProperty(XACMLRestProperties.PROP_PAP_WEBAPPS, webappDir);
- String actionDir = Webapps.getActionHome();
- FileUtils.forceMkdir(new File(actionDir));
- File actionOutFile = new File(actionDir+"/org.openecomp.Action_mypol.json");
- OutputStream actionOut = new FileOutputStream(actionOutFile);
- IOUtils.copy(actionIn, actionOut);
- actionOut.close();
-
- }catch(Exception e){
- //could not run test
- }
- PolicyDBDaoTransaction transac = dbd.getNewTransaction();
- if(filePath != null){
- try{
- transac.createPolicy(filePath, "tester");
- transac.commitTransaction();
- } catch(Exception e){
- Assert.fail();
- }
- EntityManager getData = emf.createEntityManager();
- Query getDataQuery = getData.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:name");
- getDataQuery.setParameter("scope", "org.openecomp");
- getDataQuery.setParameter("name","Action_mypol.xml");
- PolicyEntity result = null;
- try{
- result = (PolicyEntity)getDataQuery.getSingleResult();
- } catch(Exception e){
- logger.error("Exception Occured"+e);
- Assert.fail();
- }
- Assert.assertEquals(xmlFile, result.getPolicyData());
- getData.close();
- result = null;
- xmlFile = null;
- try{
- transac = dbd.getNewTransaction();
- transac.deletePolicy(filePath);
- } catch(Exception e){
- logger.error("Exception Occured"+e);
- Assert.fail();
- }
- Assert.assertTrue(transac.isTransactionOpen());
- try{
- transac.deletePolicy(filePath);
- Assert.fail();
- } catch(IllegalStateException e){
- //pass
- } catch(Exception e){
- Assert.fail();
- }
- transac.commitTransaction();
- //Assert.assertFalse(transac.isTransactionOpen());
- try{
- transac = dbd.getNewTransaction();
- transac.deletePolicy(filePath);
- } catch(Exception e){
- logger.error("Exception Occured"+e);
- Assert.fail();
- }
- transac.commitTransaction();
- //Assert.assertFalse(transac.isTransactionOpen());
- String workspaceDir = "src/test/resources/junitTestCreatedDirectory/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE)+"/admin/"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY);
- PolicyDBDaoTransaction willFail = dbd.getNewTransaction();
- File fakeFile = new File("directorythatdoesnotexist/"+workspaceDir);
- try{
- willFail.createPolicy(fakeFile.getAbsolutePath(), "user1");
- Assert.fail();
- } catch(IllegalArgumentException e){
- if(!e.getMessage().equals("The file path could not be parsed")){
- Assert.fail();
- }
- }
- willFail.close();
-
- fakeFile = new File("directorythatdoesnotexist/"+workspaceDir+"/Action_mypol2.xml");
- willFail = dbd.getNewTransaction();
- try{
- willFail.createPolicy(fakeFile.getAbsolutePath(), "user1");
- Assert.fail();
- } catch(IllegalArgumentException e){
- if(!e.getMessage().equals("The file path could not be parsed")){
- Assert.fail();
- }
- }
- willFail.close();
-
- fakeFile = new File("directorythatdoesnotexist/"+workspaceDir+"com/att/Action_mypol2.xml");
- willFail = dbd.getNewTransaction();
- try{
- willFail.createPolicy(fakeFile.getAbsolutePath(), "user1");
- Assert.fail();
- } catch(IllegalArgumentException e){
- if(!e.getMessage().equals("The file path does not exist")){
- Assert.fail();
- }
- }
- willFail.close();
-
- emf = Persistence.createEntityManagerFactory("testPU");
- EntityManager aem = emf.createEntityManager();
- Query actionQuery = aem.createQuery("SELECT a FROM ActionBodyEntity a WHERE a.actionBodyName=:actionBodyName");
- actionQuery.setParameter("actionBodyName", "org.openecomp.Action_mypol.json");
- List<?> actionQueryList = actionQuery.getResultList();
- if(actionQueryList.size() < 1){
- Assert.fail("ActionBodyEntity not found with actionBodyName=: org.openecomp.Action_mypol.json" );
- } else if(actionQueryList.size() > 1){
- //something went wrong
- Assert.fail("Somehow, more than one ActionBodyEntity with the actionBodyName = org.openecomp.Action_mypol.json");
- } else {
- ActionBodyEntity abe = (ActionBodyEntity)actionQueryList.get(0);
- logger.debug("\n\nPolicyDBDaoTest.transactionTests() Assert.assertEquals"
- + "\n abe.getActionBody() = " + abe.getActionBody()
- + "\n jsonFile = " + jsonFile
- + "\n\n");
- Assert.assertEquals(abe.getActionBody(),jsonFile);
- }
- }
- }
-