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