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 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
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
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 }