View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.test.service;
17  
18  import org.apache.ojb.broker.metadata.DescriptorRepository;
19  import org.apache.ojb.broker.metadata.MetadataManager;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
23  import org.kuali.rice.kim.api.identity.Person;
24  import org.kuali.rice.kim.api.identity.principal.Principal;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.kuali.rice.kim.impl.identity.PersonImpl;
27  import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
28  import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
29  import org.kuali.rice.kim.test.KIMTestCase;
30  import org.kuali.rice.kim.test.bo.BOContainingPerson;
31  import org.kuali.rice.kns.lookup.Lookupable;
32  import org.kuali.rice.kns.service.KNSServiceLocator;
33  import org.kuali.rice.krad.bo.BusinessObject;
34  import org.kuali.rice.krad.data.KradDataServiceLocator;
35  import org.kuali.rice.krad.service.BusinessObjectService;
36  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
37  import org.kuali.rice.test.BaselineTestCase;
38  import org.springframework.util.AutoPopulatingList;
39  
40  import javax.xml.namespace.QName;
41  
42  import java.io.InputStream;
43  import java.util.ArrayList;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import static org.junit.Assert.*;
49  
50  /**
51   * This is a description of what this class does - kellerj don't forget to fill this in.
52   *
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   *
55   */
56  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
57  public class PersonServiceImplTest extends KIMTestCase {
58  
59  	private PersonServiceImpl personService;
60  
61  	public void setUp() throws Exception {
62  		super.setUp();
63  		personService = GlobalResourceLoader.getService(new QName("personService"));
64  		
65  	}
66  
67  	/**
68  	 * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#getPersonByExternalIdentifier(java.lang.String, java.lang.String)}.
69  	 */
70  	@Test
71  	public void testGetPersonByExternalIdentifier() {
72  		//insert external identifier
73  		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal("p1");
74  
75          EntityExternalIdentifierBo externalIdentifier = new EntityExternalIdentifierBo();
76  		externalIdentifier.setId("externalIdentifierId");
77  		externalIdentifier.setEntityId(principal.getEntityId());
78  		externalIdentifier.setExternalId("000-00-0000");
79  		externalIdentifier.setExternalIdentifierTypeCode("SSN");
80          KradDataServiceLocator.getDataObjectService().save(externalIdentifier);
81  		
82  		List<Person> people = personService.getPersonByExternalIdentifier( "SSN", "000-00-0000" );
83  		assertNotNull( "result object must not be null", people );
84  		assertEquals( "exactly one record should be returned", 1, people.size() );
85  		assertEquals( "the returned principal is not correct", "p1", people.get(0).getPrincipalId() );
86  	}
87  
88  //	@Test
89  //	public void testHasRole_Inherited() {
90  //		Person p = personService.getPersonByPrincipalName( "wwren" );
91  //		assertNotNull( "person object must not be null", p );
92  //		assertTrue( "person must be a member of PA_MAINTENANCE_USERS", personService.hasRole( p, org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_DEFAULT_NAMESPACE_CODE, "PA_AP_MAINTENANCE_USERS" ) );
93  //		assertTrue( "person must be NOT a member of PA_MAINTENANCE_USERS", !personService.hasRole( p, org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_DEFAULT_NAMESPACE_CODE, "PA_MAINTENANCE_USERS" ) );
94  //	}
95  //
96  //	@Test
97  //	public void testGetPersonRoles() {
98  //		Person p = personService.getPerson( "KULUSER" );
99  //		assertNotNull( "person object must not be null", p );
100 //		List<KimRole> roles = personService.getPersonRoles( p, null );
101 //		assertNotNull( "role list must not be null", roles );
102 //		System.out.println( roles );
103 //		assertTrue( "role list must have non-zero length", roles.size() > 0 );
104 //		KimRole r = KimImplServiceLocator.getAuthorizationService().getRoleByNamespaceCodeAndName( org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_DEFAULT_NAMESPACE_CODE, "SY_FUNCTIONAL_SUPER_USERS" );
105 //		assertTrue( "one of the roles must be SY_FUNCTIONAL_SUPER_USERS", roles.contains( r ) );
106 //	}
107 //
108 //	@Test
109 //	public void testHasRole() {
110 //		Person p = personService.getPerson( "KULUSER" );
111 //		assertTrue( "person must have role SY_FUNCTIONAL_SUPER_USERS", personService.hasRole( p, org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_DEFAULT_NAMESPACE_CODE, "SY_FUNCTIONAL_SUPER_USERS" ) );
112 //	}
113 
114 	@Test
115 	public void testGetPerson() {
116 		Person p = personService.getPerson( "KULUSER" );
117 		assertNotNull( "person object must not be null", p );
118 		assertEquals( "class must match implementation class defined on service", personService.getPersonImplementationClass(), p.getClass() );
119 		assertEquals( "person name does not match", "KULUSER", p.getFirstNameUnmasked() );
120 		assertEquals( "principal name does not match", "kuluser", p.getPrincipalName() );
121 		assertEquals( "KULUSER should have no address record", "", p.getAddressLine1Unmasked() );
122 		assertEquals( "KULUSER should have no campus code", "", p.getCampusCode() );
123 		assertEquals( "KULUSER should have no email address", "", p.getEmailAddressUnmasked() );
124 		assertNotNull( "entity ID should be set", p.getEntityId() );
125 		assertNotNull( "principal ID should be set", p.getPrincipalId() );
126 		
127 		// check an employee id
128 		Person p1Person = personService.getPerson( "p1" );
129 		assertNotNull( "person object must not be null", p );
130 		assertEquals("employee ID does not match", "p1emplId", p1Person.getEmployeeId());
131 	}
132 
133 	@Test
134 	public void testGetPersonInactive() {
135 		Person p = personService.getPerson( "inactiveuserid" );
136 		assertNotNull( "person object must not be null", p );
137         assertEquals("principal ID does not match", "inactiveusernm", p.getPrincipalName());
138 
139 	}
140 	
141 	@Test
142 	public void testGetPersonByPrincipalName() {
143 		Person p = personService.getPersonByPrincipalName( "kuluser" );
144 		assertNotNull( "person object must not be null", p );
145 		assertEquals( "person name does not match", "KULUSER", p.getFirstName() );
146 		assertEquals( "principal id does not match", "KULUSER", p.getPrincipalId() );
147 	}
148 	
149 	@Test
150 	public void testGetPersonByPrincipalNameInactive() {
151 		Person p = personService.getPersonByPrincipalName( "inactiveusernm" );
152         assertEquals("principal ID does not match", "inactiveuserid", p.getPrincipalId());
153     }
154 	
155     @Test
156     public void testGetPersonByEmployeeIdNoInfo() {
157         Person p = personService.getPersonByEmployeeId( "" );
158         assertNull( "person object will be null", p );
159     }
160     
161     @Test
162     public void testGetPersonByEmployeeIdNotFound() {
163         Person p = personService.getPersonByEmployeeId( "NotFound" );
164         assertNull( "person object will be null", p );
165     }
166     
167     @Test
168     public void testGetPersonByEmployeeIdActive() {
169         Person p = personService.getPersonByEmployeeId( "0000001138" );
170         assertNotNull( "person object must not be null", p );
171         assertEquals( "person name does not match", "activeUserFirst", p.getFirstName() );
172         assertEquals( "principal id does not match", "activeuserid", p.getPrincipalId() );
173     }
174     
175     @Test
176     public void testGetPersonByEmployeeIdInactiveUser() {
177         Person p = personService.getPersonByEmployeeId( "0000001139" );
178         assertNotNull( "person object must not be null", p );
179         assertEquals( "person name does not match", "InactiveUserFirst", p.getFirstName() );
180         assertEquals( "principal id does not match", "inactiveuserid", p.getPrincipalId() );
181     }
182     
183     @Test
184     public void testGetPersonByEmployeeIdInactiveEmp() {
185         Person p = personService.getPersonByEmployeeId( "0000001140" );
186         assertNotNull( "person object must not be null", p );
187         assertEquals( "person name does not match", "InactiveEmplFirst", p.getFirstName() );
188         assertEquals( "principal id does not match", "inactiveempid", p.getPrincipalId() );
189     }
190     
191 	@Test
192 	public void testConvertPersonPropertiesToEntityProperties() {
193 		HashMap<String,String> criteria = new HashMap<String,String>();
194 		criteria.put( "firstName", "System User" );
195 		Map<String,String> entityCriteria = personService.convertPersonPropertiesToEntityProperties( criteria );
196 		assertEquals( "number of criteria is not correct", 5, entityCriteria.size() );
197 		assertNotNull( "criteria must filter for active entity types", entityCriteria.get( "entityTypeContactInfos.active" ) );
198 		assertNotNull( "criteria must filter on entity type code", entityCriteria.get( "entityTypeContactInfos.entityTypeCode" ) );
199 		assertNotNull( "criteria must filter for first name", entityCriteria.get( "names.firstName" ) );
200 		assertNotNull( "criteria must filter for active names", entityCriteria.get( "names.active" ) );
201 		assertNotNull( "criteria must filter for the default name", entityCriteria.get( "names.defaultValue" ) );
202 	}
203 
204 	/**
205 	 * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#findPeople(Map)}.
206 	 */
207     @Test
208     public void testFindPeople() {
209         HashMap<String,String> criteria = new HashMap<String,String>();
210         criteria.put( "firstName", "KULUSER" );
211         List<Person> people = personService.findPeople( criteria );
212         assertNotNull( "result must not be null", people );
213         assertEquals( "wrong number of people returned", 1, people.size() );
214         Person p = people.get( 0 );
215         assertEquals( "name must match criteria", "KULUSER", p.getFirstName() );
216         assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
217     }
218 	
219     @Test
220     public void testFindPeopleInactive() {
221         HashMap<String,String> criteria = new HashMap<String,String>();
222         criteria.put( "principals.active", "N" );
223         List<Person> people = personService.findPeople( criteria );
224         assertNotNull( "result must not be null", people );
225         assertEquals( "wrong number of people returned", 1, people.size() );
226     }
227 
228     @Test
229     public void testFindPeopleBothInactiveAndActive() {
230         HashMap<String,String> criteria = new HashMap<String,String>();
231         criteria.put( "firstName", "InactiveUserFirst" );
232         List<Person> people = personService.findPeople( criteria );
233         assertNotNull( "result must not be null", people );
234         assertEquals( "wrong number of people returned", 1, people.size() );
235     }    
236     
237     @Test
238     public void testFindPeopleByWildcard() {
239         HashMap<String,String> criteria = new HashMap<String,String>();
240         criteria.put( "principalName", "!quick*" );
241         List<Person> people = personService.findPeople( criteria );
242         assertNotNull( "result must not be null", people );
243         assertEquals( "wrong number of people returned", 53, people.size() );
244         for (Person p: people) {
245             if (p.getPrincipalName().startsWith("quick")) {
246                 fail("Invalid wildcard search results");
247             }
248         }
249     }
250 
251 	@Test
252 	public void testResolvePrincipalNamesToPrincipalIds() throws Exception {
253 		
254 		KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation("KR-KIM", "classpath:org/kuali/rice/kim/bo/datadictionary/test/SampleBO.xml" );
255 		KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
256 
257 		Map<String,String> criteria = new HashMap<String,String>();
258 		criteria.put( "anAttribute", "aValue" );
259 		criteria.put( "anotherAttribute", "anotherValue" );
260 		criteria.put( "personAttribute.principalName", "kuluser" );
261 		System.out.println( "Before: " + criteria );
262 		Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
263 		assertNotNull( "returned map must not be null", newCritiera );
264 		System.out.println( "After:  " + newCritiera );
265 		assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
266 		assertEquals( "resulting principal ID is not that expected", "KULUSER", newCritiera.get( "personPrincipalId" ) );
267 		assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "personAttribute.principalName" ) );
268 
269 		// check when already has value in result field
270 		criteria.put( "personPrincipalId", "NOT KULUSER" );
271 		System.out.println( "Before: " + criteria );
272 		newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
273 		assertNotNull( "returned map must not be null", newCritiera );
274 		System.out.println( "After:  " + newCritiera );
275 		assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
276 		assertEquals( "resulting principal ID should have been changed", "KULUSER", newCritiera.get( "personPrincipalId" ) );
277 	}
278 
279 	@Test
280 	public void testResolvePrincipalNamesToPrincipalIds_Nested() throws Exception {
281 
282         KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation( "", "classpath:org/kuali/rice/kim/bo/datadictionary/test/SampleBO.xml" );
283         KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
284 
285 		Map<String,String> criteria = new HashMap<String,String>();
286 		criteria.put( "add.childBos.childsPersonAttribute.principalName", "kuluser" );
287 		System.out.println( "Before: " + criteria );
288 		Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
289 		assertNotNull( "returned map must not be null", newCritiera );
290 		System.out.println( "After:  " + newCritiera );
291 		// TODO: property is being appended in the wrong place - fix
292 		assertTrue( "new criteria must have a childsPersonPrincipalId entry", newCritiera.containsKey( "add.childBos.childsPersonPrincipalId" ) );
293 		assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "add.childBos.childsPersonAttribute.principalName" ) );
294 	}
295 
296 	@Test
297 	public void testUpdateWhenNecessary() {
298 		SampleBO bo = new SampleBO();
299 		bo.setPersonPrincipalId( "KULUSER" );
300 		Person p = bo.getPersonAttribute();
301 		assertNotNull( "person object must not be null", p );
302 		assertEquals( "principal IDs do not match", bo.getPersonPrincipalId(), p.getPrincipalId() );
303 		assertSame( "second retrieval must return same object since ID not changed", p, bo.getPersonAttribute() );
304 	}
305 
306 	@Test
307 	public void testLookupWithPersonJoin() throws Exception {
308 		
309 		// merge the OJB file in containing the OJB metadata
310         InputStream is = new ClasspathOrFileResourceLoader().getResource("classpath:org/kuali/rice/kim/test/OJB-repository-kimunittests.xml").getInputStream();
311         MetadataManager mm = MetadataManager.getInstance();
312         DescriptorRepository dr = mm.readDescriptorRepository(is);
313         mm.mergeDescriptorRepository(dr);
314 		
315 		KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation("KR-KIM", "classpath:org/kuali/rice/kim/bo/datadictionary/test/BOContainingPerson.xml" );
316 		KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
317 		BusinessObjectService bos = KNSServiceLocator.getBusinessObjectService();
318 		bos.delete( new ArrayList(bos.findAll( BOContainingPerson.class )) );
319 		BOContainingPerson bo = new BOContainingPerson();
320 		bo.setBoPrimaryKey( "ONE" );
321 		bo.setPrincipalId( "p1" );
322 		bos.save( bo );
323 		bo = new BOContainingPerson();
324 		bo.setBoPrimaryKey( "TWO" );
325 		bo.setPrincipalId( "p2" );
326 		bos.save( bo );
327 
328 		Lookupable l = KNSServiceLocator.getKualiLookupable();
329 		l.setBusinessObjectClass( BOContainingPerson.class );
330 		Map<String,String> criteria = new HashMap<String,String>();
331 		criteria.put( "person.principalName", "principal1" );
332 		List<BOContainingPerson> results = (List)l.getSearchResultsUnbounded( (Map)criteria );
333 		System.out.println( results );
334 		assertNotNull( "results may not be null", results );
335 		assertEquals( "number of results is incorrect", 1, results.size() );
336 		bo =  results.iterator().next();
337 		assertEquals( "principalId does not match", "p1", bo.getPrincipalId() );
338 	}
339 
340 //	@Test
341 //	public void testConfirmOnlyPKUsed() {
342 //		HashMap<String,String> criteria = new HashMap<String,String>();
343 //		criteria.put( "lastName", "HUNTLEY" );
344 //		criteria.put( "firstName", "KEISHA" );
345 //		Collection<Person> people = (Collection<Person>)KRADServiceLocatorInternal.getLookupService().findCollectionBySearchUnbounded(Person.class, criteria);
346 //		personService.findPeople( criteria );
347 //		assertNotNull( "result must not be null", people );
348 //		assertEquals( "wrong number of people returned", 1, people.size() );
349 //		Person p = people.iterator().next();
350 //		assertEquals( "principal name does not match", "khuntley", p.getPrincipalName() );
351 //
352 //		criteria.put( "principalName", "kuluser" );
353 //		people = people = (Collection<Person>)KRADServiceLocatorInternal.getLookupService().findCollectionBySearchUnbounded(Person.class, criteria);
354 //		assertNotNull( "result must not be null", people );
355 //		assertEquals( "wrong number of people returned", 1, people.size() );
356 //		p = people.iterator().next();
357 //		assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
358 //	}
359 
360 	public static class SampleBO implements BusinessObject {
361 		private String anAttribute;
362 		private String anotherAttribute;
363 		private String personPrincipalId;
364 		private PersonImpl personAttribute;
365 		private List<SampleChildBOWithPerson> childBos = new AutoPopulatingList(SampleChildBOWithPerson.class);
366 		public String getAnAttribute() {
367 			return this.anAttribute;
368 		}
369 		public void setAnAttribute(String anAttribute) {
370 			this.anAttribute = anAttribute;
371 		}
372 		public String getAnotherAttribute() {
373 			return this.anotherAttribute;
374 		}
375 		public void setAnotherAttribute(String anotherAttribute) {
376 			this.anotherAttribute = anotherAttribute;
377 		}
378 		public String getPersonPrincipalId() {
379 			return this.personPrincipalId;
380 		}
381 		public void setPersonPrincipalId(String personPrincipalId) {
382 			this.personPrincipalId = personPrincipalId;
383 		}
384 		public PersonImpl getPersonAttribute() {
385 			personAttribute = (PersonImpl) KimApiServiceLocator.getPersonService().updatePersonIfNecessary( personPrincipalId, personAttribute );
386 			return personAttribute;
387 		}
388 		public void setPersonAttribute(PersonImpl personAttribute) {
389 			this.personAttribute = personAttribute;
390 		}
391 		public void refresh() {}
392 		public List<SampleChildBOWithPerson> getChildBos() {
393 			return this.childBos;
394 		}
395 		public void setChildBos(List<SampleChildBOWithPerson> childBos) {
396 			this.childBos = childBos;
397 		}
398 	}
399 
400 	public static class SampleChildBOWithPerson implements BusinessObject {
401 
402 		private String childsAttribute;
403 		private String childsPersonPrincipalId;
404 		private PersonImpl childsPersonAttribute;
405 
406 
407 
408 		public String getChildsAttribute() {
409 			return this.childsAttribute;
410 		}
411 		public void setChildsAttribute(String childsAttribute) {
412 			this.childsAttribute = childsAttribute;
413 		}
414 		public String getChildsPersonPrincipalId() {
415 			return this.childsPersonPrincipalId;
416 		}
417 		public void setChildsPersonPrincipalId(String childsPersonPrincipalId) {
418 			this.childsPersonPrincipalId = childsPersonPrincipalId;
419 		}
420 		public PersonImpl getChildsPersonAttribute() {
421 			childsPersonAttribute = (PersonImpl) KimApiServiceLocator.getPersonService().updatePersonIfNecessary( childsPersonPrincipalId, childsPersonAttribute );
422 			return childsPersonAttribute;
423 		}
424 		public void setChildsPersonAttribute(PersonImpl childsPersonAttribute) {
425 			this.childsPersonAttribute = childsPersonAttribute;
426 		}
427 		public void refresh() {}
428 	}
429 }