001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.test.service;
017
018import org.junit.Test;
019import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
020import org.kuali.rice.kim.api.identity.Person;
021import org.kuali.rice.kim.api.identity.principal.Principal;
022import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023import org.kuali.rice.kim.impl.identity.PersonImpl;
024import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
025import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
026import org.kuali.rice.kim.test.KIMTestCase;
027import org.kuali.rice.krad.bo.BusinessObject;
028import org.kuali.rice.krad.data.KradDataServiceLocator;
029import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
030import org.kuali.rice.test.BaselineTestCase;
031import org.springframework.util.AutoPopulatingList;
032
033import javax.xml.namespace.QName;
034
035import java.util.HashMap;
036import java.util.List;
037import java.util.Map;
038
039import static org.junit.Assert.*;
040
041/**
042 * This is a description of what this class does - kellerj don't forget to fill this in.
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 *
046 */
047@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
048public class PersonServiceImplTest extends KIMTestCase {
049
050        private PersonServiceImpl personService;
051
052        public void setUp() throws Exception {
053                super.setUp();
054                personService = GlobalResourceLoader.getService(new QName("personService"));
055                
056        }
057
058        /**
059         * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#getPersonByExternalIdentifier(java.lang.String, java.lang.String)}.
060         */
061        @Test
062        public void testGetPersonByExternalIdentifier() {
063                //insert external identifier
064                Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal("p1");
065
066        EntityExternalIdentifierBo externalIdentifier = new EntityExternalIdentifierBo();
067                externalIdentifier.setId("externalIdentifierId");
068                externalIdentifier.setEntityId(principal.getEntityId());
069                externalIdentifier.setExternalId("000-00-0000");
070                externalIdentifier.setExternalIdentifierTypeCode("SSN");
071        KradDataServiceLocator.getDataObjectService().save(externalIdentifier);
072                
073                List<Person> people = personService.getPersonByExternalIdentifier( "SSN", "000-00-0000" );
074                assertNotNull( "result object must not be null", people );
075                assertEquals( "exactly one record should be returned", 1, people.size() );
076                assertEquals( "the returned principal is not correct", "p1", people.get(0).getPrincipalId() );
077        }
078
079//      @Test
080//      public void testHasRole_Inherited() {
081//              Person p = personService.getPersonByPrincipalName( "wwren" );
082//              assertNotNull( "person object must not be null", p );
083//              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" ) );
084//              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" ) );
085//      }
086//
087//      @Test
088//      public void testGetPersonRoles() {
089//              Person p = personService.getPerson( "KULUSER" );
090//              assertNotNull( "person object must not be null", p );
091//              List<KimRole> roles = personService.getPersonRoles( p, null );
092//              assertNotNull( "role list must not be null", roles );
093//              System.out.println( roles );
094//              assertTrue( "role list must have non-zero length", roles.size() > 0 );
095//              KimRole r = KimImplServiceLocator.getAuthorizationService().getRoleByNamespaceCodeAndName( org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_DEFAULT_NAMESPACE_CODE, "SY_FUNCTIONAL_SUPER_USERS" );
096//              assertTrue( "one of the roles must be SY_FUNCTIONAL_SUPER_USERS", roles.contains( r ) );
097//      }
098//
099//      @Test
100//      public void testHasRole() {
101//              Person p = personService.getPerson( "KULUSER" );
102//              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" ) );
103//      }
104
105        @Test
106        public void testGetPerson() {
107                Person p = personService.getPerson( "KULUSER" );
108                assertNotNull( "person object must not be null", p );
109                assertEquals( "class must match implementation class defined on service", personService.getPersonImplementationClass(), p.getClass() );
110                assertEquals( "person name does not match", "KULUSER", p.getFirstNameUnmasked() );
111                assertEquals( "principal name does not match", "kuluser", p.getPrincipalName() );
112                assertEquals( "KULUSER should have no address record", "", p.getAddressLine1Unmasked() );
113                assertEquals( "KULUSER should have no campus code", "", p.getCampusCode() );
114                assertEquals( "KULUSER should have no email address", "", p.getEmailAddressUnmasked() );
115                assertNotNull( "entity ID should be set", p.getEntityId() );
116                assertNotNull( "principal ID should be set", p.getPrincipalId() );
117                
118                // check an employee id
119                Person p1Person = personService.getPerson( "p1" );
120                assertNotNull( "person object must not be null", p );
121                assertEquals("employee ID does not match", "p1emplId", p1Person.getEmployeeId());
122        }
123
124        @Test
125        public void testGetPersonInactive() {
126                Person p = personService.getPerson( "inactiveuserid" );
127                assertNotNull( "person object must not be null", p );
128        assertEquals("principal ID does not match", "inactiveusernm", p.getPrincipalName());
129
130        }
131        
132        @Test
133        public void testGetPersonByPrincipalName() {
134                Person p = personService.getPersonByPrincipalName( "kuluser" );
135                assertNotNull( "person object must not be null", p );
136                assertEquals( "person name does not match", "KULUSER", p.getFirstName() );
137                assertEquals( "principal id does not match", "KULUSER", p.getPrincipalId() );
138        }
139        
140        @Test
141        public void testGetPersonByPrincipalNameInactive() {
142                Person p = personService.getPersonByPrincipalName( "inactiveusernm" );
143        assertEquals("principal ID does not match", "inactiveuserid", p.getPrincipalId());
144    }
145        
146    @Test
147    public void testGetPersonByEmployeeIdNoInfo() {
148        Person p = personService.getPersonByEmployeeId( "" );
149        assertNull( "person object will be null", p );
150    }
151    
152    @Test
153    public void testGetPersonByEmployeeIdNotFound() {
154        Person p = personService.getPersonByEmployeeId( "NotFound" );
155        assertNull( "person object will be null", p );
156    }
157    
158    @Test
159    public void testGetPersonByEmployeeIdActive() {
160        Person p = personService.getPersonByEmployeeId( "0000001138" );
161        assertNotNull( "person object must not be null", p );
162        assertEquals( "person name does not match", "activeUserFirst", p.getFirstName() );
163        assertEquals( "principal id does not match", "activeuserid", p.getPrincipalId() );
164    }
165    
166    @Test
167    public void testGetPersonByEmployeeIdInactiveUser() {
168        Person p = personService.getPersonByEmployeeId( "0000001139" );
169        assertNotNull( "person object must not be null", p );
170        assertEquals( "person name does not match", "InactiveUserFirst", p.getFirstName() );
171        assertEquals( "principal id does not match", "inactiveuserid", p.getPrincipalId() );
172    }
173    
174    @Test
175    public void testGetPersonByEmployeeIdInactiveEmp() {
176        Person p = personService.getPersonByEmployeeId( "0000001140" );
177        assertNotNull( "person object must not be null", p );
178        assertEquals( "person name does not match", "InactiveEmplFirst", p.getFirstName() );
179        assertEquals( "principal id does not match", "inactiveempid", p.getPrincipalId() );
180    }
181    
182        @Test
183        public void testConvertPersonPropertiesToEntityProperties() {
184                HashMap<String,String> criteria = new HashMap<String,String>();
185                criteria.put( "firstName", "System User" );
186                Map<String,String> entityCriteria = personService.convertPersonPropertiesToEntityProperties( criteria );
187                assertEquals( "number of criteria is not correct", 5, entityCriteria.size() );
188                assertNotNull( "criteria must filter for active entity types", entityCriteria.get( "entityTypeContactInfos.active" ) );
189                assertNotNull( "criteria must filter on entity type code", entityCriteria.get( "entityTypeContactInfos.entityTypeCode" ) );
190                assertNotNull( "criteria must filter for first name", entityCriteria.get( "names.firstName" ) );
191                assertNotNull( "criteria must filter for active names", entityCriteria.get( "names.active" ) );
192                assertNotNull( "criteria must filter for the default name", entityCriteria.get( "names.defaultValue" ) );
193        }
194
195        /**
196         * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#findPeople(Map)}.
197         */
198    @Test
199    public void testFindPeople() {
200        HashMap<String,String> criteria = new HashMap<String,String>();
201        criteria.put( "firstName", "KULUSER" );
202        List<Person> people = personService.findPeople( criteria );
203        assertNotNull( "result must not be null", people );
204        assertEquals( "wrong number of people returned", 1, people.size() );
205        Person p = people.get( 0 );
206        assertEquals( "name must match criteria", "KULUSER", p.getFirstName() );
207        assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
208    }
209        
210    @Test
211    public void testFindPeopleInactive() {
212        HashMap<String,String> criteria = new HashMap<String,String>();
213        criteria.put( "principals.active", "N" );
214        List<Person> people = personService.findPeople( criteria );
215        assertNotNull( "result must not be null", people );
216        assertEquals( "wrong number of people returned", 1, people.size() );
217    }
218
219    @Test
220    public void testFindPeopleBothInactiveAndActive() {
221        HashMap<String,String> criteria = new HashMap<String,String>();
222        criteria.put( "firstName", "InactiveUserFirst" );
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 testFindPeopleByWildcard() {
230        HashMap<String,String> criteria = new HashMap<String,String>();
231        criteria.put( "principalName", "!quick*" );
232        List<Person> people = personService.findPeople( criteria );
233        assertNotNull( "result must not be null", people );
234        assertEquals( "wrong number of people returned", 53, people.size() );
235        for (Person p: people) {
236            if (p.getPrincipalName().startsWith("quick")) {
237                fail("Invalid wildcard search results");
238            }
239        }
240    }
241
242        @Test
243        public void testResolvePrincipalNamesToPrincipalIds() throws Exception {
244                
245                KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation("KR-KIM", "classpath:org/kuali/rice/kim/bo/datadictionary/test/SampleBO.xml" );
246                KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
247
248                Map<String,String> criteria = new HashMap<String,String>();
249                criteria.put( "anAttribute", "aValue" );
250                criteria.put( "anotherAttribute", "anotherValue" );
251                criteria.put( "personAttribute.principalName", "kuluser" );
252                System.out.println( "Before: " + criteria );
253                Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
254                assertNotNull( "returned map must not be null", newCritiera );
255                System.out.println( "After:  " + newCritiera );
256                assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
257                assertEquals( "resulting principal ID is not that expected", "KULUSER", newCritiera.get( "personPrincipalId" ) );
258                assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "personAttribute.principalName" ) );
259
260                // check when already has value in result field
261                criteria.put( "personPrincipalId", "NOT KULUSER" );
262                System.out.println( "Before: " + criteria );
263                newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
264                assertNotNull( "returned map must not be null", newCritiera );
265                System.out.println( "After:  " + newCritiera );
266                assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
267                assertEquals( "resulting principal ID should have been changed", "KULUSER", newCritiera.get( "personPrincipalId" ) );
268        }
269
270        @Test
271        public void testResolvePrincipalNamesToPrincipalIds_Nested() throws Exception {
272
273        KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation( "", "classpath:org/kuali/rice/kim/bo/datadictionary/test/SampleBO.xml" );
274        KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
275
276                Map<String,String> criteria = new HashMap<String,String>();
277                criteria.put( "add.childBos.childsPersonAttribute.principalName", "kuluser" );
278                System.out.println( "Before: " + criteria );
279                Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
280                assertNotNull( "returned map must not be null", newCritiera );
281                System.out.println( "After:  " + newCritiera );
282                // TODO: property is being appended in the wrong place - fix
283                assertTrue( "new criteria must have a childsPersonPrincipalId entry", newCritiera.containsKey( "add.childBos.childsPersonPrincipalId" ) );
284                assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "add.childBos.childsPersonAttribute.principalName" ) );
285        }
286
287        @Test
288        public void testUpdateWhenNecessary() {
289                SampleBO bo = new SampleBO();
290                bo.setPersonPrincipalId( "KULUSER" );
291                Person p = bo.getPersonAttribute();
292                assertNotNull( "person object must not be null", p );
293                assertEquals( "principal IDs do not match", bo.getPersonPrincipalId(), p.getPrincipalId() );
294                assertSame( "second retrieval must return same object since ID not changed", p, bo.getPersonAttribute() );
295        }
296
297        @Test
298        public void testUpdatePersonIfNecessary() throws Exception {
299        Person person = personService.updatePersonIfNecessary("p1", null);
300        assertNotNull("person object must not be null", person);
301                assertEquals("principalId does not match", "p1", person.getPrincipalId() );
302        assertEquals("principalName does not match", "principal1", person.getPrincipalName());
303        }
304
305//      @Test
306//      public void testConfirmOnlyPKUsed() {
307//              HashMap<String,String> criteria = new HashMap<String,String>();
308//              criteria.put( "lastName", "HUNTLEY" );
309//              criteria.put( "firstName", "KEISHA" );
310//              Collection<Person> people = (Collection<Person>)KRADServiceLocatorInternal.getLookupService().findCollectionBySearchUnbounded(Person.class, criteria);
311//              personService.findPeople( criteria );
312//              assertNotNull( "result must not be null", people );
313//              assertEquals( "wrong number of people returned", 1, people.size() );
314//              Person p = people.iterator().next();
315//              assertEquals( "principal name does not match", "khuntley", p.getPrincipalName() );
316//
317//              criteria.put( "principalName", "kuluser" );
318//              people = people = (Collection<Person>)KRADServiceLocatorInternal.getLookupService().findCollectionBySearchUnbounded(Person.class, criteria);
319//              assertNotNull( "result must not be null", people );
320//              assertEquals( "wrong number of people returned", 1, people.size() );
321//              p = people.iterator().next();
322//              assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
323//      }
324
325        public static class SampleBO implements BusinessObject {
326                private String anAttribute;
327                private String anotherAttribute;
328                private String personPrincipalId;
329                private PersonImpl personAttribute;
330                private List<SampleChildBOWithPerson> childBos = new AutoPopulatingList(SampleChildBOWithPerson.class);
331                public String getAnAttribute() {
332                        return this.anAttribute;
333                }
334                public void setAnAttribute(String anAttribute) {
335                        this.anAttribute = anAttribute;
336                }
337                public String getAnotherAttribute() {
338                        return this.anotherAttribute;
339                }
340                public void setAnotherAttribute(String anotherAttribute) {
341                        this.anotherAttribute = anotherAttribute;
342                }
343                public String getPersonPrincipalId() {
344                        return this.personPrincipalId;
345                }
346                public void setPersonPrincipalId(String personPrincipalId) {
347                        this.personPrincipalId = personPrincipalId;
348                }
349                public PersonImpl getPersonAttribute() {
350                        personAttribute = (PersonImpl) KimApiServiceLocator.getPersonService().updatePersonIfNecessary( personPrincipalId, personAttribute );
351                        return personAttribute;
352                }
353                public void setPersonAttribute(PersonImpl personAttribute) {
354                        this.personAttribute = personAttribute;
355                }
356                public void refresh() {}
357                public List<SampleChildBOWithPerson> getChildBos() {
358                        return this.childBos;
359                }
360                public void setChildBos(List<SampleChildBOWithPerson> childBos) {
361                        this.childBos = childBos;
362                }
363        }
364
365        public static class SampleChildBOWithPerson implements BusinessObject {
366
367                private String childsAttribute;
368                private String childsPersonPrincipalId;
369                private PersonImpl childsPersonAttribute;
370
371
372
373                public String getChildsAttribute() {
374                        return this.childsAttribute;
375                }
376                public void setChildsAttribute(String childsAttribute) {
377                        this.childsAttribute = childsAttribute;
378                }
379                public String getChildsPersonPrincipalId() {
380                        return this.childsPersonPrincipalId;
381                }
382                public void setChildsPersonPrincipalId(String childsPersonPrincipalId) {
383                        this.childsPersonPrincipalId = childsPersonPrincipalId;
384                }
385                public PersonImpl getChildsPersonAttribute() {
386                        childsPersonAttribute = (PersonImpl) KimApiServiceLocator.getPersonService().updatePersonIfNecessary( childsPersonPrincipalId, childsPersonAttribute );
387                        return childsPersonAttribute;
388                }
389                public void setChildsPersonAttribute(PersonImpl childsPersonAttribute) {
390                        this.childsPersonAttribute = childsPersonAttribute;
391                }
392                public void refresh() {}
393        }
394}