6eb5fa4e31a7a82329028b383db56c7585809cb5
[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 public class IntegrityAuditDesignationTest {
55         
56         private static Logger logger = FlexLogger.getLogger(IntegrityAuditDesignationTest.class);
57         
58         /*
59          * Provides a little cushion for timing events.
60          */
61         private static int FUDGE_FACTOR = 5000;
62         
63         private static String persistenceUnit;
64         private static Properties properties;
65         private static String resourceName;
66         private static final String TEST_LOG = "./testingLogs/common-modules/integrity-audit/debug.log";
67         @Before
68         public void setUp() throws Exception {
69
70                 
71                 System.out.println("setUp: Clearing debug.log");
72                 FileOutputStream fstream = new FileOutputStream(TEST_LOG);
73                 fstream.close();
74
75                 logger.info("setUp: Entering");
76
77                 IntegrityAudit.isUnitTesting = true;
78                 
79                 properties = new Properties();
80                 properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
81                 properties.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
82                 properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
83                 properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
84                 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
85                 properties.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
86                 
87                 /*
88                  * AuditThread.AUDIT_THREAD_SLEEP_INTERVAL is also five seconds, so
89                  * setting AUDIT_PERIOD_SECONDS to 5 ensures that whether or not audit
90                  * has already been run on a node, it will sleep the same amount of
91                  * time.
92                  */
93                 properties.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
94                                 
95                 persistenceUnit = "testPU";
96                 resourceName = "pdp1";
97                 
98                 
99                 //Clean up the DB               
100                 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
101                 
102                 EntityManager em = emf.createEntityManager();
103                 // Start a transaction
104                 EntityTransaction et = em.getTransaction();
105
106                 et.begin();
107
108                 // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not found, create a new entry
109                 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
110
111                 // commit transaction
112                 et.commit();
113                 em.close();
114                 logger.info("setUp: Exiting");
115                 
116         }
117         
118
119         @After
120         public void tearDown() throws Exception {
121                 
122                 logger.info("tearDown: Entering");
123                                 
124                 logger.info("tearDown: Exiting");
125
126         }
127
128         /*
129          * Tests designation logic when only one functioning resource is in play.  Designation
130          * should stay with single resource.
131          * 
132          * Note: console.log must be examined to ascertain whether or not this test was successful.
133          */
134         @Ignore
135         @Test
136         public void testOneResource() throws Exception {
137                 
138                 logger.info("testOneResource: Entering");
139                 
140                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
141                 integrityAudit.startAuditThread();
142                 
143                 /*
144                  * Sleep long enough to allow
145                  * 
146                  * 1) pdp1 to run audit (15 seconds)
147                  * 
148                  * 2) Logic to detect that no other node is available for designation (60 seconds)
149                  * 
150                  * 3) pdp1 to run audit again (15 seconds)
151                  */
152                 logger.info("testOneResource: Sleeping 100 seconds");
153                 Thread.sleep(100000);
154                 
155                 logger.info("testOneResource: Stopping audit thread");
156                 integrityAudit.stopAuditThread();
157                 
158                 FileInputStream fstream = new FileInputStream(TEST_LOG);
159                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
160                 String strLine;
161                 int startIndex;
162                 int endIndex;
163                 
164                 String rName = "";
165                 while ((strLine = br.readLine()) != null)   {
166                         // parse strLine to obtain what you want 
167                         if (strLine.contains("Starting audit simulation for resourceName=")) {
168                                 startIndex = strLine.indexOf("resourceName=") + 13;
169                                 endIndex = strLine.indexOf(",");
170                                 rName = strLine.substring(startIndex, endIndex);
171                                 logger.info("testOneResource: rName: " + rName);
172                                 assertEquals("pdp1", rName);
173                         }
174                 }       
175                 fstream.close();
176                 
177                 /*
178                  * Test fix for ECOMPD2TD-783: Audit fails to run when application is restarted.
179                  */
180                 integrityAudit.startAuditThread();
181                 
182                 /*
183                  * Sleep long enough to allow
184                  * 
185                  * 1) pdp1 to run audit (15 seconds)
186                  * 
187                  * 2) Logic to detect that no other node is available for designation (60 seconds)
188                  * 
189                  * 3) pdp1 to run audit again (15 seconds)
190                  */
191                 logger.info("testOneResource: Sleeping 100 seconds for second time");
192                 Thread.sleep(100000);
193                 
194                 logger.info("testOneResource: Stopping audit thread for second time");
195                 integrityAudit.stopAuditThread();
196                 
197                 fstream = new FileInputStream(TEST_LOG);
198                 br = new BufferedReader(new InputStreamReader(fstream));
199                 
200                 rName = "";
201                 while ((strLine = br.readLine()) != null)   {
202                         // parse strLine to obtain what you want 
203                         if (strLine.contains("Starting audit simulation for resourceName=")) {
204                                 startIndex = strLine.indexOf("resourceName=") + 13;
205                                 endIndex = strLine.indexOf(",");
206                                 rName = strLine.substring(startIndex, endIndex);
207                                 logger.info("testOneResource: rName: " + rName);
208                                 assertEquals("pdp1", rName);
209                         }
210                 }       
211                 fstream.close();
212                 
213                 logger.info("testOneResource: Exiting");
214
215         }
216         
217         /*
218          * Tests designation logic when two functioning resources are in play.
219          * Designation should alternate between resources.
220          * 
221          * Note: console.log must be examined to ascertain whether or not this test
222          * was successful. A quick way of examining the log is to search for the
223          * string "audit simulation":
224          * 
225          * As you can see from the "dbAuditSimulate" method, when it executes, it
226          * logs the "Starting audit simulation..." message and when it finishes, it
227          * logs the "Finished audit simulation..." message. By looking for these
228          * messages, you can verify that the audits are run by the proper resource.
229          * For example, when testFourResourcesOneDead is run, you should see a
230          * Starting.../Finished... sequence for pdp1, followed by a
231          * Starting.../Finished... sequence for pdp2, followed by a
232          * Starting.../Finished... sequence for pdp4 (pdp3 is skipped as it's
233          * dead/hung), followed by a Starting.../Finished... sequence for pdp1, etc.
234          */
235         @Ignore
236         @Test
237         public void testTwoResources() throws Exception {
238                 
239                 logger.info("testTwoResources: Entering");
240                 
241                 /*
242                  * Start audit for pdp1.
243                  */
244                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
245                 integrityAudit.startAuditThread();
246                 
247                 /*
248                  * Start audit for pdp2.
249                  */
250                 Properties properties2 = new Properties();
251                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
252                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
253                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
254                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
255                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
256                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
257                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
258                 String persistenceUnit2 = "testPU";
259                 String resourceName2 = "pdp2";
260                 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
261                 integrityAudit2.startAuditThread();
262                 
263                 /*
264                  * Sleep long enough to allow
265                  * 
266                  * 1) pdp1 to run audit (15 seconds)
267                  * 
268                  * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
269                  * 
270                  * 3) pdp2 to run audit (15 seconds)
271                  * 
272                  * 4) Logic to detect that pdp2 is stale and designate pdp1 (30 seconds)
273                  * 
274                  * 5) pdp1 to run audit (15 seconds)
275                  */
276                 Thread.sleep(120000);
277                 
278                 logger.info("testTwoResources: Stopping audit threads");
279                 integrityAudit.stopAuditThread();
280                 integrityAudit2.stopAuditThread();
281                 
282                 FileInputStream fstream = new FileInputStream(TEST_LOG);
283                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
284                 String strLine;
285                 int startIndex;
286                 int endIndex;
287                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp1"));
288                 ArrayList<String> delegates = new ArrayList<String>();
289                 while ((strLine = br.readLine()) != null)   {
290                         /* parse strLine to obtain what you want */
291                         if (strLine.contains("Starting audit simulation for resourceName=")) {
292                                 startIndex = strLine.indexOf("resourceName=") + 13;
293                                 endIndex = strLine.indexOf(",");
294                                 
295                                 String rName = strLine.substring(startIndex, endIndex);
296                                 
297                                 delegates.add(rName);
298                         }
299                 }
300                                 
301                 for (String delegate: delegates) {
302                         logger.info("testTwoResources: delegate: " + delegate);
303                 }
304                 
305                 fstream.close();
306                                 
307                 assertTrue(expectedResult.equals(delegates));
308                 
309                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3);
310                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
311                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
312                 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
313         
314                 logger.info("testTwoResources: Exiting");
315
316         }
317         
318         /*
319          * Tests designation logic when two functioning resources are in play, each
320          * with different PUs. Audits for "testPU" and "integrityAuditPU" should run
321          * simultaneously. Designation should not alternate.
322          * 
323          * Note: console.log must be examined to ascertain whether or not this test
324          * was successful.
325          */
326         @Ignore
327         @Test
328         public void testTwoResourcesDifferentPus() throws Exception {
329                 
330                 logger.info("testTwoResourcesDifferentPus: Entering");
331                 
332                 /*
333                  * Start audit for pdp1.
334                  */
335                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
336                 integrityAudit.startAuditThread();
337                 
338                 /*
339                  * Start audit for pdp2.
340                  */
341                 Properties properties2 = new Properties();
342                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
343                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
344                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
345                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
346                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
347                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
348                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
349                 String persistenceUnit2 = "integrityAuditPU";
350                 String resourceName2 = "pdp2";
351                 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
352                 integrityAudit2.startAuditThread();
353                 
354                 /*
355                  * Sleep long enough to allow
356                  * 
357                  * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
358                  * 
359                  * 2) Logic to detect that no other node is available for designation for either pdp1 or pdp2 (60 seconds)
360                  * 
361                  * 3) pdp1 and pdp2 to again run audit simultaneously (15 seconds)
362                  * 
363                  * NOTE: Based on the above, you would think a 100000ms sleep would be appropriate,
364                  * but for some reason, when all tests are run this test errors.   
365                  */
366                 logger.info("testTwoResourcesDifferentPus: Sleeping 80 seconds");
367                 Thread.sleep(100000);
368                 
369                 logger.info("testTwoResourcesDifferentPus: Stopping audit threads");
370                 integrityAudit.stopAuditThread();
371                 integrityAudit2.stopAuditThread();
372                 
373                 FileInputStream fstream = new FileInputStream(TEST_LOG);
374                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
375                 String strLine;
376                 int startIndex;
377                 int endIndex;
378                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp1", "pdp2"));
379                 ArrayList<String> delegates = new ArrayList<String>();
380                 while ((strLine = br.readLine()) != null)   {
381                         /* parse strLine to obtain what you want */
382                         if (strLine.contains("Starting audit simulation for resourceName=")) {
383                                 startIndex = strLine.indexOf("resourceName=") + 13;
384                                 endIndex = strLine.indexOf(",");
385                                 
386                                 String rName = strLine.substring(startIndex, endIndex);
387                                 
388                                 delegates.add(rName);
389                         }
390                 }
391                                 
392                 for (String delegate: delegates) {
393                         logger.info("testTwoResourcesDifferentPus: delegate: " + delegate);
394                 }
395                 
396                 fstream.close();
397                 
398                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4);
399                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
400                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
401                 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
402                 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
403                 
404                 assertTrue(expectedResult.equals(delegates));
405                                 
406                 logger.info("testTwoResourcesDifferentPus: Exiting");
407
408         }
409
410         
411         /*
412          * Tests designation logic when two resources are in play but one of them is
413          * dead/hung. Designation should move to second resource but then get
414          * restored back to original resource when it's discovered that second
415          * resource is dead.
416          * 
417          * Note: console.log must be examined to ascertain whether or not this test
418          * was successful.
419          */
420         @Ignore
421         @Test
422         public void testTwoResourcesOneDead() throws Exception {
423                 
424                 logger.info("testTwoResourcesOneDead: Entering");
425                 
426                 /*
427                  * Start audit for pdp1.
428                  */
429                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
430                 integrityAudit.startAuditThread();
431                 
432                 /*
433                  * Populate DB for pdp2, which will simulate it having registered but then having died.
434                  */
435                 Properties properties2 = new Properties();
436                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
437                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
438                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
439                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
440                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
441                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
442                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
443                 String persistenceUnit2 = "testPU";
444                 String resourceName2 = "pdp2";
445                 new DbDAO(resourceName2, persistenceUnit2, properties2);
446                 
447                 /*
448                  * Sleep long enough to allow
449                  * 
450                  * 1) pdp1 to run audit (15 seconds)
451                  * 
452                  * 2) Logic to detect that other node, pdp2, is not available for designation (60 seconds)
453                  * 
454                  * 3) pdp1 to run audit again (15 seconds)
455                  */
456                 logger.info("testTwoResourcesOneDead: Sleeping 100 seconds");
457                 Thread.sleep(100000);
458                 
459                 logger.info("testTwoResourcesOneDead: Stopping audit thread");
460                 integrityAudit.stopAuditThread();
461                 
462                 FileInputStream fstream = new FileInputStream(TEST_LOG);
463                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
464                 String strLine;
465                 int startIndex;
466                 int endIndex;
467                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp1"));
468                 ArrayList<String> delegates = new ArrayList<String>();
469                 while ((strLine = br.readLine()) != null)   {
470                         /* parse strLine to obtain what you want */
471                         if (strLine.contains("Starting audit simulation for resourceName=")) {
472                                 startIndex = strLine.indexOf("resourceName=") + 13;
473                                 endIndex = strLine.indexOf(",");
474                                 
475                                 String rName = strLine.substring(startIndex, endIndex);
476                                 
477                                 delegates.add(rName);
478                         }
479                 }
480                                 
481                 for (String delegate: delegates) {
482                         logger.info("testTwoResourcesOneDead: delegate: " + delegate);
483                 }
484                 
485                 fstream.close();
486                 
487                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2);
488                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
489                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
490                                 
491                 logger.info("testTwoResourcesOneDead: Exiting");
492
493         }
494         
495         
496         /*
497          * Tests designation logic when three functioning resources are in play.  Designation should
498          * round robin among resources.
499          * 
500          * Note: console.log must be examined to ascertain whether or not this test was successful.
501          */
502         @Ignore
503         @Test
504         public void testThreeResources() throws Exception {
505                 
506                 logger.info("testThreeResources: Entering");
507                 
508                 /*
509                  * Start audit for pdp1.
510                  */
511                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
512                 integrityAudit.startAuditThread();
513                 
514                 /*
515                  * Start audit for pdp2.
516                  */
517                 Properties properties2 = new Properties();
518                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
519                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
520                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
521                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
522                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
523                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
524                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
525                 String persistenceUnit2 = "testPU";
526                 String resourceName2 = "pdp2";
527                 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
528                 integrityAudit2.startAuditThread();
529                 
530                 /*
531                  * Start audit for pdp3.
532                  */
533                 Properties properties3 = new Properties();
534                 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
535                 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
536                 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
537                 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
538                 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
539                 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
540                 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
541                 String persistenceUnit3 = "testPU";
542                 String resourceName3 = "pdp3";
543                 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
544                 integrityAudit3.startAuditThread();
545                 
546                 /*
547                  * Sleep long enough to allow
548                  * 
549                  * 1) pdp1 to run audit (15 seconds)
550                  * 
551                  * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
552                  * 
553                  * 3) pdp2 to run audit (15 seconds)
554                  * 
555                  * 4) Logic to detect that pdp2 is stale and designate pdp3 (30 seconds)
556                  * 
557                  * 5) pdp3 to run audit (15 seconds)
558                  * 
559                  * 6) Logic to detect that pdp3 is stale and designate pdp1 (30 seconds)
560                  * 
561                  * 7) pdp1 to run audit (15 seconds)
562                  */
563                 logger.info("testThreeResources: Sleeping 160 seconds");
564                 Thread.sleep(160000);
565                 
566                 logger.info("testThreeResources: Stopping threads");
567                 integrityAudit.stopAuditThread();
568                 integrityAudit2.stopAuditThread();
569                 integrityAudit3.stopAuditThread();
570                 
571                 FileInputStream fstream = new FileInputStream(TEST_LOG);
572                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
573                 String strLine;
574                 int startIndex;
575                 int endIndex;
576                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1"));
577                 ArrayList<String> delegates = new ArrayList<String>();
578                 while ((strLine = br.readLine()) != null)   {
579                         /* parse strLine to obtain what you want */
580                         if (strLine.contains("Starting audit simulation for resourceName=")) {
581                                 startIndex = strLine.indexOf("resourceName=") + 13;
582                                 endIndex = strLine.indexOf(",");
583                                 
584                                 String rName = strLine.substring(startIndex, endIndex);
585                                 
586                                 delegates.add(rName);
587                         }
588                 }
589                 
590                 for (String delegate: delegates) {
591                         logger.info("testThreeResources: delegate: " + delegate);
592                 }
593                 
594                 fstream.close();
595                 
596                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 3);
597                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
598                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
599                 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
600                 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
601                                 
602                 logger.info("testThreeResources: Exiting");
603
604         }
605         
606         /*
607          * Tests designation logic when four functioning resources are in play, two
608          * with one PU, two with another. Audits for "testPU" and "integrityAuditPU" should run
609          * simultaneously. Designation should alternate between resources for each of the two
610          * persistence units.
611          * 
612          * Note: console.log must be examined to ascertain whether or not this test
613          * was successful.
614          */
615         @Ignore
616         @Test
617         public void testFourResourcesDifferentPus() throws Exception {
618                 
619                 logger.info("testFourResourcesDifferentPus: Entering");
620                 
621                 /*
622                  * Start audit for pdp1, testPU.
623                  */
624                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
625                 integrityAudit.startAuditThread();
626                 
627                 /*
628                  * Start audit for pdp2, integrityAuditPU.
629                  */
630                 Properties properties2 = new Properties();
631                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
632                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
633                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
634                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
635                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
636                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
637                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
638                 String persistenceUnit2 = "integrityAuditPU";
639                 String resourceName2 = "pdp2";
640                 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
641                 integrityAudit2.startAuditThread();
642                 
643                 /*
644                  * Start audit for pdp3, testPU.
645                  */
646                 Properties properties3 = new Properties();
647                 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
648                 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
649                 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
650                 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
651                 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
652                 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
653                 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
654                 String persistenceUnit3 = "testPU";
655                 String resourceName3 = "pdp3";
656                 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
657                 integrityAudit3.startAuditThread();
658                 
659                 /*
660                  * Start audit for pdp4, integrityAuditPU.
661                  */
662                 Properties properties4 = new Properties();
663                 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
664                 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
665                 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
666                 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
667                 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
668                 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
669                 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
670                 String persistenceUnit4 = "integrityAuditPU";
671                 String resourceName4 = "pdp4";
672                 IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4);
673                 integrityAudit4.startAuditThread();
674                 
675                 /*
676                  * Sleep long enough to allow
677                  * 
678                  * 1) pdp1 and pdp2 to run audit simultaneously (15 seconds)
679                  * 
680                  * 2) Logic to detect that pdp1 and pdp2 are stale and designate pdp3 (one's counterpart) and pdp4 (two's counterpart) (30 seconds)
681                  * 
682                  * 3) pdp3 and pdp4 to run audit simultaneously (15 seconds)
683                  * 
684                  * 4) Logic to detect that pdp3 and pdp4 are stale and designate pdp1 (three's counterpart) and pdp2 (four's counterpart) (30 seconds)
685                  * 
686                  * 5) pdp1 and pdp2 to run audit simultaneously (15 seconds)
687                  */
688                 logger.info("testFourResourcesDifferentPus: Sleeping 120 seconds");
689                 Thread.sleep(120000);
690                 
691                 logger.info("testFourResourcesDifferentPus: Stopping threads");
692                 integrityAudit.stopAuditThread();
693                 integrityAudit2.stopAuditThread();
694                 integrityAudit3.stopAuditThread();
695                 integrityAudit4.stopAuditThread();
696                 
697                 FileInputStream fstream = new FileInputStream(TEST_LOG);
698                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
699                 String strLine;
700                 int startIndex;
701                 int endIndex;
702                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp4", "pdp1", "pdp2"));
703                 ArrayList<String> delegates = new ArrayList<String>();
704                 while ((strLine = br.readLine()) != null)   {
705                         /* parse strLine to obtain what you want */
706                         if (strLine.contains("Starting audit simulation for resourceName=")) {
707                                 startIndex = strLine.indexOf("resourceName=") + 13;
708                                 endIndex = strLine.indexOf(",");
709                                 
710                                 String rName = strLine.substring(startIndex, endIndex);
711                                 
712                                 delegates.add(rName);
713                         }
714                 }
715                                 
716                 for (String delegate: delegates) {
717                         logger.info("testFourResourcesDifferentPus: delegate: " + delegate);
718                 }
719                 
720                 fstream.close();
721                 
722                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6);
723                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
724                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
725                 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
726                 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
727                 assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4)));
728                 assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5)));
729                                 
730                 logger.info("testFourResourcesDifferentPus: Exiting");
731
732         }
733         
734         /*
735          * Tests designation logic when four resources are in play but one is not
736          * functioning. Designation should round robin among functioning resources
737          * only.
738          * 
739          * Note: console.log must be examined to ascertain whether or not this test
740          * was successful.
741          */
742         @Ignore
743         @Test
744         public void testFourResourcesOneDead() throws Exception {
745                 
746                 logger.info("testFourResourcesOneDead: Entering");
747                 
748                 /*
749                  * Start audit for pdp1.
750                  */
751                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
752                 integrityAudit.startAuditThread();
753                 
754                 /*
755                  * Start audit for pdp2.
756                  */
757                 Properties properties2 = new Properties();
758                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
759                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
760                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
761                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
762                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
763                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
764                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
765                 String persistenceUnit2 = "testPU";
766                 String resourceName2 = "pdp2";
767                 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
768                 integrityAudit2.startAuditThread();
769                 
770                 /*
771                  * Populate DB for pdp3, which will simulate it having registered but then having died.
772                  */
773                 Properties properties3 = new Properties();
774                 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
775                 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
776                 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
777                 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
778                 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
779                 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
780                 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
781                 String persistenceUnit3 = "testPU";
782                 String resourceName3 = "pdp3";
783                 new DbDAO(resourceName3, persistenceUnit3, properties3);
784                 
785                 /*
786                  * Start audit for pdp4.
787                  */
788                 Properties properties4 = new Properties();
789                 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
790                 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
791                 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
792                 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
793                 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
794                 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
795                 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
796                 String persistenceUnit4 = "testPU";
797                 String resourceName4 = "pdp4";
798                 IntegrityAudit integrityAudit4 = new IntegrityAudit(resourceName4, persistenceUnit4, properties4);
799                 integrityAudit4.startAuditThread();
800                 
801                 /*
802                  * Sleep long enough to allow
803                  * 
804                  * 1) pdp1 to run audit (15 seconds)
805                  * 
806                  * 2) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
807                  * 
808                  * 3) pdp2 to run audit (15 seconds)
809                  * 
810                  * 4) Logic to detect that pdp2 is stale and designate pdp4 (30 seconds)
811                  * 
812                  * 5) pdp4 to run audit (15 seconds)
813                  * 
814                  * 6) Logic to detect that pdp4 is stale and designate pdp1 (30 seconds)
815                  * 
816                  * 7) pdp1 to run audit (15 seconds)
817                  * 
818                  * 8) Logic to detect that pdp1 is stale and designate pdp2 (30 seconds)
819                  * 
820                  * 7) pdp2 to run audit (15 seconds)
821                  */
822                 logger.info("testFourResourcesOneDead: Sleeping 210 seconds");
823                 Thread.sleep(210000);
824                 
825                 logger.info("testFourResourcesOneDead: Stopping threads");              
826                 integrityAudit.stopAuditThread();
827                 integrityAudit2.stopAuditThread();
828                 integrityAudit4.stopAuditThread();
829                 
830                 FileInputStream fstream = new FileInputStream(TEST_LOG);
831                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
832                 String strLine;
833                 int startIndex;
834                 int endIndex;
835                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp4", "pdp1", "pdp2", "pdp4"));
836                 ArrayList<String> delegates = new ArrayList<String>();
837                 while ((strLine = br.readLine()) != null)   {
838                         /* parse strLine to obtain what you want */
839                         if (strLine.contains("Starting audit simulation for resourceName=")) {
840                                 startIndex = strLine.indexOf("resourceName=") + 13;
841                                 endIndex = strLine.indexOf(",");
842                                 
843                                 String rName = strLine.substring(startIndex, endIndex);
844
845                                 delegates.add(rName);
846                         }
847                 }
848                 
849                 for (String delegate : delegates) {
850                         logger.info("testFourResourcesOneDead: delegate: " + delegate);
851                 }
852
853                 fstream.close();
854                 
855                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 6);
856                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
857                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
858                 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
859                 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
860                 assertTrue("delegate 4 is " + expectedResult.get(4), expectedResult.get(4).equals(delegates.get(4)));
861                 assertTrue("delegate 5 is " + expectedResult.get(5), expectedResult.get(5).equals(delegates.get(5)));
862                                 
863                 logger.info("testFourResourcesOneDead: Exiting");
864
865         }
866         
867         /*
868          * Tests designation logic when four resources are in play but only one is
869          * functioning. Designation should remain with sole functioning resource.
870          * 
871          * Note: console.log must be examined to ascertain whether or not this test
872          * was successful.
873          */
874         @Ignore
875         @Test
876         public void testFourResourcesThreeDead() throws Exception {
877                                 
878                 logger.info("testFourResourcesThreeDead: Entering");
879                 
880                 /*
881                  * Populate DB for pdp1, which will simulate it having registered but then having died.
882                  */
883                 new DbDAO(resourceName, persistenceUnit, properties);
884
885                 
886                 /*
887                  * Populate DB for pdp2, which will simulate it having registered but then having died.
888                  */
889                 Properties properties2 = new Properties();
890                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
891                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
892                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
893                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
894                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
895                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
896                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
897                 String persistenceUnit2 = "testPU";
898                 String resourceName2 = "pdp2";
899                 new DbDAO(resourceName2, persistenceUnit2, properties2);
900
901                 /*
902                  * Start audit for pdp3.
903                  */
904                 Properties properties3 = new Properties();
905                 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
906                 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
907                 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
908                 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
909                 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
910                 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
911                 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
912                 String persistenceUnit3 = "testPU";
913                 String resourceName3 = "pdp3";
914                 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
915                 integrityAudit3.startAuditThread();
916                 
917                 /*
918                  * Populate DB for pdp4, which will simulate it having registered but then having died.
919                  */
920                 Properties properties4 = new Properties();
921                 properties4.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
922                 properties4.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
923                 properties4.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
924                 properties4.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
925                 properties4.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
926                 properties4.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
927                 properties4.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
928                 String persistenceUnit4 = "testPU";
929                 String resourceName4 = "pdp4";
930                 new DbDAO(resourceName4, persistenceUnit4, properties4);
931                                 
932                 /*
933                  * Sleep long enough to allow
934                  * 
935                  * 1) pdp3 to discover that all other designation candidates are stale (30 seconds)
936                  * 
937                  * 1) pdp3 to run audit (15 seconds)
938                  * 
939                  * 2) Logic to detect that no other nodes are available for designation (60 seconds)
940                  * 
941                  * 3) pdp3 to run audit again (15 seconds)
942                  */
943                 logger.info("testFourResourcesThreeDead: Sleeping 130 seconds");
944                 Thread.sleep(130000);
945                 
946                 logger.info("testFourResourcesThreeDead: Stopping thread");
947                 integrityAudit3.stopAuditThread();
948                 
949                 FileInputStream fstream = new FileInputStream(TEST_LOG);
950                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
951                 String strLine;
952                 int startIndex;
953                 int endIndex;
954                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp3", "pdp3"));
955                 ArrayList<String> delegates = new ArrayList<String>();
956                 while ((strLine = br.readLine()) != null)   {
957                         /* parse strLine to obtain what you want */
958                         if (strLine.contains("Starting audit simulation for resourceName=")) {
959                                 startIndex = strLine.indexOf("resourceName=") + 13;
960                                 endIndex = strLine.indexOf(",");
961                                 
962                                 String rName = strLine.substring(startIndex, endIndex);
963
964                                 delegates.add(rName);
965                         }
966                 }
967                 
968                 for (String delegate : delegates) {
969                         logger.info("testFourResourcesThreeDead: delegate: " + delegate);
970                 }
971
972                 fstream.close();
973                 
974                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 2);
975                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
976                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
977                 
978                 logger.info("testFourResourcesThreeDead: Exiting");
979
980         }
981
982
983         /*
984          * Tests designation logic when the designated node dies and is no longer
985          * current
986          * 
987          * Note: console.log must be examined to ascertain whether or not this test
988          * was successful.
989          */
990         @Ignore
991         @Test
992         public void testDesignatedNodeDead() throws Exception {
993                 logger.info("testDesignatedNodeDead: Entering");
994                 
995                 /*
996                  * Instantiate audit object for pdp1.
997                  */
998                 IntegrityAudit integrityAudit = new IntegrityAudit(resourceName, persistenceUnit, properties);
999                 
1000                 /*
1001                  * Start audit for pdp2.
1002                  */
1003                 Properties properties2 = new Properties();
1004                 properties2.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
1005                 properties2.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
1006                 properties2.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
1007                 properties2.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
1008                 properties2.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
1009                 properties2.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
1010                 properties2.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
1011                 String persistenceUnit2 = "testPU";
1012                 String resourceName2 = "pdp2";
1013                 IntegrityAudit integrityAudit2 = new IntegrityAudit(resourceName2, persistenceUnit2, properties2);
1014
1015                 /*
1016                  * Instantiate audit object for pdp3.
1017                  */
1018                 Properties properties3 = new Properties();
1019                 properties3.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
1020                 properties3.put(IntegrityAuditProperties.DB_URL, IntegrityAuditProperties.DEFAULT_DB_URL);
1021                 properties3.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
1022                 properties3.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
1023                 properties3.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
1024                 properties3.put(IntegrityAuditProperties.NODE_TYPE, "pdp_xacml");
1025                 properties3.put(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS, "5");
1026                 String persistenceUnit3 = "testPU";
1027                 String resourceName3 = "pdp3";
1028                 IntegrityAudit integrityAudit3 = new IntegrityAudit(resourceName3, persistenceUnit3, properties3);
1029                                 
1030                 // Start audit on pdp1
1031                 integrityAudit.startAuditThread();                      
1032
1033                 // Sleep long enough for pdp1 figure out that it should be auditing and start the audit.
1034                 Thread.sleep(500); 
1035                 
1036                 // Start the auditing threads on other nodes.
1037                 integrityAudit2.startAuditThread();     
1038                 integrityAudit3.startAuditThread();             
1039                 
1040                 // Sleep long enough to ensure the other two audits have registered. 
1041                 Thread.sleep(500); 
1042                 
1043                 // Kill audit on pdp1
1044                 integrityAudit.stopAuditThread();
1045                 
1046                 // Sleep long enough for pdp1 to get stale and pdp2 to take over
1047                 Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); 
1048                 
1049                 // Start audit thread on pdp1 again.
1050                 integrityAudit.startAuditThread(); 
1051                 
1052                 // Sleep long enough for pdp2 to complete its audit and get stale, at
1053                 // which point pdp3 should take over
1054                 Thread.sleep((AuditThread.AUDIT_SIMULATION_SLEEP_INTERVAL * AuditThread.AUDIT_SIMULATION_ITERATIONS)
1055                                 + AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR);
1056                 
1057                 // Kill audit on pdp3
1058                 logger.info("testDesignatedNodeDead: Killing audit on pdp3");
1059                 integrityAudit3.stopAuditThread();
1060                 
1061                 // Sleep long enough for pdp3 to get stale and pdp1 to take over
1062                 Thread.sleep(AuditThread.AUDIT_COMPLETION_INTERVAL + FUDGE_FACTOR); 
1063                                 
1064                 FileInputStream fstream = new FileInputStream(TEST_LOG);
1065                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
1066                 String strLine;
1067                 int startIndex;
1068                 int endIndex;
1069                 ArrayList<String> expectedResult = new ArrayList<String>(Arrays.asList("pdp1", "pdp2", "pdp3", "pdp1"));
1070                 ArrayList<String> delegates = new ArrayList<String>();
1071                 while ((strLine = br.readLine()) != null)   {
1072                         /* parse strLine to obtain what you want */
1073                         if (strLine.contains("Starting audit simulation for resourceName=")) {
1074                                 startIndex = strLine.indexOf("resourceName=") + 13;
1075                                 endIndex = strLine.indexOf(",");
1076                                 
1077                                 String rName = strLine.substring(startIndex, endIndex);
1078                                 
1079                                 delegates.add(rName);
1080                         }
1081                 }
1082                 fstream.close();
1083                 
1084                 // Stop remaining threads.
1085                 logger.info("testDesignatedNodeDead: Stopping remaining threads");
1086                 integrityAudit.stopAuditThread();
1087                 integrityAudit2.stopAuditThread();
1088                 
1089                 for (String delegate: delegates) {
1090                         logger.info("testDesignatedNodeDead: delegate: " + delegate);
1091                 }
1092                 
1093                 assertTrue("delegate count only " + delegates.size(), delegates.size() >= 4);
1094                 assertTrue("delegate 0 is " + expectedResult.get(0), expectedResult.get(0).equals(delegates.get(0)));
1095                 assertTrue("delegate 1 is " + expectedResult.get(1), expectedResult.get(1).equals(delegates.get(1)));
1096                 assertTrue("delegate 2 is " + expectedResult.get(2), expectedResult.get(2).equals(delegates.get(2)));
1097                 assertTrue("delegate 3 is " + expectedResult.get(3), expectedResult.get(3).equals(delegates.get(3)));
1098                 
1099                 logger.info("testDesignatedNodeDead: Exiting");
1100         }
1101 }