69936bfe39a90fad4a07daae65f2a792b6db4b89
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Integrity Audit
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.common.ia.test;
22
23 import static org.junit.Assert.*;
24
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;
32
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Persistence;
37
38
39
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;
46
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;
53
54 /*
55  * All JUnits are designed to run in the local development environment
56  * where they have write privileges and can execute time-sensitive
57  * tasks.
58  */
59 public class IntegrityAuditDesignationTest {
60         
61         private static Logger logger = FlexLogger.getLogger(IntegrityAuditDesignationTest.class);
62         
63         /*
64          * Provides a little cushion for timing events.
65          */
66         private static int FUDGE_FACTOR = 15000;
67         
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";
72         @Before
73         public void setUp() throws Exception {
74
75                 
76                 System.out.println("setUp: Clearing debug.log");
77                 FileOutputStream fstream = new FileOutputStream(TEST_LOG);
78                 fstream.close();
79
80                 logger.info("setUp: Entering");
81
82                 IntegrityAudit.isUnitTesting = true;
83                 
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");
91                 
92                 /*
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
96                  * time.
97                  */
98                 properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
99                                 
100                 persistenceUnit = "testPU";
101                 resourceName = "pdp1";
102                 
103                 
104                 //Clean up the DB               
105                 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
106                 
107                 EntityManager em = emf.createEntityManager();
108                 // Start a transaction
109                 EntityTransaction et = em.getTransaction();
110
111                 et.begin();
112
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();
115
116                 // commit transaction
117                 et.commit();
118                 em.close();
119                 logger.info("setUp: Exiting");
120                 
121         }
122         
123
124         @After
125         public void tearDown() throws Exception {
126                 
127                 logger.info("tearDown: Entering");
128                                 
129                 logger.info("tearDown: Exiting");
130
131         }
132
133         /*
134          * Tests designation logic when only one functioning resource is in play.  Designation
135          * should stay with single resource.
136          * 
137          * Note: console.log must be examined to ascertain whether or not this test was successful.
138          */
139         @Ignore
140         @Test
141         public void testOneResource() throws Exception {
142                 
143                 logger.info("testOneResource: Entering");
144                 
145                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
146                 integrityAudit.startAuditThread();
147                 
148                 /*
149                  * Sleep long enough to allow
150                  * 
151                  * 1) pdp1 to run audit (15 seconds)
152                  * 
153                  * 2) Logic to detect that no other node is available for designation (60 seconds)
154                  * 
155                  * 3) pdp1 to run audit again (15 seconds)
156                  */
157                 logger.info("testOneResource: Sleeping 100 seconds");
158                 Thread.sleep(100000);
159                 
160                 logger.info("testOneResource: Stopping audit thread");
161                 integrityAudit.stopAuditThread();
162                 
163                 FileInputStream fstream = new FileInputStream(TEST_LOG);
164                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
165                 String strLine;
166                 int startIndex;
167                 int endIndex;
168                 
169                 String rName = "";
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);
178                         }
179                 }       
180                 fstream.close();
181                 
182                 /*
183                  * Test fix for ECOMPD2TD-783: Audit fails to run when application is restarted.
184                  */
185                 integrityAudit.startAuditThread();
186                 
187                 /*
188                  * Sleep long enough to allow
189                  * 
190                  * 1) pdp1 to run audit (15 seconds)
191                  * 
192                  * 2) Logic to detect that no other node is available for designation (60 seconds)
193                  * 
194                  * 3) pdp1 to run audit again (15 seconds)
195                  */
196                 logger.info("testOneResource: Sleeping 100 seconds for second time");
197                 Thread.sleep(100000);
198                 
199                 logger.info("testOneResource: Stopping audit thread for second time");
200                 integrityAudit.stopAuditThread();
201                 
202                 fstream = new FileInputStream(TEST_LOG);
203                 br = new BufferedReader(new InputStreamReader(fstream));
204                 
205                 rName = "";
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);
214                         }
215                 }       
216                 fstream.close();
217                 
218                 logger.info("testOneResource: Exiting");
219
220         }
221         
222         /*
223          * Tests designation logic when two functioning resources are in play.
224          * Designation should alternate between resources.
225          * 
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":
229          * 
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.
239          */
240         @Ignore
241         @Test
242         public void testTwoResources() throws Exception {
243                 
244                 logger.info("testTwoResources: Entering");
245                 
246                 /*
247                  * Start audit for pdp1.
248                  */
249                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
250                 integrityAudit.startAuditThread();
251                 
252                 /*
253                  * Start audit for pdp2.
254                  */
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();
267                 
268                 /*
269                  * Sleep long enough to allow
270                  * 
271                  * 1) pdp1 to run audit (15 seconds)
272                  * 
273                  * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
274                  * 
275                  * 3) pdp2 to run audit (15 seconds)
276                  * 
277                  * 4) Logic to detect that pdp2 is stale and designate pdp1 (30 seconds)
278                  * 
279                  * 5) pdp1 to run audit (15 seconds)
280                  */
281                 Thread.sleep(120000);
282                 
283                 logger.info("testTwoResources: Stopping audit threads");
284                 integrityAudit.stopAuditThread();
285                 integrityAudit2.stopAuditThread();
286                 
287                 FileInputStream fstream = new FileInputStream(TEST_LOG);
288                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
289                 String strLine;
290                 int startIndex;
291                 int endIndex;
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(",");
299                                 
300                                 String rName = strLine.substring(startIndex, endIndex);
301                                 
302                                 delegates.add(rName);
303                         }
304                 }
305                                 
306                 for (String delegate: delegates) {
307                         logger.info("testTwoResources: delegate: " + delegate);
308                 }
309                 
310                 fstream.close();
311                                 
312                 assertTrue(expectedResult.equals(delegates));
313                 
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)));
318         
319                 logger.info("testTwoResources: Exiting");
320
321         }
322         
323         /*
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.
327          * 
328          * Note: console.log must be examined to ascertain whether or not this test
329          * was successful.
330          */
331         @Ignore
332         @Test
333         public void testTwoResourcesDifferentPus() throws Exception {
334                 
335                 logger.info("testTwoResourcesDifferentPus: Entering");
336                 
337                 /*
338                  * Start audit for pdp1.
339                  */
340                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
341                 integrityAudit.startAuditThread();
342                 
343                 /*
344                  * Start audit for pdp2.
345                  */
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();
358                 
359                 /*
360                  * Sleep long enough to allow
361                  * 
362                  * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
363                  * 
364                  * 2) Logic to detect that no other node is available for designation for either pdp1 or pdp2 (60 seconds)
365                  * 
366                  * 3) pdp1 and pdp2 to again run audit simultaneously (15 seconds)
367                  * 
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.   
370                  */
371                 logger.info("testTwoResourcesDifferentPus: Sleeping 80 seconds");
372                 Thread.sleep(100000);
373                 
374                 logger.info("testTwoResourcesDifferentPus: Stopping audit threads");
375                 integrityAudit.stopAuditThread();
376                 integrityAudit2.stopAuditThread();
377                 
378                 FileInputStream fstream = new FileInputStream(TEST_LOG);
379                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
380                 String strLine;
381                 int startIndex;
382                 int endIndex;
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(",");
390                                 
391                                 String rName = strLine.substring(startIndex, endIndex);
392                                 
393                                 delegates.add(rName);
394                         }
395                 }
396                                 
397                 for (String delegate: delegates) {
398                         logger.info("testTwoResourcesDifferentPus: delegate: " + delegate);
399                 }
400                 
401                 fstream.close();
402                 
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)));
408                 
409                 assertTrue(expectedResult.equals(delegates));
410                                 
411                 logger.info("testTwoResourcesDifferentPus: Exiting");
412
413         }
414
415         
416         /*
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
420          * resource is dead.
421          * 
422          * Note: console.log must be examined to ascertain whether or not this test
423          * was successful.
424          */
425         @Ignore
426         @Test
427         public void testTwoResourcesOneDead() throws Exception {
428                 
429                 logger.info("testTwoResourcesOneDead: Entering");
430                 
431                 /*
432                  * Start audit for pdp1.
433                  */
434                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
435                 integrityAudit.startAuditThread();
436                 
437                 /*
438                  * Populate DB for pdp2, which will simulate it having registered but then having died.
439                  */
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);
451                 
452                 /*
453                  * Sleep long enough to allow
454                  * 
455                  * 1) pdp1 to run audit (15 seconds)
456                  * 
457                  * 2) Logic to detect that other node, pdp2, is not available for designation (60 seconds)
458                  * 
459                  * 3) pdp1 to run audit again (15 seconds)
460                  */
461                 logger.info("testTwoResourcesOneDead: Sleeping 100 seconds");
462                 Thread.sleep(100000);
463                 
464                 logger.info("testTwoResourcesOneDead: Stopping audit thread");
465                 integrityAudit.stopAuditThread();
466                 
467                 FileInputStream fstream = new FileInputStream(TEST_LOG);
468                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
469                 String strLine;
470                 int startIndex;
471                 int endIndex;
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(",");
479                                 
480                                 String rName = strLine.substring(startIndex, endIndex);
481                                 
482                                 delegates.add(rName);
483                         }
484                 }
485                                 
486                 for (String delegate: delegates) {
487                         logger.info("testTwoResourcesOneDead: delegate: " + delegate);
488                 }
489                 
490                 fstream.close();
491                 
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)));
495                                 
496                 logger.info("testTwoResourcesOneDead: Exiting");
497
498         }
499         
500         
501         /*
502          * Tests designation logic when three functioning resources are in play.  Designation should
503          * round robin among resources.
504          * 
505          * Note: console.log must be examined to ascertain whether or not this test was successful.
506          */
507         @Ignore
508         @Test
509         public void testThreeResources() throws Exception {
510                 
511                 logger.info("testThreeResources: Entering");
512                 
513                 /*
514                  * Start audit for pdp1.
515                  */
516                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
517                 integrityAudit.startAuditThread();
518                 
519                 /*
520                  * Start audit for pdp2.
521                  */
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();
534                 
535                 /*
536                  * Start audit for pdp3.
537                  */
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();
550                 
551                 /*
552                  * Sleep long enough to allow
553                  * 
554                  * 1) pdp1 to run audit (15 seconds)
555                  * 
556                  * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
557                  * 
558                  * 3) pdp2 to run audit (15 seconds)
559                  * 
560                  * 4) Logic to detect that pdp2 is stale and designate pdp3 (30 seconds)
561                  * 
562                  * 5) pdp3 to run audit (15 seconds)
563                  * 
564                  * 6) Logic to detect that pdp3 is stale and designate pdp1 (30 seconds)
565                  * 
566                  * 7) pdp1 to run audit (15 seconds)
567                  */
568                 logger.info("testThreeResources: Sleeping 160 seconds");
569                 Thread.sleep(160000);
570                 
571                 logger.info("testThreeResources: Stopping threads");
572                 integrityAudit.stopAuditThread();
573                 integrityAudit2.stopAuditThread();
574                 integrityAudit3.stopAuditThread();
575                 
576                 FileInputStream fstream = new FileInputStream(TEST_LOG);
577                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
578                 String strLine;
579                 int startIndex;
580                 int endIndex;
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(",");
588                                 
589                                 String rName = strLine.substring(startIndex, endIndex);
590                                 
591                                 delegates.add(rName);
592                         }
593                 }
594                 
595                 for (String delegate: delegates) {
596                         logger.info("testThreeResources: delegate: " + delegate);
597                 }
598                 
599                 fstream.close();
600                 
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)));
606                                 
607                 logger.info("testThreeResources: Exiting");
608
609         }
610         
611         /*
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
615          * persistence units.
616          * 
617          * Note: console.log must be examined to ascertain whether or not this test
618          * was successful.
619          */
620         @Ignore
621         @Test
622         public void testFourResourcesDifferentPus() throws Exception {
623                 
624                 logger.info("testFourResourcesDifferentPus: Entering");
625                 
626                 /*
627                  * Start audit for pdp1, testPU.
628                  */
629                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
630                 integrityAudit.startAuditThread();
631                 
632                 /*
633                  * Start audit for pdp2, integrityAuditPU.
634                  */
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();
647                 
648                 /*
649                  * Start audit for pdp3, testPU.
650                  */
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();
663                 
664                 /*
665                  * Start audit for pdp4, integrityAuditPU.
666                  */
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();
679                 
680                 /*
681                  * Sleep long enough to allow
682                  * 
683                  * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
684                  * 
685                  * 2) Logic to detect that pdp1 and pdp2 are stale and designate pdp3 (one's counterpart) and pdp4 (two's counterpart) (30 seconds)
686                  * 
687                  * 3) pdp3 and pdp4 to run audit simultaneously (15 seconds)
688                  * 
689                  * 4) Logic to detect that pdp3 and pdp4 are stale and designate pdp1 (three's counterpart) and pdp2 (four's counterpart) (30 seconds)
690                  * 
691                  * 5) pdp1 and pdp2 to run audit simultaneously (15 seconds)
692                  */
693                 logger.info("testFourResourcesDifferentPus: Sleeping 120 seconds");
694                 Thread.sleep(120000);
695                 
696                 logger.info("testFourResourcesDifferentPus: Stopping threads");
697                 integrityAudit.stopAuditThread();
698                 integrityAudit2.stopAuditThread();
699                 integrityAudit3.stopAuditThread();
700                 integrityAudit4.stopAuditThread();
701                 
702                 FileInputStream fstream = new FileInputStream(TEST_LOG);
703                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
704                 String strLine;
705                 int startIndex;
706                 int endIndex;
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(",");
714                                 
715                                 String rName = strLine.substring(startIndex, endIndex);
716                                 
717                                 delegates.add(rName);
718                         }
719                 }
720                                 
721                 for (String delegate: delegates) {
722                         logger.info("testFourResourcesDifferentPus: delegate: " + delegate);
723                 }
724                 
725                 fstream.close();
726                 
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)));
734                                 
735                 logger.info("testFourResourcesDifferentPus: Exiting");
736
737         }
738         
739         /*
740          * Tests designation logic when four resources are in play but one is not
741          * functioning. Designation should round robin among functioning resources
742          * only.
743          * 
744          * Note: console.log must be examined to ascertain whether or not this test
745          * was successful.
746          */
747         @Ignore
748         @Test
749         public void testFourResourcesOneDead() throws Exception {
750                 
751                 logger.info("testFourResourcesOneDead: Entering");
752                 
753                 /*
754                  * Start audit for pdp1.
755                  */
756                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
757                 integrityAudit.startAuditThread();
758                 
759                 /*
760                  * Start audit for pdp2.
761                  */
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();
774                 
775                 /*
776                  * Populate DB for pdp3, which will simulate it having registered but then having died.
777                  */
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);
789                 
790                 /*
791                  * Start audit for pdp4.
792                  */
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();
805                 
806                 /*
807                  * Sleep long enough to allow
808                  * 
809                  * 1) pdp1 to run audit (15 seconds)
810                  * 
811                  * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
812                  * 
813                  * 3) pdp2 to run audit (15 seconds)
814                  * 
815                  * 4) Logic to detect that pdp2 is stale and designate pdp4 (30 seconds)
816                  * 
817                  * 5) pdp4 to run audit (15 seconds)
818                  * 
819                  * 6) Logic to detect that pdp4 is stale and designate pdp1 (30 seconds)
820                  * 
821                  * 7) pdp1 to run audit (15 seconds)
822                  * 
823                  * 8) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
824                  * 
825                  * 7) pdp2 to run audit (15 seconds)
826                  */
827                 logger.info("testFourResourcesOneDead: Sleeping 210 seconds");
828                 Thread.sleep(210000);
829                 
830                 logger.info("testFourResourcesOneDead: Stopping threads");              
831                 integrityAudit.stopAuditThread();
832                 integrityAudit2.stopAuditThread();
833                 integrityAudit4.stopAuditThread();
834                 
835                 FileInputStream fstream = new FileInputStream(TEST_LOG);
836                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
837                 String strLine;
838                 int startIndex;
839                 int endIndex;
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(",");
847                                 
848                                 String rName = strLine.substring(startIndex, endIndex);
849
850                                 delegates.add(rName);
851                         }
852                 }
853                 
854                 for (String delegate : delegates) {
855                         logger.info("testFourResourcesOneDead: delegate: " + delegate);
856                 }
857
858                 fstream.close();
859                 
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)));
867                                 
868                 logger.info("testFourResourcesOneDead: Exiting");
869
870         }
871         
872         /*
873          * Tests designation logic when four resources are in play but only one is
874          * functioning. Designation should remain with sole functioning resource.
875          * 
876          * Note: console.log must be examined to ascertain whether or not this test
877          * was successful.
878          */
879         @Ignore
880         @Test
881         public void testFourResourcesThreeDead() throws Exception {
882                                 
883                 logger.info("testFourResourcesThreeDead: Entering");
884                 
885                 /*
886                  * Populate DB for pdp1, which will simulate it having registered but then having died.
887                  */
888                 new DbDAO(resourceName, persistenceUnit, properties);
889
890                 
891                 /*
892                  * Populate DB for pdp2, which will simulate it having registered but then having died.
893                  */
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);
905
906                 /*
907                  * Start audit for pdp3.
908                  */
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();
921                 
922                 /*
923                  * Populate DB for pdp4, which will simulate it having registered but then having died.
924                  */
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);
936                                 
937                 /*
938                  * Sleep long enough to allow
939                  * 
940                  * 1) pdp3 to discover that all other designation candidates are stale (30 seconds)
941                  * 
942                  * 1) pdp3 to run audit (15 seconds)
943                  * 
944                  * 2) Logic to detect that no other nodes are available for designation (60 seconds)
945                  * 
946                  * 3) pdp3 to run audit again (15 seconds)
947                  */
948                 logger.info("testFourResourcesThreeDead: Sleeping 130 seconds");
949                 Thread.sleep(130000);
950                 
951                 logger.info("testFourResourcesThreeDead: Stopping thread");
952                 integrityAudit3.stopAuditThread();
953                 
954                 FileInputStream fstream = new FileInputStream(TEST_LOG);
955                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
956                 String strLine;
957                 int startIndex;
958                 int endIndex;
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(",");
966                                 
967                                 String rName = strLine.substring(startIndex, endIndex);
968
969                                 delegates.add(rName);
970                         }
971                 }
972                 
973                 for (String delegate : delegates) {
974                         logger.info("testFourResourcesThreeDead: delegate: " + delegate);
975                 }
976
977                 fstream.close();
978                 
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)));
982                 
983                 logger.info("testFourResourcesThreeDead: Exiting");
984
985         }
986
987
988         /*
989          * Tests designation logic when the designated node dies and is no longer
990          * current
991          * 
992          * Note: console.log must be examined to ascertain whether or not this test
993          * was successful.
994          */
995         @Ignore
996         @Test
997         public void testDesignatedNodeDead() throws Exception {
998                 logger.info("testDesignatedNodeDead: Entering");
999                 
1000                 /*
1001                  * Instantiate audit object for pdp1.
1002                  */
1003                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
1004                 
1005                 /*
1006                  * Start audit for pdp2.
1007                  */
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);
1019
1020                 /*
1021                  * Instantiate audit object for pdp3.
1022                  */
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);
1034                                 
1035                 // Start audit on pdp1
1036                 logger.info("testDesignatedNodeDead: Start audit on pdp1");
1037                 integrityAudit.startAuditThread();                      
1038
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();             
1044                 
1045
1046                 // Kill audit on pdp1
1047                 logger.info("testDesignatedNodeDead: Kill audit on pdp1");
1048                 integrityAudit.stopAuditThread();
1049                 
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); 
1053                 
1054                 // Start audit thread on pdp1 again.
1055                 logger.info("testDesignatedNodeDead: Start audit thread on pdp1 again.");
1056                 integrityAudit.startAuditThread(); 
1057                 
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);
1063                 
1064                 // Kill audit on pdp3
1065                 logger.info("testDesignatedNodeDead: Killing audit on pdp3");
1066                 integrityAudit3.stopAuditThread();
1067                 
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); 
1071                                 
1072                 FileInputStream fstream = new FileInputStream(TEST_LOG);
1073                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
1074                 String strLine;
1075                 int startIndex;
1076                 int endIndex;
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(",");
1084                                 
1085                                 String rName = strLine.substring(startIndex, endIndex);
1086                                 
1087                                 delegates.add(rName);
1088                         }
1089                 }
1090                 fstream.close();
1091                 
1092                 // Stop remaining threads.
1093                 logger.info("testDesignatedNodeDead: Stopping remaining threads");
1094                 integrityAudit.stopAuditThread();
1095                 integrityAudit2.stopAuditThread();
1096                 
1097                 for (String delegate: delegates) {
1098                         logger.info("testDesignatedNodeDead: delegate: " + delegate);
1099                 }
1100                 
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)));
1106                 
1107                 logger.info("testDesignatedNodeDead: Exiting");
1108         }
1109 }