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 static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.junit.Test;
27  import org.kuali.rice.core.api.util.type.KualiDecimal;
28  import org.kuali.rice.kim.api.KimConstants;
29  import org.kuali.rice.kim.api.identity.CodedAttribute;
30  import org.kuali.rice.kim.api.identity.IdentityService;
31  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
32  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
33  import org.kuali.rice.kim.api.identity.citizenship.EntityCitizenship;
34  import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
35  import org.kuali.rice.kim.api.identity.entity.Entity;
36  import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
37  import org.kuali.rice.kim.api.identity.name.EntityName;
38  import org.kuali.rice.kim.api.identity.principal.Principal;
39  import org.kuali.rice.kim.api.identity.residency.EntityResidency;
40  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoContract;
41  import org.kuali.rice.kim.api.identity.visa.EntityVisa;
42  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
43  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
44  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
45  import org.kuali.rice.kim.test.KIMTestCase;
46  import org.kuali.rice.krad.data.KradDataServiceLocator;
47  import org.kuali.rice.test.BaselineTestCase;
48  
49  /**
50   *
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   */
53  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
54  public class IdentityServiceImplTest extends KIMTestCase {
55  
56  	private IdentityService identityService;
57  
58  	@Override
59      public void setUp() throws Exception {
60  		super.setUp();
61  		identityService = (IdentityService) KIMServiceLocatorInternal.getBean("kimIdentityDelegateService");
62  	}
63  
64  	@Test
65  	public void testGetPrincipal() {
66  		Principal principal = identityService.getPrincipal("KULUSER");
67  		assertNotNull("principal must not be null", principal);
68  		assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
69  	}
70  
71      @Test
72      public void testGetPrincipalsByEntityId() {
73          List<Principal> principals = identityService.getPrincipalsByEntityId("1136");
74          assertNotNull("principal must not be null", principals);
75          for (Principal principal: principals) {
76              assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
77          }
78      }
79  
80      @Test
81      public void testGetPrincipalsByEntityIdInactive() {
82          List<Principal> principals = identityService.getPrincipalsByEntityId("1139");
83          assertNotNull("principal must not be null", principals);
84          for (Principal principal: principals) {
85              assertEquals("Principal name did not match expected result","inactiveusernm", principal.getPrincipalName());
86          }
87      }
88  
89      @Test
90      public void testGetPrincipalsByEmployeeId() {
91          List<Principal> principals = identityService.getPrincipalsByEmployeeId("0000001138");
92          assertNotNull("principal must not be null", principals);
93          for (Principal principal: principals) {
94              assertEquals("Principal name did not match expected result","activeusernm", principal.getPrincipalName());
95              assertEquals("Entity Id did not match expected result","1138", principal.getEntityId());
96          }
97      }
98  
99      @Test
100     public void testGetPrincipalsByEmployeeIdInactive() {
101         List<Principal> principals = identityService.getPrincipalsByEmployeeId("0000001140");
102         assertNotNull("principal must not be null", principals);
103         for (Principal principal: principals) {
104             assertEquals("Principal name did not match expected result","inactiveempid", principal.getPrincipalName());
105             assertEquals("Entity Id did not match expected result","1140", principal.getEntityId());
106         }
107     }
108 
109 	@Test
110 	public void testGetPrincipalByPrincipalName() {
111 		Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
112 		assertNotNull("principal must not be null", principal);
113 		assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
114 	}
115 
116 	@Test
117 	public void testGetContainedAttributes() {
118 		Principal principal = identityService.getPrincipal("p1");
119 
120 		Entity entity = identityService.getEntity(principal.getEntityId());
121 		assertNotNull( "Entity Must not be null", entity );
122 		EntityTypeContactInfoContract eet = entity.getEntityTypeContactInfoByTypeCode( "PERSON" );
123 		assertNotNull( "PERSON EntityEntityType Must not be null", eet );
124 		assertEquals( "there should be 1 email address", 1, eet.getEmailAddresses().size() );
125 		assertEquals( "email address does not match", "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
126 	}
127 
128     @Test
129     public void testCreateEntity() {
130 
131         // This test will verify that all entityIds and other foreign keys are set when createEntity is called.
132         // EntityEthnicity, EntityPrivacyPreferences, EntityBioDemographics, and EntityTypeContactInfo cannot be
133         // created without a known entity ID so they were not included in this test.
134 
135         Entity.Builder entity = Entity.Builder.create();
136         entity.setActive(true);
137 
138         populateEntityForCreate(entity);
139 
140         Entity createdEntity = identityService.createEntity(entity.build());
141         Entity entityFromDb = identityService.getEntity(createdEntity.getId());
142 
143         assertNotNull("createdEntity must not be null", entityFromDb);
144 
145         List<Principal> createdPrincipals = entityFromDb.getPrincipals();
146         assertNotNull("createdPrincipals must not be null", createdPrincipals);
147         assertEquals("There must be one principal", 1, createdPrincipals.size());
148         for (Principal principal : createdPrincipals) {
149             assertNotNull("principal entityId must not be null", principal.getEntityId());
150             assertNotNull("principal principalId must not be null", principal.getPrincipalId());
151         }
152 
153         List<EntityName> createdNames = entityFromDb.getNames();
154         assertNotNull("createdNames must not be null", createdNames);
155         assertEquals("There must be one name", 1, createdNames.size());
156         for (EntityName name : createdNames) {
157             assertNotNull("name entityId must not be null", name.getEntityId());
158         }
159 
160         List<EntityExternalIdentifier> entityExternalIdentifiers = entityFromDb.getExternalIdentifiers();
161         assertNotNull("entityExternalIdentifiers must not be null", entityExternalIdentifiers);
162         assertEquals("There must be one entityExternalIdentifier", 1, entityExternalIdentifiers.size());
163         for (EntityExternalIdentifier entityExternalIdentifier : entityExternalIdentifiers) {
164             assertNotNull("entityExternalIdentifier entityId must not be null", entityExternalIdentifier.getEntityId());
165         }
166 
167         List<EntityAffiliation> createdEntityAffiliations = entityFromDb.getAffiliations();
168         assertNotNull("createdEntityAffiliations must not be null", createdEntityAffiliations);
169         assertEquals("There must be two createdEntityAffiliations", 2, createdEntityAffiliations.size());
170         for (EntityAffiliation entityAffiliation : createdEntityAffiliations) {
171             assertNotNull("entityAffiliation entityId must not be null", entityAffiliation.getEntityId());
172         }
173 
174         List<EntityEmployment> createdEntityEmployments = entityFromDb.getEmploymentInformation();
175         assertNotNull("createdEntityEmployments must not be null", createdEntityEmployments);
176         assertEquals("There must be one createdEntityEmployment", 1, createdEntityEmployments.size());
177         for (EntityEmployment entityEmployment : createdEntityEmployments) {
178             assertNotNull("entityEmployment entityId must not be null", entityEmployment.getEntityId());
179             EntityEmploymentBo entityEmploymentBo = EntityEmploymentBo.from(entityEmployment);
180             assertNotNull("entityEmploymentBo entityAffiliationId must not be null",
181                     entityEmploymentBo.getEntityAffiliationId());
182             EntityAffiliation entityAffiliation = entityEmployment.getEntityAffiliation();
183             assertNotNull("entityAffiliation Id must not be null", entityAffiliation.getId());
184             assertNotNull("entityAffiliation entityId must not be null", entityAffiliation.getEntityId());
185         }
186 
187         List<EntityCitizenship> createdEntityCitizenships = entityFromDb.getCitizenships();
188         assertNotNull("entityCitizenships must not be null", createdEntityCitizenships);
189         assertEquals("There must be 1 entityCitizenship", 1, createdEntityCitizenships.size());
190         for (EntityCitizenship entityCitizenship : createdEntityCitizenships) {
191             assertNotNull("entityCitizenship entityId must not be null", entityCitizenship.getEntityId());
192         }
193 
194         List<EntityResidency> createdEntityResidencies = entityFromDb.getResidencies();
195         assertNotNull("createdEntityResidencies must not be null", createdEntityResidencies);
196         assertEquals("There must be 1 createdEntityResidency", 1, createdEntityResidencies.size());
197         for (EntityResidency entityResidency : createdEntityResidencies) {
198             assertNotNull("entityResidency entityId must not be null", entityResidency.getEntityId());
199         }
200 
201         List<EntityVisa> createdEntityVisas = entityFromDb.getVisas();
202         assertNotNull("createdEntityVisas must not be null", createdEntityVisas);
203         assertEquals("There must be 1 createdEntityVisa", 1, createdEntityVisas.size());
204         for (EntityVisa entityVisa : createdEntityVisas) {
205             assertNotNull("entityVisa entityId must not be null", entityVisa.getEntityId());
206         }
207     }
208 
209     @Test
210     public void testUpdateEntity() {
211         Principal principal = identityService.getPrincipal("p1");
212         Entity entity = identityService.getEntity(principal.getEntityId());
213         assertNotNull("Entity Must not be null", entity);
214 
215         assertEquals("Entity should have 1 name", 1, entity.getNames().size());
216         Entity.Builder builder = Entity.Builder.create(entity);
217 
218         // add a Name
219         List<EntityName.Builder> names = builder.getNames();
220         names.add(getNewEntityName(entity.getId()));
221 
222         // add student affiliation
223         EntityAffiliation.Builder affiliationStdnt = EntityAffiliation.Builder.create();
224         affiliationStdnt.setActive(true);
225         affiliationStdnt.setCampusCode("MX");
226         affiliationStdnt.setAffiliationType(EntityAffiliationType.Builder.create("STDNT"));
227         builder.getAffiliations().add(affiliationStdnt);
228 
229         // add employment
230         EntityEmployment.Builder entityEmploymentBuilder = EntityEmployment.Builder.create();
231         EntityAffiliation.Builder affiliationStaff = EntityAffiliation.Builder.create();
232         affiliationStaff.setActive(true);
233         affiliationStaff.setCampusCode("GR");
234         affiliationStaff.setAffiliationType(EntityAffiliationType.Builder.create("STAFF"));
235         entityEmploymentBuilder.setEntityAffiliation(affiliationStaff);
236         entityEmploymentBuilder.setActive(true);
237         entityEmploymentBuilder.setEmployeeId("1234test");
238         entityEmploymentBuilder.setPrimary(false);
239         entityEmploymentBuilder.setBaseSalaryAmount(new KualiDecimal(8000));
240         entityEmploymentBuilder.setPrimaryDepartmentCode("BL-CHEM");
241         builder.getEmploymentInformation().add(entityEmploymentBuilder);
242 
243         entity = identityService.updateEntity(builder.build());
244         entity = EntityBo.to( KradDataServiceLocator.getDataObjectService().find(EntityBo.class, entity.getId()) );
245 
246         assertNotNull("Entity Must not be null", entity);
247         assertEquals("Entity should have 2 names", 2, entity.getNames().size());
248 
249         List<EntityAffiliation> createdEntityAffiliations = entity.getAffiliations();
250         assertNotNull("createdEntityAffiliations must not be null", createdEntityAffiliations);
251         assertEquals("There must be two createdEntityAffiliations", 2, createdEntityAffiliations.size());
252         for (EntityAffiliation entityAffiliation : createdEntityAffiliations) {
253             assertNotNull("entityAffiliation entityId must not be null", entityAffiliation.getEntityId());
254         }
255 
256         List<EntityEmployment> createdEntityEmployments = entity.getEmploymentInformation();
257         List<EntityEmployment> entityEmploymentToRemove = new ArrayList<EntityEmployment>();
258         assertNotNull("createdEntityEmployments must not be null", createdEntityEmployments);
259         assertEquals("There must be two createdEntityEmployments", 2, createdEntityEmployments.size());
260         for (EntityEmployment entityEmployment : createdEntityEmployments) {
261             assertNotNull("entityEmployment entityId must not be null", entityEmployment.getEntityId());
262             if (entityEmployment.getEmployeeId().equalsIgnoreCase("1234test")) {
263                 EntityEmploymentBo entityEmploymentBo = EntityEmploymentBo.from(entityEmployment);
264                 assertNotNull("entityEmploymentBo entityAffiliationId must not be null",
265                         entityEmploymentBo.getEntityAffiliationId());
266                 EntityAffiliation entityAffiliation = entityEmployment.getEntityAffiliation();
267                 assertNotNull("entityAffiliation Id must not be null", entityAffiliation.getId());
268                 assertNotNull("entityAffiliation entityId must not be null", entityAffiliation.getEntityId());
269             }
270         }
271 
272         // remove the old name - make sure collection items are removed.
273         builder = Entity.Builder.create(entity);
274         builder.setNames(Collections.singletonList(getNewEntityName(entity.getId())));
275 
276         entity = identityService.updateEntity(builder.build());
277         assertNotNull("Entity Must not be null", entity);
278         assertEquals("Entity should have 1 names", 1, entity.getNames().size());
279     }
280 
281     private EntityName.Builder getNewEntityName(String entityId) {
282 
283         EntityName.Builder builder = EntityName.Builder.create();
284         builder.setActive(true);
285         builder.setDefaultValue(false);
286         builder.setEntityId(entityId);
287         builder.setFirstName("Bob");
288         builder.setLastName("Bobbers");
289         builder.setNamePrefix("Mr");
290 
291         CodedAttribute.Builder nameType = CodedAttribute.Builder.create(identityService.getNameType(
292                 KimConstants.NameTypes.PRIMARY));
293         builder.setNameType(nameType);
294         return builder;
295     }
296 
297     private void populateEntityForCreate(Entity.Builder entityBuilder) {
298         // set up principals
299         Principal.Builder principalBuilder = Principal.Builder.create("tinMan");
300         principalBuilder.setActive(true);
301         List<Principal.Builder> principals = new ArrayList<Principal.Builder>();
302         principals.add(principalBuilder);
303         entityBuilder.setPrincipals(principals);
304 
305         // set up names
306         EntityName.Builder nameBuilder = EntityName.Builder.create();
307         nameBuilder.setFirstName("Nick");
308         nameBuilder.setLastName("Chopper");
309         nameBuilder.setDefaultValue(true);
310         List<EntityName.Builder> entityNames = new ArrayList<EntityName.Builder>();
311         entityNames.add(nameBuilder);
312         entityBuilder.setNames(entityNames);
313 
314         // set up student affiliation
315         EntityAffiliation.Builder affiliationStdnt = EntityAffiliation.Builder.create();
316         affiliationStdnt.setActive(true);
317         affiliationStdnt.setCampusCode("MX");
318         affiliationStdnt.setAffiliationType(EntityAffiliationType.Builder.create("STDNT"));
319         List<EntityAffiliation.Builder> entityAffiliations = new ArrayList<EntityAffiliation.Builder>();
320         entityAffiliations.add(affiliationStdnt);
321         entityBuilder.setAffiliations(entityAffiliations);
322 
323         // set up employment
324         EntityEmployment.Builder entityEmploymentBuilder = EntityEmployment.Builder.create();
325         EntityAffiliation.Builder affiliationStaff = EntityAffiliation.Builder.create();
326         affiliationStaff.setActive(true);
327         affiliationStaff.setCampusCode("GR");
328         affiliationStaff.setAffiliationType(EntityAffiliationType.Builder.create("STAFF"));
329         entityEmploymentBuilder.setEntityAffiliation(affiliationStaff);
330         entityEmploymentBuilder.setActive(true);
331         entityEmploymentBuilder.setEmployeeId("1234test");
332         entityEmploymentBuilder.setPrimary(true);
333         entityEmploymentBuilder.setBaseSalaryAmount(new KualiDecimal(8000));
334         entityEmploymentBuilder.setPrimaryDepartmentCode("BL-CHEM");
335         List<EntityEmployment.Builder> entityEmployments = new ArrayList<EntityEmployment.Builder>();
336         entityEmployments.add(entityEmploymentBuilder);
337         entityBuilder.setEmploymentInformation(entityEmployments);
338 
339         // set up external identifier
340         EntityExternalIdentifier.Builder externalIdentifierBuilder = EntityExternalIdentifier.Builder.create();
341         externalIdentifierBuilder.setExternalIdentifierTypeCode("SSN");
342         externalIdentifierBuilder.setExternalId("444-44-4444");
343         List<EntityExternalIdentifier.Builder> externalIdentifiers = new ArrayList<EntityExternalIdentifier.Builder>();
344         externalIdentifiers.add(externalIdentifierBuilder);
345         entityBuilder.setExternalIdentifiers(externalIdentifiers);
346 
347         // set up citizenship
348         EntityCitizenship.Builder citizenshipBuilder = EntityCitizenship.Builder.create();
349         citizenshipBuilder.setCountryCode("OZ");
350         citizenshipBuilder.setActive(true);
351         List<EntityCitizenship.Builder> entityCitizenships = new ArrayList<EntityCitizenship.Builder>();
352         entityCitizenships.add(citizenshipBuilder);
353         entityBuilder.setCitizenships(entityCitizenships);
354 
355         // set up residency
356         EntityResidency.Builder residencyBuilder = EntityResidency.Builder.create();
357         residencyBuilder.setDeterminationMethod("XXX");
358         residencyBuilder.setInState("YYY");
359         List<EntityResidency.Builder> entityResidencies = new ArrayList<EntityResidency.Builder>();
360         entityResidencies.add(residencyBuilder);
361         entityBuilder.setResidencies(entityResidencies);
362 
363         // set up visa
364         EntityVisa.Builder visaBuilder = EntityVisa.Builder.create();
365         visaBuilder.setVisaId("1234");
366         List<EntityVisa.Builder> entityVisas = new ArrayList<EntityVisa.Builder>();
367         entityVisas.add(visaBuilder);
368         entityBuilder.setVisas(entityVisas);
369     }
370 }