001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kim.test.service;
017
018 import org.junit.AfterClass;
019 import org.junit.BeforeClass;
020 import org.junit.Test;
021 import org.kuali.rice.core.api.lifecycle.Lifecycle;
022 import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
023 import org.kuali.rice.kim.api.identity.IdentityService;
024 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
025 import org.kuali.rice.kim.api.identity.entity.Entity;
026 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
027 import org.kuali.rice.kim.api.identity.principal.Principal;
028 import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
029 import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
030
031 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
032 import org.kuali.rice.kim.test.KIMTestCase;
033
034 import org.kuali.rice.test.BaselineTestCase;
035 import org.kuali.rice.test.SQLDataLoader;
036 import org.springframework.core.io.ClassPathResource;
037 import org.springframework.ldap.core.DistinguishedName;
038 import org.springframework.ldap.core.LdapTemplate;
039 import org.springframework.ldap.core.support.LdapContextSource;
040 import org.springframework.ldap.test.LdapTestUtils;
041
042 import javax.xml.namespace.QName;
043 import java.util.List;
044
045 import static org.junit.Assert.assertEquals;
046 import static org.junit.Assert.assertNotNull;
047
048 /**
049 * @author Kuali Rice Team (rice.collab@kuali.org)
050 */
051 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
052 public class LDAPIdentityServiceImplTest extends KIMTestCase {
053 private static final DistinguishedName baseName = new DistinguishedName("o=Whoniverse");
054
055 private static final String PRINCIPAL = "uid=admin,ou=system";
056 private static final String CREDENTIALS = "secret";
057
058 // This port MUST be free on local host for these unit tests to function.
059 private static int PORT = 10389;
060 private IdentityService identityService;
061
062
063 @BeforeClass
064 public static void startLDAPServer() throws Exception {
065 LdapTestUtils.startApacheDirectoryServer(PORT, baseName.toString(), "test", PRINCIPAL, CREDENTIALS, null);
066 LdapContextSource contextSource = new LdapContextSource();
067 contextSource.setUrl("ldap://127.0.0.1:" + PORT);
068 contextSource.setUserDn("");
069 contextSource.setPassword("");
070 contextSource.setPooled(false);
071 contextSource.afterPropertiesSet();
072
073 // Create the Sprint LDAP template
074 LdapTemplate template = new LdapTemplate(contextSource);
075
076 // Clear out any old data - and load the test data
077 LdapTestUtils.cleanAndSetup(template.getContextSource(), baseName, new ClassPathResource("ldap/testdata.ldif"));
078 System.out.println("____________Started LDAP_________");
079 }
080
081 @AfterClass
082 public static void shutdownLDAP() throws Exception {
083 LdapTestUtils.destroyApacheDirectoryServer(PRINCIPAL, CREDENTIALS);
084 System.out.println("____________Shutdown LDAP_________");
085 }
086
087
088 public void setUp() throws Exception {
089 super.setUp();
090 identityService = (IdentityService) KIMServiceLocatorInternal.getBean("kimLDAPIdentityDelegateService");
091
092 }
093
094
095 @Test
096 public void testGetEntity() {
097 Entity entity = identityService.getEntity("williamh");
098 assertNotNull("Entity must not be null", entity);
099 assertEquals("Entity name matched expected result", "williamh", entity.getPrincipals().get(0)
100 .getPrincipalName());
101 }
102
103 @Test
104 public void testGetEntityByPrincipalId() {
105 Entity entity = identityService.getEntityByPrincipalId("williamh");
106 assertNotNull("Entity must not be null", entity);
107 assertEquals("Entity name matched expected result", "williamh", entity.getPrincipals().get(0)
108 .getPrincipalName());
109 }
110
111 @Test
112 public void testGetEntityByPrincipalName() {
113 Entity entity = identityService.getEntityByPrincipalName("williamh");
114 assertNotNull("Entity must not be null", entity);
115 assertEquals("Entity name matched expected result", "williamh", entity.getPrincipals().get(0)
116 .getPrincipalName());
117 }
118
119 @Test
120 public void testGetEntityDefault() {
121 EntityDefault entity = identityService.getEntityDefault("williamh");
122 assertNotNull("Entity must not be null", entity);
123 assertEquals("Entity name matched expected result", "williamh", entity.getPrincipals().get(0)
124 .getPrincipalName());
125 }
126
127 @Test
128 public void testGetEntityDefaultByPrincipalId() {
129 EntityDefault entity = identityService.getEntityDefaultByPrincipalId("williamh");
130 assertNotNull("Entity must not be null", entity);
131 assertEquals("Entity name matched expected result", "williamh", entity.getPrincipals().get(0)
132 .getPrincipalName());
133 }
134
135 @Test
136 public void testGetEntityDefaultByPrincipalName() {
137 EntityDefault entity = identityService.getEntityDefaultByPrincipalName("williamh");
138 assertNotNull("Entity must not be null", entity);
139 assertEquals("Entity name matched expected result", "williamh", entity.getPrincipals().get(0)
140 .getPrincipalName());
141 }
142
143 @Test
144 public void testGetEntityPrivacyPreferences() {
145 EntityPrivacyPreferences entityPrivacyPreferences = identityService.getEntityPrivacyPreferences("williamh");
146 assertNotNull("Entity must not be null", entityPrivacyPreferences);
147
148 }
149
150 @Test
151 public void testGetPrincipal() {
152 Principal principal = identityService.getPrincipal("williamh");
153 assertNotNull("Principal must not be null", principal);
154 assertEquals("Principal name did not match expected result", "williamh", principal.getPrincipalName());
155 }
156
157 @Test
158 public void testGetPrincipalByPrincipalName() {
159 Principal principal = identityService.getPrincipalByPrincipalName("williamh");
160 assertNotNull("principal must not be null", principal);
161 assertEquals("Principal ID did not match expected result", "williamh", principal.getPrincipalId());
162 }
163
164 @Test
165 public void testGetContainedAttributes() {
166 Principal principal = identityService.getPrincipal("williamh");
167
168 Entity entity = identityService.getEntity(principal.getEntityId());
169 assertNotNull("Entity Must not be null", entity);
170 EntityTypeContactInfo eet = entity.getEntityTypeContactInfoByTypeCode("PERSON");
171 List<EntityAffiliation> ea = entity.getAffiliations();
172 assertNotNull("PERSON EntityEntityType Must not be null", eet);
173 assertEquals("there should be 1 email address", 1, eet.getEmailAddresses().size());
174 assertEquals("Email address does not match", "williamh@test.edu",
175 eet.getDefaultEmailAddress().getEmailAddressUnmasked());
176 assertEquals("there should be 1 phone number", 1, eet.getPhoneNumbers().size());
177 assertEquals("The Phone number does not match", "111-111-1111", eet.getPhoneNumbers().get(0)
178 .getPhoneNumberUnmasked());
179 assertEquals("there should be 1 phone number", 1, ea.size());
180 assertEquals("The Affiliations do not match", "STAFF", ea.get(0).getAffiliationType().getCode());
181 }
182
183 }