/modules/apps/users-admin/users-admin-test/src/testIntegration/java/com/liferay/users/admin/internal/exportimport/data/handler/test/OrganizationStagedModelDataHandlerTest.java

http://github.com/liferay/liferay-portal · Java · 314 lines · 223 code · 75 blank · 16 comment · 5 complexity · 77bfa11f7138a4b98af1d50e1a14fc9c 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.internal.exportimport.data.handler.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.exportimport.test.util.lar.BaseStagedModelDataHandlerTestCase;
  17. import com.liferay.portal.kernel.exception.PortalException;
  18. import com.liferay.portal.kernel.model.Address;
  19. import com.liferay.portal.kernel.model.EmailAddress;
  20. import com.liferay.portal.kernel.model.Group;
  21. import com.liferay.portal.kernel.model.OrgLabor;
  22. import com.liferay.portal.kernel.model.Organization;
  23. import com.liferay.portal.kernel.model.PasswordPolicy;
  24. import com.liferay.portal.kernel.model.PasswordPolicyRel;
  25. import com.liferay.portal.kernel.model.Phone;
  26. import com.liferay.portal.kernel.model.StagedModel;
  27. import com.liferay.portal.kernel.model.Website;
  28. import com.liferay.portal.kernel.service.AddressLocalServiceUtil;
  29. import com.liferay.portal.kernel.service.EmailAddressLocalServiceUtil;
  30. import com.liferay.portal.kernel.service.OrgLaborLocalServiceUtil;
  31. import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
  32. import com.liferay.portal.kernel.service.PasswordPolicyRelLocalServiceUtil;
  33. import com.liferay.portal.kernel.service.PhoneLocalServiceUtil;
  34. import com.liferay.portal.kernel.service.WebsiteLocalServiceUtil;
  35. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  36. import com.liferay.portal.kernel.test.util.OrganizationTestUtil;
  37. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  38. import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
  39. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  40. import java.util.List;
  41. import java.util.Map;
  42. import org.junit.After;
  43. import org.junit.Assert;
  44. import org.junit.ClassRule;
  45. import org.junit.Rule;
  46. import org.junit.runner.RunWith;
  47. /**
  48. * @author David Mendez Gonzalez
  49. */
  50. @RunWith(Arquillian.class)
  51. public class OrganizationStagedModelDataHandlerTest
  52. extends BaseStagedModelDataHandlerTestCase {
  53. @ClassRule
  54. @Rule
  55. public static final AggregateTestRule aggregateTestRule =
  56. new LiferayIntegrationTestRule();
  57. @After
  58. @Override
  59. public void tearDown() throws Exception {
  60. super.tearDown();
  61. if (_organization != null) {
  62. _organization =
  63. OrganizationLocalServiceUtil.
  64. fetchOrganizationByUuidAndCompanyId(
  65. _organization.getUuid(), _organization.getCompanyId());
  66. if (_organization != null) {
  67. deleteOrganizations(_organization);
  68. }
  69. }
  70. }
  71. @Override
  72. protected StagedModel addStagedModel(
  73. Group group,
  74. Map<String, List<StagedModel>> dependentStagedModelsMap)
  75. throws Exception {
  76. _organization = OrganizationTestUtil.addOrganization();
  77. Organization suborganization = OrganizationTestUtil.addOrganization(
  78. _organization.getOrganizationId(), RandomTestUtil.randomString(),
  79. false);
  80. addDependentStagedModel(
  81. dependentStagedModelsMap, Organization.class, suborganization);
  82. Address address = OrganizationTestUtil.addAddress(_organization);
  83. addDependentStagedModel(
  84. dependentStagedModelsMap, Address.class, address);
  85. EmailAddress emailAddress = OrganizationTestUtil.addEmailAddress(
  86. _organization);
  87. addDependentStagedModel(
  88. dependentStagedModelsMap, EmailAddress.class, emailAddress);
  89. OrganizationTestUtil.addOrgLabor(_organization);
  90. PasswordPolicy passwordPolicy =
  91. OrganizationTestUtil.addPasswordPolicyRel(
  92. _organization,
  93. ServiceContextTestUtil.getServiceContext(group.getGroupId()));
  94. addDependentStagedModel(
  95. dependentStagedModelsMap, PasswordPolicy.class, passwordPolicy);
  96. Phone phone = OrganizationTestUtil.addPhone(_organization);
  97. addDependentStagedModel(dependentStagedModelsMap, Phone.class, phone);
  98. Website website = OrganizationTestUtil.addWebsite(_organization);
  99. addDependentStagedModel(
  100. dependentStagedModelsMap, Website.class, website);
  101. return _organization;
  102. }
  103. protected void deleteOrganizations(Organization organization)
  104. throws Exception {
  105. List<Organization> childOrganizations =
  106. OrganizationLocalServiceUtil.getOrganizations(
  107. organization.getCompanyId(), organization.getOrganizationId());
  108. for (Organization childOrganization : childOrganizations) {
  109. deleteOrganizations(childOrganization);
  110. }
  111. OrganizationLocalServiceUtil.deleteOrganization(organization);
  112. }
  113. @Override
  114. protected void deleteStagedModel(
  115. StagedModel stagedModel,
  116. Map<String, List<StagedModel>> dependentStagedModelsMap,
  117. Group group)
  118. throws Exception {
  119. List<StagedModel> dependentOrganizationStagedModels =
  120. dependentStagedModelsMap.get(Organization.class.getSimpleName());
  121. Organization suborganization =
  122. (Organization)dependentOrganizationStagedModels.get(0);
  123. OrganizationLocalServiceUtil.deleteOrganization(suborganization);
  124. OrganizationLocalServiceUtil.deleteOrganization(
  125. (Organization)stagedModel);
  126. }
  127. @Override
  128. protected StagedModel getStagedModel(String uuid, Group group)
  129. throws PortalException {
  130. return OrganizationLocalServiceUtil.getOrganizationByUuidAndCompanyId(
  131. uuid, group.getCompanyId());
  132. }
  133. @Override
  134. protected Class<? extends StagedModel> getStagedModelClass() {
  135. return Organization.class;
  136. }
  137. @Override
  138. protected void validateImport(
  139. StagedModel stagedModel, StagedModelAssets stagedModelAssets,
  140. Map<String, List<StagedModel>> dependentStagedModelsMap,
  141. Group group)
  142. throws Exception {
  143. Organization organization =
  144. OrganizationLocalServiceUtil.fetchOrganizationByUuidAndCompanyId(
  145. stagedModel.getUuid(), group.getCompanyId());
  146. validateAssets(organization, stagedModelAssets, group);
  147. List<StagedModel> addressDependentStagedModels =
  148. dependentStagedModelsMap.get(Address.class.getSimpleName());
  149. Assert.assertEquals(
  150. addressDependentStagedModels.toString(), 1,
  151. addressDependentStagedModels.size());
  152. Address address = (Address)addressDependentStagedModels.get(0);
  153. Address importedAddress =
  154. AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
  155. address.getUuid(), group.getCompanyId());
  156. Assert.assertNotNull(importedAddress);
  157. Assert.assertEquals(
  158. organization.getOrganizationId(), importedAddress.getClassPK());
  159. List<StagedModel> emailAddressDependentStagedModels =
  160. dependentStagedModelsMap.get(EmailAddress.class.getSimpleName());
  161. Assert.assertEquals(
  162. emailAddressDependentStagedModels.toString(), 1,
  163. emailAddressDependentStagedModels.size());
  164. EmailAddress emailAddress =
  165. (EmailAddress)emailAddressDependentStagedModels.get(0);
  166. EmailAddress importedEmailAddress =
  167. EmailAddressLocalServiceUtil.fetchEmailAddressByUuidAndCompanyId(
  168. emailAddress.getUuid(), group.getCompanyId());
  169. Assert.assertNotNull(importedEmailAddress);
  170. Assert.assertEquals(
  171. organization.getOrganizationId(),
  172. importedEmailAddress.getClassPK());
  173. List<OrgLabor> importedOrgLabors =
  174. OrgLaborLocalServiceUtil.getOrgLabors(
  175. organization.getOrganizationId());
  176. Assert.assertEquals(
  177. importedOrgLabors.toString(), 1, importedOrgLabors.size());
  178. OrgLabor importedOrgLabor = importedOrgLabors.get(0);
  179. Assert.assertEquals(
  180. organization.getOrganizationId(),
  181. importedOrgLabor.getOrganizationId());
  182. List<StagedModel> passwordPolicyDependentStagedModels =
  183. dependentStagedModelsMap.get(PasswordPolicy.class.getSimpleName());
  184. Assert.assertEquals(
  185. passwordPolicyDependentStagedModels.toString(), 1,
  186. passwordPolicyDependentStagedModels.size());
  187. PasswordPolicy passwordPolicy =
  188. (PasswordPolicy)passwordPolicyDependentStagedModels.get(0);
  189. PasswordPolicyRel importedPasswordPolicyRel =
  190. PasswordPolicyRelLocalServiceUtil.fetchPasswordPolicyRel(
  191. organization.getModelClassName(),
  192. organization.getOrganizationId());
  193. Assert.assertNotNull(importedPasswordPolicyRel);
  194. Assert.assertEquals(
  195. passwordPolicy.getPasswordPolicyId(),
  196. importedPasswordPolicyRel.getPasswordPolicyId());
  197. List<StagedModel> phoneDependentStagedModels =
  198. dependentStagedModelsMap.get(Phone.class.getSimpleName());
  199. Assert.assertEquals(
  200. phoneDependentStagedModels.toString(), 1,
  201. phoneDependentStagedModels.size());
  202. Phone phone = (Phone)phoneDependentStagedModels.get(0);
  203. Phone importedPhone =
  204. PhoneLocalServiceUtil.fetchPhoneByUuidAndCompanyId(
  205. phone.getUuid(), group.getCompanyId());
  206. Assert.assertNotNull(importedPhone);
  207. Assert.assertEquals(
  208. organization.getOrganizationId(), importedPhone.getClassPK());
  209. List<StagedModel> websiteDependentStagedModels =
  210. dependentStagedModelsMap.get(Website.class.getSimpleName());
  211. Assert.assertEquals(
  212. websiteDependentStagedModels.toString(), 1,
  213. websiteDependentStagedModels.size());
  214. Website website = (Website)websiteDependentStagedModels.get(0);
  215. Website importedWebsite =
  216. WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
  217. website.getUuid(), group.getCompanyId());
  218. Assert.assertNotNull(importedWebsite);
  219. Assert.assertEquals(
  220. organization.getOrganizationId(), importedWebsite.getClassPK());
  221. }
  222. @Override
  223. protected void validateImportedStagedModel(
  224. StagedModel stagedModel, StagedModel importedStagedModel)
  225. throws Exception {
  226. super.validateImportedStagedModel(stagedModel, importedStagedModel);
  227. Organization organization = (Organization)stagedModel;
  228. Organization importedOrganization = (Organization)importedStagedModel;
  229. Assert.assertEquals(
  230. organization.getName(), importedOrganization.getName());
  231. Assert.assertEquals(
  232. organization.getType(), importedOrganization.getType());
  233. Assert.assertEquals(
  234. organization.isRecursable(), importedOrganization.isRecursable());
  235. Assert.assertEquals(
  236. organization.getComments(), importedOrganization.getComments());
  237. }
  238. private Organization _organization;
  239. }