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