2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.jpa;
23 import static org.junit.Assert.*;
25 //import org.apache.commons.logging.Log;
26 //import org.apache.commons.logging.LogFactory;
28 import org.onap.policy.rest.XACMLRestProperties;
29 import org.onap.policy.rest.jpa.ActionBodyEntity;
30 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
31 import org.onap.policy.rest.jpa.PolicyDBDaoEntity;
32 import org.onap.policy.rest.jpa.PolicyEntity;
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;
40 import java.util.Date;
41 import java.util.List;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
45 import java.util.Properties;
47 public class PolicyEntityTest {
49 private static Logger logger = FlexLogger.getLogger(PolicyEntityTest.class);
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();
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();
70 //Create a policy object
71 PolicyEntity p1 = new PolicyEntity();
76 long policyId1 = p1.getPolicyId();
78 String policyName1 = p1.getPolicyName();
80 int version1 = p1.getVersion();
82 String policyData1 = p1.getPolicyData();
84 ConfigurationDataEntity configData1 = p1.getConfigurationData();
85 String configDataStr1 = (configData1!=null ? "configurationDataId = " + configData1.getConfigurationDataId() : "configurationData is null");
87 ActionBodyEntity actionBody1 = p1.getActionBodyEntity();
88 String actionBodyStr1 = (actionBody1!=null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
90 String createdBy1 = p1.getCreatedBy();
92 Date createdDate1 = p1.getCreatedDate();
93 String createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
95 String description = p1.getDescription();
97 String modifiedBy1 = p1.getModifiedBy();
99 Date modifiedDate1 = p1.getModifiedDate();
100 String modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
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());
119 p1.setPolicyName("testPID2");
122 p1.setPolicyData("<policy>PolicyData</policy>");
124 //We will NOT set the ConfigurationDataEntity or ActionBodyEntity object just to test that it is optional
127 p1.setCreatedBy("kevin");
129 //createdDate will be set when it is persisted
132 p1.setScope("mckiou.kevin");
135 p1.setDescription("PolicyEntity Description");
138 p1.setModifiedBy("kevin");
140 //modifiedDate will be set when it is persisted
145 //Now lets get some attribute values
147 policyId1 = p1.getPolicyId();
149 policyName1 = p1.getPolicyName();
151 version1 = p1.getVersion();
153 policyData1 = p1.getPolicyData();
155 configData1 = p1.getConfigurationData();
156 configDataStr1 = (configData1!=null ? "configurationDataId = " + configData1.getConfigurationDataId() : "configurationData is null");
158 actionBody1 = p1.getActionBodyEntity();
159 actionBodyStr1 = (actionBody1!=null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
161 createdBy1 = p1.getCreatedBy();
163 createdDate1 = p1.getCreatedDate();
164 createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
166 description = p1.getDescription();
168 modifiedBy1 = p1.getModifiedBy();
170 modifiedDate1 = p1.getModifiedDate();
171 modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
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());
188 //Now lets fully configure the configurationData and actionBody
190 //Create a ConfigurationDataEntity object and set ID
191 ConfigurationDataEntity c1 = new ConfigurationDataEntity();
193 ActionBodyEntity a1 = new ActionBodyEntity();
195 //persist the configuration Data
198 c1.setConfigType("OTHER");
200 c1.setConfigBody("ABC");
202 c1.setDescription("ConfigurationDataEntity Description");
204 c1.setCreatedBy("kevin");
206 //c1.setModifiedBy("kevin");
210 //persist the action Body
214 a1.setActionBody("myActionBody");
216 a1.setActionBodyName("myActionBodyName");
218 a1.setCreatedBy("kevin");
220 a1.setModifiedBy("kevin");
222 a1.setDeleted(false);
225 long configurationDataId = c1.getConfigurationDataId();
227 int cdVersion = c1.getVersion();
229 String cdConfigType = c1.getConfigType();
231 String cdConfigBody = c1.getConfigBody();
233 String cdCreatedBy = c1.getCreatedBy();
235 Date cdCreatedDate = c1.getCreatedDate();
237 String cdDescription = c1.getDescription();
239 String cdModifiedBy = c1.getModifiedBy();
241 Date cdModifiedDate = c1.getModifiedDate();
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());
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());
267 p1.setScope("mckiou.kevin.kim");
272 //Perform policy selects
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");
277 query.setParameter("pid", p1.getPolicyId());
278 queryscope.setParameter("s", "mckiou.kevin.kim");
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);
288 fail("\nPolicyEntityTest: No PolicyEntity using scope DB entry found");
291 //The scope object on the retrieved policy object should be same as the one we used to find it
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);
304 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
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());
322 //Confirm that the retrieved policy object is the same as the persisted object
325 //Perform configurationData selects
326 Query query2 = em.createQuery("Select c from ConfigurationDataEntity c where c.configurationDataId=:cid");
328 query2.setParameter("cid", c1.getConfigurationDataId());
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);
337 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
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());
352 //Confirm the retrieved ConfigurationDataEntity object is the same as the persisted
355 //Now assign the configurationData to the policy
356 p1.setConfigurationData(c1);
358 //Perform actionBody selects
359 Query querya2 = em.createQuery("Select a from ActionBodyEntity a where a.actionBodyId=:aid");
361 querya2.setParameter("aid", a1.getActionBodyId());
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);
370 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
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());
385 //Confirm the retrieved ActionBodyEntity object is the same as the persisted
388 //Now assign the ActionBodyEntity to the policy
389 p1.setActionBodyEntity(a1);
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();
397 if(!resultList.isEmpty()){
398 // ignores multiple results
399 p2 = (PolicyEntity) resultList.get(0);
401 fail("PolicyEntityTest: No PolicyEntity DB entry found");
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());
419 //And now the ConfigurationDataEntity object
420 resultList = query2.getResultList();
422 if(!resultList.isEmpty()){
423 // ignores multiple results
424 c2 = (ConfigurationDataEntity) resultList.get(0);
426 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
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());
442 //Get the database version of the Action Body
443 resultList = querya2.getResultList();
445 if(!resultList.isEmpty()){
446 // ignores multiple results
447 a2 = (ActionBodyEntity) resultList.get(0);
449 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
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());
464 //****Now lets see if the orphanRemoval=true does anything useful***
465 //Remove the configurationData from the policy relationship
467 p1.setConfigurationData(null);
469 p1.setActionBodyEntity(null);
471 //flush the update to the DB
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();
478 if(resultList.isEmpty()){
479 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
480 + "\n Success!! No ConfigurationDataEntity DB entry found");
483 c2 = (ConfigurationDataEntity) resultList.get(0);
484 fail("\nPolicyEntityTest: ConfigurationDataEntity DB entry found - and none should exist"
485 + "\nconfigurationDataId = " + c2.getConfigurationDataId());
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();
492 if(resultList.isEmpty()){
493 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
494 + "\n Success!! No ActionBodyEntity DB entry found");
497 a2 = (ActionBodyEntity) resultList.get(0);
498 fail("\nPolicyEntityTest: ActionBodyEntity DB entry found - and none should exist"
499 + "\nactionBodyId = " + a2.getActionBodyId());
502 //Now lets put the configurationData and actionBody back into the policy object and see what appears
503 //in the DB after a flush
505 //put c1 back into the persistence context since the orphanRemoval removed it.
507 p1.setConfigurationData(c1);
510 p1.setActionBodyEntity(a1);
514 //retrieve the policy object
515 resultList = query.getResultList();
517 if(!resultList.isEmpty()){
518 // ignores multiple results
519 p2 = (PolicyEntity) resultList.get(0);
521 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
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());
541 //now lets see if it put the configurationData c1 back into the table
542 resultList = query2.getResultList();
544 if(!resultList.isEmpty()){
545 // ignores multiple results
546 c2 = (ConfigurationDataEntity) resultList.get(0);
548 fail("\nPolicyEntityTest - Check re-entry of configurationData into DB"
549 + "No ConfigurationDataEntity DB entry found");
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());
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();
569 if(!resultList.isEmpty()){
570 // ignores multiple results
571 a2 = (ActionBodyEntity) resultList.get(0);
573 fail("\nPolicyEntityTest - Check re-entry of actionBody into DB"
574 + "No ActionBodyEntity DB entry found");
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());
587 //I want to save all the above in the DB
590 logger.debug("\n\n***********PolicyEntityTest: et.commit Succeeded********");
592 logger.debug("\n\n***********PolicyEntityTest: et.commit Failed********"
593 + "\nTRANSACTION ROLLBACK "
594 + "\n with exception: " + e);
597 // Start a new transaction
598 EntityTransaction et2 = em.getTransaction();
602 //Let's test if the PolicyEntity uniqueConstraint for policyName and scopeId hold
603 PolicyEntity p3 = new PolicyEntity();
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");
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());
620 //Assert that the policyIds are NOT the same to show that the automatic sequencing is working
621 assert(p1.getPolicyId() != p3.getPolicyId());
624 //Now set the scope the same to verify the uniqueness constraint will be enforced
625 p3.setScope(p1.getScope());
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());;
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);
653 logger.debug("\n\n***********PolicyEntityTest: et2.commit Succeeded********");
655 logger.debug("\n\n***********PolicyEntityTest: et2.commit Failed********"
656 + "\nTRANSACTION ROLLBACK "
657 + "\n with exception: " + e);
660 //****************Test the PolicyDBDaoEntity************************
662 //Create a transaction
663 EntityTransaction et3 = em.getTransaction();
668 PolicyDBDaoEntity pe1 = new PolicyDBDaoEntity();
671 pe1.setDescription("This is pe1");
673 pe1.setPolicyDBDaoUrl("http://123.45.2.456:2345");
679 PolicyDBDaoEntity pe2 = new PolicyDBDaoEntity();
682 pe2.setDescription("This is pe2");
684 pe2.setPolicyDBDaoUrl("http://789.01.2.345:2345");
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()
702 //Now let's retrieve them from the DB using the named query
704 resultList = em.createNamedQuery("PolicyDBDaoEntity.findAll").getResultList();
706 PolicyDBDaoEntity pex = null;
707 PolicyDBDaoEntity pey = null;
709 if(!resultList.isEmpty()){
710 if (resultList.size() != 2){
711 fail("\nPolicyEntityTest: Number of PolicyDBDaoEntity entries = " + resultList.size() + " instead of 2");
713 for(Object policyDBDaoEntity: resultList){
714 PolicyDBDaoEntity pdbdao = (PolicyDBDaoEntity)policyDBDaoEntity;
715 if(pdbdao.getPolicyDBDaoUrl().equals("http://123.45.2.456:2345")){
717 }else if(pdbdao.getPolicyDBDaoUrl().equals("http://789.01.2.345:2345")){
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()
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")){
744 fail("\nPolicyEntityTest: No PolicyDBDaoEntity DB entry found");
747 //Now let's see if we can do an update on the PolicyDBDaoEntity which we retrieved.
749 pex.setDescription("This is pex");
753 Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyDBDaoEntity p WHERE p.description=:desc");
754 resultList = createPolicyQuery.setParameter("desc", "This is pex").getResultList();
756 PolicyDBDaoEntity pez = null;
758 if(!resultList.isEmpty()){
759 if (resultList.size() != 1){
760 fail("\nPolicyEntityTest: Update Test - Number of PolicyDBDaoEntity entries = " + resultList.size() + " instead of 1");
762 pez = (PolicyDBDaoEntity) resultList.get(0);
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()
776 //Verify the retrieved objects are the same as the ones we stored in the DB
779 fail("\nPolicyEntityTest: Update Test - No PolicyDBDaoEntity DB updated entry found");
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();
788 //Wrap up the transaction
791 logger.debug("\n\n***********PolicyEntityTest: et3.commit Succeeded********");
793 logger.debug("\n\n***********PolicyEntityTest: et3.commit Failed********"
794 + "\nTRANSACTION ROLLBACK "
795 + "\n with exception: " + e);