/modules/apps/foundation/users-admin/users-admin-test/src/testIntegration/java/com/liferay/users/admin/lar/test/OrganizationStagedModelDataHandlerTest.java

http://github.com/liferay/liferay-portal · Java · 315 lines · 224 code · 75 blank · 16 comment · 5 complexity · efca17a0a39180a50071c70fab5818ef MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.users.admin.lar.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.portal.kernel.model.Address;
  17. import com.liferay.portal.kernel.model.EmailAddress;
  18. import com.liferay.portal.kernel.model.Group;
  19. import com.liferay.portal.kernel.model.OrgLabor;
  20. import com.liferay.portal.kernel.model.Organization;
  21. import com.liferay.portal.kernel.model.PasswordPolicy;
  22. import com.liferay.portal.kernel.model.PasswordPolicyRel;
  23. import com.liferay.portal.kernel.model.Phone;
  24. import com.liferay.portal.kernel.model.StagedModel;
  25. import com.liferay.portal.kernel.model.Website;
  26. import com.liferay.portal.kernel.service.AddressLocalServiceUtil;
  27. import com.liferay.portal.kernel.service.EmailAddressLocalServiceUtil;
  28. import com.liferay.portal.kernel.service.OrgLaborLocalServiceUtil;
  29. import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
  30. import com.liferay.portal.kernel.service.PasswordPolicyRelLocalServiceUtil;
  31. import com.liferay.portal.kernel.service.PhoneLocalServiceUtil;
  32. import com.liferay.portal.kernel.service.ServiceContext;
  33. import com.liferay.portal.kernel.service.WebsiteLocalServiceUtil;
  34. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  35. import com.liferay.portal.kernel.test.rule.Sync;
  36. import com.liferay.portal.kernel.test.rule.SynchronousDestinationTestRule;
  37. import com.liferay.portal.kernel.test.rule.TransactionalTestRule;
  38. import com.liferay.portal.kernel.test.util.OrganizationTestUtil;
  39. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  40. import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
  41. import com.liferay.portal.lar.test.BaseStagedModelDataHandlerTestCase;
  42. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  43. import java.util.List;
  44. import java.util.Map;
  45. import org.junit.After;
  46. import org.junit.Assert;
  47. import org.junit.ClassRule;
  48. import org.junit.Rule;
  49. import org.junit.runner.RunWith;
  50. /**
  51. * @author David Mendez Gonzalez
  52. */
  53. @RunWith(Arquillian.class)
  54. @Sync
  55. public class OrganizationStagedModelDataHandlerTest
  56. extends BaseStagedModelDataHandlerTestCase {
  57. @ClassRule
  58. @Rule
  59. public static final AggregateTestRule aggregateTestRule =
  60. new AggregateTestRule(
  61. new LiferayIntegrationTestRule(),
  62. SynchronousDestinationTestRule.INSTANCE,
  63. TransactionalTestRule.INSTANCE);
  64. @After
  65. @Override
  66. public void tearDown() throws Exception {
  67. super.tearDown();
  68. if (_organization != null) {
  69. _organization =
  70. OrganizationLocalServiceUtil.
  71. fetchOrganizationByUuidAndCompanyId(
  72. _organization.getUuid(), _organization.getCompanyId());
  73. if (_organization != null) {
  74. deleteOrganizations(_organization);
  75. }
  76. }
  77. }
  78. @Override
  79. protected StagedModel addStagedModel(
  80. Group group,
  81. Map<String, List<StagedModel>> dependentStagedModelsMap)
  82. throws Exception {
  83. _organization = OrganizationTestUtil.addOrganization();
  84. Organization suborganization = OrganizationTestUtil.addOrganization(
  85. _organization.getOrganizationId(), RandomTestUtil.randomString(),
  86. false);
  87. addDependentStagedModel(
  88. dependentStagedModelsMap, Organization.class, suborganization);
  89. Address address = OrganizationTestUtil.addAddress(_organization);
  90. addDependentStagedModel(
  91. dependentStagedModelsMap, Address.class, address);
  92. EmailAddress emailAddress = OrganizationTestUtil.addEmailAddress(
  93. _organization);
  94. addDependentStagedModel(
  95. dependentStagedModelsMap, EmailAddress.class, emailAddress);
  96. OrganizationTestUtil.addOrgLabor(_organization);
  97. ServiceContext serviceContext =
  98. ServiceContextTestUtil.getServiceContext(group.getGroupId());
  99. PasswordPolicy passwordPolicy =
  100. OrganizationTestUtil.addPasswordPolicyRel(
  101. _organization, serviceContext);
  102. addDependentStagedModel(
  103. dependentStagedModelsMap, PasswordPolicy.class, passwordPolicy);
  104. Phone phone = OrganizationTestUtil.addPhone(_organization);
  105. addDependentStagedModel(dependentStagedModelsMap, Phone.class, phone);
  106. Website website = OrganizationTestUtil.addWebsite(_organization);
  107. addDependentStagedModel(
  108. dependentStagedModelsMap, Website.class, website);
  109. return _organization;
  110. }
  111. protected void deleteOrganizations(Organization organization)
  112. throws Exception {
  113. List<Organization> childOrganizations =
  114. OrganizationLocalServiceUtil.getOrganizations(
  115. organization.getCompanyId(), organization.getOrganizationId());
  116. for (Organization childOrganization : childOrganizations) {
  117. deleteOrganizations(childOrganization);
  118. }
  119. OrganizationLocalServiceUtil.deleteOrganization(organization);
  120. }
  121. @Override
  122. protected void deleteStagedModel(
  123. StagedModel stagedModel,
  124. Map<String, List<StagedModel>> dependentStagedModelsMap,
  125. Group group)
  126. throws Exception {
  127. List<StagedModel> dependentOrganizationStagedModels =
  128. dependentStagedModelsMap.get(Organization.class.getSimpleName());
  129. Organization suborganization =
  130. (Organization)dependentOrganizationStagedModels.get(0);
  131. OrganizationLocalServiceUtil.deleteOrganization(suborganization);
  132. OrganizationLocalServiceUtil.deleteOrganization(
  133. (Organization)stagedModel);
  134. }
  135. @Override
  136. protected StagedModel getStagedModel(String uuid, Group group) {
  137. try {
  138. return OrganizationLocalServiceUtil.
  139. fetchOrganizationByUuidAndCompanyId(uuid, group.getCompanyId());
  140. }
  141. catch (Exception e) {
  142. return null;
  143. }
  144. }
  145. @Override
  146. protected Class<? extends StagedModel> getStagedModelClass() {
  147. return Organization.class;
  148. }
  149. @Override
  150. protected void validateImport(
  151. StagedModel stagedModel, StagedModelAssets stagedModelAssets,
  152. Map<String, List<StagedModel>> dependentStagedModelsMap,
  153. Group group)
  154. throws Exception {
  155. Organization organization =
  156. OrganizationLocalServiceUtil.fetchOrganizationByUuidAndCompanyId(
  157. stagedModel.getUuid(), group.getCompanyId());
  158. validateAssets(organization, stagedModelAssets, group);
  159. List<StagedModel> addressDependentStagedModels =
  160. dependentStagedModelsMap.get(Address.class.getSimpleName());
  161. Assert.assertEquals(1, addressDependentStagedModels.size());
  162. Address address = (Address)addressDependentStagedModels.get(0);
  163. Address importedAddress =
  164. AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
  165. address.getUuid(), group.getCompanyId());
  166. Assert.assertNotNull(importedAddress);
  167. Assert.assertEquals(
  168. organization.getOrganizationId(), importedAddress.getClassPK());
  169. List<StagedModel> emailAddressDependentStagedModels =
  170. dependentStagedModelsMap.get(EmailAddress.class.getSimpleName());
  171. Assert.assertEquals(1, emailAddressDependentStagedModels.size());
  172. EmailAddress emailAddress =
  173. (EmailAddress)emailAddressDependentStagedModels.get(0);
  174. EmailAddress importedEmailAddress =
  175. EmailAddressLocalServiceUtil.fetchEmailAddressByUuidAndCompanyId(
  176. emailAddress.getUuid(), group.getCompanyId());
  177. Assert.assertNotNull(importedEmailAddress);
  178. Assert.assertEquals(
  179. organization.getOrganizationId(),
  180. importedEmailAddress.getClassPK());
  181. List<OrgLabor> importedOrgLabors =
  182. OrgLaborLocalServiceUtil.getOrgLabors(
  183. organization.getOrganizationId());
  184. Assert.assertEquals(1, importedOrgLabors.size());
  185. OrgLabor importedOrgLabor = importedOrgLabors.get(0);
  186. Assert.assertEquals(
  187. organization.getOrganizationId(),
  188. importedOrgLabor.getOrganizationId());
  189. List<StagedModel> passwordPolicyDependentStagedModels =
  190. dependentStagedModelsMap.get(PasswordPolicy.class.getSimpleName());
  191. Assert.assertEquals(1, passwordPolicyDependentStagedModels.size());
  192. PasswordPolicy passwordPolicy =
  193. (PasswordPolicy)passwordPolicyDependentStagedModels.get(0);
  194. PasswordPolicyRel importedPasswordPolicyRel =
  195. PasswordPolicyRelLocalServiceUtil.fetchPasswordPolicyRel(
  196. organization.getModelClassName(),
  197. organization.getOrganizationId());
  198. Assert.assertNotNull(importedPasswordPolicyRel);
  199. Assert.assertEquals(
  200. passwordPolicy.getPasswordPolicyId(),
  201. importedPasswordPolicyRel.getPasswordPolicyId());
  202. List<StagedModel> phoneDependentStagedModels =
  203. dependentStagedModelsMap.get(Phone.class.getSimpleName());
  204. Assert.assertEquals(1, phoneDependentStagedModels.size());
  205. Phone phone = (Phone)phoneDependentStagedModels.get(0);
  206. Phone importedPhone =
  207. PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
  208. phone.getUuid(), group.getCompanyId());
  209. Assert.assertNotNull(importedPhone);
  210. Assert.assertEquals(
  211. organization.getOrganizationId(), importedPhone.getClassPK());
  212. List<StagedModel> websiteDependentStagedModels =
  213. dependentStagedModelsMap.get(Website.class.getSimpleName());
  214. Assert.assertEquals(1, websiteDependentStagedModels.size());
  215. Website website = (Website)websiteDependentStagedModels.get(0);
  216. Website importedWebsite =
  217. WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
  218. website.getUuid(), group.getCompanyId());
  219. Assert.assertNotNull(importedWebsite);
  220. Assert.assertEquals(
  221. organization.getOrganizationId(), importedWebsite.getClassPK());
  222. }
  223. @Override
  224. protected void validateImportedStagedModel(
  225. StagedModel stagedModel, StagedModel importedStagedModel)
  226. throws Exception {
  227. super.validateImportedStagedModel(stagedModel, importedStagedModel);
  228. Organization organization = (Organization)stagedModel;
  229. Organization importedOrganization = (Organization)importedStagedModel;
  230. Assert.assertEquals(
  231. organization.getName(), importedOrganization.getName());
  232. Assert.assertEquals(
  233. organization.getType(), importedOrganization.getType());
  234. Assert.assertEquals(
  235. organization.isRecursable(), importedOrganization.isRecursable());
  236. Assert.assertEquals(
  237. organization.getComments(), importedOrganization.getComments());
  238. }
  239. private Organization _organization;
  240. }