View Javadoc

1   /**
2    * Copyright 2005-2013 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.AfterClass;
19  import org.junit.BeforeClass;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.lifecycle.Lifecycle;
22  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
23  import org.kuali.rice.kim.api.identity.IdentityService;
24  import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
25  import org.kuali.rice.kim.api.identity.entity.Entity;
26  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
27  import org.kuali.rice.kim.api.identity.principal.Principal;
28  import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
29  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
30  
31  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
32  import org.kuali.rice.kim.test.KIMTestCase;
33  
34  import org.kuali.rice.test.BaselineTestCase;
35  import org.kuali.rice.test.SQLDataLoader;
36  import org.springframework.core.io.ClassPathResource;
37  import org.springframework.ldap.core.DistinguishedName;
38  import org.springframework.ldap.core.LdapTemplate;
39  import org.springframework.ldap.core.support.LdapContextSource;
40  import org.springframework.ldap.test.LdapTestUtils;
41  
42  import javax.xml.namespace.QName;
43  import java.util.List;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  
48  /**
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   */
51  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
52  public class LDAPIdentityServiceImplTest extends KIMTestCase {
53      private static final DistinguishedName baseName = new DistinguishedName("o=Whoniverse");
54  
55      private static final String PRINCIPAL = "uid=admin,ou=system";
56      private static final String CREDENTIALS = "secret";
57  
58      // This port MUST be free on local host for these unit tests to function.
59      private static int PORT = 10389;
60      private IdentityService identityService;
61  
62  
63      @BeforeClass
64      public static void startLDAPServer() throws Exception {
65          LdapTestUtils.startApacheDirectoryServer(PORT, baseName.toString(), "test", PRINCIPAL, CREDENTIALS, null);
66          LdapContextSource contextSource = new LdapContextSource();
67          contextSource.setUrl("ldap://127.0.0.1:" + PORT);
68          contextSource.setUserDn("");
69          contextSource.setPassword("");
70          contextSource.setPooled(false);
71          contextSource.afterPropertiesSet();
72  
73          // Create the Sprint LDAP template
74          LdapTemplate template = new LdapTemplate(contextSource);
75  
76          // Clear out any old data - and load the test data
77          LdapTestUtils.cleanAndSetup(template.getContextSource(), baseName, new ClassPathResource("ldap/testdata.ldif"));
78          System.out.println("____________Started LDAP_________");
79      }
80  
81      @AfterClass
82      public static void shutdownLDAP() throws Exception {
83          LdapTestUtils.destroyApacheDirectoryServer(PRINCIPAL, CREDENTIALS);
84           System.out.println("____________Shutdown LDAP_________");
85      }
86  
87  
88      public void setUp() throws Exception {
89          super.setUp();
90          identityService = (IdentityService) KIMServiceLocatorInternal.getBean("kimLDAPIdentityDelegateService");
91  
92      }
93  
94  
95      @Test
96      public void testGetEntity() {
97          Entity entity = identityService.getEntity("williamh");
98          assertNotNull("Entity must not be null", entity);
99          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 }