2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.drools.controller.test;
23 import static org.junit.Assert.assertTrue;
26 import java.io.FileInputStream;
27 import java.util.Date;
28 import java.util.Properties;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Persistence;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.openecomp.policy.common.ia.IntegrityAudit;
41 import org.openecomp.policy.common.im.StateManagement;
42 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
43 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
44 import org.openecomp.policy.drools.core.PolicyContainer;
45 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
46 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
47 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
48 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
49 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
50 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
51 import org.openecomp.policy.drools.persistence.PersistenceFeature;
52 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
53 import org.openecomp.policy.drools.system.Main;
54 import org.openecomp.policy.drools.system.PolicyEngine;
57 * Cloned from StandbyStateManagement.java in support of US673632.
58 * See MultiSite_v1-10.ppt, slide 38
60 public class IntegrityAuditIntegrationTest {
63 public static final String INTEGRITY_MONITOR_PROPERTIES_FILE="src/test/server/config/IntegrityMonitor.properties";
66 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
68 private long interruptRecoveryTime = 5000;
71 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
74 public static void setUpClass() throws Exception {
76 PolicyLogger.info("setUpClass: Entering");
78 String userDir = System.getProperty("user.dir");
79 PolicyLogger.debug("setUpClass: userDir=" + userDir);
80 System.setProperty("com.sun.management.jmxremote.port", "9980");
81 System.setProperty("com.sun.management.jmxremote.authenticate","false");
83 // Make sure path to config directory is set correctly in PolicyContainer.main
84 // Also make sure we ignore HTTP server failures resulting from port conflicts.
85 PolicyContainer.isUnitTesting = true;
88 * Setting isUnitTesting to true ensures
90 * 1) That we load test version of properties files
94 * 2) that we use dbAuditSimulate() method, because all we care about
95 * for this JUnit testing is that the audits are executed.
97 IntegrityAudit.isUnitTesting = true;
101 PolicyLogger.info("setUpClass: Exiting");
106 public static void tearDownClass() throws Exception {
111 public void setUp() throws Exception {
116 public void tearDown() throws Exception {
122 * Verifies that audit thread starts successfully.
126 public void testAuditInit() throws Exception {
128 PolicyLogger.debug("\n\ntestAuditInit: Entering\n\n");
130 PolicyLogger.debug("testAuditInit: Reading IntegrityMonitorProperties");
131 Properties integrityMonitorProperties = new Properties();
132 integrityMonitorProperties.load(new FileInputStream(new File(
133 INTEGRITY_MONITOR_PROPERTIES_FILE)));
134 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
135 String thisPdpId = IntegrityMonitorProperties
136 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
138 PolicyLogger.debug("testAuditInit: Reading xacmlPersistenceProperties");
139 Properties xacmlPersistenceProperties = new Properties();
140 xacmlPersistenceProperties.load(new FileInputStream(new File(
141 "src/test/server/config/xacmlPersistence.properties")));
142 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
144 PolicyLogger.debug("testAuditInit: Creating emfXacml");
145 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
146 "junitXacmlPU", xacmlPersistenceProperties);
148 PolicyLogger.debug("testAuditInit: Reading droolsPersistenceProperties");
149 Properties droolsPersistenceProperties = new Properties();
150 droolsPersistenceProperties.load(new FileInputStream(new File(
151 "src/test/server/config/droolsPersistence.properties")));
152 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
154 PolicyLogger.debug("testAuditInit: Creating emfDrools");
155 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
156 "junitDroolsPU", droolsPersistenceProperties);
158 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
160 PolicyLogger.debug("testAuditInit: Cleaning up tables");
161 conn.deleteAllSessions();
162 conn.deleteAllPdps();
165 * Insert this PDP as designated. Initial standby state will be
166 * either null or cold standby.
168 PolicyLogger.debug("testAuditInit: Inserting PDP=" + thisPdpId + " as designated");
169 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
171 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
172 PolicyLogger.debug("testAuditInit: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
173 + droolsPdpEntity.isDesignated());
174 assertTrue(droolsPdpEntity.isDesignated() == true);
176 PolicyLogger.debug("testAuditInit: Instantiating stateManagement object");
177 StateManagement sm = new StateManagement(emfXacml, "dummy");
178 sm.deleteAllStateManagementEntities();
179 sm = new StateManagement(emfXacml, thisPdpId);
180 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
181 sm.addObserver(pmStandbyStateChangeNotifier);
183 PolicyLogger.debug("testAuditInit: Running policy-management.Main class, designated="
184 + conn.getPdp(thisPdpId).isDesignated());
185 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
186 policyManagementRunner.start();
188 PolicyLogger.debug("testAuditInit: Runner started; Sleeping "
189 + interruptRecoveryTime + "ms before promoting PDP="
191 Thread.sleep(interruptRecoveryTime);
193 IntegrityAudit integrityAudit = PersistenceFeature.getIntegrityAudit();
194 PolicyLogger.debug("testAuditInit: isThreadInitialized=" + integrityAudit.isThreadInitialized());
195 assertTrue("AuditThread not initialized!?",integrityAudit.isThreadInitialized());
197 PolicyLogger.debug("testAuditInit: Stopping auditThread");
198 integrityAudit.stopAuditThread();
200 //This will interrupt thread. However, the thread will not die. It keeps on ticking and trying to
202 assertTrue("AuditThread not still running after stopAuditThread invoked!?",integrityAudit.isThreadInitialized());
204 PolicyLogger.debug("testAuditInit: Stopping policyManagementRunner");
205 policyManagementRunner.stopRunner();
207 PolicyLogger.debug("\n\ntestAuditInit: Exiting\n\n");
208 Thread.sleep(interruptRecoveryTime);
213 * This method initializes and cleans the DB so that PDP-D will be able to
214 * store IntegrityAuditEntity in the DB.
216 public static void initializeDb(){
218 PolicyLogger.debug("initializeDb: Entering");
220 Properties cleanProperties = new Properties();
221 cleanProperties.put(DroolsPersistenceProperties.DB_DRIVER,"org.h2.Driver");
222 cleanProperties.put(DroolsPersistenceProperties.DB_URL, "jdbc:h2:file:./sql/drools");
223 cleanProperties.put(DroolsPersistenceProperties.DB_USER, "sa");
224 cleanProperties.put(DroolsPersistenceProperties.DB_PWD, "");
225 //EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaPU", cleanProperties);
226 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitDroolsPU", cleanProperties);
228 EntityManager em = emf.createEntityManager();
229 // Start a transaction
230 EntityTransaction et = em.getTransaction();
235 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
237 // commit transaction
241 PolicyLogger.debug("initializeDb: Exiting");
244 private class PolicyManagementRunner extends Thread {
247 PolicyLogger.info("PolicyManagementRunner.run: Entering");
248 String args[] = { "src/main/server/config" };
251 } catch (Exception e) {
253 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
256 PolicyLogger.info("PolicyManagementRunner.run: Exiting");
259 public void stopRunner() {
260 PolicyEngine.manager.shutdown();