PageRenderTime 56ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/liferay/liferay-portal
Java | 574 lines | 399 code | 159 blank | 16 comment | 1 complexity | afc0b8d747b522df002e3e06ac3c2b2c 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.NoSuchUserGroupException;
  23. import com.liferay.portal.kernel.model.UserGroup;
  24. import com.liferay.portal.kernel.service.UserGroupLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.UserGroupPersistence;
  26. import com.liferay.portal.kernel.service.persistence.UserGroupUtil;
  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 UserGroupPersistenceTest {
  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 = UserGroupUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<UserGroup> iterator = _userGroups.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. UserGroup userGroup = _persistence.create(pk);
  82. Assert.assertNotNull(userGroup);
  83. Assert.assertEquals(userGroup.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. UserGroup newUserGroup = addUserGroup();
  88. _persistence.remove(newUserGroup);
  89. UserGroup existingUserGroup = _persistence.fetchByPrimaryKey(
  90. newUserGroup.getPrimaryKey());
  91. Assert.assertNull(existingUserGroup);
  92. }
  93. @Test
  94. public void testUpdateNew() throws Exception {
  95. addUserGroup();
  96. }
  97. @Test
  98. public void testUpdateExisting() throws Exception {
  99. long pk = RandomTestUtil.nextLong();
  100. UserGroup newUserGroup = _persistence.create(pk);
  101. newUserGroup.setMvccVersion(RandomTestUtil.nextLong());
  102. newUserGroup.setCtCollectionId(RandomTestUtil.nextLong());
  103. newUserGroup.setUuid(RandomTestUtil.randomString());
  104. newUserGroup.setExternalReferenceCode(RandomTestUtil.randomString());
  105. newUserGroup.setCompanyId(RandomTestUtil.nextLong());
  106. newUserGroup.setUserId(RandomTestUtil.nextLong());
  107. newUserGroup.setUserName(RandomTestUtil.randomString());
  108. newUserGroup.setCreateDate(RandomTestUtil.nextDate());
  109. newUserGroup.setModifiedDate(RandomTestUtil.nextDate());
  110. newUserGroup.setParentUserGroupId(RandomTestUtil.nextLong());
  111. newUserGroup.setName(RandomTestUtil.randomString());
  112. newUserGroup.setDescription(RandomTestUtil.randomString());
  113. newUserGroup.setAddedByLDAPImport(RandomTestUtil.randomBoolean());
  114. _userGroups.add(_persistence.update(newUserGroup));
  115. UserGroup existingUserGroup = _persistence.findByPrimaryKey(
  116. newUserGroup.getPrimaryKey());
  117. Assert.assertEquals(
  118. existingUserGroup.getMvccVersion(), newUserGroup.getMvccVersion());
  119. Assert.assertEquals(
  120. existingUserGroup.getCtCollectionId(),
  121. newUserGroup.getCtCollectionId());
  122. Assert.assertEquals(
  123. existingUserGroup.getUuid(), newUserGroup.getUuid());
  124. Assert.assertEquals(
  125. existingUserGroup.getExternalReferenceCode(),
  126. newUserGroup.getExternalReferenceCode());
  127. Assert.assertEquals(
  128. existingUserGroup.getUserGroupId(), newUserGroup.getUserGroupId());
  129. Assert.assertEquals(
  130. existingUserGroup.getCompanyId(), newUserGroup.getCompanyId());
  131. Assert.assertEquals(
  132. existingUserGroup.getUserId(), newUserGroup.getUserId());
  133. Assert.assertEquals(
  134. existingUserGroup.getUserName(), newUserGroup.getUserName());
  135. Assert.assertEquals(
  136. Time.getShortTimestamp(existingUserGroup.getCreateDate()),
  137. Time.getShortTimestamp(newUserGroup.getCreateDate()));
  138. Assert.assertEquals(
  139. Time.getShortTimestamp(existingUserGroup.getModifiedDate()),
  140. Time.getShortTimestamp(newUserGroup.getModifiedDate()));
  141. Assert.assertEquals(
  142. existingUserGroup.getParentUserGroupId(),
  143. newUserGroup.getParentUserGroupId());
  144. Assert.assertEquals(
  145. existingUserGroup.getName(), newUserGroup.getName());
  146. Assert.assertEquals(
  147. existingUserGroup.getDescription(), newUserGroup.getDescription());
  148. Assert.assertEquals(
  149. existingUserGroup.isAddedByLDAPImport(),
  150. newUserGroup.isAddedByLDAPImport());
  151. }
  152. @Test
  153. public void testCountByUuid() throws Exception {
  154. _persistence.countByUuid("");
  155. _persistence.countByUuid("null");
  156. _persistence.countByUuid((String)null);
  157. }
  158. @Test
  159. public void testCountByUuid_C() throws Exception {
  160. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  161. _persistence.countByUuid_C("null", 0L);
  162. _persistence.countByUuid_C((String)null, 0L);
  163. }
  164. @Test
  165. public void testCountByCompanyId() throws Exception {
  166. _persistence.countByCompanyId(RandomTestUtil.nextLong());
  167. _persistence.countByCompanyId(0L);
  168. }
  169. @Test
  170. public void testCountByC_P() throws Exception {
  171. _persistence.countByC_P(
  172. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  173. _persistence.countByC_P(0L, 0L);
  174. }
  175. @Test
  176. public void testCountByC_N() throws Exception {
  177. _persistence.countByC_N(RandomTestUtil.nextLong(), "");
  178. _persistence.countByC_N(0L, "null");
  179. _persistence.countByC_N(0L, (String)null);
  180. }
  181. @Test
  182. public void testCountByC_LikeN() throws Exception {
  183. _persistence.countByC_LikeN(RandomTestUtil.nextLong(), "");
  184. _persistence.countByC_LikeN(0L, "null");
  185. _persistence.countByC_LikeN(0L, (String)null);
  186. }
  187. @Test
  188. public void testCountByU_C_P() throws Exception {
  189. _persistence.countByU_C_P(
  190. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  191. RandomTestUtil.nextLong());
  192. _persistence.countByU_C_P(0L, 0L, 0L);
  193. }
  194. @Test
  195. public void testCountByC_ERC() throws Exception {
  196. _persistence.countByC_ERC(RandomTestUtil.nextLong(), "");
  197. _persistence.countByC_ERC(0L, "null");
  198. _persistence.countByC_ERC(0L, (String)null);
  199. }
  200. @Test
  201. public void testFindByPrimaryKeyExisting() throws Exception {
  202. UserGroup newUserGroup = addUserGroup();
  203. UserGroup existingUserGroup = _persistence.findByPrimaryKey(
  204. newUserGroup.getPrimaryKey());
  205. Assert.assertEquals(existingUserGroup, newUserGroup);
  206. }
  207. @Test(expected = NoSuchUserGroupException.class)
  208. public void testFindByPrimaryKeyMissing() throws Exception {
  209. long pk = RandomTestUtil.nextLong();
  210. _persistence.findByPrimaryKey(pk);
  211. }
  212. @Test
  213. public void testFindAll() throws Exception {
  214. _persistence.findAll(
  215. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  216. }
  217. protected OrderByComparator<UserGroup> getOrderByComparator() {
  218. return OrderByComparatorFactoryUtil.create(
  219. "UserGroup", "mvccVersion", true, "ctCollectionId", true, "uuid",
  220. true, "externalReferenceCode", true, "userGroupId", true,
  221. "companyId", true, "userId", true, "userName", true, "createDate",
  222. true, "modifiedDate", true, "parentUserGroupId", true, "name", true,
  223. "description", true, "addedByLDAPImport", true);
  224. }
  225. @Test
  226. public void testFetchByPrimaryKeyExisting() throws Exception {
  227. UserGroup newUserGroup = addUserGroup();
  228. UserGroup existingUserGroup = _persistence.fetchByPrimaryKey(
  229. newUserGroup.getPrimaryKey());
  230. Assert.assertEquals(existingUserGroup, newUserGroup);
  231. }
  232. @Test
  233. public void testFetchByPrimaryKeyMissing() throws Exception {
  234. long pk = RandomTestUtil.nextLong();
  235. UserGroup missingUserGroup = _persistence.fetchByPrimaryKey(pk);
  236. Assert.assertNull(missingUserGroup);
  237. }
  238. @Test
  239. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  240. throws Exception {
  241. UserGroup newUserGroup1 = addUserGroup();
  242. UserGroup newUserGroup2 = addUserGroup();
  243. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  244. primaryKeys.add(newUserGroup1.getPrimaryKey());
  245. primaryKeys.add(newUserGroup2.getPrimaryKey());
  246. Map<Serializable, UserGroup> userGroups =
  247. _persistence.fetchByPrimaryKeys(primaryKeys);
  248. Assert.assertEquals(2, userGroups.size());
  249. Assert.assertEquals(
  250. newUserGroup1, userGroups.get(newUserGroup1.getPrimaryKey()));
  251. Assert.assertEquals(
  252. newUserGroup2, userGroups.get(newUserGroup2.getPrimaryKey()));
  253. }
  254. @Test
  255. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  256. throws Exception {
  257. long pk1 = RandomTestUtil.nextLong();
  258. long pk2 = RandomTestUtil.nextLong();
  259. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  260. primaryKeys.add(pk1);
  261. primaryKeys.add(pk2);
  262. Map<Serializable, UserGroup> userGroups =
  263. _persistence.fetchByPrimaryKeys(primaryKeys);
  264. Assert.assertTrue(userGroups.isEmpty());
  265. }
  266. @Test
  267. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  268. throws Exception {
  269. UserGroup newUserGroup = addUserGroup();
  270. long pk = RandomTestUtil.nextLong();
  271. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  272. primaryKeys.add(newUserGroup.getPrimaryKey());
  273. primaryKeys.add(pk);
  274. Map<Serializable, UserGroup> userGroups =
  275. _persistence.fetchByPrimaryKeys(primaryKeys);
  276. Assert.assertEquals(1, userGroups.size());
  277. Assert.assertEquals(
  278. newUserGroup, userGroups.get(newUserGroup.getPrimaryKey()));
  279. }
  280. @Test
  281. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  282. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  283. Map<Serializable, UserGroup> userGroups =
  284. _persistence.fetchByPrimaryKeys(primaryKeys);
  285. Assert.assertTrue(userGroups.isEmpty());
  286. }
  287. @Test
  288. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  289. UserGroup newUserGroup = addUserGroup();
  290. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  291. primaryKeys.add(newUserGroup.getPrimaryKey());
  292. Map<Serializable, UserGroup> userGroups =
  293. _persistence.fetchByPrimaryKeys(primaryKeys);
  294. Assert.assertEquals(1, userGroups.size());
  295. Assert.assertEquals(
  296. newUserGroup, userGroups.get(newUserGroup.getPrimaryKey()));
  297. }
  298. @Test
  299. public void testActionableDynamicQuery() throws Exception {
  300. final IntegerWrapper count = new IntegerWrapper();
  301. ActionableDynamicQuery actionableDynamicQuery =
  302. UserGroupLocalServiceUtil.getActionableDynamicQuery();
  303. actionableDynamicQuery.setPerformActionMethod(
  304. new ActionableDynamicQuery.PerformActionMethod<UserGroup>() {
  305. @Override
  306. public void performAction(UserGroup userGroup) {
  307. Assert.assertNotNull(userGroup);
  308. count.increment();
  309. }
  310. });
  311. actionableDynamicQuery.performActions();
  312. Assert.assertEquals(count.getValue(), _persistence.countAll());
  313. }
  314. @Test
  315. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  316. UserGroup newUserGroup = addUserGroup();
  317. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  318. UserGroup.class, _dynamicQueryClassLoader);
  319. dynamicQuery.add(
  320. RestrictionsFactoryUtil.eq(
  321. "userGroupId", newUserGroup.getUserGroupId()));
  322. List<UserGroup> result = _persistence.findWithDynamicQuery(
  323. dynamicQuery);
  324. Assert.assertEquals(1, result.size());
  325. UserGroup existingUserGroup = result.get(0);
  326. Assert.assertEquals(existingUserGroup, newUserGroup);
  327. }
  328. @Test
  329. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  330. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  331. UserGroup.class, _dynamicQueryClassLoader);
  332. dynamicQuery.add(
  333. RestrictionsFactoryUtil.eq(
  334. "userGroupId", RandomTestUtil.nextLong()));
  335. List<UserGroup> result = _persistence.findWithDynamicQuery(
  336. dynamicQuery);
  337. Assert.assertEquals(0, result.size());
  338. }
  339. @Test
  340. public void testDynamicQueryByProjectionExisting() throws Exception {
  341. UserGroup newUserGroup = addUserGroup();
  342. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  343. UserGroup.class, _dynamicQueryClassLoader);
  344. dynamicQuery.setProjection(
  345. ProjectionFactoryUtil.property("userGroupId"));
  346. Object newUserGroupId = newUserGroup.getUserGroupId();
  347. dynamicQuery.add(
  348. RestrictionsFactoryUtil.in(
  349. "userGroupId", new Object[] {newUserGroupId}));
  350. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  351. Assert.assertEquals(1, result.size());
  352. Object existingUserGroupId = result.get(0);
  353. Assert.assertEquals(existingUserGroupId, newUserGroupId);
  354. }
  355. @Test
  356. public void testDynamicQueryByProjectionMissing() throws Exception {
  357. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  358. UserGroup.class, _dynamicQueryClassLoader);
  359. dynamicQuery.setProjection(
  360. ProjectionFactoryUtil.property("userGroupId"));
  361. dynamicQuery.add(
  362. RestrictionsFactoryUtil.in(
  363. "userGroupId", new Object[] {RandomTestUtil.nextLong()}));
  364. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  365. Assert.assertEquals(0, result.size());
  366. }
  367. @Test
  368. public void testResetOriginalValues() throws Exception {
  369. UserGroup newUserGroup = addUserGroup();
  370. _persistence.clearCache();
  371. UserGroup existingUserGroup = _persistence.findByPrimaryKey(
  372. newUserGroup.getPrimaryKey());
  373. Assert.assertEquals(
  374. Long.valueOf(existingUserGroup.getCompanyId()),
  375. ReflectionTestUtil.<Long>invoke(
  376. existingUserGroup, "getOriginalCompanyId", new Class<?>[0]));
  377. Assert.assertTrue(
  378. Objects.equals(
  379. existingUserGroup.getName(),
  380. ReflectionTestUtil.invoke(
  381. existingUserGroup, "getOriginalName", new Class<?>[0])));
  382. Assert.assertEquals(
  383. Long.valueOf(existingUserGroup.getCompanyId()),
  384. ReflectionTestUtil.<Long>invoke(
  385. existingUserGroup, "getOriginalCompanyId", new Class<?>[0]));
  386. Assert.assertTrue(
  387. Objects.equals(
  388. existingUserGroup.getExternalReferenceCode(),
  389. ReflectionTestUtil.invoke(
  390. existingUserGroup, "getOriginalExternalReferenceCode",
  391. new Class<?>[0])));
  392. }
  393. protected UserGroup addUserGroup() throws Exception {
  394. long pk = RandomTestUtil.nextLong();
  395. UserGroup userGroup = _persistence.create(pk);
  396. userGroup.setMvccVersion(RandomTestUtil.nextLong());
  397. userGroup.setCtCollectionId(RandomTestUtil.nextLong());
  398. userGroup.setUuid(RandomTestUtil.randomString());
  399. userGroup.setExternalReferenceCode(RandomTestUtil.randomString());
  400. userGroup.setCompanyId(RandomTestUtil.nextLong());
  401. userGroup.setUserId(RandomTestUtil.nextLong());
  402. userGroup.setUserName(RandomTestUtil.randomString());
  403. userGroup.setCreateDate(RandomTestUtil.nextDate());
  404. userGroup.setModifiedDate(RandomTestUtil.nextDate());
  405. userGroup.setParentUserGroupId(RandomTestUtil.nextLong());
  406. userGroup.setName(RandomTestUtil.randomString());
  407. userGroup.setDescription(RandomTestUtil.randomString());
  408. userGroup.setAddedByLDAPImport(RandomTestUtil.randomBoolean());
  409. _userGroups.add(_persistence.update(userGroup));
  410. return userGroup;
  411. }
  412. private List<UserGroup> _userGroups = new ArrayList<UserGroup>();
  413. private UserGroupPersistence _persistence;
  414. private ClassLoader _dynamicQueryClassLoader;
  415. }