aboutsummaryrefslogtreecommitdiffstats
path: root/policy-persistence/src/test/java/org/openecomp/policy/drools/controller/test/IntegrityAuditIntegrationTest.java
blob: 6b6547328431ffe6eeb5aba549a97ac78caaeed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*-
 * ============LICENSE_START=======================================================
 * policy-persistence
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.drools.controller.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import org.openecomp.policy.common.logging.eelf.PolicyLogger;
import org.openecomp.policy.common.ia.IntegrityAudit;
import org.openecomp.policy.common.im.AdministrativeStateException;
import org.openecomp.policy.common.im.IntegrityMonitor;
import org.openecomp.policy.common.im.StandbyStatusException;
import org.openecomp.policy.common.im.StateManagement;
import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
import org.openecomp.policy.drools.core.PolicyContainer;
import org.openecomp.policy.drools.im.PMStandbyStateChangeNotifier;
import org.openecomp.policy.drools.persistence.DroolsPdpEntity;
import org.openecomp.policy.drools.persistence.DroolsPdpImpl;
import org.openecomp.policy.drools.persistence.DroolsPdpsConnector;
import org.openecomp.policy.drools.persistence.JpaDroolsPdpsConnector;
import org.openecomp.policy.drools.persistence.DroolsPersistenceProperties;
import org.openecomp.policy.drools.persistence.PersistenceFeature;
import org.openecomp.policy.drools.persistence.XacmlPersistenceProperties;
import org.openecomp.policy.drools.system.Main;
import org.openecomp.policy.drools.system.PolicyEngine;

/*
 * Cloned from StandbyStateManagement.java in support of US673632.
 * See MultiSite_v1-10.ppt, slide 38
 */
public class IntegrityAuditIntegrationTest {
		
	
	public static final String INTEGRITY_MONITOR_PROPERTIES_FILE="src/test/server/config/IntegrityMonitor.properties";

	/*
	 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every ten seconds, starting 
	 * at ten seconds after the minute boundary (e.g. 13:05:10). So, an 80 second sleep should be 
	 * sufficient to ensure that we wait for the DesignationWaiter to do its job, before 
	 * checking the results. 
	 */
	private long sleepTime = 80000;
		
	/*
	 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
	 */
	private long interruptRecoveryTime = 5000;
	
	/*
	 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
	 */
	@BeforeClass
	public static void setUpClass() throws Exception {
		
		PolicyLogger.info("setUpClass: Entering");

		String userDir = System.getProperty("user.dir");
		PolicyLogger.debug("setUpClass: userDir=" + userDir);
		System.setProperty("com.sun.management.jmxremote.port", "9980");
		System.setProperty("com.sun.management.jmxremote.authenticate","false");
				
		// Make sure path to config directory is set correctly in PolicyContainer.main
		// Also make sure we ignore HTTP server failures resulting from port conflicts.
		PolicyContainer.isUnitTesting = true;
		
		/*
		 * Setting isUnitTesting to true ensures
		 * 
		 * 1) That we load test version of properties files
		 * 
		 * and
		 * 
		 * 2) that we use dbAuditSimulate() method, because all we care about
		 * for this JUnit testing is that the audits are executed.
		 */
		IntegrityAudit.isUnitTesting = true;
		
		initializeDb();
		
		PolicyLogger.info("setUpClass: Exiting");
		
	}

	@AfterClass
	public static void tearDownClass() throws Exception {
				
	}

	@Before
	public void setUp() throws Exception {
		
	}

	@After
	public void tearDown() throws Exception {
		
	}

	
	/*
	 * Verifies that audit thread starts successfully.
	 */
	//@Ignore
	@Test
	public void testAuditInit() throws Exception {
		
		PolicyLogger.debug("\n\ntestAuditInit: Entering\n\n");

		PolicyLogger.debug("testAuditInit: Reading IntegrityMonitorProperties");
		Properties integrityMonitorProperties = new Properties();
		integrityMonitorProperties.load(new FileInputStream(new File(
				INTEGRITY_MONITOR_PROPERTIES_FILE)));
		IntegrityMonitorProperties.initProperties(integrityMonitorProperties);
		String thisPdpId = IntegrityMonitorProperties
				.getProperty(IntegrityMonitorProperties.PDP_INSTANCE_ID);
		
		PolicyLogger.debug("testAuditInit: Reading xacmlPersistenceProperties");
		Properties xacmlPersistenceProperties = new Properties();
		xacmlPersistenceProperties.load(new FileInputStream(new File(
				"src/test/server/config/xacmlPersistence.properties")));
		XacmlPersistenceProperties.initProperties(xacmlPersistenceProperties);
		
		PolicyLogger.debug("testAuditInit: Creating emfXacml");
		EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
				"junitXacmlPU", xacmlPersistenceProperties);
		
		PolicyLogger.debug("testAuditInit: Reading droolsPersistenceProperties");
		Properties droolsPersistenceProperties = new Properties();
		droolsPersistenceProperties.load(new FileInputStream(new File(
				"src/test/server/config/droolsPersistence.properties")));
		DroolsPersistenceProperties.initProperties(droolsPersistenceProperties);

		PolicyLogger.debug("testAuditInit: Creating emfDrools");
		EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
				"junitDroolsPU", droolsPersistenceProperties);
		
		DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
		
		PolicyLogger.debug("testAuditInit: Cleaning up tables");
		conn.deleteAllSessions();
		conn.deleteAllPdps();
		
		/*
		 * Insert this PDP as designated.  Initial standby state will be 
		 * either null or cold standby.   
		 */
		PolicyLogger.debug("testAuditInit: Inserting PDP=" + thisPdpId + " as designated");
		DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
		conn.insertPdp(pdp);
		DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
		PolicyLogger.debug("testAuditInit: After insertion, PDP=" + thisPdpId + " has DESIGNATED="
				+ droolsPdpEntity.isDesignated());
		assertTrue(droolsPdpEntity.isDesignated() == true);
		
		PolicyLogger.debug("testAuditInit: Instantiating stateManagement object");
		StateManagement sm = new StateManagement(emfXacml, "dummy");
		sm.deleteAllStateManagementEntities();
		sm = new StateManagement(emfXacml, thisPdpId);
		PMStandbyStateChangeNotifier pmStandbyStateChangeNotifier = new PMStandbyStateChangeNotifier();
		sm.addObserver(pmStandbyStateChangeNotifier);
				
		PolicyLogger.debug("testAuditInit: Running policy-management.Main class, designated="
				+ conn.getPdp(thisPdpId).isDesignated());
		PolicyManagementRunner policyManagementRunner = new PolicyManagementRunner();
		policyManagementRunner.start();
		
		PolicyLogger.debug("testAuditInit: Runner started; Sleeping "
				+ interruptRecoveryTime + "ms before promoting PDP="
				+ thisPdpId);
		Thread.sleep(interruptRecoveryTime);
		
		IntegrityAudit integrityAudit = PersistenceFeature.getIntegrityAudit();
		PolicyLogger.debug("testAuditInit: isThreadInitialized=" + integrityAudit.isThreadInitialized());
		assertTrue("AuditThread not initialized!?",integrityAudit.isThreadInitialized());
				
		PolicyLogger.debug("testAuditInit: Stopping auditThread");
		integrityAudit.stopAuditThread();
		Thread.sleep(1000);
		//This will interrupt thread.  However, the thread will not die.  It keeps on ticking and trying to
		//run the audit.
		assertTrue("AuditThread not still running after stopAuditThread invoked!?",integrityAudit.isThreadInitialized());

		PolicyLogger.debug("testAuditInit: Stopping policyManagementRunner");
		policyManagementRunner.stopRunner();
		
		PolicyLogger.debug("\n\ntestAuditInit: Exiting\n\n");
		Thread.sleep(interruptRecoveryTime);

	}	
	
    /*
     * This method initializes and cleans the DB so that PDP-D will be able to 
     * store IntegrityAuditEntity in the DB.
     */
	public static void initializeDb(){
		
		PolicyLogger.debug("initializeDb: Entering");
		
    	Properties cleanProperties = new Properties();
    	cleanProperties.put(DroolsPersistenceProperties.DB_DRIVER,"org.h2.Driver");
    	cleanProperties.put(DroolsPersistenceProperties.DB_URL, "jdbc:h2:file:./sql/drools");
    	cleanProperties.put(DroolsPersistenceProperties.DB_USER, "sa");
    	cleanProperties.put(DroolsPersistenceProperties.DB_PWD, "");
    	//EntityManagerFactory emf = Persistence.createEntityManagerFactory("schemaPU", cleanProperties);
    	EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitDroolsPU", cleanProperties);
		
		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();
		
		PolicyLogger.debug("initializeDb: Exiting");
	}	
	
	private class PolicyManagementRunner extends Thread {

		public void run() {
			PolicyLogger.info("PolicyManagementRunner.run: Entering");
			String args[] = { "src/main/server/config" };
			try {
				Main.main(args);
			} catch (Exception e) {
				PolicyLogger
						.info("PolicyManagementRunner.run: Exception thrown from Main.main(), message="
								+ e.getMessage());
			}
			PolicyLogger.info("PolicyManagementRunner.run: Exiting");
		}
		
		public void stopRunner() {
			PolicyEngine.manager.shutdown();
		}

	}
	
}