PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/test/persistence-test/src/testIntegration/java/com/liferay/portal/service/persistence/test/OrganizationPersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 646 lines | 451 code | 179 blank | 16 comment | 1 complexity | 8869828c11fe85b0ac0095ac45edd4dd MD5 | raw file
Possible License(s): LGPL-2.0
  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.portal.service.persistence.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
  17. import com.liferay.portal.kernel.dao.orm.DynamicQuery;
  18. import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
  19. import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
  20. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  21. import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
  22. import com.liferay.portal.kernel.exception.NoSuchOrganizationException;
  23. import com.liferay.portal.kernel.model.Organization;
  24. import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.OrganizationPersistence;
  26. import com.liferay.portal.kernel.service.persistence.OrganizationUtil;
  27. import com.liferay.portal.kernel.test.ReflectionTestUtil;
  28. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  29. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  30. import com.liferay.portal.kernel.transaction.Propagation;
  31. import com.liferay.portal.kernel.util.IntegerWrapper;
  32. import com.liferay.portal.kernel.util.OrderByComparator;
  33. import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
  34. import com.liferay.portal.kernel.util.Time;
  35. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  36. import com.liferay.portal.test.rule.PersistenceTestRule;
  37. import com.liferay.portal.test.rule.TransactionalTestRule;
  38. import java.io.Serializable;
  39. import java.util.ArrayList;
  40. import java.util.HashSet;
  41. import java.util.Iterator;
  42. import java.util.List;
  43. import java.util.Map;
  44. import java.util.Objects;
  45. import java.util.Set;
  46. import org.junit.After;
  47. import org.junit.Assert;
  48. import org.junit.Before;
  49. import org.junit.ClassRule;
  50. import org.junit.Rule;
  51. import org.junit.Test;
  52. import org.junit.runner.RunWith;
  53. /**
  54. * @generated
  55. */
  56. @RunWith(Arquillian.class)
  57. public class OrganizationPersistenceTest {
  58. @ClassRule
  59. @Rule
  60. public static final AggregateTestRule aggregateTestRule =
  61. new AggregateTestRule(
  62. new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE,
  63. new TransactionalTestRule(Propagation.REQUIRED));
  64. @Before
  65. public void setUp() {
  66. _persistence = OrganizationUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<Organization> iterator = _organizations.iterator();
  73. while (iterator.hasNext()) {
  74. _persistence.remove(iterator.next());
  75. iterator.remove();
  76. }
  77. }
  78. @Test
  79. public void testCreate() throws Exception {
  80. long pk = RandomTestUtil.nextLong();
  81. Organization organization = _persistence.create(pk);
  82. Assert.assertNotNull(organization);
  83. Assert.assertEquals(organization.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. Organization newOrganization = addOrganization();
  88. _persistence.remove(newOrganization);
  89. Organization existingOrganization = _persistence.fetchByPrimaryKey(
  90. newOrganization.getPrimaryKey());
  91. Assert.assertNull(existingOrganization);
  92. }
  93. @Test
  94. public void testUpdateNew() throws Exception {
  95. addOrganization();
  96. }
  97. @Test
  98. public void testUpdateExisting() throws Exception {
  99. long pk = RandomTestUtil.nextLong();
  100. Organization newOrganization = _persistence.create(pk);
  101. newOrganization.setMvccVersion(RandomTestUtil.nextLong());
  102. newOrganization.setCtCollectionId(RandomTestUtil.nextLong());
  103. newOrganization.setUuid(RandomTestUtil.randomString());
  104. newOrganization.setExternalReferenceCode(RandomTestUtil.randomString());
  105. newOrganization.setCompanyId(RandomTestUtil.nextLong());
  106. newOrganization.setUserId(RandomTestUtil.nextLong());
  107. newOrganization.setUserName(RandomTestUtil.randomString());
  108. newOrganization.setCreateDate(RandomTestUtil.nextDate());
  109. newOrganization.setModifiedDate(RandomTestUtil.nextDate());
  110. newOrganization.setParentOrganizationId(RandomTestUtil.nextLong());
  111. newOrganization.setTreePath(RandomTestUtil.randomString());
  112. newOrganization.setName(RandomTestUtil.randomString());
  113. newOrganization.setType(RandomTestUtil.randomString());
  114. newOrganization.setRecursable(RandomTestUtil.randomBoolean());
  115. newOrganization.setRegionId(RandomTestUtil.nextLong());
  116. newOrganization.setCountryId(RandomTestUtil.nextLong());
  117. newOrganization.setStatusId(RandomTestUtil.nextLong());
  118. newOrganization.setComments(RandomTestUtil.randomString());
  119. newOrganization.setLogoId(RandomTestUtil.nextLong());
  120. _organizations.add(_persistence.update(newOrganization));
  121. Organization existingOrganization = _persistence.findByPrimaryKey(
  122. newOrganization.getPrimaryKey());
  123. Assert.assertEquals(
  124. existingOrganization.getMvccVersion(),
  125. newOrganization.getMvccVersion());
  126. Assert.assertEquals(
  127. existingOrganization.getCtCollectionId(),
  128. newOrganization.getCtCollectionId());
  129. Assert.assertEquals(
  130. existingOrganization.getUuid(), newOrganization.getUuid());
  131. Assert.assertEquals(
  132. existingOrganization.getExternalReferenceCode(),
  133. newOrganization.getExternalReferenceCode());
  134. Assert.assertEquals(
  135. existingOrganization.getOrganizationId(),
  136. newOrganization.getOrganizationId());
  137. Assert.assertEquals(
  138. existingOrganization.getCompanyId(),
  139. newOrganization.getCompanyId());
  140. Assert.assertEquals(
  141. existingOrganization.getUserId(), newOrganization.getUserId());
  142. Assert.assertEquals(
  143. existingOrganization.getUserName(), newOrganization.getUserName());
  144. Assert.assertEquals(
  145. Time.getShortTimestamp(existingOrganization.getCreateDate()),
  146. Time.getShortTimestamp(newOrganization.getCreateDate()));
  147. Assert.assertEquals(
  148. Time.getShortTimestamp(existingOrganization.getModifiedDate()),
  149. Time.getShortTimestamp(newOrganization.getModifiedDate()));
  150. Assert.assertEquals(
  151. existingOrganization.getParentOrganizationId(),
  152. newOrganization.getParentOrganizationId());
  153. Assert.assertEquals(
  154. existingOrganization.getTreePath(), newOrganization.getTreePath());
  155. Assert.assertEquals(
  156. existingOrganization.getName(), newOrganization.getName());
  157. Assert.assertEquals(
  158. existingOrganization.getType(), newOrganization.getType());
  159. Assert.assertEquals(
  160. existingOrganization.isRecursable(),
  161. newOrganization.isRecursable());
  162. Assert.assertEquals(
  163. existingOrganization.getRegionId(), newOrganization.getRegionId());
  164. Assert.assertEquals(
  165. existingOrganization.getCountryId(),
  166. newOrganization.getCountryId());
  167. Assert.assertEquals(
  168. existingOrganization.getStatusId(), newOrganization.getStatusId());
  169. Assert.assertEquals(
  170. existingOrganization.getComments(), newOrganization.getComments());
  171. Assert.assertEquals(
  172. existingOrganization.getLogoId(), newOrganization.getLogoId());
  173. }
  174. @Test
  175. public void testCountByUuid() throws Exception {
  176. _persistence.countByUuid("");
  177. _persistence.countByUuid("null");
  178. _persistence.countByUuid((String)null);
  179. }
  180. @Test
  181. public void testCountByUuid_C() throws Exception {
  182. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  183. _persistence.countByUuid_C("null", 0L);
  184. _persistence.countByUuid_C((String)null, 0L);
  185. }
  186. @Test
  187. public void testCountByCompanyId() throws Exception {
  188. _persistence.countByCompanyId(RandomTestUtil.nextLong());
  189. _persistence.countByCompanyId(0L);
  190. }
  191. @Test
  192. public void testCountByLocations() throws Exception {
  193. _persistence.countByLocations(RandomTestUtil.nextLong());
  194. _persistence.countByLocations(0L);
  195. }
  196. @Test
  197. public void testCountByC_P() throws Exception {
  198. _persistence.countByC_P(
  199. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  200. _persistence.countByC_P(0L, 0L);
  201. }
  202. @Test
  203. public void testCountByC_T() throws Exception {
  204. _persistence.countByC_T(RandomTestUtil.nextLong(), "");
  205. _persistence.countByC_T(0L, "null");
  206. _persistence.countByC_T(0L, (String)null);
  207. }
  208. @Test
  209. public void testCountByC_N() throws Exception {
  210. _persistence.countByC_N(RandomTestUtil.nextLong(), "");
  211. _persistence.countByC_N(0L, "null");
  212. _persistence.countByC_N(0L, (String)null);
  213. }
  214. @Test
  215. public void testCountByC_LikeN() throws Exception {
  216. _persistence.countByC_LikeN(RandomTestUtil.nextLong(), "");
  217. _persistence.countByC_LikeN(0L, "null");
  218. _persistence.countByC_LikeN(0L, (String)null);
  219. }
  220. @Test
  221. public void testCountByO_C_P() throws Exception {
  222. _persistence.countByO_C_P(
  223. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  224. RandomTestUtil.nextLong());
  225. _persistence.countByO_C_P(0L, 0L, 0L);
  226. }
  227. @Test
  228. public void testCountByC_P_LikeN() throws Exception {
  229. _persistence.countByC_P_LikeN(
  230. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(), "");
  231. _persistence.countByC_P_LikeN(0L, 0L, "null");
  232. _persistence.countByC_P_LikeN(0L, 0L, (String)null);
  233. }
  234. @Test
  235. public void testCountByC_ERC() throws Exception {
  236. _persistence.countByC_ERC(RandomTestUtil.nextLong(), "");
  237. _persistence.countByC_ERC(0L, "null");
  238. _persistence.countByC_ERC(0L, (String)null);
  239. }
  240. @Test
  241. public void testFindByPrimaryKeyExisting() throws Exception {
  242. Organization newOrganization = addOrganization();
  243. Organization existingOrganization = _persistence.findByPrimaryKey(
  244. newOrganization.getPrimaryKey());
  245. Assert.assertEquals(existingOrganization, newOrganization);
  246. }
  247. @Test(expected = NoSuchOrganizationException.class)
  248. public void testFindByPrimaryKeyMissing() throws Exception {
  249. long pk = RandomTestUtil.nextLong();
  250. _persistence.findByPrimaryKey(pk);
  251. }
  252. @Test
  253. public void testFindAll() throws Exception {
  254. _persistence.findAll(
  255. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  256. }
  257. protected OrderByComparator<Organization> getOrderByComparator() {
  258. return OrderByComparatorFactoryUtil.create(
  259. "Organization_", "mvccVersion", true, "ctCollectionId", true,
  260. "uuid", true, "externalReferenceCode", true, "organizationId", true,
  261. "companyId", true, "userId", true, "userName", true, "createDate",
  262. true, "modifiedDate", true, "parentOrganizationId", true,
  263. "treePath", true, "name", true, "type", true, "recursable", true,
  264. "regionId", true, "countryId", true, "statusId", true, "comments",
  265. true, "logoId", true);
  266. }
  267. @Test
  268. public void testFetchByPrimaryKeyExisting() throws Exception {
  269. Organization newOrganization = addOrganization();
  270. Organization existingOrganization = _persistence.fetchByPrimaryKey(
  271. newOrganization.getPrimaryKey());
  272. Assert.assertEquals(existingOrganization, newOrganization);
  273. }
  274. @Test
  275. public void testFetchByPrimaryKeyMissing() throws Exception {
  276. long pk = RandomTestUtil.nextLong();
  277. Organization missingOrganization = _persistence.fetchByPrimaryKey(pk);
  278. Assert.assertNull(missingOrganization);
  279. }
  280. @Test
  281. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  282. throws Exception {
  283. Organization newOrganization1 = addOrganization();
  284. Organization newOrganization2 = addOrganization();
  285. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  286. primaryKeys.add(newOrganization1.getPrimaryKey());
  287. primaryKeys.add(newOrganization2.getPrimaryKey());
  288. Map<Serializable, Organization> organizations =
  289. _persistence.fetchByPrimaryKeys(primaryKeys);
  290. Assert.assertEquals(2, organizations.size());
  291. Assert.assertEquals(
  292. newOrganization1,
  293. organizations.get(newOrganization1.getPrimaryKey()));
  294. Assert.assertEquals(
  295. newOrganization2,
  296. organizations.get(newOrganization2.getPrimaryKey()));
  297. }
  298. @Test
  299. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  300. throws Exception {
  301. long pk1 = RandomTestUtil.nextLong();
  302. long pk2 = RandomTestUtil.nextLong();
  303. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  304. primaryKeys.add(pk1);
  305. primaryKeys.add(pk2);
  306. Map<Serializable, Organization> organizations =
  307. _persistence.fetchByPrimaryKeys(primaryKeys);
  308. Assert.assertTrue(organizations.isEmpty());
  309. }
  310. @Test
  311. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  312. throws Exception {
  313. Organization newOrganization = addOrganization();
  314. long pk = RandomTestUtil.nextLong();
  315. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  316. primaryKeys.add(newOrganization.getPrimaryKey());
  317. primaryKeys.add(pk);
  318. Map<Serializable, Organization> organizations =
  319. _persistence.fetchByPrimaryKeys(primaryKeys);
  320. Assert.assertEquals(1, organizations.size());
  321. Assert.assertEquals(
  322. newOrganization,
  323. organizations.get(newOrganization.getPrimaryKey()));
  324. }
  325. @Test
  326. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  327. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  328. Map<Serializable, Organization> organizations =
  329. _persistence.fetchByPrimaryKeys(primaryKeys);
  330. Assert.assertTrue(organizations.isEmpty());
  331. }
  332. @Test
  333. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  334. Organization newOrganization = addOrganization();
  335. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  336. primaryKeys.add(newOrganization.getPrimaryKey());
  337. Map<Serializable, Organization> organizations =
  338. _persistence.fetchByPrimaryKeys(primaryKeys);
  339. Assert.assertEquals(1, organizations.size());
  340. Assert.assertEquals(
  341. newOrganization,
  342. organizations.get(newOrganization.getPrimaryKey()));
  343. }
  344. @Test
  345. public void testActionableDynamicQuery() throws Exception {
  346. final IntegerWrapper count = new IntegerWrapper();
  347. ActionableDynamicQuery actionableDynamicQuery =
  348. OrganizationLocalServiceUtil.getActionableDynamicQuery();
  349. actionableDynamicQuery.setPerformActionMethod(
  350. new ActionableDynamicQuery.PerformActionMethod<Organization>() {
  351. @Override
  352. public void performAction(Organization organization) {
  353. Assert.assertNotNull(organization);
  354. count.increment();
  355. }
  356. });
  357. actionableDynamicQuery.performActions();
  358. Assert.assertEquals(count.getValue(), _persistence.countAll());
  359. }
  360. @Test
  361. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  362. Organization newOrganization = addOrganization();
  363. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  364. Organization.class, _dynamicQueryClassLoader);
  365. dynamicQuery.add(
  366. RestrictionsFactoryUtil.eq(
  367. "organizationId", newOrganization.getOrganizationId()));
  368. List<Organization> result = _persistence.findWithDynamicQuery(
  369. dynamicQuery);
  370. Assert.assertEquals(1, result.size());
  371. Organization existingOrganization = result.get(0);
  372. Assert.assertEquals(existingOrganization, newOrganization);
  373. }
  374. @Test
  375. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  376. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  377. Organization.class, _dynamicQueryClassLoader);
  378. dynamicQuery.add(
  379. RestrictionsFactoryUtil.eq(
  380. "organizationId", RandomTestUtil.nextLong()));
  381. List<Organization> result = _persistence.findWithDynamicQuery(
  382. dynamicQuery);
  383. Assert.assertEquals(0, result.size());
  384. }
  385. @Test
  386. public void testDynamicQueryByProjectionExisting() throws Exception {
  387. Organization newOrganization = addOrganization();
  388. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  389. Organization.class, _dynamicQueryClassLoader);
  390. dynamicQuery.setProjection(
  391. ProjectionFactoryUtil.property("organizationId"));
  392. Object newOrganizationId = newOrganization.getOrganizationId();
  393. dynamicQuery.add(
  394. RestrictionsFactoryUtil.in(
  395. "organizationId", new Object[] {newOrganizationId}));
  396. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  397. Assert.assertEquals(1, result.size());
  398. Object existingOrganizationId = result.get(0);
  399. Assert.assertEquals(existingOrganizationId, newOrganizationId);
  400. }
  401. @Test
  402. public void testDynamicQueryByProjectionMissing() throws Exception {
  403. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  404. Organization.class, _dynamicQueryClassLoader);
  405. dynamicQuery.setProjection(
  406. ProjectionFactoryUtil.property("organizationId"));
  407. dynamicQuery.add(
  408. RestrictionsFactoryUtil.in(
  409. "organizationId", new Object[] {RandomTestUtil.nextLong()}));
  410. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  411. Assert.assertEquals(0, result.size());
  412. }
  413. @Test
  414. public void testResetOriginalValues() throws Exception {
  415. Organization newOrganization = addOrganization();
  416. _persistence.clearCache();
  417. Organization existingOrganization = _persistence.findByPrimaryKey(
  418. newOrganization.getPrimaryKey());
  419. Assert.assertEquals(
  420. Long.valueOf(existingOrganization.getCompanyId()),
  421. ReflectionTestUtil.<Long>invoke(
  422. existingOrganization, "getOriginalCompanyId", new Class<?>[0]));
  423. Assert.assertTrue(
  424. Objects.equals(
  425. existingOrganization.getName(),
  426. ReflectionTestUtil.invoke(
  427. existingOrganization, "getOriginalName", new Class<?>[0])));
  428. Assert.assertEquals(
  429. Long.valueOf(existingOrganization.getCompanyId()),
  430. ReflectionTestUtil.<Long>invoke(
  431. existingOrganization, "getOriginalCompanyId", new Class<?>[0]));
  432. Assert.assertTrue(
  433. Objects.equals(
  434. existingOrganization.getExternalReferenceCode(),
  435. ReflectionTestUtil.invoke(
  436. existingOrganization, "getOriginalExternalReferenceCode",
  437. new Class<?>[0])));
  438. }
  439. protected Organization addOrganization() throws Exception {
  440. long pk = RandomTestUtil.nextLong();
  441. Organization organization = _persistence.create(pk);
  442. organization.setMvccVersion(RandomTestUtil.nextLong());
  443. organization.setCtCollectionId(RandomTestUtil.nextLong());
  444. organization.setUuid(RandomTestUtil.randomString());
  445. organization.setExternalReferenceCode(RandomTestUtil.randomString());
  446. organization.setCompanyId(RandomTestUtil.nextLong());
  447. organization.setUserId(RandomTestUtil.nextLong());
  448. organization.setUserName(RandomTestUtil.randomString());
  449. organization.setCreateDate(RandomTestUtil.nextDate());
  450. organization.setModifiedDate(RandomTestUtil.nextDate());
  451. organization.setParentOrganizationId(RandomTestUtil.nextLong());
  452. organization.setTreePath(RandomTestUtil.randomString());
  453. organization.setName(RandomTestUtil.randomString());
  454. organization.setType(RandomTestUtil.randomString());
  455. organization.setRecursable(RandomTestUtil.randomBoolean());
  456. organization.setRegionId(RandomTestUtil.nextLong());
  457. organization.setCountryId(RandomTestUtil.nextLong());
  458. organization.setStatusId(RandomTestUtil.nextLong());
  459. organization.setComments(RandomTestUtil.randomString());
  460. organization.setLogoId(RandomTestUtil.nextLong());
  461. _organizations.add(_persistence.update(organization));
  462. return organization;
  463. }
  464. private List<Organization> _organizations = new ArrayList<Organization>();
  465. private OrganizationPersistence _persistence;
  466. private ClassLoader _dynamicQueryClassLoader;
  467. }