Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3.  
  4. * Version 1.0 (the "License"); you may not use this file except in
  5. * compliance with the License. You may obtain a copy of the License at
  6. * http://license.openmrs.org
  7. *
  8. * Software distributed under the License is distributed on an "AS IS"
  9. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  10. * License for the specific language governing rights and limitations
  11. * under the License.
  12. *
  13. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  14. */
  15.  
  16. package org.openmrs.module.metadatadeploy.handler.impl;
  17.  
  18.  
  19. import org.hibernate.FlushMode;
  20.  
  21.  
  22.  
  23.  
  24. import org.hibernate.SessionFactory;
  25. import org.junit.Assert;
  26. import org.junit.Test;
  27. import org.openmrs.Privilege;
  28. import org.openmrs.Role;
  29. import org.openmrs.api.context.Context;
  30. import org.openmrs.module.metadatadeploy.MetadataUtils;
  31. import org.openmrs.module.metadatadeploy.api.MetadataDeployService;
  32. import org.openmrs.test.BaseModuleContextSensitiveTest;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34.  
  35. import static org.hamcrest.Matchers.contains;
  36. import static org.hamcrest.Matchers.containsInAnyOrder;
  37.  
  38. import static org.hamcrest.Matchers.is;
  39.  
  40. import static org.hamcrest.Matchers.nullValue;
  41. import static org.openmrs.module.metadatadeploy.bundle.CoreConstructors.idSet;
  42. import static org.openmrs.module.metadatadeploy.bundle.CoreConstructors.privilege;
  43. import static org.openmrs.module.metadatadeploy.bundle.CoreConstructors.role;
  44.  
  45. import java.lang.reflect.Method;
  46.  
  47. /**
  48. * Tests for {@link RoleDeployHandler}
  49. */
  50. public class RoleDeployHandlerTest extends BaseModuleContextSensitiveTest {
  51.  
  52. @Autowired
  53. private MetadataDeployService deployService;
  54.  
  55. @Autowired
  56. private SessionFactory sessionFactory;
  57.  
  58.  
  59. /**
  60. * Tests use of handler for installation
  61. */
  62.  
  63. /**
  64. * Replicates DPLY-1: Overwriting of existing roles can lose inherited roles
  65. */
  66. @Test
  67. public void integration_shouldNotLoseInheritedRoles() throws Exception {
  68. deployService.installObject(role("Role1", "Testing", null, null));
  69. deployService.installObject(role("Role2", "Testing", idSet("Role1"), null));
  70.  
  71. Context.flushSession();
  72. Context.clearSession();
  73.  
  74. //TestUtil.printOutTableContents(getConnection(), "role_role");
  75.  
  76. deployService.installObject(role("Role1", "Testing", null, null));
  77. deployService.installObject(role("Role2", "Testing", idSet("Role1"), null));
  78.  
  79. Context.flushSession();
  80. Context.clearSession();
  81.  
  82. //TestUtil.printOutTableContents(getConnection(), "role_role");
  83.  
  84. Role role1 = MetadataUtils.existing(Role.class, "Role1");
  85. Role role2 = MetadataUtils.existing(Role.class, "Role2");
  86.  
  87. Assert.assertThat(role2.getInheritedRoles(), contains(role1));
  88. }
  89.  
  90. /**
  91. * Gets the current hibernate session while taking care of the hibernate 3 and 4 differences.
  92. *
  93. * @return the current hibernate session.
  94. */
  95. private org.hibernate.Session getCurrentSession() {
  96. try {
  97. return sessionFactory.getCurrentSession();
  98. }
  99. catch (NoSuchMethodError ex) {
  100. try {
  101. Method method = sessionFactory.getClass().getMethod("getCurrentSession", null);
  102. return (org.hibernate.Session)method.invoke(sessionFactory, null);
  103. }
  104. catch (Exception e) {
  105. throw new RuntimeException("Failed to get the current hibernate session", e);
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement