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.Ignore;
40 import org.junit.Test;
42 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
43 import org.openecomp.policy.common.ia.IntegrityAudit;
44 import org.openecomp.policy.common.im.AdministrativeStateException;
45 import org.openecomp.policy.common.im.IntegrityMonitor;
46 import org.openecomp.policy.common.im.StandbyStatusException;
47 import org.openecomp.policy.common.im.StateManagement;
48 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
49 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
50 import org.openecomp.policy.drools.core.PolicyContainer;
51 import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
52 import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
53 import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
54 import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
55 import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
56 import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
57 import org.openecomp.policy.drools.persistence.PersistenceFeature;
58 import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
59 import org.openecomp.policy.drools.system.Main;
60 import org.openecomp.policy.drools.system.PolicyEngine;
63 * Cloned from StandbyStateManagement.java in support of US673632.
64 * See MultiSite_v1-10.ppt, slide 38
66 public class IntegrityAuditIntegrationTest {
69 public static final String INTEGRITY_MONITOR_PROPERTIES_FILE="src/test/server/config/IntegrityMonitor.properties";
72 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting
73 * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be
74 * sufficient to ensure that we wait for the DesignationWaiter to do its job, before
75 * checking the results.
77 private long sleepTime = 80000;
80 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
82 private long interruptRecoveryTime = 5000;
85 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
88 public static void setUpClass() throws Exception {
90 PolicyLogger.info("setUpClass: Entering");
92 String userDir = System.getProperty("user.dir");
93 PolicyLogger.debug("setUpClass: userDir=" + userDir);
94 System.setProperty("com.sun.management.jmxremote.port", "9980");
95 System.setProperty("com.sun.management.jmxremote.authenticate","false");
97 // Make sure path to config directory is set correctly in PolicyContainer.main
98 // Also make sure we ignore HTTP server failures resulting from port conflicts.
99 PolicyContainer.isUnitTesting = true;
102 * Setting isUnitTesting to true ensures
104 * 1) That we load test version of properties files
108 * 2) that we use dbAuditSimulate() method, because all we care about
109 * for this JUnit testing is that the audits are executed.
111 IntegrityAudit.isUnitTesting = true;
115 PolicyLogger.info("setUpClass: Exiting");
120 public static void tearDownClass() throws Exception {
125 public void setUp() throws Exception {
130 public void tearDown() throws Exception {
136 * Verifies that audit thread starts successfully.
140 public void testAuditInit() throws Exception {
142 PolicyLogger.debug("\n\ntestAuditInit: Entering\n\n");
144 PolicyLogger.debug("testAuditInit: Reading IntegrityMonitorProperties");
145 Properties integrityMonitorProperties = new Properties();
146 integrityMonitorProperties.load(new FileInputStream(new File(
147 INTEGRITY_MONITOR_PROPERTIES_FILE)));
148 IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
149 String thisPdpId = IntegrityMonitorProperties
150 .getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
152 PolicyLogger.debug("testAuditInit: Reading xacmlPersistenceProperties");
153 Properties xacmlPersistenceProperties = new Properties();
154 xacmlPersistenceProperties.load(new FileInputStream(new File(
155 "src/test/server/config/xacmlPersistence.properties")));
156 XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
158 PolicyLogger.debug("testAuditInit: Creating emfXacml");
159 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
160 "junitXacmlPU", xacmlPersistenceProperties);
162 PolicyLogger.debug("testAuditInit: Reading droolsPersistenceProperties");
163 Properties droolsPersistenceProperties = new Properties();
164 droolsPersistenceProperties.load(new FileInputStream(new File(
165 "src/test/server/config/droolsPersistence.properties")));
166 DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);
168 PolicyLogger.debug("testAuditInit: Creating emfDrools");
169 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
170 "junitDroolsPU", droolsPersistenceProperties);
172 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
174 PolicyLogger.debug("testAuditInit: Cleaning up tables");
175 conn.deleteAllSessions();
176 conn.deleteAllPdps();
179 * Insert this PDP as designated. Initial standby state will be
180 * either null or cold standby.
182 PolicyLogger.debug("testAuditInit: Inserting PDP=" + thisPdpId + " as designated");
183 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
185 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
186 PolicyLogger.debug("testAuditInit: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
187 + droolsPdpEntity.isDesignated());
188 assertTrue(droolsPdpEntity.isDesignated() == true);
190 PolicyLogger.debug("testAuditInit: Instantiating stateManagement object");
191 StateManagement sm = new StateManagement(emfXacml, "dummy");
192 sm.deleteAllStateManagementEntities();
193 sm = new StateManagement(emfXacml, thisPdpId);
194 PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
195 sm.addObserver(pmStandbyStateChangeNotifier);
197 PolicyLogger.debug("testAuditInit: Running policy-management.Main class, designated="
198 + conn.getPdp(thisPdpId).isDesignated());
199 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
200 policyManagementRunner.start();
202 PolicyLogger.debug("testAuditInit: Runner started; Sleeping "
203 + interruptRecoveryTime + "ms before promoting PDP="
205 Thread.sleep(interruptRecoveryTime);
207 IntegrityAudit integrityAudit = PersistenceFeature.getIntegrityAudit();
208 PolicyLogger.debug("testAuditInit: isThreadInitialized=" + integrityAudit.isThreadInitialized());
209 assertTrue("AuditThread not initialized!?",integrityAudit.isThreadInitialized());
211 PolicyLogger.debug("testAuditInit: Stopping auditThread");
212 integrityAudit.stopAuditThread();
214 //This will interrupt thread. However, the thread will not die. It keeps on ticking and trying to
216 assertTrue("AuditThread not still running after stopAuditThread invoked!?",integrityAudit.isThreadInitialized());
218 PolicyLogger.debug("testAuditInit: Stopping policyManagementRunner");
219 policyManagementRunner.stopRunner();
221 PolicyLogger.debug("\n\ntestAuditInit: Exiting\n\n");
222 Thread.sleep(interruptRecoveryTime);
227 * This method initializes and cleans the DB so that PDP-D will be able to
228 * store IntegrityAuditEntity in the DB.
230 public static void initializeDb(){
232 PolicyLogger.debug("initializeDb: Entering");
234 Properties cleanProperties = new Properties();
235 cleanProperties.put(DroolsPersistenceProperties.DB_DRIVER,"org.h2.Driver");
236 cleanProperties.put(DroolsPersistenceProperties.DB_URL, "jdbc:h2:file:./sql/drools");
237 cleanProperties.put(DroolsPersistenceProperties.DB_USER, "sa");
238 cleanProperties.put(DroolsPersistenceProperties.DB_PWD, "");
239 //EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaPU", cleanProperties);
240 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitDroolsPU", cleanProperties);
242 EntityManager em = emf.createEntityManager();
243 // Start a transaction
244 EntityTransaction et = em.getTransaction();
249 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
251 // commit transaction
255 PolicyLogger.debug("initializeDb: Exiting");
258 private class PolicyManagementRunner extends Thread {
261 PolicyLogger.info("PolicyManagementRunner.run: Entering");
262 String args[] = { "src/main/server/config" };
265 } catch (Exception e) {
267 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
270 PolicyLogger.info("PolicyManagementRunner.run: Exiting");
273 public void stopRunner() {
274 PolicyEngine.manager.shutdown();