/modules/cma-impl/src/test/java/com/rivetlogic/core/cma/impl/PeopleServiceImplTest.java

https://bitbucket.org/vlalov/rivetlogic-cma · Java · 265 lines · 176 code · 53 blank · 36 comment · 4 complexity · db01d8b0c1427445200965d661c47c34 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Rivet Logic Corporation.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package com.rivetlogic.core.cma.impl;
  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.alfresco.service.cmr.repository.NodeRef;
  26. import org.alfresco.service.namespace.QName;
  27. import com.rivetlogic.core.cma.exception.AuthorityNotFoundException;
  28. import com.rivetlogic.core.cma.repo.Ticket;
  29. /**
  30. * test peopleService methods
  31. *
  32. * @author Hyanghee Lim
  33. *
  34. */
  35. public class PeopleServiceImplTest extends CmaTestBase {
  36. PeopleServiceImpl peopleService = null;
  37. // variables
  38. NodeRef userRef1 = null;
  39. NodeRef userRef2 = null;
  40. // group variables
  41. NodeRef parentGroupRef = null;
  42. NodeRef subGroupRef1 = null;
  43. NodeRef subGroupRef2 = null;
  44. String groupName = "cmaTestGroupParent " + System.currentTimeMillis();
  45. String subGroupName1 = "cmaTestSubGroup " + System.currentTimeMillis();
  46. String subGroupName2 = "cmaTestSubGroup " + System.currentTimeMillis() + 1;
  47. // user variables
  48. String firstName1 = "cmaTestUser1FirstName";
  49. String lastName1 = "cmaTestUser1LastName";
  50. String userName1 = "peopleServiceCmaTestUser " + System.currentTimeMillis();
  51. String password1 = "peopleServiceCmaTestUser1";
  52. String firstName2 = "cmaTestUser2FirstName";
  53. String lastName2 = "cmaTestUser2LastName";
  54. String userName2 = "peopleServiceCmaTestUser " + System.currentTimeMillis() + 1;
  55. String password2 = "peopleServiceCmaTestUser2";
  56. private Ticket ticket = null;
  57. Map<QName, Serializable> properties = null;
  58. // create property qnames
  59. QName firstNameQName = QName.createQName("{http://www.alfresco.org/model/content/1.0}firstName");
  60. QName lastNameQName = QName.createQName("{http://www.alfresco.org/model/content/1.0}lastName");
  61. public void onSetUp() throws Exception {
  62. peopleService = (PeopleServiceImpl) configurationApplicationContext.getBean("peopleService");
  63. assertNotNull(peopleService);
  64. ticket = getTicket();
  65. assertNotNull(ticket);
  66. }
  67. public void tearDown() {
  68. try {
  69. userRef1 = peopleService.getPerson(ticket, userName1);
  70. peopleService.deletePerson(ticket, userRef1);
  71. } catch (Exception e) {}
  72. try {
  73. userRef2 = peopleService.getPerson(ticket, userName2);
  74. peopleService.deletePerson(ticket, userRef2);
  75. } catch (Exception e) {}
  76. try {
  77. subGroupRef1 = peopleService.getGroup(ticket, subGroupName1);
  78. peopleService.deleteGroup(ticket, subGroupRef1);
  79. } catch (Exception e) {}
  80. try {
  81. subGroupRef2 = peopleService.getGroup(ticket, subGroupName2);
  82. peopleService.deleteGroup(ticket, subGroupRef2);
  83. } catch (Exception e) {}
  84. try {
  85. parentGroupRef = peopleService.getGroup(ticket, groupName);
  86. peopleService.deleteGroup(ticket, parentGroupRef);
  87. } catch (Exception e) {}
  88. }
  89. public void testPeopleService() throws Exception {
  90. parentGroupRef = peopleService.createGroup(ticket, groupName);
  91. assertNotNull(parentGroupRef);
  92. subGroupRef1 = peopleService.createGroup(ticket, parentGroupRef, subGroupName1);
  93. assertNotNull(subGroupRef1);
  94. subGroupRef2 = peopleService.createGroup(ticket, parentGroupRef, subGroupName2);
  95. assertNotNull(subGroupRef2);
  96. properties = new HashMap<QName, Serializable>(4);
  97. // homeFolder value must be a nodeRef
  98. properties.put(firstNameQName, firstName1);
  99. properties.put(lastNameQName, lastName1);
  100. userRef1 = peopleService.createPerson(ticket, userName1, userName1.toCharArray(), properties);
  101. assertNotNull(userRef1);
  102. Ticket user1Ticket = getTicket(userName1, userName1);
  103. assertNotNull(user1Ticket);
  104. properties = new HashMap<QName, Serializable>(4);
  105. properties.put(firstNameQName, firstName2);
  106. properties.put(lastNameQName, lastName2);
  107. userRef2 = peopleService.createPerson(ticket, userName2, userName2.toCharArray(), properties);
  108. assertNotNull(userRef2);
  109. Ticket user2Ticket = getTicket(userName2, userName2);
  110. assertNotNull(user2Ticket);
  111. // alfresco 3.1.1 - returns an empty list
  112. // List<NodeRef> people = peopleService.getAllPeople(ticket);
  113. // assertNotNull(people);
  114. properties = new HashMap<QName, Serializable>(1);
  115. properties.put(lastNameQName, "NewLastName");
  116. peopleService.setPersonProperties(ticket, userRef2, properties);
  117. subGroupRef1 = peopleService.getGroup(ticket, subGroupName1);
  118. assertNotNull(subGroupRef1);
  119. userRef1 = peopleService.getPerson(ticket, userName1);
  120. assertNotNull(userRef1);
  121. peopleService.addAuthority(ticket, subGroupRef1, userRef1);
  122. subGroupRef2 = peopleService.getGroup(ticket, subGroupName2);
  123. assertNotNull(subGroupRef2);
  124. peopleService.addAuthority(ticket, subGroupRef2, userRef1);
  125. userRef2 = peopleService.getPerson(ticket, userName2);
  126. assertNotNull(userRef2);
  127. parentGroupRef = peopleService.getGroup(ticket, groupName);
  128. assertNotNull(parentGroupRef);
  129. peopleService.addAuthority(ticket, parentGroupRef, userRef2);
  130. // check # of members in parent group
  131. List<NodeRef> members = peopleService.getMembers(ticket, parentGroupRef);
  132. assertEquals(1, members.size());
  133. // check # of members in parent group including sub-groups
  134. members = peopleService.getMembers(ticket, parentGroupRef, true);
  135. assertEquals(2, members.size());
  136. List<NodeRef> groups = peopleService.getContainerGroups(ticket, userRef1);
  137. assertEquals(3, groups.size());
  138. userRef1 = peopleService.getPerson(ticket, userName1);
  139. subGroupRef1 = peopleService.getGroup(ticket, subGroupName1);
  140. peopleService.removeAuthority(ticket, subGroupRef1, userRef1);
  141. groups = peopleService.getContainerGroups(ticket, userRef1);
  142. assertEquals(2, groups.size());
  143. try {
  144. userRef2 = peopleService.getPerson(ticket, userName2);
  145. peopleService.deletePerson(ticket, userRef2);
  146. userRef2 = peopleService.getPerson(ticket, userName2);
  147. fail(userName2 + " is not removed");
  148. } catch (AuthorityNotFoundException e) {
  149. // success
  150. }
  151. try {
  152. subGroupRef1 = peopleService.getGroup(ticket, subGroupName1);
  153. peopleService.deleteGroup(ticket, subGroupRef1);
  154. subGroupRef1 = peopleService.getGroup(ticket, subGroupName1);
  155. fail(subGroupName1 + " is not removed");
  156. } catch (AuthorityNotFoundException e) {
  157. // success
  158. }
  159. }
  160. public void testAddAuthorityByName() throws Exception {
  161. parentGroupRef = peopleService.createGroup(ticket, groupName);
  162. assertNotNull(parentGroupRef);
  163. properties = new HashMap<QName, Serializable>(4);
  164. // homeFolder value must be a nodeRef
  165. properties.put(firstNameQName, firstName1);
  166. properties.put(lastNameQName, lastName1);
  167. userRef1 = peopleService.createPerson(ticket, userName1,
  168. userName1.toCharArray(), properties);
  169. assertNotNull(userRef1);
  170. Ticket user1Ticket = getTicket(userName1, userName1);
  171. assertNotNull(user1Ticket);
  172. peopleService.addAuthority(ticket, groupName, userName1);
  173. List<NodeRef> members = peopleService.getMembers(ticket, parentGroupRef);
  174. assertEquals(1, members.size());
  175. }
  176. public void testAddAuthoritiesByName() throws Exception {
  177. parentGroupRef = peopleService.createGroup(ticket, groupName);
  178. assertNotNull(parentGroupRef);
  179. properties = new HashMap<QName, Serializable>(4);
  180. // homeFolder value must be a nodeRef
  181. properties.put(firstNameQName, firstName1);
  182. properties.put(lastNameQName, lastName1);
  183. userRef1 = peopleService.createPerson(ticket, userName1,
  184. userName1.toCharArray(), properties);
  185. assertNotNull(userRef1);
  186. Ticket user1Ticket = getTicket(userName1, userName1);
  187. assertNotNull(user1Ticket);
  188. properties = new HashMap<QName, Serializable>(4);
  189. properties.put(firstNameQName, firstName2);
  190. properties.put(lastNameQName, lastName2);
  191. userRef2 = peopleService.createPerson(ticket, userName2,
  192. userName2.toCharArray(), properties);
  193. assertNotNull(userRef2);
  194. Ticket user2Ticket = getTicket(userName2, userName2);
  195. assertNotNull(user2Ticket);
  196. List<String> authorities = new ArrayList<String>(2);
  197. authorities.add(userName1);
  198. authorities.add(userName2);
  199. peopleService.addAuthorities(ticket, groupName, authorities);
  200. List<NodeRef> members = peopleService.getMembers(ticket, parentGroupRef);
  201. assertEquals(2, members.size());
  202. }
  203. public void printCollection(String name, Collection collection) {
  204. System.out.println("printing a collection of " + name);
  205. if (collection != null) {
  206. for (Object obj : collection)
  207. System.out.println("- " + obj);
  208. } else {
  209. System.out.println(name + " is empty");
  210. }
  211. }
  212. }