From c3140f4308f0752153fb279badc88b7d54094171 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 22 Mar 2018 23:16:50 +0000 Subject: [PATCH] Add database initialization to XACML PDP test This issue arose due to the order of execution of JUnit tests. The tests in XACMLPAPTest failed becasue the database was not initialized. Up till now, the @Before method in the test class org.onap.policy.pap.ia.DbAuditCompareEntriesTesti initiated the database. THis bug fix adds a similar @Before method to the XACMLPAPTest class to initialize the database. All other classes in the module were tested with both @Before methods commented out to ensure that no other tests will fail due to this issue. Issue-ID: POLICY-707 Change-Id: Ibce5fad5c006f7a92b3ef77bde4dc5ed6f7039ed Signed-off-by: liamfallon --- .../org/onap/policy/pap/test/XACMLPAPTest.java | 51 +++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java index ca2b6797d..5208ad7a7 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java @@ -35,18 +35,25 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import org.onap.policy.common.ia.IntegrityAuditProperties; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController; import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController; @@ -73,8 +80,8 @@ import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; import com.mockrunner.mock.web.MockServletInputStream; - public class XACMLPAPTest { + private static final Log logger = LogFactory.getLog(XACMLPAPTest.class); private static final String ENVIRONMENT_HEADER = "Environment"; private List headers = new ArrayList<>(); @@ -86,6 +93,48 @@ public class XACMLPAPTest { private SessionFactory sessionFactory; private CommonClassDao commonClassDao; + private static final String DEFAULT_DB_DRIVER = "org.h2.Driver"; + private static final String DEFAULT_DB_USER = "sa"; + private static final String DEFAULT_DB_PWD = ""; + + @Before + public void setUpDB() throws Exception { + logger.info("setUpDB: Entering"); + + Properties properties = new Properties(); + properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPAPTest.DEFAULT_DB_DRIVER); + properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest"); + properties.put(IntegrityAuditProperties.DB_USER, XACMLPAPTest.DEFAULT_DB_USER); + properties.put(IntegrityAuditProperties.DB_PWD, XACMLPAPTest.DEFAULT_DB_PWD); + properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA"); + properties.put(IntegrityAuditProperties.NODE_TYPE, "pap"); + + //Clean the iaTest DB table for IntegrityAuditEntity entries + cleanDb("testPapPU", properties); + + logger.info("setUpDB: Exiting"); + } + + public void cleanDb(String persistenceUnit, Properties properties){ + logger.debug("cleanDb: enter"); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + + EntityManager em = emf.createEntityManager(); + // Start a transaction + EntityTransaction et = em.getTransaction(); + + et.begin(); + + // Clean up the DB + em.createQuery("Delete from IntegrityAuditEntity").executeUpdate(); + + // commit transaction + et.commit(); + em.close(); + logger.debug("cleanDb: exit"); + } + @Before public void setUp() throws ServletException { httpServletRequest = Mockito.mock(HttpServletRequest.class); -- 2.16.6