2 * ============LICENSE_START=======================================================
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
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 java.util.Date;
26 import java.util.List;
27 import java.util.Properties;
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;
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;
44 public class PolicyEntityTest {
46 private static Logger logger = FlexLogger.getLogger(PolicyEntityTest.class);
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();
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();
67 // Create a policy object
68 PolicyEntity p1 = new PolicyEntity();
73 long policyId1 = p1.getPolicyId();
75 String policyName1 = p1.getPolicyName();
77 int version1 = p1.getVersion();
79 String policyData1 = p1.getPolicyData();
81 ConfigurationDataEntity configData1 = p1.getConfigurationData();
82 String configDataStr1 = (configData1 != null ? "configurationDataId = " + configData1.getConfigurationDataId()
83 : "configurationData is null");
85 ActionBodyEntity actionBody1 = p1.getActionBodyEntity();
86 String actionBodyStr1 =
87 (actionBody1 != null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
89 String createdBy1 = p1.getCreatedBy();
91 Date createdDate1 = p1.getCreatedDate();
92 String createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
94 String description = p1.getDescription();
96 String modifiedBy1 = p1.getModifiedBy();
98 Date modifiedDate1 = p1.getModifiedDate();
99 String modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
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 = "
111 p1.setPolicyName("testPID2");
114 p1.setPolicyData("<policy>PolicyData</policy>");
116 // We will NOT set the ConfigurationDataEntity or ActionBodyEntity object just to test that it is optional
119 p1.setCreatedBy("kevin");
121 // createdDate will be set when it is persisted
124 p1.setScope("mckiou.kevin");
127 p1.setDescription("PolicyEntity Description");
130 p1.setModifiedBy("kevin");
132 // modifiedDate will be set when it is persisted
137 // Now lets get some attribute values
139 policyId1 = p1.getPolicyId();
141 policyName1 = p1.getPolicyName();
143 version1 = p1.getVersion();
145 policyData1 = p1.getPolicyData();
147 configData1 = p1.getConfigurationData();
148 configDataStr1 = (configData1 != null ? "configurationDataId = " + configData1.getConfigurationDataId()
149 : "configurationData is null");
151 actionBody1 = p1.getActionBodyEntity();
153 (actionBody1 != null ? "actionBodyId = " + actionBody1.getActionBodyId() : "actionBody is null");
155 createdBy1 = p1.getCreatedBy();
157 createdDate1 = p1.getCreatedDate();
158 createdDateStr1 = (createdDate1 != null ? createdDate1.toString() : "createdDate is null");
160 description = p1.getDescription();
162 modifiedBy1 = p1.getModifiedBy();
164 modifiedDate1 = p1.getModifiedDate();
165 modifiedDateStr1 = (modifiedDate1 != null ? modifiedDate1.toString() : "modifiedDate is null");
167 logger.debug("\n\n********PolicyEntityTest: Local PolicyEntity and Configuration objects after "
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());
175 // Now lets fully configure the configurationData and actionBody
177 // Create a ConfigurationDataEntity object and set ID
178 ConfigurationDataEntity c1 = new ConfigurationDataEntity();
180 ActionBodyEntity a1 = new ActionBodyEntity();
182 // persist the configuration Data
185 c1.setConfigType("OTHER");
187 c1.setConfigBody("ABC");
189 c1.setDescription("ConfigurationDataEntity Description");
191 c1.setCreatedBy("kevin");
193 // c1.setModifiedBy("kevin");
197 // persist the action Body
201 a1.setActionBody("myActionBody");
203 a1.setActionBodyName("myActionBodyName");
205 a1.setCreatedBy("kevin");
207 a1.setModifiedBy("kevin");
209 a1.setDeleted(false);
211 long configurationDataId = c1.getConfigurationDataId();
213 int cdVersion = c1.getVersion();
215 String cdConfigType = c1.getConfigType();
217 String cdConfigBody = c1.getConfigBody();
219 String cdCreatedBy = c1.getCreatedBy();
221 Date cdCreatedDate = c1.getCreatedDate();
223 String cdDescription = c1.getDescription();
225 String cdModifiedBy = c1.getModifiedBy();
227 Date cdModifiedDate = c1.getModifiedDate();
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());
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());
241 p1.setScope("mckiou.kevin.kim");
246 // Perform policy selects
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");
251 query.setParameter("pid", p1.getPolicyId());
252 queryscope.setParameter("s", "mckiou.kevin.kim");
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);
262 fail("\nPolicyEntityTest: No PolicyEntity using scope DB entry found");
265 // The scope object on the retrieved policy object should be same as the one we used to find it
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);
277 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
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")
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 = "
298 // Confirm that the retrieved policy object is the same as the persisted object
301 // Perform configurationData selects
302 Query query2 = em.createQuery("Select c from ConfigurationDataEntity c where c.configurationDataId=:cid");
304 query2.setParameter("cid", c1.getConfigurationDataId());
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);
313 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
317 "\n\n********PolicyEntityTest: Configuration object after retrieving from DB BEFORE assigning to "
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());
325 // Confirm the retrieved ConfigurationDataEntity object is the same as the persisted
328 // Now assign the configurationData to the policy
329 p1.setConfigurationData(c1);
331 // Perform actionBody selects
332 Query querya2 = em.createQuery("Select a from ActionBodyEntity a where a.actionBodyId=:aid");
334 querya2.setParameter("aid", a1.getActionBodyId());
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);
343 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
347 "\n\n********PolicyEntityTest: Local Action Body object after retrieving from DB BEFORE assigning to "
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());
355 // Confirm the retrieved ActionBodyEntity object is the same as the persisted
358 // Now assign the ActionBodyEntity to the policy
359 p1.setActionBodyEntity(a1);
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();
367 if (!resultList.isEmpty()) {
368 // ignores multiple results
369 p2 = (PolicyEntity) resultList.get(0);
371 fail("PolicyEntityTest: No PolicyEntity DB entry found");
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")
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 = "
392 // And now the ConfigurationDataEntity object
393 resultList = query2.getResultList();
395 if (!resultList.isEmpty()) {
396 // ignores multiple results
397 c2 = (ConfigurationDataEntity) resultList.get(0);
399 fail("\nPolicyEntityTest: No ConfigurationDataEntity DB entry found");
403 "\n\n********PolicyEntityTest: Configuration object after retrieving from DB AFTER assigning to "
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());
411 // Get the database version of the Action Body
412 resultList = querya2.getResultList();
414 if (!resultList.isEmpty()) {
415 // ignores multiple results
416 a2 = (ActionBodyEntity) resultList.get(0);
418 fail("\nPolicyEntityTest: No ActionBodyEntity DB entry found");
422 "\n\n********PolicyEntityTest: Local Action Body object after retrieving from DB AFTER assigning to "
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());
430 // ****Now lets see if the orphanRemoval=true does anything useful***
431 // Remove the configurationData from the policy relationship
433 p1.setConfigurationData(null);
435 p1.setActionBodyEntity(null);
437 // flush the update to the DB
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();
444 if (resultList.isEmpty()) {
445 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
446 + "\n Success!! No ConfigurationDataEntity DB entry found");
449 c2 = (ConfigurationDataEntity) resultList.get(0);
450 fail("\nPolicyEntityTest: ConfigurationDataEntity DB entry found - and none should exist"
451 + "\nconfigurationDataId = " + c2.getConfigurationDataId());
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();
458 if (resultList.isEmpty()) {
459 logger.debug("\n\n********PolicyEntityTest: orphanRemoval=true******"
460 + "\n Success!! No ActionBodyEntity DB entry found");
463 a2 = (ActionBodyEntity) resultList.get(0);
464 fail("\nPolicyEntityTest: ActionBodyEntity DB entry found - and none should exist" + "\nactionBodyId = "
465 + a2.getActionBodyId());
468 // Now lets put the configurationData and actionBody back into the policy object and see what appears
469 // in the DB after a flush
471 // put c1 back into the persistence context since the orphanRemoval removed it.
473 p1.setConfigurationData(c1);
476 p1.setActionBodyEntity(a1);
480 // retrieve the policy object
481 resultList = query.getResultList();
483 if (!resultList.isEmpty()) {
484 // ignores multiple results
485 p2 = (PolicyEntity) resultList.get(0);
487 fail("\nPolicyEntityTest: No PolicyEntity DB entry found");
490 // output what we policy object found
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")
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 = "
508 // now lets see if it put the configurationData c1 back into the table
509 resultList = query2.getResultList();
511 if (!resultList.isEmpty()) {
512 // ignores multiple results
513 c2 = (ConfigurationDataEntity) resultList.get(0);
515 fail("\nPolicyEntityTest - Check re-entry of configurationData into DB"
516 + "No ConfigurationDataEntity DB entry found");
519 // output what configurationData object we found
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());
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();
533 if (!resultList.isEmpty()) {
534 // ignores multiple results
535 a2 = (ActionBodyEntity) resultList.get(0);
537 fail("\nPolicyEntityTest - Check re-entry of actionBody into DB" + "No ActionBodyEntity DB entry found");
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());
549 // I want to save all the above in the DB
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);
558 // Start a new transaction
559 EntityTransaction et2 = em.getTransaction();
563 // Let's test if the PolicyEntity uniqueConstraint for policyName and scopeId hold
564 PolicyEntity p3 = new PolicyEntity();
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");
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());
576 // Assert that the policyIds are NOT the same to show that the automatic sequencing is working
577 assert (p1.getPolicyId() != p3.getPolicyId());
580 // Now set the scope the same to verify the uniqueness constraint will be enforced
581 p3.setScope(p1.getScope());
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()
590 } catch (Exception e) {
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()
597 + p3.getScope() + "\n with excpetion: " + e);
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);
608 // ****************Test the PolicyDBDaoEntity************************
610 // Create a transaction
611 EntityTransaction et3 = em.getTransaction();
616 PolicyDBDaoEntity pe1 = new PolicyDBDaoEntity();
619 pe1.setDescription("This is pe1");
621 pe1.setPolicyDBDaoUrl("http://123.45.2.456:2345");
627 PolicyDBDaoEntity pe2 = new PolicyDBDaoEntity();
630 pe2.setDescription("This is pe2");
632 pe2.setPolicyDBDaoUrl("http://789.01.2.345:2345");
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());
645 // Now let's retrieve them from the DB using the named query
647 resultList = em.createNamedQuery("PolicyDBDaoEntity.findAll").getResultList();
649 PolicyDBDaoEntity pex = null;
650 PolicyDBDaoEntity pey = null;
652 if (!resultList.isEmpty()) {
653 if (resultList.size() != 2) {
654 fail("\nPolicyEntityTest: Number of PolicyDBDaoEntity entries = " + resultList.size()
657 for (Object policyDBDaoEntity : resultList) {
658 PolicyDBDaoEntity pdbdao = (PolicyDBDaoEntity) policyDBDaoEntity;
659 if (pdbdao.getPolicyDBDaoUrl().equals("http://123.45.2.456:2345")) {
661 } else if (pdbdao.getPolicyDBDaoUrl().equals("http://789.01.2.345:2345")) {
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);
680 assertSame(pe2, pex);
681 assertSame(pe1, pey);
685 fail("\nPolicyEntityTest: No PolicyDBDaoEntity DB entry found");
688 // Now let's see if we can do an update on the PolicyDBDaoEntity which we retrieved.
690 pex.setDescription("This is pex");
694 Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyDBDaoEntity p WHERE p.description=:desc");
695 resultList = createPolicyQuery.setParameter("desc", "This is pex").getResultList();
697 PolicyDBDaoEntity pez = null;
699 if (!resultList.isEmpty()) {
700 if (resultList.size() != 1) {
701 fail("\nPolicyEntityTest: Update Test - Number of PolicyDBDaoEntity entries = " + resultList.size()
704 pez = (PolicyDBDaoEntity) resultList.get(0);
706 // Print them to the log before flushing
708 "\n\n***********PolicyEntityTest: Update Test - PolicyDBDaoEntity objects retrieved from "
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);
720 fail("\nPolicyEntityTest: Update Test - No PolicyDBDaoEntity DB updated entry found");
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();
729 // Wrap up the transaction
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);