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  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.junit.Test;
25  import org.kuali.rice.kim.api.KimConstants;
26  import org.kuali.rice.kim.api.identity.CodedAttribute;
27  import org.kuali.rice.kim.api.identity.IdentityService;
28  import org.kuali.rice.kim.api.identity.entity.Entity;
29  import org.kuali.rice.kim.api.identity.name.EntityName;
30  import org.kuali.rice.kim.api.identity.principal.Principal;
31  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoContract;
32  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
33  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
34  import org.kuali.rice.kim.test.KIMTestCase;
35  import org.kuali.rice.krad.data.KradDataServiceLocator;
36  import org.kuali.rice.test.BaselineTestCase;
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  	@Override
48      public void setUp() throws Exception {
49  		super.setUp();
50  		identityService = (IdentityService) KIMServiceLocatorInternal.getBean("kimIdentityDelegateService");
51  	}
52  
53  	@Test
54  	public void testGetPrincipal() {
55  		Principal principal = identityService.getPrincipal("KULUSER");
56  		assertNotNull("principal must not be null", principal);
57  		assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
58  	}
59  
60      @Test
61      public void testGetPrincipalsByEntityId() {
62          List<Principal> principals = identityService.getPrincipalsByEntityId("1136");
63          assertNotNull("principal must not be null", principals);
64          for (Principal principal: principals) {
65              assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
66          }
67      }
68  
69      @Test
70      public void testGetPrincipalsByEntityIdInactive() {
71          List<Principal> principals = identityService.getPrincipalsByEntityId("1139");
72          assertNotNull("principal must not be null", principals);
73          for (Principal principal: principals) {
74              assertEquals("Principal name did not match expected result","inactiveusernm", principal.getPrincipalName());
75          }
76      }
77  
78      @Test
79      public void testGetPrincipalsByEmployeeId() {
80          List<Principal> principals = identityService.getPrincipalsByEmployeeId("0000001138");
81          assertNotNull("principal must not be null", principals);
82          for (Principal principal: principals) {
83              assertEquals("Principal name did not match expected result","activeusernm", principal.getPrincipalName());
84              assertEquals("Entity Id did not match expected result","1138", principal.getEntityId());
85          }
86      }
87  
88      @Test
89      public void testGetPrincipalsByEmployeeIdInactive() {
90          List<Principal> principals = identityService.getPrincipalsByEmployeeId("0000001140");
91          assertNotNull("principal must not be null", principals);
92          for (Principal principal: principals) {
93              assertEquals("Principal name did not match expected result","inactiveempid", principal.getPrincipalName());
94              assertEquals("Entity Id did not match expected result","1140", principal.getEntityId());
95          }
96      }
97  
98  	@Test
99  	public void testGetPrincipalByPrincipalName() {
100 		Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
101 		assertNotNull("principal must not be null", principal);
102 		assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
103 	}
104 
105 	@Test
106 	public void testGetContainedAttributes() {
107 		Principal principal = identityService.getPrincipal("p1");
108 
109 		Entity entity = identityService.getEntity(principal.getEntityId());
110 		assertNotNull( "Entity Must not be null", entity );
111 		EntityTypeContactInfoContract eet = entity.getEntityTypeContactInfoByTypeCode( "PERSON" );
112 		assertNotNull( "PERSON EntityEntityType Must not be null", eet );
113 		assertEquals( "there should be 1 email address", 1, eet.getEmailAddresses().size() );
114 		assertEquals( "email address does not match", "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
115 	}
116 
117     @Test
118     public void testEntityUpdate() {
119         Principal principal = identityService.getPrincipal("p1");
120         Entity entity = identityService.getEntity(principal.getEntityId());
121         assertNotNull("Entity Must not be null", entity);
122 
123         assertEquals("Entity should have 1 name", 1, entity.getNames().size());
124         Entity.Builder builder = Entity.Builder.create(entity);
125         //lets add a "Name"
126 
127         List<EntityName.Builder> names = builder.getNames();
128         names.add(getNewEntityName(entity.getId()));
129 
130         entity = identityService.updateEntity(builder.build());
131         entity = EntityBo.to( KradDataServiceLocator.getDataObjectService().find(EntityBo.class, entity.getId()) );
132         assertNotNull("Entity Must not be null", entity);
133         assertEquals("Entity should have 2 names", 2, entity.getNames().size());
134 
135         //remove the old name - make sure collection items are removed.
136         builder = Entity.Builder.create(entity);
137         builder.setNames(Collections.singletonList(getNewEntityName(entity.getId())));
138 
139         entity = identityService.updateEntity(builder.build());
140         assertNotNull("Entity Must not be null", entity);
141         assertEquals("Entity should have 1 names", 1, entity.getNames().size());
142 
143     }
144 
145     private EntityName.Builder getNewEntityName(String entityId) {
146 
147         EntityName.Builder builder = EntityName.Builder.create();
148         builder.setActive(true);
149         builder.setDefaultValue(false);
150         builder.setEntityId(entityId);
151         builder.setFirstName("Bob");
152         builder.setLastName("Bobbers");
153         builder.setNamePrefix("Mr");
154 
155         CodedAttribute.Builder nameType = CodedAttribute.Builder.create(identityService.getNameType(
156                 KimConstants.NameTypes.PRIMARY));
157         builder.setNameType(nameType);
158         return builder;
159     }
160 
161 }