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.openecomp.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.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;
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.openecomp.policy.common.logging.flexlogger.FlexLogger;
43 import org.openecomp.policy.common.logging.flexlogger.Logger;
45 import java.util.Properties;
47 public class PolicyEntityTest {
49 private static Logger logger = FlexLogger.getLogger(PolicyEntityTest.class);
53 public void testAllOps(){
54 Properties properties = new Properties();
55 properties.put(XACMLRestProperties.PROP_PAP_DB_DRIVER,"org.h2.Driver");
56 properties.put(XACMLRestProperties.PROP_PAP_DB_URL, "jdbc:h2:file:./sql/xacmlTest");
57 properties.put(XACMLRestProperties.PROP_PAP_DB_USER, "sa");
58 properties.put(XACMLRestProperties.PROP_PAP_DB_PASSWORD, "");
59 EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPapPU", properties);
60 EntityManager em = emf.createEntityManager();
61 // Start a transaction
62 EntityTransaction et = em.getTransaction();
65 //Make sure the DB is clean
66 em.createQuery("DELETE FROM PolicyDBDaoEntity").executeUpdate();
67 em.createQuery("DELETE FROM PolicyEntity").executeUpdate();
68 em.createQuery("DELETE FROM ConfigurationDataEntity").executeUpdate();
69 em.createQuery("DELETE FROM ActionBodyEntity").executeUpdate();
71 //Create a policy object
72 PolicyEntity p1 = new PolicyEntity();
77 long policyId1 = p1.getPolicyId();
79 String policyName1 = p1.getPolicyName();
81 int version1 = p1.getVersion();
83 String policyData1 = p1.getPolicyData();
85 ConfigurationDataEntity configData1 = p1.getConfigurationData();
86 String configDataStr1 = (configData1!=null ? "configurationDataId = " + configData1.getConfigurationDataId() : "configurationData is null");
88 ActionBodyEntity actionBody1 = p1.getActionBodyEntity();
89 String actionBodyStr1 = (actionBody1!=null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
91 String createdBy1 = p1.getCreatedBy();
93 Date createdDate1 = p1.getCreatedDate();
94 String createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
96 String description = p1.getDescription();
98 String modifiedBy1 = p1.getModifiedBy();
100 Date modifiedDate1 = p1.getModifiedDate();
101 String modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
104 logger.debug("\n\n********PolicyEntityTest: Local PolicyEntity and Configuration objects before persist*********"
105 + "\npolicyId1 = " + policyId1
106 + "\npolicyName1 = " + policyName1
107 + "\nversion1 = " + version1
108 + "\npolicyData1 = " + policyData1
109 + "\nconfigDataStr1 = " + configDataStr1
110 + "\nactionBodyStr1 = " + actionBodyStr1
111 + "\nscope = " + p1.getScope()
112 + "\ncreatedBy1 = " + createdBy1
113 + "\ncreatedDateStr1 = " + createdDateStr1
114 + "\ndescription = " + description
115 + "\nmodifiedBy1 = " + modifiedBy1
116 + "\nmodifiedDateStr1 = " + modifiedDateStr1
117 + "\ndeleted = " + p1.isDeleted());
120 p1.setPolicyName("testPID2");
123 p1.setPolicyData("<policy>PolicyData</policy>");
125 //We will NOT set the ConfigurationDataEntity or ActionBodyEntity object just to test that it is optional
128 p1.setCreatedBy("super-admin");
130 //createdDate will be set when it is persisted
133 p1.setScope("com.test");
136 p1.setDescription("PolicyEntity Description");
139 p1.setModifiedBy("super-admin");
141 //modifiedDate will be set when it is persisted
146 //Now lets get some attribute values
148 policyId1 = p1.getPolicyId();
150 policyName1 = p1.getPolicyName();
152 version1 = p1.getVersion();
154 policyData1 = p1.getPolicyData();
156 configData1 = p1.getConfigurationData();
157 configDataStr1 = (configData1!=null ? "configurationDataId = " + configData1.getConfigurationDataId() : "configurationData is null");
159 actionBody1 = p1.getActionBodyEntity();
160 actionBodyStr1 = (actionBody1!=null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
162 createdBy1 = p1.getCreatedBy();
164 createdDate1 = p1.getCreatedDate();
165 createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
167 description = p1.getDescription();
169 modifiedBy1 = p1.getModifiedBy();
171 modifiedDate1 = p1.getModifiedDate();
172 modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
174 logger.debug("\n\n********PolicyEntityTest: Local PolicyEntity and Configuration objects after persist*********"
175 + "\npolicyId1 = " + policyId1
176 + "\npolicyName1 = " + policyName1
177 + "\nversion1 = " + version1
178 + "\npolicyData1 = " + policyData1
179 + "\nconfigDataStr1 = " + configDataStr1
180 + "\nactionBodyStr1 = " + actionBodyStr1
181 + "\nscopeId = " + p1.getScope()
182 + "\ncreatedBy1 = " + createdBy1
183 + "\ncreatedDateStr1 = " + createdDateStr1
184 + "\ndescription = " + description
185 + "\nmodifiedBy1 = " + modifiedBy1
186 + "\nmodifiedDateStr1 = " + modifiedDateStr1
187 + "\ndeleted = " + p1.isDeleted());
189 //Now lets fully configure the configurationData and actionBody
191 //Create a ConfigurationDataEntity object and set ID
192 ConfigurationDataEntity c1 = new ConfigurationDataEntity();
194 ActionBodyEntity a1 = new ActionBodyEntity();
196 //persist the configuration Data
199 c1.setConfigType("OTHER");
201 c1.setConfigBody("ABC");
203 c1.setDescription("ConfigurationDataEntity Description");
205 c1.setCreatedBy("super-admin");
209 //persist the action Body
213 a1.setActionBody("myActionBody");
215 a1.setActionBodyName("myActionBodyName");
217 a1.setCreatedBy("super-admin");
219 a1.setModifiedBy("super-admin");
221 a1.setDeleted(false);
224 long configurationDataId = c1.getConfigurationDataId();
226 int cdVersion = c1.getVersion();
228 String cdConfigType = c1.getConfigType();
230 String cdConfigBody = c1.getConfigBody();
232 String cdCreatedBy = c1.getCreatedBy();
234 Date cdCreatedDate = c1.getCreatedDate();
236 String cdDescription = c1.getDescription();
238 String cdModifiedBy = c1.getModifiedBy();
240 Date cdModifiedDate = c1.getModifiedDate();
242 logger.debug("\n\n********PolicyEntityTest: Local Configuration object after setting values *********"
243 + "\nconfigurationDataId = " + configurationDataId
244 + "\ncdVersion = " + cdVersion
245 + "\ncdConfigType = " + cdConfigType
246 + "\ncdConfigBody = " + cdConfigBody
247 + "\ncdCreatedBy = " + cdCreatedBy
248 + "\ncdCreatedDate = " + cdCreatedDate
249 + "\ncdDescription = " + cdDescription
250 + "\ncdModifiedBy = " + cdModifiedBy
251 + "\ncdModifiedDate = " + cdModifiedDate
252 + "\ndeleted = " + c1.isDeleted());
256 logger.debug("\n\n********PolicyEntityTest: Local Action Body object after setting values *********"
257 + "\nactionBodyId = " + a1.getActionBodyId()
258 + "\nactionBodyVersion = " + a1.getVersion()
259 + "\nactionBody = " + a1.getActionBody()
260 + "\nactionBodyCeatedBy = " + a1.getCreatedBy()
261 + "\nactionBodyCreatedDate = " + a1.getCreatedDate()
262 + "\nactionBodyModifiedBy = " + a1.getModifiedBy()
263 + "\nactionBodyModifiedDate = " + a1.getModifiedDate()
264 + "\nactionBodyDeleted = " + a1.isDeleted());
266 p1.setScope("mckiou.kevin.kim");
271 //Perform policy selects
273 Query query = em.createQuery("Select p from PolicyEntity p where p.policyId=:pid");
274 Query queryscope = em.createQuery("Select p from PolicyEntity p where p.scope=:s");
276 query.setParameter("pid", p1.getPolicyId());
277 queryscope.setParameter("s", "com.user");
279 //Just test that we are retrieving the right object
280 @SuppressWarnings("rawtypes")
281 List psList = queryscope.getResultList();
282 PolicyEntity px = null;
283 if(!psList.isEmpty()){
284 //ignores multiple results
285 px = (PolicyEntity) psList.get(0);
287 fail("\nPolicyEntityTest: No PolicyEntity using scope DB entry found");
290 //The scope object on the retrieved policy object should be same as the one we used to find it
294 //Because getSingleResult() throws an unchecked exception which is an indication of a
295 //programming error, we are not going to use it.
296 @SuppressWarnings("rawtypes")
297 List resultList = query.getResultList();
298 PolicyEntity p2 = null;
299 if(!resultList.isEmpty()){
300 // ignores multiple results
301 p2 = (PolicyEntity) resultList.get(0);
303 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
306 logger.debug("\n\n********PolicyEntityTest: PolicyEntity object after retrieving from DB BEFORE assigning configurationData*********"
307 + "\npolicyId2 = " + p2.getPolicyId()
308 + "\npolicyName2 = " + p2.getPolicyName()
309 + "\nversion2 = " + p2.getVersion()
310 + "\npolicyData2 = " + p2.getPolicyData()
311 + "\nconfigurationData2 = " + (p2.getConfigurationData()!=null ? "configurationDataId = " + p2.getConfigurationData().getConfigurationDataId() : "configurationData is null")
312 + "\nactionBody2 = " + (p2.getActionBodyEntity()!=null ? "actionBodyId = " + p2.getActionBodyEntity().getActionBodyId() : "actionBody is null")
313 + "\nscope2 = " + p2.getScope()
314 + "\ncreatedBy2 = " + p2.getCreatedBy()
315 + "\ncreatedDate2 = " + p2.getCreatedDate()
316 + "\ndescription2 = " + p2.getDescription()
317 + "\nmodifiedBy2 = " + p2.getModifiedBy()
318 + "\nmodifiedDate2 = " + p2.getModifiedDate()
319 + "\ndeleted2 = " + p2.isDeleted());
321 //Confirm that the retrieved policy object is the same as the persisted object
324 //Perform configurationData selects
325 Query query2 = em.createQuery("Select c from ConfigurationDataEntity c where c.configurationDataId=:cid");
327 query2.setParameter("cid", c1.getConfigurationDataId());
329 //Get the database version of the Configuration Data
330 resultList = query2.getResultList();
331 ConfigurationDataEntity c2 = null;
332 if(!resultList.isEmpty()){
333 // ignores multiple results
334 c2 = (ConfigurationDataEntity) resultList.get(0);
336 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
339 logger.debug("\n\n********PolicyEntityTest: Configuration object after retrieving from DB BEFORE assigning to policy*********"
340 + "\nconfigurationDataId2 = " + c2.getConfigurationDataId()
341 + "\nversion2 = " + c2.getVersion()
342 + "\nconfigType2 = " + c2.getConfigType()
343 + "\nconfigBody2 = " + c2.getConfigBody()
344 + "\ncreatedBy2 = " + c2.getCreatedBy()
345 + "\ncreatedDate2 = " + c2.getCreatedDate()
346 + "\ndescription2 = " + c2.getDescription()
347 + "\nmodifiedBy2 = " + c2.getModifiedBy()
348 + "\nmodifiedDate2 = " + c2.getModifiedDate()
349 + "\ndeleted2 = " + c2.isDeleted());
351 //Confirm the retrieved ConfigurationDataEntity object is the same as the persisted
354 //Now assign the configurationData to the policy
355 p1.setConfigurationData(c1);
357 //Perform actionBody selects
358 Query querya2 = em.createQuery("Select a from ActionBodyEntity a where a.actionBodyId=:aid");
360 querya2.setParameter("aid", a1.getActionBodyId());
362 //Get the database version of the Action Body
363 resultList = querya2.getResultList();
364 ActionBodyEntity a2 = null;
365 if(!resultList.isEmpty()){
366 // ignores multiple results
367 a2 = (ActionBodyEntity) resultList.get(0);
369 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
373 logger.debug("\n\n********PolicyEntityTest: Local Action Body object after retrieving from DB BEFORE assigning to policy *********"
374 + "\nactionBodyId2 = " + a2.getActionBodyId()
375 + "\nactionBodyVersion2 = " + a2.getVersion()
376 + "\nactionBody2 = " + a2.getActionBody()
377 + "\nactionBodyCeatedBy2 = " + a2.getCreatedBy()
378 + "\nactionBodyCreatedDate2 = " + a2.getCreatedDate()
379 + "\nactionBodyModifiedBy2 = " + a2.getModifiedBy()
380 + "\nactionBodyModifiedDate2 = " + a2.getModifiedDate()
381 + "\nactionBodyDeleted2 = " + a2.isDeleted());
384 //Confirm the retrieved ActionBodyEntity object is the same as the persisted
387 //Now assign the ActionBodyEntity to the policy
388 p1.setActionBodyEntity(a1);
392 //Let's retrieve the policy, configurationData and actionBody from the DB and look at them
393 //Here is the policy object
394 resultList = query.getResultList();
396 if(!resultList.isEmpty()){
397 // ignores multiple results
398 p2 = (PolicyEntity) resultList.get(0);
400 fail("PolicyEntityTest: No PolicyEntity DB entry found");
403 logger.debug("\n\n********PolicyEntityTest: PolicyEntity object after retrieving from DB AFTER assigning configurationData*********"
404 + "\npolicyId2 = " + p2.getPolicyId()
405 + "\npolicyName2 = " + p2.getPolicyName()
406 + "\nversion2 = " + p2.getVersion()
407 + "\npolicyData2 = " + p2.getPolicyData()
408 + "\nconfigurationData2 = " + (p2.getConfigurationData()!=null ? "configurationDataId = " + p2.getConfigurationData().getConfigurationDataId() : "configurationData is null")
409 + "\nactionBody2 = " + (p2.getActionBodyEntity()!=null ? "actionBodyId = " + p2.getActionBodyEntity().getActionBodyId() : "actionBody is null")
410 + "\nscope2 = " + p2.getScope()
411 + "\ncreatedBy2 = " + p2.getCreatedBy()
412 + "\ncreatedDate2 = " + p2.getCreatedDate()
413 + "\ndescription2 = " + p2.getDescription()
414 + "\nmodifiedBy2 = " + p2.getModifiedBy()
415 + "\nmodifiedDate2 = " + p2.getModifiedDate()
416 + "\ndeleted2 = " + p2.isDeleted());
418 //And now the ConfigurationDataEntity object
419 resultList = query2.getResultList();
421 if(!resultList.isEmpty()){
422 // ignores multiple results
423 c2 = (ConfigurationDataEntity) resultList.get(0);
425 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
428 logger.debug("\n\n********PolicyEntityTest: Configuration object after retrieving from DB AFTER assigning to policy*********"
429 + "\nconfigurationDataId2 = " + c2.getConfigurationDataId()
430 + "\nversion2 = " + c2.getVersion()
431 + "\nconfigType2 = " + c2.getConfigType()
432 + "\nconfigBody2 = " + c2.getConfigBody()
433 + "\ncreatedBy2 = " + c2.getCreatedBy()
434 + "\ncreatedDate2 = " + c2.getCreatedDate()
435 + "\ndescription2 = " + c2.getDescription()
436 + "\nmodifiedBy = " + c2.getModifiedBy()
437 + "\nmodifiedDate = " + c2.getModifiedDate()
438 + "\ndeleted2 = " + c2.isDeleted());
441 //Get the database version of the Action Body
442 resultList = querya2.getResultList();
444 if(!resultList.isEmpty()){
445 // ignores multiple results
446 a2 = (ActionBodyEntity) resultList.get(0);
448 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
452 logger.debug("\n\n********PolicyEntityTest: Local Action Body object after retrieving from DB AFTER assigning to policy *********"
453 + "\nactionBodyId2 = " + a2.getActionBodyId()
454 + "\nactionBodyVersion2 = " + a2.getVersion()
455 + "\nactionBody2 = " + a2.getActionBody()
456 + "\nactionBodyCeatedBy2 = " + a2.getCreatedBy()
457 + "\nactionBodyCreatedDate2 = " + a2.getCreatedDate()
458 + "\nactionBodyModifiedBy2 = " + a2.getModifiedBy()
459 + "\nactionBodyModifiedDate2 = " + a2.getModifiedDate()
460 + "\nactionBodyDeleted2 = " + a2.isDeleted());
463 //****Now lets see if the orphanRemoval=true does anything useful***
464 //Remove the configurationData from the policy relationship
466 p1.setConfigurationData(null);
468 p1.setActionBodyEntity(null);
470 //flush the update to the DB
473 //Attempt to retrieve the configuration data object from the db. It should not be there
474 //Reusing the previous query
475 resultList = query2.getResultList();
477 if(resultList.isEmpty()){
478 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
479 + "\n Success!! No ConfigurationDataEntity DB entry found");
482 c2 = (ConfigurationDataEntity) resultList.get(0);
483 fail("\nPolicyEntityTest: ConfigurationDataEntity DB entry found - and none should exist"
484 + "\nconfigurationDataId = " + c2.getConfigurationDataId());
487 //Attempt to retrieve the actionBody data object from the db. It should not be there
488 //Reusing the previous query
489 resultList = querya2.getResultList();
491 if(resultList.isEmpty()){
492 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
493 + "\n Success!! No ActionBodyEntity DB entry found");
496 a2 = (ActionBodyEntity) resultList.get(0);
497 fail("\nPolicyEntityTest: ActionBodyEntity DB entry found - and none should exist"
498 + "\nactionBodyId = " + a2.getActionBodyId());
501 //Now lets put the configurationData and actionBody back into the policy object and see what appears
502 //in the DB after a flush
504 //put c1 back into the persistence context since the orphanRemoval removed it.
506 p1.setConfigurationData(c1);
509 p1.setActionBodyEntity(a1);
513 //retrieve the policy object
514 resultList = query.getResultList();
516 if(!resultList.isEmpty()){
517 // ignores multiple results
518 p2 = (PolicyEntity) resultList.get(0);
520 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
523 //output what we policy object found
524 logger.debug("\n\n********PolicyEntityTest: PolicyEntity object after again adding ConfigurationDataEntity and retrieving from DB*********"
525 + "\npolicyId2 = " + p2.getPolicyId()
526 + "\npolicyName2 = " + p2.getPolicyName()
527 + "\nversion2 = " + p2.getVersion()
528 + "\npolicyData2 = " + p2.getPolicyData()
529 + "\nconfigurationData2 = " + (p2.getConfigurationData()!=null ? "configurationDataId = " + p2.getConfigurationData().getConfigurationDataId() : "configurationData is null")
530 + "\nactionBody2 = " + (p2.getActionBodyEntity()!=null ? "actionBodyId = " + p2.getActionBodyEntity().getActionBodyId() : "actionBody is null")
531 + "\nscope2 = " + p2.getScope()
532 + "\ncreatedBy2 = " + p2.getCreatedBy()
533 + "\ncreatedDate2 = " + p2.getCreatedDate()
534 + "\ndescription2 = " + p2.getDescription()
535 + "\nmodifiedBy2 = " + p2.getModifiedBy()
536 + "\nmodifiedDate2 = " + p2.getModifiedDate()
537 + "\ndeleted2 = " + p2.isDeleted());
540 //now lets see if it put the configurationData c1 back into the table
541 resultList = query2.getResultList();
543 if(!resultList.isEmpty()){
544 // ignores multiple results
545 c2 = (ConfigurationDataEntity) resultList.get(0);
547 fail("\nPolicyEntityTest - Check re-entry of configurationData into DB"
548 + "No ConfigurationDataEntity DB entry found");
551 //output what configurationData object we found
552 logger.debug("\n\n********PolicyEntityTest: Configuration object after re-enter into policy object and retrieving from DB *********"
553 + "\nconfigurationDataId2 = " + c2.getConfigurationDataId()
554 + "\nversion2 = " + c2.getVersion()
555 + "\nconfigType2 = " + c2.getConfigType()
556 + "\nconfigBody2 = " + c2.getConfigBody()
557 + "\ncreatedBy2 = " + c2.getCreatedBy()
558 + "\ncreatedDate2 = " + c2.getCreatedDate()
559 + "\ndescription2 = " + c2.getDescription()
560 + "\nmodifiedBy = " + c2.getModifiedBy()
561 + "\nmodifiedDate = " + c2.getModifiedDate()
562 + "\ndeleted2 = " + c2.isDeleted());
564 //now lets see if it put the actionBody a1 back into the table
565 //Get the database version of the Action Body
566 resultList = querya2.getResultList();
568 if(!resultList.isEmpty()){
569 // ignores multiple results
570 a2 = (ActionBodyEntity) resultList.get(0);
572 fail("\nPolicyEntityTest - Check re-entry of actionBody into DB"
573 + "No ActionBodyEntity DB entry found");
576 logger.debug("\n\n********PolicyEntityTest: Local Action Body object after re-enter into policy object and retrieving from DB *********"
577 + "\nactionBodyId2 = " + a2.getActionBodyId()
578 + "\nactionBodyVersion2 = " + a2.getVersion()
579 + "\nactionBody2 = " + a2.getActionBody()
580 + "\nactionBodyCeatedBy2 = " + a2.getCreatedBy()
581 + "\nactionBodyCreatedDate2 = " + a2.getCreatedDate()
582 + "\nactionBodyModifiedBy2 = " + a2.getModifiedBy()
583 + "\nactionBodyModifiedDate2 = " + a2.getModifiedDate()
584 + "\nactionBodyDeleted2 = " + a2.isDeleted());
586 //I want to save all the above in the DB
589 logger.debug("\n\n***********PolicyEntityTest: et.commit Succeeded********");
591 logger.debug("\n\n***********PolicyEntityTest: et.commit Failed********"
592 + "\nTRANSACTION ROLLBACK "
593 + "\n with exception: " + e);
596 // Start a new transaction
597 EntityTransaction et2 = em.getTransaction();
601 //Let's test if the PolicyEntity uniqueConstraint for policyName and scopeId hold
602 PolicyEntity p3 = new PolicyEntity();
606 //first let's assure that you can save with the same name but a different scope
607 p3.setPolicyName(p1.getPolicyName());
608 p3.setScope("mckiou.kevin.kory");
610 logger.debug("\n\n***********PolicyEntityTest: PolicyEntity Unique test for policyName and scope********"
611 + "\nSuccess! PolicyEntity uniqueness constraint allowed "
612 + "\n policyId1 " + p1.getPolicyId()
613 + "\n policyName1 " + p1.getPolicyName()
614 + "\n scope1 = " + p1.getScope()
615 + "\n policyId3 " + p3.getPolicyId()
616 + "\n policyName3 " + p3.getPolicyName()
617 + "\n scope3 = " + p3.getScope());
619 //Assert that the policyIds are NOT the same to show that the automatic sequencing is working
620 assert(p1.getPolicyId() != p3.getPolicyId());
623 //Now set the scope the same to verify the uniqueness constraint will be enforced
624 p3.setScope(p1.getScope());
627 fail("\n\n***********PolicyEntityTest: PolicyEntity Unique test for policyName and scope********"
628 + "\nFailed! PolicyEntity Uniqueness constraint FAILED and DID allow "
629 + "\n policyId1 " + p1.getPolicyId()
630 + "\n policyName1 " + p1.getPolicyName()
631 + "\n scope1 = " + p1.getScope()
632 + "\n policyId3 " + p3.getPolicyId()
633 + "\n policyName3 " + p3.getPolicyName()
634 + "\n scope3 = " + p3.getScope());;
638 logger.debug("\n\n***********PolicyEntityTest: PolicyEntity Unique test for policyName and scope********"
639 + "\nSuccess! PolicyEntity Uniqueness constraint SUCCEEDED and did NOT allow "
640 + "\n policyId1 " + p1.getPolicyId()
641 + "\n policyName1 " + p1.getPolicyName()
642 + "\n scope1 = " + p1.getScope()
643 + "\n policyId3 " + p3.getPolicyId()
644 + "\n policyName3 " + p3.getPolicyName()
645 + "\n scope3 = " + p3.getScope()
646 + "\n with excpetion: " + e);
652 logger.debug("\n\n***********PolicyEntityTest: et2.commit Succeeded********");
654 logger.debug("\n\n***********PolicyEntityTest: et2.commit Failed********"
655 + "\nTRANSACTION ROLLBACK "
656 + "\n with exception: " + e);
659 //****************Test the PolicyDBDaoEntity************************
661 //Create a transaction
662 EntityTransaction et3 = em.getTransaction();
667 PolicyDBDaoEntity pe1 = new PolicyDBDaoEntity();
670 pe1.setDescription("This is pe1");
672 pe1.setPolicyDBDaoUrl("http://10.11.12.13:2345");
678 PolicyDBDaoEntity pe2 = new PolicyDBDaoEntity();
681 pe2.setDescription("This is pe2");
683 pe2.setPolicyDBDaoUrl("http://10.11.12.13:2345");
685 //Print them to the log before flushing
686 logger.debug("\n\n***********PolicyEntityTest: PolicyDBDaoEntity objects before flush********"
687 + "\n policyDBDaoUrl-1 = " + pe1.getPolicyDBDaoUrl()
688 + "\n description-1 = " + pe1.getDescription()
689 + "\n createdDate-1 = " + pe1.getCreatedDate()
690 + "\n modifiedDate-1 " + pe1.getModifiedDate()
691 + "\n*****************************************"
692 + "\n policyDBDaoUrl-2 = " + pe2.getPolicyDBDaoUrl()
693 + "\n description-2 = " + pe2.getDescription()
694 + "\n createdDate-2 = " + pe2.getCreatedDate()
695 + "\n modifiedDate-2 " + pe2.getModifiedDate()
701 //Now let's retrieve them from the DB using the named query
703 resultList = em.createNamedQuery("PolicyDBDaoEntity.findAll").getResultList();
705 PolicyDBDaoEntity pex = null;
706 PolicyDBDaoEntity pey = null;
708 if(!resultList.isEmpty()){
709 if (resultList.size() != 2){
710 fail("\nPolicyEntityTest: Number of PolicyDBDaoEntity entries = " + resultList.size() + " instead of 2");
712 for(Object policyDBDaoEntity: resultList){
713 PolicyDBDaoEntity pdbdao = (PolicyDBDaoEntity)policyDBDaoEntity;
714 if(pdbdao.getPolicyDBDaoUrl().equals("http://10.11.12.13:2345")){
716 }else if(pdbdao.getPolicyDBDaoUrl().equals("http://10.11.12.13:2345")){
721 //Print them to the log before flushing
722 logger.debug("\n\n***********PolicyEntityTest: PolicyDBDaoEntity objects retrieved from DB********"
723 + "\n policyDBDaoUrl-x = " + pex.getPolicyDBDaoUrl()
724 + "\n description-x = " + pex.getDescription()
725 + "\n createdDate-x = " + pex.getCreatedDate()
726 + "\n modifiedDate-x " + pex.getModifiedDate()
727 + "\n*****************************************"
728 + "\n policyDBDaoUrl-y = " + pey.getPolicyDBDaoUrl()
729 + "\n description-y = " + pey.getDescription()
730 + "\n createdDate-y = " + pey.getCreatedDate()
731 + "\n modifiedDate-y " + pey.getModifiedDate()
733 //Verify the retrieved objects are the same as the ones we stored in the DB
734 if(pex.getPolicyDBDaoUrl().equals("http://10.11.12.13:2345")){
743 fail("\nPolicyEntityTest: No PolicyDBDaoEntity DB entry found");
746 //Now let's see if we can do an update on the PolicyDBDaoEntity which we retrieved.
748 pex.setDescription("This is pex");
752 Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyDBDaoEntity p WHERE p.description=:desc");
753 resultList = createPolicyQuery.setParameter("desc", "This is pex").getResultList();
755 PolicyDBDaoEntity pez = null;
757 if(!resultList.isEmpty()){
758 if (resultList.size() != 1){
759 fail("\nPolicyEntityTest: Update Test - Number of PolicyDBDaoEntity entries = " + resultList.size() + " instead of 1");
761 pez = (PolicyDBDaoEntity) resultList.get(0);
763 //Print them to the log before flushing
764 logger.debug("\n\n***********PolicyEntityTest: Update Test - PolicyDBDaoEntity objects retrieved from DB********"
765 + "\n policyDBDaoUrl-x = " + pex.getPolicyDBDaoUrl()
766 + "\n description-x = " + pex.getDescription()
767 + "\n createdDate-x = " + pex.getCreatedDate()
768 + "\n modifiedDate-x " + pex.getModifiedDate()
769 + "\n*****************************************"
770 + "\n policyDBDaoUrl-z = " + pez.getPolicyDBDaoUrl()
771 + "\n description-z = " + pez.getDescription()
772 + "\n createdDate-z = " + pez.getCreatedDate()
773 + "\n modifiedDate-z " + pez.getModifiedDate()
775 //Verify the retrieved objects are the same as the ones we stored in the DB
778 fail("\nPolicyEntityTest: Update Test - No PolicyDBDaoEntity DB updated entry found");
782 em.createQuery("DELETE FROM PolicyDBDaoEntity").executeUpdate();
783 em.createQuery("DELETE FROM PolicyEntity").executeUpdate();
784 em.createQuery("DELETE FROM ConfigurationDataEntity").executeUpdate();
785 em.createQuery("DELETE FROM ActionBodyEntity").executeUpdate();
787 //Wrap up the transaction
790 logger.debug("\n\n***********PolicyEntityTest: et3.commit Succeeded********");
792 logger.debug("\n\n***********PolicyEntityTest: et3.commit Failed********"
793 + "\nTRANSACTION ROLLBACK "
794 + "\n with exception: " + e);