001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.kim.test.service;
017    
018    import org.apache.ojb.broker.metadata.DescriptorRepository;
019    import org.apache.ojb.broker.metadata.MetadataManager;
020    import org.junit.Test;
021    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022    import org.kuali.rice.kim.api.identity.Person;
023    import org.kuali.rice.kim.api.identity.principal.Principal;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
026    import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
027    import org.kuali.rice.kim.test.KIMTestCase;
028    import org.kuali.rice.kim.test.bo.BOContainingPerson;
029    import org.kuali.rice.kns.lookup.Lookupable;
030    import org.kuali.rice.kns.service.KNSServiceLocator;
031    import org.kuali.rice.krad.bo.BusinessObject;
032    import org.kuali.rice.krad.service.BusinessObjectService;
033    import org.kuali.rice.krad.service.KRADServiceLocator;
034    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
035    import org.kuali.rice.krad.service.SequenceAccessorService;
036    import org.kuali.rice.test.BaselineTestCase;
037    import org.springframework.core.io.DefaultResourceLoader;
038    import org.springframework.util.AutoPopulatingList;
039    
040    import javax.xml.namespace.QName;
041    import java.io.InputStream;
042    import java.util.ArrayList;
043    import java.util.HashMap;
044    import java.util.List;
045    import java.util.Map;
046    
047    import static org.junit.Assert.*;
048    
049    /**
050     * This is a description of what this class does - kellerj don't forget to fill this in.
051     *
052     * @author Kuali Rice Team (rice.collab@kuali.org)
053     *
054     */
055    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
056    public class PersonServiceImplTest extends KIMTestCase {
057    
058            private PersonServiceImpl personService;
059    
060            public void setUp() throws Exception {
061                    super.setUp();
062                    personService = (PersonServiceImpl) GlobalResourceLoader.getService(new QName("personService"));
063                    
064            }
065    
066            /**
067             * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#getPersonByExternalIdentifier(java.lang.String, java.lang.String)}.
068             */
069            @Test
070            public void testGetPersonByExternalIdentifier() {
071                    //insert external identifier
072                    Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal("p1");
073                    
074                    SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
075                    Long externalIdentifierId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_EXT_ID_ID_S", EntityExternalIdentifierBo.class);
076                    EntityExternalIdentifierBo externalIdentifier = new EntityExternalIdentifierBo();
077                    externalIdentifier.setId("externalIdentifierId");
078                    externalIdentifier.setEntityId(principal.getEntityId());
079                    externalIdentifier.setExternalId("000-00-0000");
080                    externalIdentifier.setExternalIdentifierTypeCode("SSN");
081                    KRADServiceLocator.getBusinessObjectService().save(externalIdentifier);
082                    
083                    List<Person> people = personService.getPersonByExternalIdentifier( "SSN", "000-00-0000" );
084                    assertNotNull( "result object must not be null", people );
085                    assertEquals( "exactly one record should be returned", 1, people.size() );
086                    assertEquals( "the returned principal is not correct", "p1", people.get(0).getPrincipalId() );
087            }
088    
089    //      @Test
090    //      public void testHasRole_Inherited() {
091    //              Person p = personService.getPersonByPrincipalName( "wwren" );
092    //              assertNotNull( "person object must not be null", p );
093    //              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" ) );
094    //              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" ) );
095    //      }
096    //
097    //      @Test
098    //      public void testGetPersonRoles() {
099    //              Person p = personService.getPerson( "KULUSER" );
100    //              assertNotNull( "person object must not be null", p );
101    //              List<KimRole> roles = personService.getPersonRoles( p, null );
102    //              assertNotNull( "role list must not be null", roles );
103    //              System.out.println( roles );
104    //              assertTrue( "role list must have non-zero length", roles.size() > 0 );
105    //              KimRole r = KimImplServiceLocator.getAuthorizationService().getRoleByNameAndNamespaceCode( org.kuali.rice.kim.util.KimApiConstants.KIM_GROUP_DEFAULT_NAMESPACE_CODE, "SY_FUNCTIONAL_SUPER_USERS" );
106    //              assertTrue( "one of the roles must be SY_FUNCTIONAL_SUPER_USERS", roles.contains( r ) );
107    //      }
108    //
109    //      @Test
110    //      public void testHasRole() {
111    //              Person p = personService.getPerson( "KULUSER" );
112    //              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" ) );
113    //      }
114    
115            @Test
116            public void testGetPerson() {
117                    Person p = personService.getPerson( "KULUSER" );
118                    assertNotNull( "person object must not be null", p );
119                    assertEquals( "class must match implementation class defined on service", personService.getPersonImplementationClass(), p.getClass() );
120                    assertEquals( "person name does not match", "KULUSER", p.getFirstNameUnmasked() );
121                    assertEquals( "principal name does not match", "kuluser", p.getPrincipalName() );
122                    assertEquals( "KULUSER should have no address record", "", p.getAddressLine1Unmasked() );
123                    assertEquals( "KULUSER should have no campus code", "", p.getCampusCode() );
124                    assertEquals( "KULUSER should have no email address", "", p.getEmailAddressUnmasked() );
125                    assertNotNull( "entity ID should be set", p.getEntityId() );
126                    assertNotNull( "principal ID should be set", p.getPrincipalId() );
127                    
128                    // check an employee id
129                    Person p1Person = personService.getPerson( "p1" );
130                    assertNotNull( "person object must not be null", p );
131                    assertEquals("employee ID does not match", "p1emplId", p1Person.getEmployeeId());
132            }
133    
134            @Test
135            public void testGetPersonByPrincipalName() {
136                    Person p = personService.getPersonByPrincipalName( "kuluser" );
137                    assertNotNull( "person object must not be null", p );
138                    assertEquals( "person name does not match", "KULUSER", p.getFirstName() );
139                    assertEquals( "principal id does not match", "KULUSER", p.getPrincipalId() );
140            }
141    
142    
143            @Test
144            public void testConvertPersonPropertiesToEntityProperties() {
145                    HashMap<String,String> criteria = new HashMap<String,String>();
146                    criteria.put( "firstName", "System User" );
147                    Map<String,String> entityCriteria = personService.convertPersonPropertiesToEntityProperties( criteria );
148                    assertEquals( "number of criteria is not correct", 6, entityCriteria.size() );
149                    assertNotNull( "criteria must filter for active entities", entityCriteria.get( "active" ) );
150                    assertNotNull( "criteria must filter for active entity types", entityCriteria.get( "entityTypeContactInfos.active" ) );
151                    assertNotNull( "criteria must filter on entity type code", entityCriteria.get( "entityTypeContactInfos.entityTypeCode" ) );
152                    assertNotNull( "criteria must filter for first name", entityCriteria.get( "names.firstName" ) );
153                    assertNotNull( "criteria must filter for active names", entityCriteria.get( "names.active" ) );
154                    assertNotNull( "criteria must filter for the default name", entityCriteria.get( "names.defaultValue" ) );
155            }
156    
157            /**
158             * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#findPeople(Map)}.
159             */
160            @Test
161            public void testFindPeople() {
162                    HashMap<String,String> criteria = new HashMap<String,String>();
163                    criteria.put( "firstName", "KULUSER" );
164                    List<Person> people = personService.findPeople( criteria );
165                    assertNotNull( "result must not be null", people );
166                    assertEquals( "wrong number of people returned", 1, people.size() );
167                    Person p = people.get( 0 );
168                    assertEquals( "name must match criteria", "KULUSER", p.getFirstName() );
169                    assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
170            }
171    
172            @Test
173            public void testResolvePrincipalNamesToPrincipalIds() throws Exception {
174                    
175                    KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation( "classpath:org/kuali/rice/kim/bo/datadictionary/test/SampleBO.xml" );
176                    KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
177    
178                    Map<String,String> criteria = new HashMap<String,String>();
179                    criteria.put( "anAttribute", "aValue" );
180                    criteria.put( "anotherAttribute", "anotherValue" );
181                    criteria.put( "personAttribute.principalName", "kuluser" );
182                    System.out.println( "Before: " + criteria );
183                    Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
184                    assertNotNull( "returned map must not be null", newCritiera );
185                    System.out.println( "After:  " + newCritiera );
186                    assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
187                    assertEquals( "resulting principal ID is not that expected", "KULUSER", newCritiera.get( "personPrincipalId" ) );
188                    assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "personAttribute.principalName" ) );
189    
190                    // check when already has value in result field
191                    criteria.put( "personPrincipalId", "NOT KULUSER" );
192                    System.out.println( "Before: " + criteria );
193                    newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
194                    assertNotNull( "returned map must not be null", newCritiera );
195                    System.out.println( "After:  " + newCritiera );
196                    assertTrue( "new criteria must have a personPrincipalId entry", newCritiera.containsKey( "personPrincipalId" ) );
197                    assertEquals( "resulting principal ID should have been changed", "KULUSER", newCritiera.get( "personPrincipalId" ) );
198            }
199    
200            @Test
201            public void testResolvePrincipalNamesToPrincipalIds_Nested() throws Exception {
202                    Map<String,String> criteria = new HashMap<String,String>();
203                    criteria.put( "add.childBos.childsPersonAttribute.principalName", "kuluser" );
204                    System.out.println( "Before: " + criteria );
205                    Map<String,String> newCritiera = personService.resolvePrincipalNamesToPrincipalIds( new SampleBO(), criteria );
206                    assertNotNull( "returned map must not be null", newCritiera );
207                    System.out.println( "After:  " + newCritiera );
208                    // TODO: property is being appended in the wrong place - fix
209                    assertTrue( "new criteria must have a childsPersonPrincipalId entry", newCritiera.containsKey( "add.childBos.childsPersonPrincipalId" ) );
210                    assertFalse( "new criteria must not contain the original PrincipalName entry", newCritiera.containsKey( "add.childBos.childsPersonAttribute.principalName" ) );
211            }
212    
213            @Test
214            public void testUpdateWhenNecessary() {
215                    SampleBO bo = new SampleBO();
216                    bo.setPersonPrincipalId( "KULUSER" );
217                    Person p = bo.getPersonAttribute();
218                    assertNotNull( "person object must not be null", p );
219                    assertEquals( "principal IDs do not match", bo.getPersonPrincipalId(), p.getPrincipalId() );
220                    assertSame( "second retrieval must return same object since ID not changed", p, bo.getPersonAttribute() );
221            }
222    
223            @Test
224            public void testLookupWithPersonJoin() throws Exception {
225                    
226                    // merge the OJB file in containing the OJB metadata
227            InputStream is = new DefaultResourceLoader().getResource("classpath:org/kuali/rice/kim/test/OJB-repository-kimunittests.xml").getInputStream();
228            MetadataManager mm = MetadataManager.getInstance();
229            DescriptorRepository dr = mm.readDescriptorRepository(is);
230            mm.mergeDescriptorRepository(dr);
231                    
232                    KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().addConfigFileLocation( "classpath:org/kuali/rice/kim/bo/datadictionary/test/BOContainingPerson.xml" );
233                    KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().parseDataDictionaryConfigurationFiles( false );
234                    BusinessObjectService bos = KRADServiceLocator.getBusinessObjectService();
235                    bos.delete( new ArrayList(bos.findAll( BOContainingPerson.class )) );
236                    BOContainingPerson bo = new BOContainingPerson();
237                    bo.setBoPrimaryKey( "ONE" );
238                    bo.setPrincipalId( "p1" );
239                    bos.save( bo );
240                    bo = new BOContainingPerson();
241                    bo.setBoPrimaryKey( "TWO" );
242                    bo.setPrincipalId( "p2" );
243                    bos.save( bo );
244    
245                    Lookupable l = KNSServiceLocator.getKualiLookupable();
246                    l.setBusinessObjectClass( BOContainingPerson.class );
247                    Map<String,String> criteria = new HashMap<String,String>();
248                    criteria.put( "person.principalName", "principal1" );
249                    List<BOContainingPerson> results = (List)l.getSearchResultsUnbounded( (Map)criteria );
250                    System.out.println( results );
251                    assertNotNull( "results may not be null", results );
252                    assertEquals( "number of results is incorrect", 1, results.size() );
253                    bo =  results.iterator().next();
254                    assertEquals( "principalId does not match", "p1", bo.getPrincipalId() );
255            }
256    
257    //      @Test
258    //      public void testConfirmOnlyPKUsed() {
259    //              HashMap<String,String> criteria = new HashMap<String,String>();
260    //              criteria.put( "lastName", "HUNTLEY" );
261    //              criteria.put( "firstName", "KEISHA" );
262    //              Collection<Person> people = (Collection<Person>)KRADServiceLocatorInternal.getLookupService().findCollectionBySearchUnbounded(Person.class, criteria);
263    //              personService.findPeople( criteria );
264    //              assertNotNull( "result must not be null", people );
265    //              assertEquals( "wrong number of people returned", 1, people.size() );
266    //              Person p = people.iterator().next();
267    //              assertEquals( "principal name does not match", "khuntley", p.getPrincipalName() );
268    //
269    //              criteria.put( "principalName", "kuluser" );
270    //              people = people = (Collection<Person>)KRADServiceLocatorInternal.getLookupService().findCollectionBySearchUnbounded(Person.class, criteria);
271    //              assertNotNull( "result must not be null", people );
272    //              assertEquals( "wrong number of people returned", 1, people.size() );
273    //              p = people.iterator().next();
274    //              assertEquals( "principal name must be kuluser", "kuluser", p.getPrincipalName() );
275    //      }
276    
277            public static class SampleBO implements BusinessObject {
278                    private String anAttribute;
279                    private String anotherAttribute;
280                    private String personPrincipalId;
281                    private Person personAttribute;
282                    private List<SampleChildBOWithPerson> childBos = new AutoPopulatingList(SampleChildBOWithPerson.class);
283                    public String getAnAttribute() {
284                            return this.anAttribute;
285                    }
286                    public void setAnAttribute(String anAttribute) {
287                            this.anAttribute = anAttribute;
288                    }
289                    public String getAnotherAttribute() {
290                            return this.anotherAttribute;
291                    }
292                    public void setAnotherAttribute(String anotherAttribute) {
293                            this.anotherAttribute = anotherAttribute;
294                    }
295                    public String getPersonPrincipalId() {
296                            return this.personPrincipalId;
297                    }
298                    public void setPersonPrincipalId(String personPrincipalId) {
299                            this.personPrincipalId = personPrincipalId;
300                    }
301                    public Person getPersonAttribute() {
302                            personAttribute = KimApiServiceLocator.getPersonService().updatePersonIfNecessary( personPrincipalId, personAttribute );
303                            return personAttribute;
304                    }
305                    public void setPersonAttribute(Person personAttribute) {
306                            this.personAttribute = personAttribute;
307                    }
308                    public void refresh() {}
309                    public List<SampleChildBOWithPerson> getChildBos() {
310                            return this.childBos;
311                    }
312                    public void setChildBos(List<SampleChildBOWithPerson> childBos) {
313                            this.childBos = childBos;
314                    }
315            }
316    
317            public static class SampleChildBOWithPerson implements BusinessObject {
318    
319                    private String childsAttribute;
320                    private String childsPersonPrincipalId;
321                    private Person childsPersonAttribute;
322    
323    
324    
325                    public String getChildsAttribute() {
326                            return this.childsAttribute;
327                    }
328                    public void setChildsAttribute(String childsAttribute) {
329                            this.childsAttribute = childsAttribute;
330                    }
331                    public String getChildsPersonPrincipalId() {
332                            return this.childsPersonPrincipalId;
333                    }
334                    public void setChildsPersonPrincipalId(String childsPersonPrincipalId) {
335                            this.childsPersonPrincipalId = childsPersonPrincipalId;
336                    }
337                    public Person getChildsPersonAttribute() {
338                            childsPersonAttribute = KimApiServiceLocator.getPersonService().updatePersonIfNecessary( childsPersonPrincipalId, childsPersonAttribute );
339                            return childsPersonAttribute;
340                    }
341                    public void setChildsPersonAttribute(Person childsPersonAttribute) {
342                            this.childsPersonAttribute = childsPersonAttribute;
343                    }
344                    public void refresh() {}
345            }
346    }