ONAP-REST DAO Sonar/Checkstyle clean and knock-on
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / HandleIncomingNotificationsTest.java
index f979d1e..86a7af1 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP-PAP-REST
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.pap.xacml.rest.components;
 
-import static org.junit.Assert.fail;
+import com.att.research.xacml.api.pap.PAPException;
 import com.att.research.xacml.util.XACMLProperties;
-import java.nio.file.Path;
-import java.nio.file.Paths;
+
+import java.io.File;
+import java.io.IOException;
+
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
+
 import org.apache.commons.io.IOUtils;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
-import org.junit.Assert;
+import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.dao.PolicyDbException;
 import org.onap.policy.rest.jpa.GroupEntity;
 import org.onap.policy.rest.jpa.PdpEntity;
 import org.onap.policy.xacml.std.pap.StdEngine;
 
 public class HandleIncomingNotificationsTest {
-    private static PolicyDBDao dbd;
-    private static Path repository;
+    private static final String API = "API";
+    private static final String TWO = "2";
+    private static final String PDP = "pdp";
+    private static final String GROUP = "group";
+    private static final String ONE = "1";
+    private static PolicyDbDao dbd;
     private static StdEngine stdEngine = null;
     private static SessionFactory sessionFactory = null;
     private static HandleIncomingNotifications handleIncomingNotifications;
     private static GroupEntity groupEntity;
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
 
     /**
      * Sets the up before class.
@@ -53,59 +66,63 @@ public class HandleIncomingNotificationsTest {
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
-        try {
-            sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testHandleIncoming");
-            handleIncomingNotifications = new HandleIncomingNotifications(sessionFactory);
-        } catch (Exception e) {
-            Assert.fail();
-        }
-
-        PolicyDBDao.setJunit(true);
-        dbd = PolicyDBDao.getPolicyDBDaoInstance();
-        PolicyDBDao.setJunit(true);
-        repository = Paths.get("src/test/resources/pdps");
-        stdEngine = new StdEngine(repository);
-        dbd.setPapEngine(stdEngine);
+        sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testHandleIncoming");
+        handleIncomingNotifications = new HandleIncomingNotifications(sessionFactory);
+        PolicyDbDao.setJunit(true);
+        dbd = PolicyDbDao.getPolicyDbDaoInstance();
+        PolicyDbDao.setJunit(true);
         populateDb();
     }
 
+    /**
+     * Setup.
+     *
+     * @throws IOException Signals that an I/O exception has occurred.
+     * @throws PAPException the PAP exception
+     */
+    @Before
+    public void setup() throws IOException, PAPException {
+        File tmpGrpDir = folder.newFolder("src", "test", "resources", "pdps", "grpTest");
+        stdEngine = new StdEngine(tmpGrpDir.toPath());
+        dbd.setPapEngine(stdEngine);
+    }
 
     @Test
     public void testHandleIncomingHttpNotification() {
-        handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "group", null, null);
-        handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "group", null, null);
-        handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "policy", null, null);
-        handleIncomingNotifications.handleIncomingHttpNotification(null, "1", "pdp", null, null);
-        populatePdpEntityDb("2", groupEntity);
-        handleIncomingNotifications.handleIncomingHttpNotification(null, "2", "pdp", null, null);
+        handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, GROUP, null, null);
+        handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, GROUP, null, null);
+        handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, "policy", null, null);
+        handleIncomingNotifications.handleIncomingHttpNotification(null, ONE, PDP, null, null);
+        populatePdpEntityDb(TWO, groupEntity);
+        handleIncomingNotifications.handleIncomingHttpNotification(null, TWO, PDP, null, null);
     }
 
-    private static void populateDb() {
+    private static void populateDb() throws PolicyDbException, IOException {
         groupEntity = new GroupEntity();
-        groupEntity.setCreatedBy("API");
+        groupEntity.setCreatedBy(API);
         groupEntity.setDefaultGroup(false);
         groupEntity.setDeleted(false);
         groupEntity.setDescription("a test group");
-        groupEntity.setGroupId("1");
-        groupEntity.setGroupName("1");
+        groupEntity.setGroupId(ONE);
+        groupEntity.setGroupName(ONE);
         groupEntity.prePersist();
         Session session = sessionFactory.openSession();
         session.getTransaction().begin();
         session.persist(groupEntity);
         session.getTransaction().commit();
         session.close();
-        populatePdpEntityDb("1", groupEntity);
+        populatePdpEntityDb(ONE, groupEntity);
         populatePolicyInDb();
     }
 
     private static void populatePdpEntityDb(String pdpId, GroupEntity groupEntity) {
         PdpEntity pdpEntity = new PdpEntity();
-        pdpEntity.setCreatedBy("API");
+        pdpEntity.setCreatedBy(API);
         pdpEntity.setDeleted(false);
         pdpEntity.setDescription("test pdp");
         pdpEntity.setGroup(groupEntity);
         pdpEntity.setJmxPort(9993);
-        pdpEntity.setModifiedBy("API");
+        pdpEntity.setModifiedBy(API);
         pdpEntity.setPdpId(pdpId);
         pdpEntity.setPdpName("grouptest");
         pdpEntity.prePersist();
@@ -117,7 +134,7 @@ public class HandleIncomingNotificationsTest {
         session.close();
     }
 
-    private static void populatePolicyInDb() {
+    private static void populatePolicyInDb() throws PolicyDbException, IOException {
         Policy policyObject = new ConfigPolicy();
         policyObject.policyAdapter = new PolicyRestAdapter();
         policyObject.policyAdapter.setConfigName("testpolicyhandle");
@@ -127,30 +144,23 @@ public class HandleIncomingNotificationsTest {
         policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
         policyObject.policyAdapter.setPolicyType("Config");
         policyObject.policyAdapter.setDomainDir("com");
-        policyObject.policyAdapter.setVersion("1");
+        policyObject.policyAdapter.setVersion(ONE);
         policyObject.policyAdapter.setHighestVersion(1);
         PolicyType policyTypeObject = new PolicyType();
         policyObject.policyAdapter.setPolicyData(policyTypeObject);
-        ClassLoader classLoader = HandleIncomingNotificationsTest.class.getClassLoader();
+
         PolicyType policyConfig = new PolicyType();
-        policyConfig.setVersion("1");
+        policyConfig.setVersion(ONE);
         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();
-        }
+        ClassLoader classLoader = HandleIncomingNotificationsTest.class.getClassLoader();
+        policyObject.policyAdapter
+                .setParentPath(IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml")));
+
+        PolicyDbDaoTransaction transaction = dbd.getNewTransaction();
+        transaction.createPolicy(policyObject, "testuser1");
+        transaction.commitTransaction();
+
     }
 }