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.common.ia.test;
23 import static org.junit.Assert.*;
25 import java.io.BufferedReader;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Properties;
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Persistence;
40 //import org.apache.commons.logging.Log;
41 //import org.apache.commons.logging.LogFactory;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Ignore;
45 import org.junit.Test;
47 import org.openecomp.policy.common.ia.AuditThread;
48 import org.openecomp.policy.common.ia.DbDAO;
49 import org.openecomp.policy.common.ia.IntegrityAudit;
50 import org.openecomp.policy.common.ia.IntegrityAuditProperties;
51 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
52 import org.openecomp.policy.common.logging.flexlogger.Logger;
54 public class IntegrityAuditDesignationTest {
56 private static Logger logger = FlexLogger.getLogger(IntegrityAuditDesignationTest.class);
59 * Provides a little cushion for timing events.
61 private static int FUDGE_FACTOR = 5000;
63 private static String persistenceUnit;
64 private static Properties properties;
65 private static String resourceName;
66 private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log";
68 public void setUp() throws Exception {
71 System.out.println("setUp: Clearing debug.log");
72 FileOutputStream fstream = new FileOutputStream(TEST_LOG);
75 logger.info("setUp: Entering");
77 IntegrityAudit.isUnitTesting = true;
79 properties = new Properties();
80 properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
81 properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
82 properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
83 properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
84 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
85 properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
88 * AuditThread.AUDIT_THREAD_SLEEP_INTERVAL is also five seconds, so
89 * setting AUDIT_PERIOD_SECONDS to 5 ensures that whether or not audit
90 * has already been run on a node, it will sleep the same amount of
93 properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
95 persistenceUnit = "testPU";
96 resourceName = "pdp1";
100 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
102 EntityManager em = emf.createEntityManager();
103 // Start a transaction
104 EntityTransaction et = em.getTransaction();
108 // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry
109 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
111 // commit transaction
114 logger.info("setUp: Exiting");
120 public void tearDown() throws Exception {
122 logger.info("tearDown: Entering");
124 logger.info("tearDown: Exiting");
129 * Tests designation logic when only one functioning resource is in play. Designation
130 * should stay with single resource.
132 * Note: console.log must be examined to ascertain whether or not this test was successful.
136 public void testOneResource() throws Exception {
138 logger.info("testOneResource: Entering");
140 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
141 integrityAudit.startAuditThread();
144 * Sleep long enough to allow
146 * 1) pdp1 to run audit (15 seconds)
148 * 2) Logic to detect that no other node is available for designation (60 seconds)
150 * 3) pdp1 to run audit again (15 seconds)
152 logger.info("testOneResource: Sleeping 100 seconds");
153 Thread.sleep(100000);
155 logger.info("testOneResource: Stopping audit thread");
156 integrityAudit.stopAuditThread();
158 FileInputStream fstream = new FileInputStream(TEST_LOG);
159 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
165 while ((strLine = br.readLine()) != null) {
166 // parse strLine to obtain what you want
167 if (strLine.contains("Starting audit simulation for resourceName=")) {
168 startIndex = strLine.indexOf("resourceName=") + 13;
169 endIndex = strLine.indexOf(",");
170 rName = strLine.substring(startIndex, endIndex);
171 logger.info("testOneResource: rName: " + rName);
172 assertEquals("pdp1", rName);
178 * Test fix for ECOMPD2TD-783: Audit fails to run when application is restarted.
180 integrityAudit.startAuditThread();
183 * Sleep long enough to allow
185 * 1) pdp1 to run audit (15 seconds)
187 * 2) Logic to detect that no other node is available for designation (60 seconds)
189 * 3) pdp1 to run audit again (15 seconds)
191 logger.info("testOneResource: Sleeping 100 seconds for second time");
192 Thread.sleep(100000);
194 logger.info("testOneResource: Stopping audit thread for second time");
195 integrityAudit.stopAuditThread();
197 fstream = new FileInputStream(TEST_LOG);
198 br = new BufferedReader(new InputStreamReader(fstream));
201 while ((strLine = br.readLine()) != null) {
202 // parse strLine to obtain what you want
203 if (strLine.contains("Starting audit simulation for resourceName=")) {
204 startIndex = strLine.indexOf("resourceName=") + 13;
205 endIndex = strLine.indexOf(",");
206 rName = strLine.substring(startIndex, endIndex);
207 logger.info("testOneResource: rName: " + rName);
208 assertEquals("pdp1", rName);
213 logger.info("testOneResource: Exiting");
218 * Tests designation logic when two functioning resources are in play.
219 * Designation should alternate between resources.
221 * Note: console.log must be examined to ascertain whether or not this test
222 * was successful. A quick way of examining the log is to search for the
223 * string "audit simulation":
225 * As you can see from the "dbAuditSimulate" method, when it executes, it
226 * logs the "Starting audit simulation..." message and when it finishes, it
227 * logs the "Finished audit simulation..." message. By looking for these
228 * messages, you can verify that the audits are run by the proper resource.
229 * For example, when testFourResourcesOneDead is run, you should see a
230 * Starting.../Finished... sequence for pdp1, followed by a
231 * Starting.../Finished... sequence for pdp2, followed by a
232 * Starting.../Finished... sequence for pdp4 (pdp3 is skipped as it's
233 * dead/hung), followed by a Starting.../Finished... sequence for pdp1, etc.
237 public void testTwoResources() throws Exception {
239 logger.info("testTwoResources: Entering");
242 * Start audit for pdp1.
244 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
245 integrityAudit.startAuditThread();
248 * Start audit for pdp2.
250 Properties properties2 = new Properties();
251 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
252 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
253 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
254 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
255 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
256 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
257 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
258 String persistenceUnit2 = "testPU";
259 String resourceName2 = "pdp2";
260 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
261 integrityAudit2.startAuditThread();
264 * Sleep long enough to allow
266 * 1) pdp1 to run audit (15 seconds)
268 * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
270 * 3) pdp2 to run audit (15 seconds)
272 * 4) Logic to detect that pdp2 is stale and designate pdp1 (30 seconds)
274 * 5) pdp1 to run audit (15 seconds)
276 Thread.sleep(120000);
278 logger.info("testTwoResources: Stopping audit threads");
279 integrityAudit.stopAuditThread();
280 integrityAudit2.stopAuditThread();
282 FileInputStream fstream = new FileInputStream(TEST_LOG);
283 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
287 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp1"));
288 ArrayList<String> delegates = new ArrayList<String>();
289 while ((strLine = br.readLine()) != null) {
290 /* parse strLine to obtain what you want */
291 if (strLine.contains("Starting audit simulation for resourceName=")) {
292 startIndex = strLine.indexOf("resourceName=") + 13;
293 endIndex = strLine.indexOf(",");
295 String rName = strLine.substring(startIndex, endIndex);
297 delegates.add(rName);
301 for (String delegate: delegates) {
302 logger.info("testTwoResources: delegate: " + delegate);
307 assertTrue(expectedResult.equals(delegates));
309 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3);
310 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
311 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
312 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
314 logger.info("testTwoResources: Exiting");
319 * Tests designation logic when two functioning resources are in play, each
320 * with different PUs. Audits for "testPU" and "integrityAuditPU" should run
321 * simultaneously. Designation should not alternate.
323 * Note: console.log must be examined to ascertain whether or not this test
328 public void testTwoResourcesDifferentPus() throws Exception {
330 logger.info("testTwoResourcesDifferentPus: Entering");
333 * Start audit for pdp1.
335 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
336 integrityAudit.startAuditThread();
339 * Start audit for pdp2.
341 Properties properties2 = new Properties();
342 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
343 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
344 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
345 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
346 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
347 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
348 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
349 String persistenceUnit2 = "integrityAuditPU";
350 String resourceName2 = "pdp2";
351 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
352 integrityAudit2.startAuditThread();
355 * Sleep long enough to allow
357 * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
359 * 2) Logic to detect that no other node is available for designation for either pdp1 or pdp2 (60 seconds)
361 * 3) pdp1 and pdp2 to again run audit simultaneously (15 seconds)
363 * NOTE: Based on the above, you would think a 100000ms sleep would be appropriate,
364 * but for some reason, when all tests are run this test errors.
366 logger.info("testTwoResourcesDifferentPus: Sleeping 80 seconds");
367 Thread.sleep(100000);
369 logger.info("testTwoResourcesDifferentPus: Stopping audit threads");
370 integrityAudit.stopAuditThread();
371 integrityAudit2.stopAuditThread();
373 FileInputStream fstream = new FileInputStream(TEST_LOG);
374 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
378 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp1", "pdp2"));
379 ArrayList<String> delegates = new ArrayList<String>();
380 while ((strLine = br.readLine()) != null) {
381 /* parse strLine to obtain what you want */
382 if (strLine.contains("Starting audit simulation for resourceName=")) {
383 startIndex = strLine.indexOf("resourceName=") + 13;
384 endIndex = strLine.indexOf(",");
386 String rName = strLine.substring(startIndex, endIndex);
388 delegates.add(rName);
392 for (String delegate: delegates) {
393 logger.info("testTwoResourcesDifferentPus: delegate: " + delegate);
398 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4);
399 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
400 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
401 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
402 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
404 assertTrue(expectedResult.equals(delegates));
406 logger.info("testTwoResourcesDifferentPus: Exiting");
412 * Tests designation logic when two resources are in play but one of them is
413 * dead/hung. Designation should move to second resource but then get
414 * restored back to original resource when it's discovered that second
417 * Note: console.log must be examined to ascertain whether or not this test
422 public void testTwoResourcesOneDead() throws Exception {
424 logger.info("testTwoResourcesOneDead: Entering");
427 * Start audit for pdp1.
429 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
430 integrityAudit.startAuditThread();
433 * Populate DB for pdp2, which will simulate it having registered but then having died.
435 Properties properties2 = new Properties();
436 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
437 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
438 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
439 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
440 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
441 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
442 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
443 String persistenceUnit2 = "testPU";
444 String resourceName2 = "pdp2";
445 new DbDAO(resourceName2, persistenceUnit2, properties2);
448 * Sleep long enough to allow
450 * 1) pdp1 to run audit (15 seconds)
452 * 2) Logic to detect that other node, pdp2, is not available for designation (60 seconds)
454 * 3) pdp1 to run audit again (15 seconds)
456 logger.info("testTwoResourcesOneDead: Sleeping 100 seconds");
457 Thread.sleep(100000);
459 logger.info("testTwoResourcesOneDead: Stopping audit thread");
460 integrityAudit.stopAuditThread();
462 FileInputStream fstream = new FileInputStream(TEST_LOG);
463 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
467 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp1"));
468 ArrayList<String> delegates = new ArrayList<String>();
469 while ((strLine = br.readLine()) != null) {
470 /* parse strLine to obtain what you want */
471 if (strLine.contains("Starting audit simulation for resourceName=")) {
472 startIndex = strLine.indexOf("resourceName=") + 13;
473 endIndex = strLine.indexOf(",");
475 String rName = strLine.substring(startIndex, endIndex);
477 delegates.add(rName);
481 for (String delegate: delegates) {
482 logger.info("testTwoResourcesOneDead: delegate: " + delegate);
487 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2);
488 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
489 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
491 logger.info("testTwoResourcesOneDead: Exiting");
497 * Tests designation logic when three functioning resources are in play. Designation should
498 * round robin among resources.
500 * Note: console.log must be examined to ascertain whether or not this test was successful.
504 public void testThreeResources() throws Exception {
506 logger.info("testThreeResources: Entering");
509 * Start audit for pdp1.
511 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
512 integrityAudit.startAuditThread();
515 * Start audit for pdp2.
517 Properties properties2 = new Properties();
518 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
519 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
520 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
521 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
522 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
523 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
524 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
525 String persistenceUnit2 = "testPU";
526 String resourceName2 = "pdp2";
527 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
528 integrityAudit2.startAuditThread();
531 * Start audit for pdp3.
533 Properties properties3 = new Properties();
534 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
535 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
536 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
537 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
538 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
539 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
540 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
541 String persistenceUnit3 = "testPU";
542 String resourceName3 = "pdp3";
543 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
544 integrityAudit3.startAuditThread();
547 * Sleep long enough to allow
549 * 1) pdp1 to run audit (15 seconds)
551 * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
553 * 3) pdp2 to run audit (15 seconds)
555 * 4) Logic to detect that pdp2 is stale and designate pdp3 (30 seconds)
557 * 5) pdp3 to run audit (15 seconds)
559 * 6) Logic to detect that pdp3 is stale and designate pdp1 (30 seconds)
561 * 7) pdp1 to run audit (15 seconds)
563 logger.info("testThreeResources: Sleeping 160 seconds");
564 Thread.sleep(160000);
566 logger.info("testThreeResources: Stopping threads");
567 integrityAudit.stopAuditThread();
568 integrityAudit2.stopAuditThread();
569 integrityAudit3.stopAuditThread();
571 FileInputStream fstream = new FileInputStream(TEST_LOG);
572 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
576 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1"));
577 ArrayList<String> delegates = new ArrayList<String>();
578 while ((strLine = br.readLine()) != null) {
579 /* parse strLine to obtain what you want */
580 if (strLine.contains("Starting audit simulation for resourceName=")) {
581 startIndex = strLine.indexOf("resourceName=") + 13;
582 endIndex = strLine.indexOf(",");
584 String rName = strLine.substring(startIndex, endIndex);
586 delegates.add(rName);
590 for (String delegate: delegates) {
591 logger.info("testThreeResources: delegate: " + delegate);
596 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3);
597 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
598 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
599 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
600 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
602 logger.info("testThreeResources: Exiting");
607 * Tests designation logic when four functioning resources are in play, two
608 * with one PU, two with another. Audits for "testPU" and "integrityAuditPU" should run
609 * simultaneously. Designation should alternate between resources for each of the two
612 * Note: console.log must be examined to ascertain whether or not this test
617 public void testFourResourcesDifferentPus() throws Exception {
619 logger.info("testFourResourcesDifferentPus: Entering");
622 * Start audit for pdp1, testPU.
624 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
625 integrityAudit.startAuditThread();
628 * Start audit for pdp2, integrityAuditPU.
630 Properties properties2 = new Properties();
631 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
632 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
633 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
634 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
635 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
636 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
637 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
638 String persistenceUnit2 = "integrityAuditPU";
639 String resourceName2 = "pdp2";
640 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
641 integrityAudit2.startAuditThread();
644 * Start audit for pdp3, testPU.
646 Properties properties3 = new Properties();
647 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
648 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
649 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
650 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
651 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
652 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
653 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
654 String persistenceUnit3 = "testPU";
655 String resourceName3 = "pdp3";
656 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
657 integrityAudit3.startAuditThread();
660 * Start audit for pdp4, integrityAuditPU.
662 Properties properties4 = new Properties();
663 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
664 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
665 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
666 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
667 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
668 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
669 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
670 String persistenceUnit4 = "integrityAuditPU";
671 String resourceName4 = "pdp4";
672 IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4);
673 integrityAudit4.startAuditThread();
676 * Sleep long enough to allow
678 * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
680 * 2) Logic to detect that pdp1 and pdp2 are stale and designate pdp3 (one's counterpart) and pdp4 (two's counterpart) (30 seconds)
682 * 3) pdp3 and pdp4 to run audit simultaneously (15 seconds)
684 * 4) Logic to detect that pdp3 and pdp4 are stale and designate pdp1 (three's counterpart) and pdp2 (four's counterpart) (30 seconds)
686 * 5) pdp1 and pdp2 to run audit simultaneously (15 seconds)
688 logger.info("testFourResourcesDifferentPus: Sleeping 120 seconds");
689 Thread.sleep(120000);
691 logger.info("testFourResourcesDifferentPus: Stopping threads");
692 integrityAudit.stopAuditThread();
693 integrityAudit2.stopAuditThread();
694 integrityAudit3.stopAuditThread();
695 integrityAudit4.stopAuditThread();
697 FileInputStream fstream = new FileInputStream(TEST_LOG);
698 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
702 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp4", "pdp1", "pdp2"));
703 ArrayList<String> delegates = new ArrayList<String>();
704 while ((strLine = br.readLine()) != null) {
705 /* parse strLine to obtain what you want */
706 if (strLine.contains("Starting audit simulation for resourceName=")) {
707 startIndex = strLine.indexOf("resourceName=") + 13;
708 endIndex = strLine.indexOf(",");
710 String rName = strLine.substring(startIndex, endIndex);
712 delegates.add(rName);
716 for (String delegate: delegates) {
717 logger.info("testFourResourcesDifferentPus: delegate: " + delegate);
722 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6);
723 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
724 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
725 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
726 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
727 assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4)));
728 assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5)));
730 logger.info("testFourResourcesDifferentPus: Exiting");
735 * Tests designation logic when four resources are in play but one is not
736 * functioning. Designation should round robin among functioning resources
739 * Note: console.log must be examined to ascertain whether or not this test
744 public void testFourResourcesOneDead() throws Exception {
746 logger.info("testFourResourcesOneDead: Entering");
749 * Start audit for pdp1.
751 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
752 integrityAudit.startAuditThread();
755 * Start audit for pdp2.
757 Properties properties2 = new Properties();
758 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
759 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
760 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
761 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
762 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
763 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
764 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
765 String persistenceUnit2 = "testPU";
766 String resourceName2 = "pdp2";
767 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
768 integrityAudit2.startAuditThread();
771 * Populate DB for pdp3, which will simulate it having registered but then having died.
773 Properties properties3 = new Properties();
774 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
775 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
776 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
777 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
778 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
779 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
780 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
781 String persistenceUnit3 = "testPU";
782 String resourceName3 = "pdp3";
783 new DbDAO(resourceName3, persistenceUnit3, properties3);
786 * Start audit for pdp4.
788 Properties properties4 = new Properties();
789 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
790 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
791 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
792 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
793 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
794 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
795 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
796 String persistenceUnit4 = "testPU";
797 String resourceName4 = "pdp4";
798 IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4);
799 integrityAudit4.startAuditThread();
802 * Sleep long enough to allow
804 * 1) pdp1 to run audit (15 seconds)
806 * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
808 * 3) pdp2 to run audit (15 seconds)
810 * 4) Logic to detect that pdp2 is stale and designate pdp4 (30 seconds)
812 * 5) pdp4 to run audit (15 seconds)
814 * 6) Logic to detect that pdp4 is stale and designate pdp1 (30 seconds)
816 * 7) pdp1 to run audit (15 seconds)
818 * 8) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
820 * 7) pdp2 to run audit (15 seconds)
822 logger.info("testFourResourcesOneDead: Sleeping 210 seconds");
823 Thread.sleep(210000);
825 logger.info("testFourResourcesOneDead: Stopping threads");
826 integrityAudit.stopAuditThread();
827 integrityAudit2.stopAuditThread();
828 integrityAudit4.stopAuditThread();
830 FileInputStream fstream = new FileInputStream(TEST_LOG);
831 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
835 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp4", "pdp1", "pdp2", "pdp4"));
836 ArrayList<String> delegates = new ArrayList<String>();
837 while ((strLine = br.readLine()) != null) {
838 /* parse strLine to obtain what you want */
839 if (strLine.contains("Starting audit simulation for resourceName=")) {
840 startIndex = strLine.indexOf("resourceName=") + 13;
841 endIndex = strLine.indexOf(",");
843 String rName = strLine.substring(startIndex, endIndex);
845 delegates.add(rName);
849 for (String delegate : delegates) {
850 logger.info("testFourResourcesOneDead: delegate: " + delegate);
855 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6);
856 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
857 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
858 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
859 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
860 assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4)));
861 assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5)));
863 logger.info("testFourResourcesOneDead: Exiting");
868 * Tests designation logic when four resources are in play but only one is
869 * functioning. Designation should remain with sole functioning resource.
871 * Note: console.log must be examined to ascertain whether or not this test
876 public void testFourResourcesThreeDead() throws Exception {
878 logger.info("testFourResourcesThreeDead: Entering");
881 * Populate DB for pdp1, which will simulate it having registered but then having died.
883 new DbDAO(resourceName, persistenceUnit, properties);
887 * Populate DB for pdp2, which will simulate it having registered but then having died.
889 Properties properties2 = new Properties();
890 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
891 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
892 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
893 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
894 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
895 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
896 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
897 String persistenceUnit2 = "testPU";
898 String resourceName2 = "pdp2";
899 new DbDAO(resourceName2, persistenceUnit2, properties2);
902 * Start audit for pdp3.
904 Properties properties3 = new Properties();
905 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
906 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
907 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
908 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
909 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
910 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
911 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
912 String persistenceUnit3 = "testPU";
913 String resourceName3 = "pdp3";
914 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
915 integrityAudit3.startAuditThread();
918 * Populate DB for pdp4, which will simulate it having registered but then having died.
920 Properties properties4 = new Properties();
921 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
922 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
923 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
924 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
925 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
926 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
927 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
928 String persistenceUnit4 = "testPU";
929 String resourceName4 = "pdp4";
930 new DbDAO(resourceName4, persistenceUnit4, properties4);
933 * Sleep long enough to allow
935 * 1) pdp3 to discover that all other designation candidates are stale (30 seconds)
937 * 1) pdp3 to run audit (15 seconds)
939 * 2) Logic to detect that no other nodes are available for designation (60 seconds)
941 * 3) pdp3 to run audit again (15 seconds)
943 logger.info("testFourResourcesThreeDead: Sleeping 130 seconds");
944 Thread.sleep(130000);
946 logger.info("testFourResourcesThreeDead: Stopping thread");
947 integrityAudit3.stopAuditThread();
949 FileInputStream fstream = new FileInputStream(TEST_LOG);
950 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
954 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp3", "pdp3"));
955 ArrayList<String> delegates = new ArrayList<String>();
956 while ((strLine = br.readLine()) != null) {
957 /* parse strLine to obtain what you want */
958 if (strLine.contains("Starting audit simulation for resourceName=")) {
959 startIndex = strLine.indexOf("resourceName=") + 13;
960 endIndex = strLine.indexOf(",");
962 String rName = strLine.substring(startIndex, endIndex);
964 delegates.add(rName);
968 for (String delegate : delegates) {
969 logger.info("testFourResourcesThreeDead: delegate: " + delegate);
974 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2);
975 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
976 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
978 logger.info("testFourResourcesThreeDead: Exiting");
984 * Tests designation logic when the designated node dies and is no longer
987 * Note: console.log must be examined to ascertain whether or not this test
992 public void testDesignatedNodeDead() throws Exception {
993 logger.info("testDesignatedNodeDead: Entering");
996 * Instantiate audit object for pdp1.
998 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
1001 * Start audit for pdp2.
1003 Properties properties2 = new Properties();
1004 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
1005 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
1006 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
1007 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
1008 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
1009 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
1010 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
1011 String persistenceUnit2 = "testPU";
1012 String resourceName2 = "pdp2";
1013 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
1016 * Instantiate audit object for pdp3.
1018 Properties properties3 = new Properties();
1019 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
1020 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
1021 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
1022 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
1023 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
1024 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
1025 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
1026 String persistenceUnit3 = "testPU";
1027 String resourceName3 = "pdp3";
1028 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
1030 // Start audit on pdp1
1031 integrityAudit.startAuditThread();
1033 // Sleep long enough for pdp1 figure out that it should be auditing and start the audit.
1036 // Start the auditing threads on other nodes.
1037 integrityAudit2.startAuditThread();
1038 integrityAudit3.startAuditThread();
1040 // Sleep long enough to ensure the other two audits have registered.
1043 // Kill audit on pdp1
1044 integrityAudit.stopAuditThread();
1046 // Sleep long enough for pdp1 to get stale and pdp2 to take over
1047 Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1049 // Start audit thread on pdp1 again.
1050 integrityAudit.startAuditThread();
1052 // Sleep long enough for pdp2 to complete its audit and get stale, at
1053 // which point pdp3 should take over
1054 Thread.sleep((AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS)
1055 + AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1057 // Kill audit on pdp3
1058 logger.info("testDesignatedNodeDead: Killing audit on pdp3");
1059 integrityAudit3.stopAuditThread();
1061 // Sleep long enough for pdp3 to get stale and pdp1 to take over
1062 Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1064 FileInputStream fstream = new FileInputStream(TEST_LOG);
1065 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
1069 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1"));
1070 ArrayList<String> delegates = new ArrayList<String>();
1071 while ((strLine = br.readLine()) != null) {
1072 /* parse strLine to obtain what you want */
1073 if (strLine.contains("Starting audit simulation for resourceName=")) {
1074 startIndex = strLine.indexOf("resourceName=") + 13;
1075 endIndex = strLine.indexOf(",");
1077 String rName = strLine.substring(startIndex, endIndex);
1079 delegates.add(rName);
1084 // Stop remaining threads.
1085 logger.info("testDesignatedNodeDead: Stopping remaining threads");
1086 integrityAudit.stopAuditThread();
1087 integrityAudit2.stopAuditThread();
1089 for (String delegate: delegates) {
1090 logger.info("testDesignatedNodeDead: delegate: " + delegate);
1093 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4);
1094 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
1095 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
1096 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
1097 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
1099 logger.info("testDesignatedNodeDead: Exiting");