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;
55 * All JUnits are designed to run in the local development environment
56 * where they have write privileges and can execute time-sensitive
59 public class IntegrityAuditDesignationTest {
61 private static Logger logger = FlexLogger.getLogger(IntegrityAuditDesignationTest.class);
64 * Provides a little cushion for timing events.
66 private static int FUDGE_FACTOR = 15000;
68 private static String persistenceUnit;
69 private static Properties properties;
70 private static String resourceName;
71 private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log";
73 public void setUp() throws Exception {
76 System.out.println("setUp: Clearing debug.log");
77 FileOutputStream fstream = new FileOutputStream(TEST_LOG);
80 logger.info("setUp: Entering");
82 IntegrityAudit.isUnitTesting = true;
84 properties = new Properties();
85 properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
86 properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
87 properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
88 properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
89 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
90 properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
93 * AuditThread.AUDIT_THREAD_SLEEP_INTERVAL is also five seconds, so
94 * setting AUDIT_PERIOD_SECONDS to 5 ensures that whether or not audit
95 * has already been run on a node, it will sleep the same amount of
98 properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
100 persistenceUnit = "testPU";
101 resourceName = "pdp1";
105 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
107 EntityManager em = emf.createEntityManager();
108 // Start a transaction
109 EntityTransaction et = em.getTransaction();
113 // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry
114 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
116 // commit transaction
119 logger.info("setUp: Exiting");
125 public void tearDown() throws Exception {
127 logger.info("tearDown: Entering");
129 logger.info("tearDown: Exiting");
134 * Tests designation logic when only one functioning resource is in play. Designation
135 * should stay with single resource.
137 * Note: console.log must be examined to ascertain whether or not this test was successful.
141 public void testOneResource() throws Exception {
143 logger.info("testOneResource: Entering");
145 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
146 integrityAudit.startAuditThread();
149 * Sleep long enough to allow
151 * 1) pdp1 to run audit (15 seconds)
153 * 2) Logic to detect that no other node is available for designation (60 seconds)
155 * 3) pdp1 to run audit again (15 seconds)
157 logger.info("testOneResource: Sleeping 100 seconds");
158 Thread.sleep(100000);
160 logger.info("testOneResource: Stopping audit thread");
161 integrityAudit.stopAuditThread();
163 FileInputStream fstream = new FileInputStream(TEST_LOG);
164 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
170 while ((strLine = br.readLine()) != null) {
171 // parse strLine to obtain what you want
172 if (strLine.contains("Starting audit simulation for resourceName=")) {
173 startIndex = strLine.indexOf("resourceName=") + 13;
174 endIndex = strLine.indexOf(",");
175 rName = strLine.substring(startIndex, endIndex);
176 logger.info("testOneResource: rName: " + rName);
177 assertEquals("pdp1", rName);
183 * Test fix for ECOMPD2TD-783: Audit fails to run when application is restarted.
185 integrityAudit.startAuditThread();
188 * Sleep long enough to allow
190 * 1) pdp1 to run audit (15 seconds)
192 * 2) Logic to detect that no other node is available for designation (60 seconds)
194 * 3) pdp1 to run audit again (15 seconds)
196 logger.info("testOneResource: Sleeping 100 seconds for second time");
197 Thread.sleep(100000);
199 logger.info("testOneResource: Stopping audit thread for second time");
200 integrityAudit.stopAuditThread();
202 fstream = new FileInputStream(TEST_LOG);
203 br = new BufferedReader(new InputStreamReader(fstream));
206 while ((strLine = br.readLine()) != null) {
207 // parse strLine to obtain what you want
208 if (strLine.contains("Starting audit simulation for resourceName=")) {
209 startIndex = strLine.indexOf("resourceName=") + 13;
210 endIndex = strLine.indexOf(",");
211 rName = strLine.substring(startIndex, endIndex);
212 logger.info("testOneResource: rName: " + rName);
213 assertEquals("pdp1", rName);
218 logger.info("testOneResource: Exiting");
223 * Tests designation logic when two functioning resources are in play.
224 * Designation should alternate between resources.
226 * Note: console.log must be examined to ascertain whether or not this test
227 * was successful. A quick way of examining the log is to search for the
228 * string "audit simulation":
230 * As you can see from the "dbAuditSimulate" method, when it executes, it
231 * logs the "Starting audit simulation..." message and when it finishes, it
232 * logs the "Finished audit simulation..." message. By looking for these
233 * messages, you can verify that the audits are run by the proper resource.
234 * For example, when testFourResourcesOneDead is run, you should see a
235 * Starting.../Finished... sequence for pdp1, followed by a
236 * Starting.../Finished... sequence for pdp2, followed by a
237 * Starting.../Finished... sequence for pdp4 (pdp3 is skipped as it's
238 * dead/hung), followed by a Starting.../Finished... sequence for pdp1, etc.
242 public void testTwoResources() throws Exception {
244 logger.info("testTwoResources: Entering");
247 * Start audit for pdp1.
249 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
250 integrityAudit.startAuditThread();
253 * Start audit for pdp2.
255 Properties properties2 = new Properties();
256 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
257 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
258 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
259 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
260 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
261 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
262 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
263 String persistenceUnit2 = "testPU";
264 String resourceName2 = "pdp2";
265 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
266 integrityAudit2.startAuditThread();
269 * Sleep long enough to allow
271 * 1) pdp1 to run audit (15 seconds)
273 * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
275 * 3) pdp2 to run audit (15 seconds)
277 * 4) Logic to detect that pdp2 is stale and designate pdp1 (30 seconds)
279 * 5) pdp1 to run audit (15 seconds)
281 Thread.sleep(120000);
283 logger.info("testTwoResources: Stopping audit threads");
284 integrityAudit.stopAuditThread();
285 integrityAudit2.stopAuditThread();
287 FileInputStream fstream = new FileInputStream(TEST_LOG);
288 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
292 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp1"));
293 ArrayList<String> delegates = new ArrayList<String>();
294 while ((strLine = br.readLine()) != null) {
295 /* parse strLine to obtain what you want */
296 if (strLine.contains("Starting audit simulation for resourceName=")) {
297 startIndex = strLine.indexOf("resourceName=") + 13;
298 endIndex = strLine.indexOf(",");
300 String rName = strLine.substring(startIndex, endIndex);
302 delegates.add(rName);
306 for (String delegate: delegates) {
307 logger.info("testTwoResources: delegate: " + delegate);
312 assertTrue(expectedResult.equals(delegates));
314 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3);
315 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
316 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
317 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
319 logger.info("testTwoResources: Exiting");
324 * Tests designation logic when two functioning resources are in play, each
325 * with different PUs. Audits for "testPU" and "integrityAuditPU" should run
326 * simultaneously. Designation should not alternate.
328 * Note: console.log must be examined to ascertain whether or not this test
333 public void testTwoResourcesDifferentPus() throws Exception {
335 logger.info("testTwoResourcesDifferentPus: Entering");
338 * Start audit for pdp1.
340 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
341 integrityAudit.startAuditThread();
344 * Start audit for pdp2.
346 Properties properties2 = new Properties();
347 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
348 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
349 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
350 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
351 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
352 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
353 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
354 String persistenceUnit2 = "integrityAuditPU";
355 String resourceName2 = "pdp2";
356 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
357 integrityAudit2.startAuditThread();
360 * Sleep long enough to allow
362 * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
364 * 2) Logic to detect that no other node is available for designation for either pdp1 or pdp2 (60 seconds)
366 * 3) pdp1 and pdp2 to again run audit simultaneously (15 seconds)
368 * NOTE: Based on the above, you would think a 100000ms sleep would be appropriate,
369 * but for some reason, when all tests are run this test errors.
371 logger.info("testTwoResourcesDifferentPus: Sleeping 80 seconds");
372 Thread.sleep(100000);
374 logger.info("testTwoResourcesDifferentPus: Stopping audit threads");
375 integrityAudit.stopAuditThread();
376 integrityAudit2.stopAuditThread();
378 FileInputStream fstream = new FileInputStream(TEST_LOG);
379 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
383 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp1", "pdp2"));
384 ArrayList<String> delegates = new ArrayList<String>();
385 while ((strLine = br.readLine()) != null) {
386 /* parse strLine to obtain what you want */
387 if (strLine.contains("Starting audit simulation for resourceName=")) {
388 startIndex = strLine.indexOf("resourceName=") + 13;
389 endIndex = strLine.indexOf(",");
391 String rName = strLine.substring(startIndex, endIndex);
393 delegates.add(rName);
397 for (String delegate: delegates) {
398 logger.info("testTwoResourcesDifferentPus: delegate: " + delegate);
403 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4);
404 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
405 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
406 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
407 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
409 assertTrue(expectedResult.equals(delegates));
411 logger.info("testTwoResourcesDifferentPus: Exiting");
417 * Tests designation logic when two resources are in play but one of them is
418 * dead/hung. Designation should move to second resource but then get
419 * restored back to original resource when it's discovered that second
422 * Note: console.log must be examined to ascertain whether or not this test
427 public void testTwoResourcesOneDead() throws Exception {
429 logger.info("testTwoResourcesOneDead: Entering");
432 * Start audit for pdp1.
434 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
435 integrityAudit.startAuditThread();
438 * Populate DB for pdp2, which will simulate it having registered but then having died.
440 Properties properties2 = new Properties();
441 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
442 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
443 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
444 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
445 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
446 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
447 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
448 String persistenceUnit2 = "testPU";
449 String resourceName2 = "pdp2";
450 new DbDAO(resourceName2, persistenceUnit2, properties2);
453 * Sleep long enough to allow
455 * 1) pdp1 to run audit (15 seconds)
457 * 2) Logic to detect that other node, pdp2, is not available for designation (60 seconds)
459 * 3) pdp1 to run audit again (15 seconds)
461 logger.info("testTwoResourcesOneDead: Sleeping 100 seconds");
462 Thread.sleep(100000);
464 logger.info("testTwoResourcesOneDead: Stopping audit thread");
465 integrityAudit.stopAuditThread();
467 FileInputStream fstream = new FileInputStream(TEST_LOG);
468 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
472 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp1"));
473 ArrayList<String> delegates = new ArrayList<String>();
474 while ((strLine = br.readLine()) != null) {
475 /* parse strLine to obtain what you want */
476 if (strLine.contains("Starting audit simulation for resourceName=")) {
477 startIndex = strLine.indexOf("resourceName=") + 13;
478 endIndex = strLine.indexOf(",");
480 String rName = strLine.substring(startIndex, endIndex);
482 delegates.add(rName);
486 for (String delegate: delegates) {
487 logger.info("testTwoResourcesOneDead: delegate: " + delegate);
492 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2);
493 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
494 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
496 logger.info("testTwoResourcesOneDead: Exiting");
502 * Tests designation logic when three functioning resources are in play. Designation should
503 * round robin among resources.
505 * Note: console.log must be examined to ascertain whether or not this test was successful.
509 public void testThreeResources() throws Exception {
511 logger.info("testThreeResources: Entering");
514 * Start audit for pdp1.
516 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
517 integrityAudit.startAuditThread();
520 * Start audit for pdp2.
522 Properties properties2 = new Properties();
523 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
524 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
525 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
526 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
527 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
528 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
529 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
530 String persistenceUnit2 = "testPU";
531 String resourceName2 = "pdp2";
532 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
533 integrityAudit2.startAuditThread();
536 * Start audit for pdp3.
538 Properties properties3 = new Properties();
539 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
540 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
541 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
542 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
543 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
544 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
545 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
546 String persistenceUnit3 = "testPU";
547 String resourceName3 = "pdp3";
548 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
549 integrityAudit3.startAuditThread();
552 * Sleep long enough to allow
554 * 1) pdp1 to run audit (15 seconds)
556 * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
558 * 3) pdp2 to run audit (15 seconds)
560 * 4) Logic to detect that pdp2 is stale and designate pdp3 (30 seconds)
562 * 5) pdp3 to run audit (15 seconds)
564 * 6) Logic to detect that pdp3 is stale and designate pdp1 (30 seconds)
566 * 7) pdp1 to run audit (15 seconds)
568 logger.info("testThreeResources: Sleeping 160 seconds");
569 Thread.sleep(160000);
571 logger.info("testThreeResources: Stopping threads");
572 integrityAudit.stopAuditThread();
573 integrityAudit2.stopAuditThread();
574 integrityAudit3.stopAuditThread();
576 FileInputStream fstream = new FileInputStream(TEST_LOG);
577 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
581 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1"));
582 ArrayList<String> delegates = new ArrayList<String>();
583 while ((strLine = br.readLine()) != null) {
584 /* parse strLine to obtain what you want */
585 if (strLine.contains("Starting audit simulation for resourceName=")) {
586 startIndex = strLine.indexOf("resourceName=") + 13;
587 endIndex = strLine.indexOf(",");
589 String rName = strLine.substring(startIndex, endIndex);
591 delegates.add(rName);
595 for (String delegate: delegates) {
596 logger.info("testThreeResources: delegate: " + delegate);
601 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3);
602 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
603 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
604 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
605 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
607 logger.info("testThreeResources: Exiting");
612 * Tests designation logic when four functioning resources are in play, two
613 * with one PU, two with another. Audits for "testPU" and "integrityAuditPU" should run
614 * simultaneously. Designation should alternate between resources for each of the two
617 * Note: console.log must be examined to ascertain whether or not this test
622 public void testFourResourcesDifferentPus() throws Exception {
624 logger.info("testFourResourcesDifferentPus: Entering");
627 * Start audit for pdp1, testPU.
629 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
630 integrityAudit.startAuditThread();
633 * Start audit for pdp2, integrityAuditPU.
635 Properties properties2 = new Properties();
636 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
637 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
638 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
639 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
640 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
641 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
642 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
643 String persistenceUnit2 = "integrityAuditPU";
644 String resourceName2 = "pdp2";
645 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
646 integrityAudit2.startAuditThread();
649 * Start audit for pdp3, testPU.
651 Properties properties3 = new Properties();
652 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
653 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
654 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
655 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
656 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
657 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
658 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
659 String persistenceUnit3 = "testPU";
660 String resourceName3 = "pdp3";
661 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
662 integrityAudit3.startAuditThread();
665 * Start audit for pdp4, integrityAuditPU.
667 Properties properties4 = new Properties();
668 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
669 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
670 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
671 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
672 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
673 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
674 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
675 String persistenceUnit4 = "integrityAuditPU";
676 String resourceName4 = "pdp4";
677 IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4);
678 integrityAudit4.startAuditThread();
681 * Sleep long enough to allow
683 * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
685 * 2) Logic to detect that pdp1 and pdp2 are stale and designate pdp3 (one's counterpart) and pdp4 (two's counterpart) (30 seconds)
687 * 3) pdp3 and pdp4 to run audit simultaneously (15 seconds)
689 * 4) Logic to detect that pdp3 and pdp4 are stale and designate pdp1 (three's counterpart) and pdp2 (four's counterpart) (30 seconds)
691 * 5) pdp1 and pdp2 to run audit simultaneously (15 seconds)
693 logger.info("testFourResourcesDifferentPus: Sleeping 120 seconds");
694 Thread.sleep(120000);
696 logger.info("testFourResourcesDifferentPus: Stopping threads");
697 integrityAudit.stopAuditThread();
698 integrityAudit2.stopAuditThread();
699 integrityAudit3.stopAuditThread();
700 integrityAudit4.stopAuditThread();
702 FileInputStream fstream = new FileInputStream(TEST_LOG);
703 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
707 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp4", "pdp1", "pdp2"));
708 ArrayList<String> delegates = new ArrayList<String>();
709 while ((strLine = br.readLine()) != null) {
710 /* parse strLine to obtain what you want */
711 if (strLine.contains("Starting audit simulation for resourceName=")) {
712 startIndex = strLine.indexOf("resourceName=") + 13;
713 endIndex = strLine.indexOf(",");
715 String rName = strLine.substring(startIndex, endIndex);
717 delegates.add(rName);
721 for (String delegate: delegates) {
722 logger.info("testFourResourcesDifferentPus: delegate: " + delegate);
727 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6);
728 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
729 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
730 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
731 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
732 assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4)));
733 assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5)));
735 logger.info("testFourResourcesDifferentPus: Exiting");
740 * Tests designation logic when four resources are in play but one is not
741 * functioning. Designation should round robin among functioning resources
744 * Note: console.log must be examined to ascertain whether or not this test
749 public void testFourResourcesOneDead() throws Exception {
751 logger.info("testFourResourcesOneDead: Entering");
754 * Start audit for pdp1.
756 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
757 integrityAudit.startAuditThread();
760 * Start audit for pdp2.
762 Properties properties2 = new Properties();
763 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
764 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
765 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
766 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
767 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
768 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
769 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
770 String persistenceUnit2 = "testPU";
771 String resourceName2 = "pdp2";
772 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
773 integrityAudit2.startAuditThread();
776 * Populate DB for pdp3, which will simulate it having registered but then having died.
778 Properties properties3 = new Properties();
779 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
780 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
781 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
782 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
783 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
784 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
785 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
786 String persistenceUnit3 = "testPU";
787 String resourceName3 = "pdp3";
788 new DbDAO(resourceName3, persistenceUnit3, properties3);
791 * Start audit for pdp4.
793 Properties properties4 = new Properties();
794 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
795 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
796 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
797 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
798 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
799 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
800 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
801 String persistenceUnit4 = "testPU";
802 String resourceName4 = "pdp4";
803 IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4);
804 integrityAudit4.startAuditThread();
807 * Sleep long enough to allow
809 * 1) pdp1 to run audit (15 seconds)
811 * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
813 * 3) pdp2 to run audit (15 seconds)
815 * 4) Logic to detect that pdp2 is stale and designate pdp4 (30 seconds)
817 * 5) pdp4 to run audit (15 seconds)
819 * 6) Logic to detect that pdp4 is stale and designate pdp1 (30 seconds)
821 * 7) pdp1 to run audit (15 seconds)
823 * 8) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
825 * 7) pdp2 to run audit (15 seconds)
827 logger.info("testFourResourcesOneDead: Sleeping 210 seconds");
828 Thread.sleep(210000);
830 logger.info("testFourResourcesOneDead: Stopping threads");
831 integrityAudit.stopAuditThread();
832 integrityAudit2.stopAuditThread();
833 integrityAudit4.stopAuditThread();
835 FileInputStream fstream = new FileInputStream(TEST_LOG);
836 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
840 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp4", "pdp1", "pdp2", "pdp4"));
841 ArrayList<String> delegates = new ArrayList<String>();
842 while ((strLine = br.readLine()) != null) {
843 /* parse strLine to obtain what you want */
844 if (strLine.contains("Starting audit simulation for resourceName=")) {
845 startIndex = strLine.indexOf("resourceName=") + 13;
846 endIndex = strLine.indexOf(",");
848 String rName = strLine.substring(startIndex, endIndex);
850 delegates.add(rName);
854 for (String delegate : delegates) {
855 logger.info("testFourResourcesOneDead: delegate: " + delegate);
860 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6);
861 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
862 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
863 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
864 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
865 assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4)));
866 assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5)));
868 logger.info("testFourResourcesOneDead: Exiting");
873 * Tests designation logic when four resources are in play but only one is
874 * functioning. Designation should remain with sole functioning resource.
876 * Note: console.log must be examined to ascertain whether or not this test
881 public void testFourResourcesThreeDead() throws Exception {
883 logger.info("testFourResourcesThreeDead: Entering");
886 * Populate DB for pdp1, which will simulate it having registered but then having died.
888 new DbDAO(resourceName, persistenceUnit, properties);
892 * Populate DB for pdp2, which will simulate it having registered but then having died.
894 Properties properties2 = new Properties();
895 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
896 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
897 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
898 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
899 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
900 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
901 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
902 String persistenceUnit2 = "testPU";
903 String resourceName2 = "pdp2";
904 new DbDAO(resourceName2, persistenceUnit2, properties2);
907 * Start audit for pdp3.
909 Properties properties3 = new Properties();
910 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
911 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
912 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
913 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
914 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
915 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
916 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
917 String persistenceUnit3 = "testPU";
918 String resourceName3 = "pdp3";
919 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
920 integrityAudit3.startAuditThread();
923 * Populate DB for pdp4, which will simulate it having registered but then having died.
925 Properties properties4 = new Properties();
926 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
927 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
928 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
929 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
930 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
931 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
932 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
933 String persistenceUnit4 = "testPU";
934 String resourceName4 = "pdp4";
935 new DbDAO(resourceName4, persistenceUnit4, properties4);
938 * Sleep long enough to allow
940 * 1) pdp3 to discover that all other designation candidates are stale (30 seconds)
942 * 1) pdp3 to run audit (15 seconds)
944 * 2) Logic to detect that no other nodes are available for designation (60 seconds)
946 * 3) pdp3 to run audit again (15 seconds)
948 logger.info("testFourResourcesThreeDead: Sleeping 130 seconds");
949 Thread.sleep(130000);
951 logger.info("testFourResourcesThreeDead: Stopping thread");
952 integrityAudit3.stopAuditThread();
954 FileInputStream fstream = new FileInputStream(TEST_LOG);
955 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
959 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp3", "pdp3"));
960 ArrayList<String> delegates = new ArrayList<String>();
961 while ((strLine = br.readLine()) != null) {
962 /* parse strLine to obtain what you want */
963 if (strLine.contains("Starting audit simulation for resourceName=")) {
964 startIndex = strLine.indexOf("resourceName=") + 13;
965 endIndex = strLine.indexOf(",");
967 String rName = strLine.substring(startIndex, endIndex);
969 delegates.add(rName);
973 for (String delegate : delegates) {
974 logger.info("testFourResourcesThreeDead: delegate: " + delegate);
979 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2);
980 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
981 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
983 logger.info("testFourResourcesThreeDead: Exiting");
989 * Tests designation logic when the designated node dies and is no longer
992 * Note: console.log must be examined to ascertain whether or not this test
997 public void testDesignatedNodeDead() throws Exception {
998 logger.info("testDesignatedNodeDead: Entering");
1001 * Instantiate audit object for pdp1.
1003 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
1006 * Start audit for pdp2.
1008 Properties properties2 = new Properties();
1009 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
1010 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
1011 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
1012 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
1013 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
1014 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
1015 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
1016 String persistenceUnit2 = "testPU";
1017 String resourceName2 = "pdp2";
1018 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
1021 * Instantiate audit object for pdp3.
1023 Properties properties3 = new Properties();
1024 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
1025 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
1026 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
1027 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
1028 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
1029 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
1030 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
1031 String persistenceUnit3 = "testPU";
1032 String resourceName3 = "pdp3";
1033 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
1035 // Start audit on pdp1
1036 logger.info("testDesignatedNodeDead: Start audit on pdp1");
1037 integrityAudit.startAuditThread();
1039 // Start the auditing threads on other nodes.
1040 logger.info("testDesignatedNodeDead: Start audit on pdp2");
1041 integrityAudit2.startAuditThread();
1042 logger.info("testDesignatedNodeDead: Start audit on pdp3");
1043 integrityAudit3.startAuditThread();
1046 // Kill audit on pdp1
1047 logger.info("testDesignatedNodeDead: Kill audit on pdp1");
1048 integrityAudit.stopAuditThread();
1050 // Sleep long enough for pdp1 to get stale and pdp2 to take over
1051 logger.info("testDesignatedNodeDead: Sleep long enough for pdp1 to get stale and pdp2 to take over");
1052 Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1054 // Start audit thread on pdp1 again.
1055 logger.info("testDesignatedNodeDead: Start audit thread on pdp1 again.");
1056 integrityAudit.startAuditThread();
1058 // Sleep long enough for pdp2 to complete its audit and get stale, at
1059 // which point pdp3 should take over
1060 logger.info("testDesignatedNodeDead: Sleep long enough for pdp2 to complete its audit and get stale, at which point pdp3 should take over");
1061 Thread.sleep((AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS)
1062 + AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1064 // Kill audit on pdp3
1065 logger.info("testDesignatedNodeDead: Killing audit on pdp3");
1066 integrityAudit3.stopAuditThread();
1068 // Sleep long enough for pdp3 to get stale and pdp1 to take over
1069 logger.info("testDesignatedNodeDead: Sleep long enough for pdp3 to get stale and pdp1 to take over");
1070 Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1072 FileInputStream fstream = new FileInputStream(TEST_LOG);
1073 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
1077 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1"));
1078 ArrayList<String> delegates = new ArrayList<String>();
1079 while ((strLine = br.readLine()) != null) {
1080 /* parse strLine to obtain what you want */
1081 if (strLine.contains("Starting audit simulation for resourceName=")) {
1082 startIndex = strLine.indexOf("resourceName=") + 13;
1083 endIndex = strLine.indexOf(",");
1085 String rName = strLine.substring(startIndex, endIndex);
1087 delegates.add(rName);
1092 // Stop remaining threads.
1093 logger.info("testDesignatedNodeDead: Stopping remaining threads");
1094 integrityAudit.stopAuditThread();
1095 integrityAudit2.stopAuditThread();
1097 for (String delegate: delegates) {
1098 logger.info("testDesignatedNodeDead: delegate: " + delegate);
1101 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4);
1102 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
1103 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
1104 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
1105 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
1107 logger.info("testDesignatedNodeDead: Exiting");