1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.kim.api.identity.Person;
23 import org.kuali.rice.kim.api.identity.principal.Principal;
24 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25 import org.kuali.rice.kim.impl.KIMPropertyConstants;
26 import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
27 import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
28 import org.kuali.rice.kim.test.KIMTestCase;
29 import org.kuali.rice.kim.test.bo.BOContainingPerson;
30 import org.kuali.rice.kns.lookup.Lookupable;
31 import org.kuali.rice.kns.service.KNSServiceLocator;
32 import org.kuali.rice.krad.bo.BusinessObject;
33 import org.kuali.rice.krad.service.BusinessObjectService;
34 import org.kuali.rice.krad.service.KRADServiceLocator;
35 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
36 import org.kuali.rice.krad.service.SequenceAccessorService;
37 import org.kuali.rice.test.BaselineTestCase;
38 import org.springframework.core.io.DefaultResourceLoader;
39 import org.springframework.util.AutoPopulatingList;
40
41 import javax.xml.namespace.QName;
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
52
53
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 = (PersonServiceImpl) GlobalResourceLoader.getService(new QName("personService"));
64
65 }
66
67
68
69
70 @Test
71 public void testGetPersonByExternalIdentifier() {
72
73 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal("p1");
74
75 SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
76 Long externalIdentifierId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_EXT_ID_ID_S", EntityExternalIdentifierBo.class);
77 EntityExternalIdentifierBo externalIdentifier = new EntityExternalIdentifierBo();
78 externalIdentifier.setId("externalIdentifierId");
79 externalIdentifier.setEntityId(principal.getEntityId());
80 externalIdentifier.setExternalId("000-00-0000");
81 externalIdentifier.setExternalIdentifierTypeCode("SSN");
82 KRADServiceLocator.getBusinessObjectService().save(externalIdentifier);
83
84 List<Person> people = personService.getPersonByExternalIdentifier( "SSN", "000-00-0000" );
85 assertNotNull( "result object must not be null", people );
86 assertEquals( "exactly one record should be returned", 1, people.size() );
87 assertEquals( "the returned principal is not correct", "p1", people.get(0).getPrincipalId() );
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 @Test
117 public void testGetPerson() {
118 Person p = personService.getPerson( "KULUSER" );
119 assertNotNull( "person object must not be null", p );
120 assertEquals( "class must match implementation class defined on service", personService.getPersonImplementationClass(), p.getClass() );
121 assertEquals( "person name does not match", "KULUSER", p.getFirstNameUnmasked() );
122 assertEquals( "principal name does not match", "kuluser", p.getPrincipalName() );
123 assertEquals( "KULUSER should have no address record", "", p.getAddressLine1Unmasked() );
124 assertEquals( "KULUSER should have no campus code", "", p.getCampusCode() );
125 assertEquals( "KULUSER should have no email address", "", p.getEmailAddressUnmasked() );
126 assertNotNull( "entity ID should be set", p.getEntityId() );
127 assertNotNull( "principal ID should be set", p.getPrincipalId() );
128
129
130 Person p1Person = personService.getPerson( "p1" );
131 assertNotNull( "person object must not be null", p );
132 assertEquals("employee ID does not match", "p1emplId", p1Person.getEmployeeId());
133 }
134
135 @Test
136 public void testGetPersonInactive() {
137 Person p = personService.getPerson( "inactiveuserid" );
138 assertNotNull( "person object must not be null", p );
139 assertEquals("principal ID does not match", "inactiveusernm", p.getPrincipalName());
140
141 }
142
143 @Test
144 public void testGetPersonByPrincipalName() {
145 Person p = personService.getPersonByPrincipalName( "kuluser" );
146 assertNotNull( "person object must not be null", p );
147 assertEquals( "person name does not match", "KULUSER", p.getFirstName() );
148 assertEquals( "principal id does not match", "KULUSER", p.getPrincipalId() );
149 }
150
151 @Test
152 public void testGetPersonByPrincipalNameInactive() {
153 Person p = personService.getPersonByPrincipalName( "inactiveusernm" );
154 assertEquals("principal ID does not match", "inactiveuserid", p.getPrincipalId());
155 }
156
157 @Test
158 public void testGetPersonByEmployeeIdNoInfo() {
159 Person p = personService.getPersonByEmployeeId( "" );
160 assertNull( "person object will be null", p );
161 }
162
163 @Test
164 public void testGetPersonByEmployeeIdNotFound() {
165 Person p = personService.getPersonByEmployeeId( "NotFound" );
166 assertNull( "person object will be null", p );
167 }
168
169 @Test
170 public void testGetPersonByEmployeeIdActive() {
171 Person p = personService.getPersonByEmployeeId( "0000001138" );
172 assertNotNull( "person object must not be null", p );
173 assertEquals( "person name does not match", "activeUserFirst", p.getFirstName() );
174 assertEquals( "principal id does not match", "activeuserid", p.getPrincipalId() );
175 }
176
177 @Test
178 public void testGetPersonByEmployeeIdInactiveUser() {
179 Person p = personService.getPersonByEmployeeId( "0000001139" );
180 assertNotNull( "person object must not be null", p );
181 assertEquals( "person name does not match", "InactiveUserFirst", p.getFirstName() );
182 assertEquals( "principal id does not match", "inactiveuserid", p.getPrincipalId() );
183 }
184
185 @Test
186 public void testGetPersonByEmployeeIdInactiveEmp() {
187 Person p = personService.getPersonByEmployeeId( "0000001140" );
188 assertNotNull( "person object must not be null", p );
189 assertEquals( "person name does not match", "InactiveEmplFirst", p.getFirstName() );
190 assertEquals( "principal id does not match", "inactiveempid", p.getPrincipalId() );
191 }
192
193 @Test
194 public void testConvertPersonPropertiesToEntityProperties() {
195 HashMap<String,String> criteria = new HashMap<String,String>();
196 criteria.put( "firstName", "System User" );
197 Map<String,String> entityCriteria = personService.convertPersonPropertiesToEntityProperties( criteria );
198 assertEquals( "number of criteria is not correct", 5, entityCriteria.size() );
199 assertNotNull( "criteria must filter for active entity types", entityCriteria.get( "entityTypeContactInfos.active" ) );
200 assertNotNull( "criteria must filter on entity type code", entityCriteria.get( "entityTypeContactInfos.entityTypeCode" ) );
201 assertNotNull( "criteria must filter for first name", entityCriteria.get( "names.firstName" ) );
202 assertNotNull( "criteria must filter for active names", entityCriteria.get( "names.active" ) );
203 assertNotNull( "criteria must filter for the default name", entityCriteria.get( "names.defaultValue" ) );
204 }
205
206
207
208
209 @Test
210 public void testFindPeople() {
211 HashMap<String,String> criteria = new HashMap<String,String>();
212 criteria.put( "firstName", "KULUSER" );
213 List<Person> people = personService.findPeople( criteria );
214 assertNotNull( "result must not be null", people );
215 assertEquals( "wrong number of people returned", 1, people.size() );
216 Person p = people.get( 0 );
217 assertEquals( "name must match criteria", "KULUSER", p.getFirstName() );
218 assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
219 }
220
221 @Test
222 public void testFindPeopleInactive() {
223 HashMap<String,String> criteria = new HashMap<String,String>();
224 criteria.put( "active", "N" );
225 List<Person> people = personService.findPeople( criteria );
226 assertNotNull( "result must not be null", people );
227 assertEquals( "wrong number of people returned", 1, people.size() );
228 }
229
230 @Test
231 public void testFindPeopleBothInactiveAndActive() {
232 HashMap<String,String> criteria = new HashMap<String,String>();
233 criteria.put( "firstName", "InactiveUserFirst" );
234 List<Person> people = personService.findPeople( criteria );
235 assertNotNull( "result must not be null", people );
236 assertEquals( "wrong number of people returned", 1, people.size() );
237 }
238
239 @Test
240 public void testFindPeopleByWildcard() {
241 HashMap<String,String> criteria = new HashMap<String,String>();
242 criteria.put( "principalName", "!quick*" );
243 List<Person> people = personService.findPeople( criteria );
244 assertNotNull( "result must not be null", people );
245 assertEquals( "wrong number of people returned", 53, people.size() );
246 for (Person p: people) {
247 if (p.getPrincipalName().startsWith("quick")) {
248 fail("Invalid wildcard search results");
249 }
250 }
251 }
252
253 @Test
254 public void testResolvePrincipalNamesToPrincipalIds() throws Exception {
255
256 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation( "classpath:org/kuali/rice/kim/bo/datadictionary/test/SampleBO.xml" );
257 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
258
259 Map<String,String> criteria = new HashMap<String,String>();
260 criteria.put( "anAttribute", "aValue" );
261 criteria.put( "anotherAttribute", "anotherValue" );
262 criteria.put( "personAttribute.principalName", "kuluser" );
263 System.out.println( "Before: " + criteria );
264 Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
265 assertNotNull( "returned map must not be null", newCritiera );
266 System.out.println( "After: " + newCritiera );
267 assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
268 assertEquals( "resulting principal ID is not that expected", "KULUSER", newCritiera.get( "personPrincipalId" ) );
269 assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "personAttribute.principalName" ) );
270
271
272 criteria.put( "personPrincipalId", "NOT KULUSER" );
273 System.out.println( "Before: " + criteria );
274 newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
275 assertNotNull( "returned map must not be null", newCritiera );
276 System.out.println( "After: " + newCritiera );
277 assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
278 assertEquals( "resulting principal ID should have been changed", "KULUSER", newCritiera.get( "personPrincipalId" ) );
279 }
280
281 @Test
282 public void testResolvePrincipalNamesToPrincipalIds_Nested() throws Exception {
283 Map<String,String> criteria = new HashMap<String,String>();
284 criteria.put( "add.childBos.childsPersonAttribute.principalName", "kuluser" );
285 System.out.println( "Before: " + criteria );
286 Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
287 assertNotNull( "returned map must not be null", newCritiera );
288 System.out.println( "After: " + newCritiera );
289
290 assertTrue( "new criteria must have a childsPersonPrincipalId entry", newCritiera.containsKey( "add.childBos.childsPersonPrincipalId" ) );
291 assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "add.childBos.childsPersonAttribute.principalName" ) );
292 }
293
294 @Test
295 public void testUpdateWhenNecessary() {
296 SampleBO bo = new SampleBO();
297 bo.setPersonPrincipalId( "KULUSER" );
298 Person p = bo.getPersonAttribute();
299 assertNotNull( "person object must not be null", p );
300 assertEquals( "principal IDs do not match", bo.getPersonPrincipalId(), p.getPrincipalId() );
301 assertSame( "second retrieval must return same object since ID not changed", p, bo.getPersonAttribute() );
302 }
303
304 @Test
305 public void testLookupWithPersonJoin() throws Exception {
306
307
308 InputStream is = new DefaultResourceLoader().getResource("classpath:org/kuali/rice/kim/test/OJB-repository-kimunittests.xml").getInputStream();
309 MetadataManager mm = MetadataManager.getInstance();
310 DescriptorRepository dr = mm.readDescriptorRepository(is);
311 mm.mergeDescriptorRepository(dr);
312
313 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation( "classpath:org/kuali/rice/kim/bo/datadictionary/test/BOContainingPerson.xml" );
314 KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
315 BusinessObjectService bos = KRADServiceLocator.getBusinessObjectService();
316 bos.delete( new ArrayList(bos.findAll( BOContainingPerson.class )) );
317 BOContainingPerson bo = new BOContainingPerson();
318 bo.setBoPrimaryKey( "ONE" );
319 bo.setPrincipalId( "p1" );
320 bos.save( bo );
321 bo = new BOContainingPerson();
322 bo.setBoPrimaryKey( "TWO" );
323 bo.setPrincipalId( "p2" );
324 bos.save( bo );
325
326 Lookupable l = KNSServiceLocator.getKualiLookupable();
327 l.setBusinessObjectClass( BOContainingPerson.class );
328 Map<String,String> criteria = new HashMap<String,String>();
329 criteria.put( "person.principalName", "principal1" );
330 List<BOContainingPerson> results = (List)l.getSearchResultsUnbounded( (Map)criteria );
331 System.out.println( results );
332 assertNotNull( "results may not be null", results );
333 assertEquals( "number of results is incorrect", 1, results.size() );
334 bo = results.iterator().next();
335 assertEquals( "principalId does not match", "p1", bo.getPrincipalId() );
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358 public static class SampleBO implements BusinessObject {
359 private String anAttribute;
360 private String anotherAttribute;
361 private String personPrincipalId;
362 private Person personAttribute;
363 private List<SampleChildBOWithPerson> childBos = new AutoPopulatingList(SampleChildBOWithPerson.class);
364 public String getAnAttribute() {
365 return this.anAttribute;
366 }
367 public void setAnAttribute(String anAttribute) {
368 this.anAttribute = anAttribute;
369 }
370 public String getAnotherAttribute() {
371 return this.anotherAttribute;
372 }
373 public void setAnotherAttribute(String anotherAttribute) {
374 this.anotherAttribute = anotherAttribute;
375 }
376 public String getPersonPrincipalId() {
377 return this.personPrincipalId;
378 }
379 public void setPersonPrincipalId(String personPrincipalId) {
380 this.personPrincipalId = personPrincipalId;
381 }
382 public Person getPersonAttribute() {
383 personAttribute = KimApiServiceLocator.getPersonService().updatePersonIfNecessary( personPrincipalId, personAttribute );
384 return personAttribute;
385 }
386 public void setPersonAttribute(Person personAttribute) {
387 this.personAttribute = personAttribute;
388 }
389 public void refresh() {}
390 public List<SampleChildBOWithPerson> getChildBos() {
391 return this.childBos;
392 }
393 public void setChildBos(List<SampleChildBOWithPerson> childBos) {
394 this.childBos = childBos;
395 }
396 }
397
398 public static class SampleChildBOWithPerson implements BusinessObject {
399
400 private String childsAttribute;
401 private String childsPersonPrincipalId;
402 private Person childsPersonAttribute;
403
404
405
406 public String getChildsAttribute() {
407 return this.childsAttribute;
408 }
409 public void setChildsAttribute(String childsAttribute) {
410 this.childsAttribute = childsAttribute;
411 }
412 public String getChildsPersonPrincipalId() {
413 return this.childsPersonPrincipalId;
414 }
415 public void setChildsPersonPrincipalId(String childsPersonPrincipalId) {
416 this.childsPersonPrincipalId = childsPersonPrincipalId;
417 }
418 public Person getChildsPersonAttribute() {
419 childsPersonAttribute = KimApiServiceLocator.getPersonService().updatePersonIfNecessary( childsPersonPrincipalId, childsPersonAttribute );
420 return childsPersonAttribute;
421 }
422 public void setChildsPersonAttribute(Person childsPersonAttribute) {
423 this.childsPersonAttribute = childsPersonAttribute;
424 }
425 public void refresh() {}
426 }
427 }