View Javadoc

1   /**
2    * Copyright 2005-2011 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.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.identity.PersonServiceImpl;
26  import org.kuali.rice.kim.impl.identity.external.EntityExternalIdentifierBo;
27  import org.kuali.rice.kim.test.KIMTestCase;
28  import org.kuali.rice.kim.test.bo.BOContainingPerson;
29  import org.kuali.rice.kns.lookup.Lookupable;
30  import org.kuali.rice.kns.service.KNSServiceLocator;
31  import org.kuali.rice.krad.bo.BusinessObject;
32  import org.kuali.rice.krad.service.BusinessObjectService;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
35  import org.kuali.rice.krad.service.SequenceAccessorService;
36  import org.kuali.rice.test.BaselineTestCase;
37  import org.springframework.core.io.DefaultResourceLoader;
38  import org.springframework.util.AutoPopulatingList;
39  
40  import javax.xml.namespace.QName;
41  import java.io.InputStream;
42  import java.util.ArrayList;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  import static org.junit.Assert.*;
48  
49  /**
50   * This is a description of what this class does - kellerj don't forget to fill this in.
51   *
52   * @author Kuali Rice Team (rice.collab@kuali.org)
53   *
54   */
55  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
56  public class PersonServiceImplTest extends KIMTestCase {
57  
58  	private PersonServiceImpl personService;
59  
60  	public void setUp() throws Exception {
61  		super.setUp();
62  		personService = (PersonServiceImpl) GlobalResourceLoader.getService(new QName("personService"));
63  		
64  	}
65  
66  	/**
67  	 * Test method for {@link org.kuali.rice.kim.impl.identity.PersonServiceImpl#getPersonByExternalIdentifier(java.lang.String, java.lang.String)}.
68  	 */
69  	@Test
70  	public void testGetPersonByExternalIdentifier() {
71  		//insert external identifier
72  		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal("p1");
73  		
74  		SequenceAccessorService sas = KRADServiceLocator.getSequenceAccessorService();
75  		Long externalIdentifierId = sas.getNextAvailableSequenceNumber("KRIM_ENTITY_EXT_ID_ID_S", EntityExternalIdentifierBo.class);
76  		EntityExternalIdentifierBo externalIdentifier = new EntityExternalIdentifierBo();
77  		externalIdentifier.setId("externalIdentifierId");
78  		externalIdentifier.setEntityId(principal.getEntityId());
79  		externalIdentifier.setExternalId("000-00-0000");
80  		externalIdentifier.setExternalIdentifierTypeCode("SSN");
81  		KRADServiceLocator.getBusinessObjectService().save(externalIdentifier);
82  		
83  		List<Person> people = personService.getPersonByExternalIdentifier( "SSN", "000-00-0000" );
84  		assertNotNull( "result object must not be null", people );
85  		assertEquals( "exactly one record should be returned", 1, people.size() );
86  		assertEquals( "the returned principal is not correct", "p1", people.get(0).getPrincipalId() );
87  	}
88  
89  //	@Test
90  //	public void testHasRole_Inherited() {
91  //		Person p = personService.getPersonByPrincipalName( "wwren" );
92  //		assertNotNull( "person object must not be null", p );
93  //		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" ) );
94  //		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" ) );
95  //	}
96  //
97  //	@Test
98  //	public void testGetPersonRoles() {
99  //		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 }