Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / test / java / org / openecomp / policy / pap / xacml / rest / jpa / PolicyEntityTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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.openecomp.policy.pap.xacml.rest.jpa;
22
23 import static org.junit.Assert.*;
24
25 //import org.apache.commons.logging.Log;
26 //import org.apache.commons.logging.LogFactory;
27 import org.junit.*;
28 import org.openecomp.policy.rest.XACMLRestProperties;
29 import org.openecomp.policy.rest.jpa.ActionBodyEntity;
30 import org.openecomp.policy.rest.jpa.ConfigurationDataEntity;
31 import org.openecomp.policy.rest.jpa.PolicyDBDaoEntity;
32 import org.openecomp.policy.rest.jpa.PolicyEntity;
33
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityManagerFactory;
36 import javax.persistence.EntityTransaction;
37 import javax.persistence.Persistence;
38 import javax.persistence.Query;
39
40 import java.util.Date;
41 import java.util.List;
42 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
43 import org.openecomp.policy.common.logging.flexlogger.Logger;
44
45 import java.util.Properties;
46
47 public class PolicyEntityTest {
48         
49         private static Logger logger = FlexLogger.getLogger(PolicyEntityTest.class);
50         
51 //    @Ignore
52     @Test
53     public void testAllOps(){
54         Properties properties = new Properties();
55         properties.put(XACMLRestProperties.PROP_PAP_DB_DRIVER,"org.h2.Driver");
56         properties.put(XACMLRestProperties.PROP_PAP_DB_URL, "jdbc:h2:file:./sql/xacmlTest");
57         properties.put(XACMLRestProperties.PROP_PAP_DB_USER, "sa");
58         properties.put(XACMLRestProperties.PROP_PAP_DB_PASSWORD, "");
59         EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPapPU", properties);
60         EntityManager em = emf.createEntityManager();
61         // Start a transaction
62         EntityTransaction et = em.getTransaction();
63
64         et.begin();
65         //Make sure the DB is clean
66         em.createQuery("DELETE FROM PolicyDBDaoEntity").executeUpdate();
67         em.createQuery("DELETE FROM PolicyEntity").executeUpdate();
68         em.createQuery("DELETE FROM ConfigurationDataEntity").executeUpdate();
69         em.createQuery("DELETE FROM ActionBodyEntity").executeUpdate();
70
71         //Create a policy object
72         PolicyEntity p1 = new PolicyEntity();
73         
74         //persist the policy    
75         em.persist(p1);
76
77         long policyId1 = p1.getPolicyId();
78         
79         String policyName1 = p1.getPolicyName();
80         
81         int version1 = p1.getVersion();
82         
83         String policyData1 = p1.getPolicyData();
84         
85         ConfigurationDataEntity configData1 = p1.getConfigurationData();
86         String configDataStr1 = (configData1!=null ? "configurationDataId = " + configData1.getConfigurationDataId() : "configurationData is null");
87         
88         ActionBodyEntity actionBody1 = p1.getActionBodyEntity();
89         String actionBodyStr1 = (actionBody1!=null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
90         
91         String createdBy1 = p1.getCreatedBy();
92         
93         Date createdDate1 = p1.getCreatedDate();
94         String createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
95         
96         String description = p1.getDescription();
97         
98         String modifiedBy1 = p1.getModifiedBy();
99         
100         Date modifiedDate1 = p1.getModifiedDate();
101         String modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
102         
103         
104         logger.debug("\n\n********PolicyEntityTest: Local PolicyEntity and Configuration objects before persist*********"
105                         + "\npolicyId1 = " + policyId1
106                         + "\npolicyName1 = " + policyName1
107                         + "\nversion1 = " + version1
108                         + "\npolicyData1 = " + policyData1
109                         + "\nconfigDataStr1 = " + configDataStr1
110                         + "\nactionBodyStr1 = " + actionBodyStr1
111                         + "\nscope = " + p1.getScope()
112                         + "\ncreatedBy1 = " + createdBy1
113                         + "\ncreatedDateStr1 = " + createdDateStr1
114                         + "\ndescription = " + description
115                         + "\nmodifiedBy1 = " + modifiedBy1
116                         + "\nmodifiedDateStr1 = " + modifiedDateStr1
117                         + "\ndeleted = " + p1.isDeleted());
118         
119         //Set policyID
120         p1.setPolicyName("testPID2");
121         
122         //Set policyData
123         p1.setPolicyData("<policy>PolicyData</policy>");
124         
125         //We will NOT set the ConfigurationDataEntity or ActionBodyEntity object just to test that it is optional
126         
127         //set createdBy
128         p1.setCreatedBy("super-admin");
129         
130         //createdDate will be set when it is persisted
131         
132         //set scope
133         p1.setScope("com.test");
134         
135         //set description
136         p1.setDescription("PolicyEntity Description");
137         
138         //set modifiedBy
139         p1.setModifiedBy("super-admin");
140         
141         //modifiedDate will be set when it is persisted
142       
143         //Flush to the DB
144         em.flush();
145         
146         //Now lets get some attribute values
147   
148         policyId1 = p1.getPolicyId();
149         
150         policyName1 = p1.getPolicyName();
151         
152         version1 = p1.getVersion();
153         
154         policyData1 = p1.getPolicyData();
155         
156         configData1 = p1.getConfigurationData();
157         configDataStr1 = (configData1!=null ?  "configurationDataId = " + configData1.getConfigurationDataId() : "configurationData is null");
158         
159         actionBody1 = p1.getActionBodyEntity();
160         actionBodyStr1 = (actionBody1!=null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
161         
162         createdBy1 = p1.getCreatedBy();
163         
164         createdDate1 = p1.getCreatedDate();
165         createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
166         
167         description = p1.getDescription();
168         
169         modifiedBy1 = p1.getModifiedBy();
170         
171         modifiedDate1 = p1.getModifiedDate();
172         modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
173         
174         logger.debug("\n\n********PolicyEntityTest: Local PolicyEntity and Configuration objects after persist*********"
175                         + "\npolicyId1 = " + policyId1
176                         + "\npolicyName1 = " + policyName1
177                         + "\nversion1 = " + version1
178                         + "\npolicyData1 = " + policyData1
179                         + "\nconfigDataStr1 = " + configDataStr1
180                         + "\nactionBodyStr1 = " + actionBodyStr1
181                         + "\nscopeId = " + p1.getScope()
182                         + "\ncreatedBy1 = " + createdBy1
183                         + "\ncreatedDateStr1 = " + createdDateStr1
184                         + "\ndescription = " + description
185                         + "\nmodifiedBy1 = " + modifiedBy1
186                         + "\nmodifiedDateStr1 = " + modifiedDateStr1
187                         + "\ndeleted = " + p1.isDeleted());
188
189         //Now lets fully configure the configurationData and actionBody
190         
191         //Create a ConfigurationDataEntity object and set ID
192       ConfigurationDataEntity c1 = new ConfigurationDataEntity();      
193       
194       ActionBodyEntity a1 = new ActionBodyEntity();
195       
196         //persist the configuration Data
197         em.persist(c1);        
198         
199         c1.setConfigType("OTHER");
200         
201         c1.setConfigBody("ABC");
202         
203         c1.setDescription("ConfigurationDataEntity Description");
204         
205         c1.setCreatedBy("super-admin");
206         
207         c1.setDeleted(true);
208         
209         //persist the action Body
210         
211         em.persist(a1);
212         
213         a1.setActionBody("myActionBody");
214         
215         a1.setActionBodyName("myActionBodyName");
216         
217         a1.setCreatedBy("super-admin");
218         
219         a1.setModifiedBy("super-admin");
220         
221         a1.setDeleted(false);
222         
223         
224         long configurationDataId = c1.getConfigurationDataId();
225         
226         int cdVersion = c1.getVersion();
227         
228         String cdConfigType = c1.getConfigType();
229         
230         String cdConfigBody = c1.getConfigBody();
231         
232         String cdCreatedBy = c1.getCreatedBy();
233         
234         Date cdCreatedDate = c1.getCreatedDate();
235         
236         String cdDescription = c1.getDescription();
237         
238         String cdModifiedBy = c1.getModifiedBy();
239         
240         Date cdModifiedDate = c1.getModifiedDate();
241         
242         logger.debug("\n\n********PolicyEntityTest: Local Configuration object after setting values *********"
243                         + "\nconfigurationDataId = " + configurationDataId
244                         + "\ncdVersion = " + cdVersion
245                         + "\ncdConfigType = " + cdConfigType
246                         + "\ncdConfigBody = " + cdConfigBody
247                         + "\ncdCreatedBy = " + cdCreatedBy
248                         + "\ncdCreatedDate = " + cdCreatedDate
249                         + "\ncdDescription = " + cdDescription
250                         + "\ncdModifiedBy = " + cdModifiedBy
251                         + "\ncdModifiedDate = " + cdModifiedDate
252                         + "\ndeleted = " + c1.isDeleted());        
253         
254
255         
256         logger.debug("\n\n********PolicyEntityTest: Local Action Body object after setting values *********"
257                         + "\nactionBodyId = " + a1.getActionBodyId()
258                         + "\nactionBodyVersion = " + a1.getVersion()
259                         + "\nactionBody = " + a1.getActionBody()
260                         + "\nactionBodyCeatedBy = " + a1.getCreatedBy()
261                         + "\nactionBodyCreatedDate = " + a1.getCreatedDate()
262                         + "\nactionBodyModifiedBy = " + a1.getModifiedBy()
263                         + "\nactionBodyModifiedDate = " + a1.getModifiedDate()
264                         + "\nactionBodyDeleted = " + a1.isDeleted());        
265
266         p1.setScope("mckiou.kevin.kim");        
267         
268         //flush to the db
269         em.flush();
270         
271         //Perform policy selects
272         
273         Query query = em.createQuery("Select p from PolicyEntity p where p.policyId=:pid");
274         Query queryscope = em.createQuery("Select p from PolicyEntity p where p.scope=:s");
275         
276         query.setParameter("pid", p1.getPolicyId());
277         queryscope.setParameter("s", "com.user");
278         
279         //Just test that we are retrieving the right object
280         @SuppressWarnings("rawtypes")
281         List psList = queryscope.getResultList();
282         PolicyEntity px = null;
283         if(!psList.isEmpty()){
284                 //ignores multiple results
285                 px = (PolicyEntity) psList.get(0);
286         }else{
287                 fail("\nPolicyEntityTest: No PolicyEntity using scope DB entry found");
288         }
289         
290         //The scope object on the retrieved policy object should be same as the one we used to find it
291         assertSame(p1,px);
292         
293        
294         //Because getSingleResult() throws an unchecked exception which is an indication of a 
295         //programming error, we are not going to use it.
296         @SuppressWarnings("rawtypes")
297                 List resultList = query.getResultList();
298         PolicyEntity p2 = null;
299         if(!resultList.isEmpty()){
300             // ignores multiple results
301             p2 = (PolicyEntity) resultList.get(0);
302         }else{
303                 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
304         }
305         
306         logger.debug("\n\n********PolicyEntityTest: PolicyEntity object after retrieving from DB BEFORE assigning configurationData*********"
307                         + "\npolicyId2 = " + p2.getPolicyId()
308                         + "\npolicyName2 = " + p2.getPolicyName()
309                         + "\nversion2 = " + p2.getVersion()
310                         + "\npolicyData2 = " + p2.getPolicyData()
311                         + "\nconfigurationData2 = " + (p2.getConfigurationData()!=null ? "configurationDataId = " + p2.getConfigurationData().getConfigurationDataId() : "configurationData is null")
312                         + "\nactionBody2 = " + (p2.getActionBodyEntity()!=null ? "actionBodyId = " + p2.getActionBodyEntity().getActionBodyId() : "actionBody is null")
313                         + "\nscope2 = " + p2.getScope()
314                         + "\ncreatedBy2 = " + p2.getCreatedBy()
315                         + "\ncreatedDate2 = " + p2.getCreatedDate()
316                         + "\ndescription2 = " + p2.getDescription()
317                         + "\nmodifiedBy2 = " + p2.getModifiedBy()
318                         + "\nmodifiedDate2 = " + p2.getModifiedDate()
319                         + "\ndeleted2 = " + p2.isDeleted());
320
321         //Confirm that the retrieved policy object is the same as the persisted object
322         assertSame(p1,p2);
323         
324         //Perform configurationData selects
325         Query query2 = em.createQuery("Select c from ConfigurationDataEntity c where c.configurationDataId=:cid");
326         
327         query2.setParameter("cid", c1.getConfigurationDataId());
328         
329         //Get the database version of the Configuration Data
330                 resultList = query2.getResultList();
331         ConfigurationDataEntity c2 = null;
332         if(!resultList.isEmpty()){
333             // ignores multiple results
334             c2 = (ConfigurationDataEntity) resultList.get(0);
335         }else{
336                 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
337         }
338         
339         logger.debug("\n\n********PolicyEntityTest: Configuration object after retrieving from DB BEFORE assigning to policy*********"
340                         + "\nconfigurationDataId2 = " + c2.getConfigurationDataId()
341                         + "\nversion2 = " + c2.getVersion()
342                         + "\nconfigType2 = " + c2.getConfigType()
343                         + "\nconfigBody2 = " + c2.getConfigBody()
344                         + "\ncreatedBy2 = " + c2.getCreatedBy()
345                         + "\ncreatedDate2 = " + c2.getCreatedDate()
346                         + "\ndescription2 = " + c2.getDescription()
347                         + "\nmodifiedBy2 = " + c2.getModifiedBy()
348                         + "\nmodifiedDate2 = " + c2.getModifiedDate()
349                         + "\ndeleted2 = " + c2.isDeleted());
350         
351         //Confirm the retrieved ConfigurationDataEntity object is the same as the persisted
352         assertSame(c1,c2);
353         
354         //Now assign the configurationData to the policy 
355         p1.setConfigurationData(c1);
356         
357         //Perform actionBody selects
358         Query querya2 = em.createQuery("Select a from ActionBodyEntity a where a.actionBodyId=:aid");
359         
360         querya2.setParameter("aid", a1.getActionBodyId());
361         
362         //Get the database version of the Action Body
363                 resultList = querya2.getResultList();
364         ActionBodyEntity a2 = null;
365         if(!resultList.isEmpty()){
366             // ignores multiple results
367             a2 = (ActionBodyEntity) resultList.get(0);
368         }else{
369                 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
370         }
371         
372         
373         logger.debug("\n\n********PolicyEntityTest: Local Action Body object after retrieving from DB BEFORE assigning to policy *********"
374                         + "\nactionBodyId2 = " + a2.getActionBodyId()
375                         + "\nactionBodyVersion2 = " + a2.getVersion()
376                         + "\nactionBody2 = " + a2.getActionBody()
377                         + "\nactionBodyCeatedBy2 = " + a2.getCreatedBy()
378                         + "\nactionBodyCreatedDate2 = " + a2.getCreatedDate()
379                         + "\nactionBodyModifiedBy2 = " + a2.getModifiedBy()
380                         + "\nactionBodyModifiedDate2 = " + a2.getModifiedDate()
381                         + "\nactionBodyDeleted2 = " + a2.isDeleted());        
382
383         
384         //Confirm the retrieved ActionBodyEntity object is the same as the persisted
385         assertSame(a1,a2);
386         
387         //Now assign the ActionBodyEntity to the policy 
388         p1.setActionBodyEntity(a1);
389
390         em.flush();
391         
392         //Let's retrieve the policy, configurationData and actionBody from the DB and look at them
393         //Here is the policy object
394         resultList = query.getResultList();
395         p2 = null;
396         if(!resultList.isEmpty()){
397             // ignores multiple results
398             p2 = (PolicyEntity) resultList.get(0);
399         }else{
400                 fail("PolicyEntityTest: No PolicyEntity DB entry found");
401         }
402         
403         logger.debug("\n\n********PolicyEntityTest: PolicyEntity object after retrieving from DB AFTER assigning configurationData*********"
404                         + "\npolicyId2 = " + p2.getPolicyId()
405                         + "\npolicyName2 = " + p2.getPolicyName()
406                         + "\nversion2 = " + p2.getVersion()
407                         + "\npolicyData2 = " + p2.getPolicyData()
408                         + "\nconfigurationData2 = " + (p2.getConfigurationData()!=null ? "configurationDataId = " + p2.getConfigurationData().getConfigurationDataId() : "configurationData is null")
409                         + "\nactionBody2 = " + (p2.getActionBodyEntity()!=null ? "actionBodyId = " + p2.getActionBodyEntity().getActionBodyId() : "actionBody is null")
410                         + "\nscope2 = " + p2.getScope()
411                         + "\ncreatedBy2 = " + p2.getCreatedBy()
412                         + "\ncreatedDate2 = " + p2.getCreatedDate()
413                         + "\ndescription2 = " + p2.getDescription()
414                         + "\nmodifiedBy2 = " + p2.getModifiedBy()
415                         + "\nmodifiedDate2 = " + p2.getModifiedDate()
416                         + "\ndeleted2 = " + p2.isDeleted());
417
418         //And now the ConfigurationDataEntity object
419                 resultList = query2.getResultList();
420         c2 = null;
421         if(!resultList.isEmpty()){
422             // ignores multiple results
423             c2 = (ConfigurationDataEntity) resultList.get(0);
424         }else{
425                 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
426         }
427         
428         logger.debug("\n\n********PolicyEntityTest: Configuration object after retrieving from DB AFTER assigning to policy*********"
429                         + "\nconfigurationDataId2 = " + c2.getConfigurationDataId()
430                         + "\nversion2 = " + c2.getVersion()
431                         + "\nconfigType2 = " + c2.getConfigType()
432                         + "\nconfigBody2 = " + c2.getConfigBody()
433                         + "\ncreatedBy2 = " + c2.getCreatedBy()
434                         + "\ncreatedDate2 = " + c2.getCreatedDate()
435                         + "\ndescription2 = " + c2.getDescription()
436                         + "\nmodifiedBy = " + c2.getModifiedBy()
437                         + "\nmodifiedDate = " + c2.getModifiedDate()
438                         + "\ndeleted2 = " + c2.isDeleted());
439         
440         
441         //Get the database version of the Action Body
442                 resultList = querya2.getResultList();
443         a2 = null;
444         if(!resultList.isEmpty()){
445             // ignores multiple results
446             a2 = (ActionBodyEntity) resultList.get(0);
447         }else{
448                 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
449         }
450         
451         
452         logger.debug("\n\n********PolicyEntityTest: Local Action Body object after retrieving from DB AFTER assigning to policy *********"
453                         + "\nactionBodyId2 = " + a2.getActionBodyId()
454                         + "\nactionBodyVersion2 = " + a2.getVersion()
455                         + "\nactionBody2 = " + a2.getActionBody()
456                         + "\nactionBodyCeatedBy2 = " + a2.getCreatedBy()
457                         + "\nactionBodyCreatedDate2 = " + a2.getCreatedDate()
458                         + "\nactionBodyModifiedBy2 = " + a2.getModifiedBy()
459                         + "\nactionBodyModifiedDate2 = " + a2.getModifiedDate()
460                         + "\nactionBodyDeleted2 = " + a2.isDeleted());        
461
462         
463         //****Now lets see if the orphanRemoval=true does anything useful***
464         //Remove the configurationData from the policy relationship
465         
466         p1.setConfigurationData(null);
467         
468         p1.setActionBodyEntity(null);
469         
470         //flush the update to the DB
471         em.flush();
472         
473         //Attempt to retrieve the configuration data object from the db. It should not be there
474         //Reusing the previous query
475                 resultList = query2.getResultList();
476         c2 = null;
477         if(resultList.isEmpty()){
478                 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
479                                 + "\n Success!! No ConfigurationDataEntity DB entry found");
480             
481         }else{
482                 c2 = (ConfigurationDataEntity) resultList.get(0);
483                 fail("\nPolicyEntityTest: ConfigurationDataEntity DB entry found - and none should exist"
484                                 + "\nconfigurationDataId = " + c2.getConfigurationDataId());
485         }
486         
487         //Attempt to retrieve the actionBody data object from the db. It should not be there
488         //Reusing the previous query
489                 resultList = querya2.getResultList();
490         a2 = null;
491         if(resultList.isEmpty()){
492                 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
493                                 + "\n Success!! No ActionBodyEntity DB entry found");
494             
495         }else{
496                 a2 = (ActionBodyEntity) resultList.get(0);
497                 fail("\nPolicyEntityTest: ActionBodyEntity DB entry found - and none should exist"
498                                 + "\nactionBodyId = " + a2.getActionBodyId());
499         }
500         
501         //Now lets put the configurationData and actionBody back into the policy object and see what appears
502         //in the DB after a flush
503         
504         //put c1 back into the persistence context since the orphanRemoval removed it.
505         em.persist(c1);
506         p1.setConfigurationData(c1);
507         
508         em.persist(a1);
509         p1.setActionBodyEntity(a1);
510         
511         em.flush();
512         
513         //retrieve the policy object
514                 resultList = query.getResultList();
515         p2 = null;
516         if(!resultList.isEmpty()){
517             // ignores multiple results
518             p2 = (PolicyEntity) resultList.get(0);
519         }else{
520                 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
521         }
522         
523         //output what we policy object found
524         logger.debug("\n\n********PolicyEntityTest: PolicyEntity object after again adding ConfigurationDataEntity and retrieving from DB*********"
525                         + "\npolicyId2 = " + p2.getPolicyId()
526                         + "\npolicyName2 = " + p2.getPolicyName()
527                         + "\nversion2 = " + p2.getVersion()
528                         + "\npolicyData2 = " + p2.getPolicyData()
529                         + "\nconfigurationData2 = " + (p2.getConfigurationData()!=null ? "configurationDataId = " + p2.getConfigurationData().getConfigurationDataId() : "configurationData is null")
530                         + "\nactionBody2 = " + (p2.getActionBodyEntity()!=null ? "actionBodyId = " + p2.getActionBodyEntity().getActionBodyId() : "actionBody is null")
531                         +  "\nscope2 = " + p2.getScope()
532                         + "\ncreatedBy2 = " + p2.getCreatedBy()
533                         + "\ncreatedDate2 = " + p2.getCreatedDate()
534                         + "\ndescription2 = " + p2.getDescription()
535                         + "\nmodifiedBy2 = " + p2.getModifiedBy()
536                         + "\nmodifiedDate2 = " + p2.getModifiedDate()
537                         + "\ndeleted2 = " + p2.isDeleted());
538
539
540         //now lets see if it put the configurationData c1 back into the table
541                 resultList = query2.getResultList();
542         c2 = null;
543         if(!resultList.isEmpty()){
544             // ignores multiple results
545             c2 = (ConfigurationDataEntity) resultList.get(0);
546         }else{
547                 fail("\nPolicyEntityTest - Check re-entry of configurationData into DB"
548                                 + "No ConfigurationDataEntity DB entry found");
549         }
550         
551         //output what configurationData object we found
552         logger.debug("\n\n********PolicyEntityTest: Configuration object after re-enter into policy object and retrieving from DB *********"
553                         + "\nconfigurationDataId2 = " + c2.getConfigurationDataId()
554                         + "\nversion2 = " + c2.getVersion()
555                         + "\nconfigType2 = " + c2.getConfigType()
556                         + "\nconfigBody2 = " + c2.getConfigBody()
557                         + "\ncreatedBy2 = " + c2.getCreatedBy()
558                         + "\ncreatedDate2 = " + c2.getCreatedDate()
559                         + "\ndescription2 = " + c2.getDescription()
560                         + "\nmodifiedBy = " + c2.getModifiedBy()
561                         + "\nmodifiedDate = " + c2.getModifiedDate()
562                         + "\ndeleted2 = " + c2.isDeleted());
563
564         //now lets see if it put the actionBody a1 back into the table
565         //Get the database version of the Action Body
566                 resultList = querya2.getResultList();
567          a2 = null;
568          if(!resultList.isEmpty()){
569              // ignores multiple results
570              a2 = (ActionBodyEntity) resultList.get(0);
571          }else{
572                 fail("\nPolicyEntityTest - Check re-entry of actionBody into DB"
573                                 + "No ActionBodyEntity DB entry found");
574          }
575          
576          logger.debug("\n\n********PolicyEntityTest: Local Action Body object after re-enter into policy object and retrieving from DB *********"
577                         + "\nactionBodyId2 = " + a2.getActionBodyId()
578                         + "\nactionBodyVersion2 = " + a2.getVersion()
579                         + "\nactionBody2 = " + a2.getActionBody()
580                         + "\nactionBodyCeatedBy2 = " + a2.getCreatedBy()
581                         + "\nactionBodyCreatedDate2 = " + a2.getCreatedDate()
582                         + "\nactionBodyModifiedBy2 = " + a2.getModifiedBy()
583                         + "\nactionBodyModifiedDate2 = " + a2.getModifiedDate()
584                         + "\nactionBodyDeleted2 = " + a2.isDeleted());        
585
586         //I want to save all the above in the DB
587         try{
588                 et.commit();
589                 logger.debug("\n\n***********PolicyEntityTest: et.commit Succeeded********");
590         }catch(Exception e){
591                 logger.debug("\n\n***********PolicyEntityTest: et.commit Failed********"
592                                 + "\nTRANSACTION ROLLBACK "
593                                 + "\n   with exception: " + e);
594         }
595
596         // Start a new transaction
597         EntityTransaction et2 = em.getTransaction();
598
599         et2.begin();
600         
601         //Let's test if the PolicyEntity uniqueConstraint for policyName and scopeId hold
602         PolicyEntity p3 = new PolicyEntity();
603         em.persist(p3);
604
605         
606         //first let's assure that you can save with the same name but a different scope
607         p3.setPolicyName(p1.getPolicyName());
608         p3.setScope("mckiou.kevin.kory");
609         em.flush();
610         logger.debug("\n\n***********PolicyEntityTest: PolicyEntity Unique test for policyName and scope********"
611                         + "\nSuccess!  PolicyEntity uniqueness constraint allowed "
612                         + "\n   policyId1 " + p1.getPolicyId() 
613                         + "\n   policyName1 " + p1.getPolicyName() 
614                         + "\n   scope1 = " + p1.getScope()
615                         + "\n   policyId3 " + p3.getPolicyId() 
616                         + "\n   policyName3 " + p3.getPolicyName() 
617                         + "\n   scope3 = " + p3.getScope());
618
619         //Assert that the policyIds are NOT the same to show that the automatic sequencing is working
620         assert(p1.getPolicyId() != p3.getPolicyId());
621
622         try{
623                 //Now set the scope the same to verify the uniqueness constraint will be enforced
624                 p3.setScope(p1.getScope());
625                 
626                 em.flush();
627                 fail("\n\n***********PolicyEntityTest: PolicyEntity Unique test for policyName and scope********"
628                                 + "\nFailed! PolicyEntity Uniqueness constraint FAILED and DID allow "
629                                 + "\n   policyId1 " + p1.getPolicyId() 
630                                 + "\n   policyName1 " + p1.getPolicyName() 
631                                 + "\n   scope1 = " + p1.getScope()
632                                 + "\n   policyId3 " + p3.getPolicyId() 
633                                 + "\n   policyName3 " + p3.getPolicyName() 
634                                 + "\n   scope3 = " + p3.getScope());;
635         }
636         catch(Exception e){
637                 //Success
638                 logger.debug("\n\n***********PolicyEntityTest: PolicyEntity Unique test for policyName and scope********"
639                                 + "\nSuccess!  PolicyEntity Uniqueness constraint SUCCEEDED and did NOT allow "
640                                 + "\n   policyId1 " + p1.getPolicyId() 
641                                 + "\n   policyName1 " + p1.getPolicyName() 
642                                 + "\n   scope1 = " + p1.getScope()
643                                 + "\n   policyId3 " + p3.getPolicyId() 
644                                 + "\n   policyName3 " + p3.getPolicyName() 
645                                 + "\n   scope3 = " + p3.getScope()
646                                 + "\n   with excpetion: " + e);
647         }
648
649         
650         try{
651                 et2.commit();
652                 logger.debug("\n\n***********PolicyEntityTest: et2.commit Succeeded********");
653         }catch(Exception e){
654                 logger.debug("\n\n***********PolicyEntityTest: et2.commit Failed********"
655                                 + "\nTRANSACTION ROLLBACK "
656                                 + "\n   with exception: " + e);
657         }
658         
659         //****************Test the PolicyDBDaoEntity************************
660         
661         //Create a transaction
662         EntityTransaction et3 = em.getTransaction();
663
664         et3.begin();
665         
666         //create one 
667         PolicyDBDaoEntity pe1 = new PolicyDBDaoEntity();
668         em.persist(pe1);
669         
670         pe1.setDescription("This is pe1");
671         
672         pe1.setPolicyDBDaoUrl("http://10.11.12.13:2345");
673         
674         //push it to the DB
675         em.flush();
676         
677         //create another
678         PolicyDBDaoEntity pe2 = new PolicyDBDaoEntity();
679         em.persist(pe2);
680         
681         pe2.setDescription("This is pe2");
682         
683         pe2.setPolicyDBDaoUrl("http://10.11.12.13:2345");
684         
685         //Print them to the log before flushing
686         logger.debug("\n\n***********PolicyEntityTest: PolicyDBDaoEntity objects before flush********"
687                         + "\n   policyDBDaoUrl-1 = " + pe1.getPolicyDBDaoUrl()
688                         + "\n   description-1 = " + pe1.getDescription()
689                         + "\n   createdDate-1 = " + pe1.getCreatedDate()
690                         + "\n   modifiedDate-1 " + pe1.getModifiedDate()
691                         + "\n*****************************************"
692                         + "\n   policyDBDaoUrl-2 = " + pe2.getPolicyDBDaoUrl()
693                         + "\n   description-2 = " + pe2.getDescription()
694                         + "\n   createdDate-2 = " + pe2.getCreatedDate()
695                         + "\n   modifiedDate-2 " + pe2.getModifiedDate()
696                         );
697         
698         //push it to the DB
699         em.flush();
700         
701         //Now let's retrieve them from the DB using the named query
702         
703                 resultList = em.createNamedQuery("PolicyDBDaoEntity.findAll").getResultList();
704
705                 PolicyDBDaoEntity pex = null;
706                 PolicyDBDaoEntity pey = null;
707                 
708         if(!resultList.isEmpty()){
709                 if (resultList.size() != 2){
710                         fail("\nPolicyEntityTest: Number of PolicyDBDaoEntity entries = " + resultList.size() + " instead of 2");
711                 }
712                 for(Object policyDBDaoEntity: resultList){
713                         PolicyDBDaoEntity pdbdao = (PolicyDBDaoEntity)policyDBDaoEntity;
714                         if(pdbdao.getPolicyDBDaoUrl().equals("http://10.11.12.13:2345")){
715                                 pex = pdbdao;
716                         }else if(pdbdao.getPolicyDBDaoUrl().equals("http://10.11.12.13:2345")){
717                                 pey = pdbdao;
718                         }
719                 }
720                 
721             //Print them to the log before flushing
722             logger.debug("\n\n***********PolicyEntityTest: PolicyDBDaoEntity objects retrieved from DB********"
723                                 + "\n   policyDBDaoUrl-x = " + pex.getPolicyDBDaoUrl()
724                                 + "\n   description-x = " + pex.getDescription()
725                                 + "\n   createdDate-x = " + pex.getCreatedDate()
726                                 + "\n   modifiedDate-x " + pex.getModifiedDate()
727                                 + "\n*****************************************"
728                                 + "\n   policyDBDaoUrl-y = " + pey.getPolicyDBDaoUrl()
729                                 + "\n   description-y = " + pey.getDescription()
730                                 + "\n   createdDate-y = " + pey.getCreatedDate()
731                                 + "\n   modifiedDate-y " + pey.getModifiedDate()
732                         );
733                 //Verify the retrieved objects are the same as the ones we stored in the DB
734                 if(pex.getPolicyDBDaoUrl().equals("http://10.11.12.13:2345")){
735                         assertSame(pe1,pex);
736                         assertSame(pe2,pey);
737                 }else{
738                         assertSame(pe2,pex);
739                         assertSame(pe1,pey);
740                 }
741             
742         }else{
743                 fail("\nPolicyEntityTest: No PolicyDBDaoEntity DB entry found");
744         }
745        
746         //Now let's see if we can do an update on the PolicyDBDaoEntity which we retrieved.
747         //em.persist(pex);
748         pex.setDescription("This is pex");
749         em.flush();
750         
751         //retrieve it
752         Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyDBDaoEntity p WHERE p.description=:desc");
753                 resultList = createPolicyQuery.setParameter("desc", "This is pex").getResultList();
754                 
755                 PolicyDBDaoEntity pez = null;
756         
757         if(!resultList.isEmpty()){
758                 if (resultList.size() != 1){
759                         fail("\nPolicyEntityTest: Update Test - Number of PolicyDBDaoEntity entries = " + resultList.size() + " instead of 1");
760                 }
761                 pez = (PolicyDBDaoEntity) resultList.get(0);
762                 
763             //Print them to the log before flushing
764             logger.debug("\n\n***********PolicyEntityTest: Update Test - PolicyDBDaoEntity objects retrieved from DB********"
765                                 + "\n   policyDBDaoUrl-x = " + pex.getPolicyDBDaoUrl()
766                                 + "\n   description-x = " + pex.getDescription()
767                                 + "\n   createdDate-x = " + pex.getCreatedDate()
768                                 + "\n   modifiedDate-x " + pex.getModifiedDate()
769                                 + "\n*****************************************"
770                                 + "\n   policyDBDaoUrl-z = " + pez.getPolicyDBDaoUrl()
771                                 + "\n   description-z = " + pez.getDescription()
772                                 + "\n   createdDate-z = " + pez.getCreatedDate()
773                                 + "\n   modifiedDate-z " + pez.getModifiedDate()
774                         );
775                 //Verify the retrieved objects are the same as the ones we stored in the DB
776                 assertSame(pex,pez);
777         }else{
778                 fail("\nPolicyEntityTest: Update Test - No PolicyDBDaoEntity DB updated entry found");
779         }
780         
781         //Clean up the DB
782         em.createQuery("DELETE FROM PolicyDBDaoEntity").executeUpdate();
783         em.createQuery("DELETE FROM PolicyEntity").executeUpdate();
784         em.createQuery("DELETE FROM ConfigurationDataEntity").executeUpdate();
785         em.createQuery("DELETE FROM ActionBodyEntity").executeUpdate();
786         
787         //Wrap up the transaction
788         try{
789                 et3.commit();
790                 logger.debug("\n\n***********PolicyEntityTest: et3.commit Succeeded********");
791         }catch(Exception e){
792                 logger.debug("\n\n***********PolicyEntityTest: et3.commit Failed********"
793                                 + "\nTRANSACTION ROLLBACK "
794                                 + "\n   with exception: " + e);
795         }
796          
797         
798         //Tidy up
799         em.close();
800     }
801     
802 }