c57e3e97cf248e7ba8027f81dd35aeaf5e625d5f
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-persistence
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.drools.controller.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.util.Date;
28 import java.util.Properties;
29
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Persistence;
34
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;
55
56 /*
57  * Cloned from StandbyStateManagement.java in support of US673632.
58  * See MultiSite_v1-10.ppt, slide 38
59  */
60 public class IntegrityAuditIntegrationTest {
61                 
62         
63         public static final String INTEGRITY_MONITOR_PROPERTIES_FILE="src/test/server/config/IntegrityMonitor.properties";
64                 
65         /*
66          * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
67          */
68         private long interruptRecoveryTime = 5000;
69         
70         /*
71          * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
72          */
73         @BeforeClass
74         public static void setUpClass() throws Exception {
75                 
76                 PolicyLogger.info("setUpClass: Entering");
77
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");
82                                 
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;
86                 
87                 /*
88                  * Setting isUnitTesting to true ensures
89                  * 
90                  * 1) That we load test version of properties files
91                  * 
92                  * and
93                  * 
94                  * 2) that we use dbAuditSimulate() method, because all we care about
95                  * for this JUnit testing is that the audits are executed.
96                  */
97                 IntegrityAudit.isUnitTesting = true;
98                 
99                 initializeDb();
100                 
101                 PolicyLogger.info("setUpClass: Exiting");
102                 
103         }
104
105         @AfterClass
106         public static void tearDownClass() throws Exception {
107                                 
108         }
109
110         @Before
111         public void setUp() throws Exception {
112                 
113         }
114
115         @After
116         public void tearDown() throws Exception {
117                 
118         }
119
120         
121         /*
122          * Verifies that audit thread starts successfully.
123          */
124         //@Ignore
125         @Test
126         public void testAuditInit() throws Exception {
127                 
128                 PolicyLogger.debug("\n\ntestAuditInit: Entering\n\n");
129
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);
137                 
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);
143                 
144                 PolicyLogger.debug("testAuditInit: Creating emfXacml");
145                 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
146                                 "junitXacmlPU", xacmlPersistenceProperties);
147                 
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);
153
154                 PolicyLogger.debug("testAuditInit: Creating emfDrools");
155                 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
156                                 "junitDroolsPU", droolsPersistenceProperties);
157                 
158                 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
159                 
160                 PolicyLogger.debug("testAuditInit: Cleaning up tables");
161                 conn.deleteAllSessions();
162                 conn.deleteAllPdps();
163                 
164                 /*
165                  * Insert this PDP as designated.  Initial standby state will be 
166                  * either null or cold standby.   
167                  */
168                 PolicyLogger.debug("testAuditInit: Inserting PDP=" + thisPdpId + " as designated");
169                 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
170                 conn.insertPdp(pdp);
171                 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
172                 PolicyLogger.debug("testAuditInit: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
173                                 + droolsPdpEntity.isDesignated());
174                 assertTrue(droolsPdpEntity.isDesignated() == true);
175                 
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);
182                                 
183                 PolicyLogger.debug("testAuditInit: Running policy-management.Main class, designated="
184                                 + conn.getPdp(thisPdpId).isDesignated());
185                 PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
186                 policyManagementRunner.start();
187                 
188                 PolicyLogger.debug("testAuditInit: Runner started; Sleeping "
189                                 + interruptRecoveryTime + "ms before promoting PDP="
190                                 + thisPdpId);
191                 Thread.sleep(interruptRecoveryTime);
192                 
193                 IntegrityAudit integrityAudit = PersistenceFeature.getIntegrityAudit();
194                 PolicyLogger.debug("testAuditInit: isThreadInitialized=" + integrityAudit.isThreadInitialized());
195                 assertTrue("AuditThread not initialized!?",integrityAudit.isThreadInitialized());
196                                 
197                 PolicyLogger.debug("testAuditInit: Stopping auditThread");
198                 integrityAudit.stopAuditThread();
199                 Thread.sleep(1000);
200                 //This will interrupt thread.  However, the thread will not die.  It keeps on ticking and trying to
201                 //run the audit.
202                 assertTrue("AuditThread not still running after stopAuditThread invoked!?",integrityAudit.isThreadInitialized());
203
204                 PolicyLogger.debug("testAuditInit: Stopping policyManagementRunner");
205                 policyManagementRunner.stopRunner();
206                 
207                 PolicyLogger.debug("\n\ntestAuditInit: Exiting\n\n");
208                 Thread.sleep(interruptRecoveryTime);
209
210         }       
211         
212     /*
213      * This method initializes and cleans the DB so that PDP-D will be able to 
214      * store IntegrityAuditEntity in the DB.
215      */
216         public static void initializeDb(){
217                 
218                 PolicyLogger.debug("initializeDb: Entering");
219                 
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);
227                 
228                 EntityManager em = emf.createEntityManager();
229                 // Start a transaction
230                 EntityTransaction et = em.getTransaction();
231
232                 et.begin();
233
234                 // Clean up the DB
235                 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
236
237                 // commit transaction
238                 et.commit();
239                 em.close();
240                 
241                 PolicyLogger.debug("initializeDb: Exiting");
242         }       
243         
244         private class PolicyManagementRunner extends Thread {
245
246                 public void run() {
247                         PolicyLogger.info("PolicyManagementRunner.run: Entering");
248                         String args[] = { "src/main/server/config" };
249                         try {
250                                 Main.main(args);
251                         } catch (Exception e) {
252                                 PolicyLogger
253                                                 .info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
254                                                                 + e.getMessage());
255                         }
256                         PolicyLogger.info("PolicyManagementRunner.run: Exiting");
257                 }
258                 
259                 public void stopRunner() {
260                         PolicyEngine.manager.shutdown();
261                 }
262
263         }
264         
265 }