[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / ia / DbAuditCompareEntriesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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.onap.policy.pap.ia;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Properties;
30
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.EntityTransaction;
34 import javax.persistence.Persistence;
35
36 import org.apache.commons.lang3.SerializationUtils;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.policy.common.ia.DbAudit;
43 import org.onap.policy.common.ia.DbDAO;
44 import org.onap.policy.common.ia.IntegrityAuditProperties;
45 import org.onap.policy.common.ia.jpa.IntegrityAuditEntity;
46 import org.onap.policy.common.im.jpa.ForwardProgressEntity;
47 import org.onap.policy.common.im.jpa.ResourceRegistrationEntity;
48 import org.onap.policy.common.im.jpa.StateManagementEntity;
49 import org.onap.policy.jpa.BackUpMonitorEntity;
50
51 public class DbAuditCompareEntriesTest {
52
53         private static Log logger = LogFactory.getLog(DbAuditCompareEntriesTest.class);
54         private DbDAO dbDAO;
55         private String persistenceUnit;
56         private Properties properties;
57         private String resourceName;
58         private String dbDriver;
59         private String dbUrl;
60         private String dbUser;
61         private String dbPwd;
62         private String siteName;
63         private String nodeType;
64         
65         @Before
66         public void setUp() throws Exception {
67                 logger.info("setUp: Entering");
68
69                 properties = new Properties();
70                 properties.put(IntegrityAuditProperties.DB_DRIVER, IntegrityAuditProperties.DEFAULT_DB_DRIVER);
71                 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
72                 properties.put(IntegrityAuditProperties.DB_USER, IntegrityAuditProperties.DEFAULT_DB_USER);
73                 properties.put(IntegrityAuditProperties.DB_PWD, IntegrityAuditProperties.DEFAULT_DB_PWD);
74                 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
75                 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
76
77                 dbDriver = IntegrityAuditProperties.DEFAULT_DB_DRIVER;
78                 dbUrl = "jdbc:h2:file:./sql/xacmlTest";
79                 dbUser = IntegrityAuditProperties.DEFAULT_DB_USER;
80                 dbPwd = IntegrityAuditProperties.DEFAULT_DB_PWD;
81                 siteName = "SiteA";
82                 nodeType = "pap";
83                 persistenceUnit = "testPapPU";
84                 resourceName = "siteA.pap1";
85                 
86                 //Clean the iaTest DB table for IntegrityAuditEntity entries
87                 cleanDb(persistenceUnit, properties);
88                 
89                 logger.info("setUp: Exiting");
90         }
91
92         @After
93         public void tearDown() throws Exception {
94                 logger.info("tearDown: Entering");
95                 //nothing to do
96                 logger.info("tearDown: Exiting");
97         }
98         
99         public void cleanDb(String persistenceUnit, Properties properties){
100                 logger.debug("cleanDb: enter");
101
102                 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
103                 
104                 EntityManager em = emf.createEntityManager();
105                 // Start a transaction
106                 EntityTransaction et = em.getTransaction();
107
108                 et.begin();
109
110                 // Clean up the DB
111                 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
112
113                 // commit transaction
114                 et.commit();
115                 em.close();
116                 logger.debug("cleanDb: exit");
117         }
118         
119
120         /*
121          * Tests that a comparison between hashsets is successful if
122          * the entries match
123          */
124         @Test
125         public void runAllTests() throws Exception {
126                 logger.info("runAllTests: Entering");
127                 
128                 
129                 testIntegrityAuditEntity();
130                 testBackupMonitorEntity();
131                 testStateManagementEntity();
132                 testForwardProgressEntity();
133                 testResourceRegistrationEntity();
134                 
135                 //clean up the IntegrityAuditEntity table
136                 cleanDb(persistenceUnit, properties);
137                 
138                 logger.info("runAllTests: Exit");
139         }
140
141
142         public void testIntegrityAuditEntity() throws Exception {
143                 logger.info("testIntegrityAuditEntity: Entering");
144                 
145                 dbDAO = new DbDAO(resourceName, persistenceUnit, properties);
146                 DbAudit dbAudit = new DbAudit(dbDAO);
147                 
148                 String className = null;
149                 //There is only one entry IntegrityAuditEntity, but we will check anyway
150                 HashSet<String> classNameSet = dbDAO.getPersistenceClassNames();
151                 for(String c : classNameSet){
152                         if (c.equals("org.onap.policy.common.ia.jpa.IntegrityAuditEntity")){
153                                 className = c;
154                         }
155                 }
156                 String resourceName1 = resourceName;
157                 String resourceName2 = resourceName;
158                 
159                 IntegrityAuditEntity entry1 = new IntegrityAuditEntity();
160                 IntegrityAuditEntity entry2 = new IntegrityAuditEntity();
161                 Date date = new Date();
162                 
163                 /*
164                  * Two entries with the same field values
165                  */
166                 entry1.setDesignated(false);
167                 entry1.setJdbcDriver(dbDriver);
168                 entry1.setJdbcPassword(dbPwd);
169                 entry1.setJdbcUrl(dbUrl);
170                 entry1.setJdbcUser(dbUser);
171                 entry1.setLastUpdated(date);
172                 entry1.setNodeType(nodeType);
173                 entry1.setPersistenceUnit(persistenceUnit);
174                 entry1.setResourceName(resourceName1);
175                 entry1.setSite(siteName);
176                 
177                 entry2 = SerializationUtils.clone(entry1);
178                 
179                 dbAudit.writeAuditDebugLog(className, resourceName1, resourceName2, entry1, entry2);
180                 
181                 HashMap<Object, Object> myEntries = new HashMap<>();
182                 HashMap<Object, Object> theirEntries = new HashMap<>();
183                 
184                 myEntries.put("pdp1", entry1);
185                 theirEntries.put("pdp1", entry2);
186                                 
187                 HashSet<Object> result = dbAudit.compareEntries(myEntries, theirEntries);
188                 
189                 /*
190                  * Assert that there are no mismatches returned
191                  */
192                 assertTrue(result.isEmpty());
193                 
194                 /*
195                  * ************************************
196                  * Now test with a mis-matched entry
197                  * ************************************
198                  */
199                 
200                 /*
201                  * Change the entry2 to different designated value
202                  */
203                 entry2.setDesignated(true);
204                                 
205                 myEntries = new HashMap<>();
206                 theirEntries = new HashMap<>();
207                 
208                 myEntries.put("pdp1", entry1);
209                 theirEntries.put("pdp1", entry2);
210                 
211                 result = dbAudit.compareEntries(myEntries, theirEntries);
212                 
213                 /*
214                  * Assert that there was one mismatch
215                  */
216                 assertEquals(1, result.size());
217                 logger.info("testIntegrityAuditEntity: Exit");
218         }
219         
220         void testBackupMonitorEntity() throws Exception {
221                 logger.info("testBackupMonitorEntity: Entering");
222                 
223                 dbDAO = new DbDAO(resourceName, persistenceUnit, properties);
224                 DbAudit dbAudit = new DbAudit(dbDAO);
225                 
226                 BackUpMonitorEntity entry1 = new BackUpMonitorEntity();
227                 BackUpMonitorEntity entry2 = new BackUpMonitorEntity();
228                 
229                 // Two entries with the same field values
230                  
231                 
232                 entry1.setFlag("flag1");
233                 entry1.setResoruceNodeName("node1");
234                 entry1.setResourceName("resourceName");
235                 entry1.setTimeStamp(new Date());
236                 
237                 // Clone the first entry
238                 entry2 = SerializationUtils.clone(entry1);
239                 
240                 HashMap<Object, Object> myEntries = new HashMap<>();
241                 HashMap<Object, Object> theirEntries = new HashMap<>();
242                 
243                 myEntries.put("pdp1", entry1);
244                 theirEntries.put("pdp1", entry2);
245                                 
246                 HashSet<Object> result = dbAudit.compareEntries(myEntries, theirEntries);
247                 
248                 
249                 // Assert that there are no mismatches returned
250                  
251                 assertTrue(result.isEmpty());
252                 
253                 
254                  /* ************************************
255                  * Now test with a mis-matched entry
256                  * ************************************/
257                  
258                 
259                 
260                 // Change a field on entry2
261                  
262                 entry2.setFlag("flag2");
263                                 
264                 myEntries = new HashMap<>();
265                 theirEntries = new HashMap<>();
266                 
267                 myEntries.put("pdp1", entry1);
268                 theirEntries.put("pdp1", entry2);
269                 
270                 result = dbAudit.compareEntries(myEntries, theirEntries);
271                 
272                 
273                 //Assert that there was one mismatch
274                  
275                 assertEquals(1, result.size());
276                 logger.info("testBackupMonitorEntity: Exit");
277         }
278
279         void testStateManagementEntity() throws Exception {
280                 logger.info("testStateManagementEntity: Entering");
281                 
282                 dbDAO = new DbDAO(resourceName, persistenceUnit, properties);
283                 DbAudit dbAudit = new DbAudit(dbDAO);
284                 
285                 StateManagementEntity entry1 = new StateManagementEntity();
286                 StateManagementEntity entry2 = new StateManagementEntity();
287                 
288                 // Two entries with the same field values
289                 
290                 entry1.setAdminState("locked");
291                 entry1.setAvailStatus("null");
292                 entry1.setModifiedDate(new Date());
293                 entry1.setOpState("enabled");
294                 entry1.setResourceName("myResource");
295                 entry1.setStandbyStatus("coldstandby");
296                 
297                 // Clone the first entry
298                 entry2 = SerializationUtils.clone(entry1);
299                 
300                 HashMap<Object, Object> myEntries = new HashMap<>();
301                 HashMap<Object, Object> theirEntries = new HashMap<>();
302                 
303                 myEntries.put("pdp1", entry1);
304                 theirEntries.put("pdp1", entry2);
305                                 
306                 HashSet<Object> result = dbAudit.compareEntries(myEntries, theirEntries);
307                 
308                 
309                 // Assert that there are no mismatches returned
310                  
311                 assertTrue(result.isEmpty());
312                 
313                 
314                  /* ************************************
315                  * Now test with a mis-matched entry
316                  * ************************************/
317                  
318                 
319                 
320                 // Change a field on entry2
321                  
322                 entry2.setAdminState("unlocked");
323                                 
324                 myEntries = new HashMap<>();
325                 theirEntries = new HashMap<>();
326                 
327                 myEntries.put("pdp1", entry1);
328                 theirEntries.put("pdp1", entry2);
329                 
330                 result = dbAudit.compareEntries(myEntries, theirEntries);
331                 
332                 
333                 //Assert that there was one mismatch
334                  
335                 assertEquals(1, result.size());
336                 logger.info("testStateManagementEntity: Exit");
337         }
338         
339         void testForwardProgressEntity() throws Exception {
340                 logger.info("testForwardProgressEntity: Entering");
341                 
342                 dbDAO = new DbDAO(resourceName, persistenceUnit, properties);
343                 DbAudit dbAudit = new DbAudit(dbDAO);
344                 
345                 ForwardProgressEntity entry1 = new ForwardProgressEntity();
346                 ForwardProgressEntity entry2 = new ForwardProgressEntity();
347
348                 // Two entries with the same field values
349                 
350                 entry1.setFpcCount(123L);
351                 entry1.setLastUpdated(new Date());
352                 entry1.setResourceName("myResource");
353                 
354                 // Clone the first entry
355                 entry2 = SerializationUtils.clone(entry1);
356                 
357                 HashMap<Object, Object> myEntries = new HashMap<Object, Object>();
358                 HashMap<Object, Object> theirEntries = new HashMap<Object, Object>();
359                 
360                 myEntries.put("pdp1", entry1);
361                 theirEntries.put("pdp1", entry2);
362                                 
363                 HashSet<Object> result = dbAudit.compareEntries(myEntries, theirEntries);
364                 
365                 
366                 // Assert that there are no mismatches returned
367                  
368                 assertTrue(result.isEmpty());
369                 
370                 
371                  /* ************************************
372                  * Now test with a mis-matched entry
373                  * ************************************/
374                 
375                 // Change a field on entry2
376                  
377                 entry2.setFpcCount(321L);
378                                 
379                 myEntries = new HashMap<>();
380                 theirEntries = new HashMap<>();
381                 
382                 myEntries.put("pdp1", entry1);
383                 theirEntries.put("pdp1", entry2);
384                 
385                 result = dbAudit.compareEntries(myEntries, theirEntries);
386                 
387                 
388                 //Assert that there was one mismatch
389                  
390                 assertEquals(1, result.size());
391                 logger.info("testForwardProgressEntity: Exit");
392         }
393
394         void testResourceRegistrationEntity() throws Exception {
395                 logger.info("testResourceRegistrationEntity: Entering");
396                 
397                 dbDAO = new DbDAO(resourceName, persistenceUnit, properties);
398                 DbAudit dbAudit = new DbAudit(dbDAO);
399                 
400                 ResourceRegistrationEntity entry1 = new ResourceRegistrationEntity();
401                 ResourceRegistrationEntity entry2 = new ResourceRegistrationEntity();
402                 
403                 // Two entries with the same field values
404                 
405                 entry1.setNodeType("pap");
406                 entry1.setLastUpdated(new Date());
407                 entry1.setResourceName("myResource");
408                 entry1.setResourceUrl("http://nowhere.com");
409                 entry1.setSite("site_1");
410                 
411                 // Clone the first entry
412                 entry2 = SerializationUtils.clone(entry1);
413                 
414                 HashMap<Object, Object> myEntries = new HashMap<>();
415                 HashMap<Object, Object> theirEntries = new HashMap<>();
416                 
417                 myEntries.put("pdp1", entry1);
418                 theirEntries.put("pdp1", entry2);
419                                 
420                 HashSet<Object> result = dbAudit.compareEntries(myEntries, theirEntries);
421                 
422                 
423                 // Assert that there are no mismatches returned
424                  
425                 assertTrue(result.isEmpty());
426                 
427                 
428                  /* ************************************
429                  * Now test with a mis-matched entry
430                  * ************************************/
431                 
432                 // Change a field on entry2
433                  
434                 entry2.setSite("site_1a");
435                                 
436                 myEntries = new HashMap<>();
437                 theirEntries = new HashMap<>();
438                 
439                 myEntries.put("pdp1", entry1);
440                 theirEntries.put("pdp1", entry2);
441                 
442                 result = dbAudit.compareEntries(myEntries, theirEntries);
443                 
444                 
445                 //Assert that there was one mismatch
446                  
447                 assertEquals(1, result.size());
448                 logger.info("testResourceRegistrationEntity: Exit");
449         }
450 }