1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 testGetPrincipalsByEntityId() {
61 List<Principal> principals = identityService.getPrincipalsByEntityId("1136");
62 assertNotNull("principal must not be null", principals);
63 for (Principal principal: principals) {
64 assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
65 }
66 }
67
68 @Test
69 public void testGetPrincipalsByEntityIdInactive() {
70 List<Principal> principals = identityService.getPrincipalsByEntityId("1139");
71 assertNotNull("principal must not be null", principals);
72 for (Principal principal: principals) {
73 assertEquals("Principal name did not match expected result","inactiveusernm", principal.getPrincipalName());
74 }
75 }
76
77 @Test
78 public void testGetPrincipalsByEmployeeId() {
79 List<Principal> principals = identityService.getPrincipalsByEmployeeId("0000001138");
80 assertNotNull("principal must not be null", principals);
81 for (Principal principal: principals) {
82 assertEquals("Principal name did not match expected result","activeusernm", principal.getPrincipalName());
83 assertEquals("Entity Id did not match expected result","1138", principal.getEntityId());
84 }
85 }
86
87 @Test
88 public void testGetPrincipalsByEmployeeIdInactive() {
89 List<Principal> principals = identityService.getPrincipalsByEmployeeId("0000001140");
90 assertNotNull("principal must not be null", principals);
91 for (Principal principal: principals) {
92 assertEquals("Principal name did not match expected result","inactiveempid", principal.getPrincipalName());
93 assertEquals("Entity Id did not match expected result","1140", principal.getEntityId());
94 }
95 }
96
97 @Test
98 public void testGetPrincipalByPrincipalName() {
99 Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
100 assertNotNull("principal must not be null", principal);
101 assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
102 }
103
104 @Test
105 public void testGetContainedAttributes() {
106 Principal principal = identityService.getPrincipal("p1");
107
108 Entity entity = identityService.getEntity(principal.getEntityId());
109 assertNotNull( "Entity Must not be null", entity );
110 EntityTypeContactInfoContract eet = entity.getEntityTypeContactInfoByTypeCode( "PERSON" );
111 assertNotNull( "PERSON EntityEntityType Must not be null", eet );
112 assertEquals( "there should be 1 email address", 1, eet.getEmailAddresses().size() );
113 assertEquals( "email address does not match", "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
114 }
115
116 @Test
117 public void testEntityUpdate() {
118 Principal principal = identityService.getPrincipal("p1");
119 Entity entity = identityService.getEntity(principal.getEntityId());
120 assertNotNull("Entity Must not be null", entity);
121
122 assertEquals("Entity should have 1 name", 1, entity.getNames().size());
123 Entity.Builder builder = Entity.Builder.create(entity);
124
125
126 List<EntityName.Builder> names = builder.getNames();
127 names.add(getNewEntityName(entity.getId()));
128
129 entity = identityService.updateEntity(builder.build());
130 assertNotNull("Entity Must not be null", entity);
131 assertEquals("Entity should have 2 names", 2, entity.getNames().size());
132
133
134 builder = Entity.Builder.create(entity);
135 builder.setNames(Collections.singletonList(getNewEntityName(entity.getId())));
136
137 entity = identityService.updateEntity(builder.build());
138 assertNotNull("Entity Must not be null", entity);
139 assertEquals("Entity should have 1 names", 1, entity.getNames().size());
140
141 }
142
143 private EntityName.Builder getNewEntityName(String entityId) {
144
145 EntityName.Builder builder = EntityName.Builder.create();
146 builder.setActive(true);
147 builder.setDefaultValue(false);
148 builder.setEntityId(entityId);
149 builder.setFirstName("Bob");
150 builder.setLastName("Bobbers");
151 builder.setNamePrefix("Mr");
152
153 CodedAttribute.Builder nameType = CodedAttribute.Builder.create(identityService.getNameType(
154 KimConstants.NameTypes.PRIMARY));
155 builder.setNameType(nameType);
156 return builder;
157 }
158
159 }