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.junit.Test;
19  import org.kuali.rice.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.identity.CodedAttribute;
21  import org.kuali.rice.kim.api.identity.IdentityService;
22  import org.kuali.rice.kim.api.identity.entity.Entity;
23  import org.kuali.rice.kim.api.identity.name.EntityName;
24  import org.kuali.rice.kim.api.identity.principal.Principal;
25  
26  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoContract;
27  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
28  import org.kuali.rice.kim.impl.identity.IdentityServiceImpl;
29  import org.kuali.rice.kim.test.KIMTestCase;
30  import org.kuali.rice.test.BaselineTestCase;
31  
32  import java.util.Collections;
33  import java.util.List;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNotNull;
37  
38  /**
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
43  public class IdentityServiceImplTest extends KIMTestCase {
44  
45  	private IdentityService identityService;
46  
47  	public void setUp() throws Exception {
48  		super.setUp();
49  		identityService = (IdentityService) KIMServiceLocatorInternal.getBean("kimIdentityDelegateService");
50  	}
51  
52  	@Test
53  	public void testGetPrincipal() {
54  		Principal principal = identityService.getPrincipal("KULUSER");
55  		assertNotNull("principal must not be null", principal);
56  		assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
57  	}
58  
59  	@Test
60  	public void testGetPrincipalByPrincipalName() {
61  		Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
62  		assertNotNull("principal must not be null", principal);
63  		assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
64  	}
65  	
66  	@Test
67  	public void testGetContainedAttributes() {
68  		Principal principal = identityService.getPrincipal("p1");
69  		
70  		Entity entity = identityService.getEntity(principal.getEntityId());
71  		assertNotNull( "Entity Must not be null", entity );
72  		EntityTypeContactInfoContract eet = entity.getEntityTypeContactInfoByTypeCode( "PERSON" );
73  		assertNotNull( "PERSON EntityEntityType Must not be null", eet );
74  		assertEquals( "there should be 1 email address", 1, eet.getEmailAddresses().size() );
75  		assertEquals( "email address does not match", "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
76  	}
77  
78      @Test
79      public void testEntityUpdate() {
80          Principal principal = identityService.getPrincipal("p1");
81          Entity entity = identityService.getEntity(principal.getEntityId());
82          assertNotNull("Entity Must not be null", entity);
83  
84          assertEquals("Entity should have 1 name", 1, entity.getNames().size());
85          Entity.Builder builder = Entity.Builder.create(entity);
86          //lets add a "Name"
87  
88          List<EntityName.Builder> names = builder.getNames();
89          names.add(getNewEntityName(entity.getId()));
90  
91          entity = identityService.updateEntity(builder.build());
92          assertNotNull("Entity Must not be null", entity);
93          assertEquals("Entity should have 2 names", 2, entity.getNames().size());
94  
95          //remove the old name - make sure collection items are removed.
96          builder = Entity.Builder.create(entity);
97          builder.setNames(Collections.singletonList(getNewEntityName(entity.getId())));
98  
99          entity = identityService.updateEntity(builder.build());
100         assertNotNull("Entity Must not be null", entity);
101         assertEquals("Entity should have 1 names", 1, entity.getNames().size());
102 
103     }
104 
105     private EntityName.Builder getNewEntityName(String entityId) {
106 
107         EntityName.Builder builder = EntityName.Builder.create();
108         builder.setActive(true);
109         builder.setDefaultValue(false);
110         builder.setEntityId(entityId);
111         builder.setFirstName("Bob");
112         builder.setLastName("Bobbers");
113         builder.setNamePrefix("Mr");
114 
115         CodedAttribute.Builder nameType = CodedAttribute.Builder.create(identityService.getNameType(
116                 KimConstants.NameTypes.PRIMARY));
117         builder.setNameType(nameType);
118         return builder;
119     }
120 
121 }