View Javadoc
1   /**
2    * Copyright 2005-2011 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.Test;
19  import org.kuali.rice.core.api.criteria.Predicate;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.kim.api.KimApiConstants;
23  import org.kuali.rice.kim.api.identity.IdentityService;
24  import org.kuali.rice.kim.api.identity.entity.Entity;
25  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
26  import org.kuali.rice.kim.api.identity.entity.EntityDefaultQueryResults;
27  import org.kuali.rice.kim.api.identity.name.EntityName;
28  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
29  import org.kuali.rice.kim.api.identity.principal.Principal;
30  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
31  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
32  import org.kuali.rice.kim.impl.KIMPropertyConstants;
33  import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
34  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
35  import org.kuali.rice.kim.test.KIMTestCase;
36  import org.kuali.rice.test.BaselineTestCase;
37  
38  import javax.xml.namespace.QName;
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  import static org.junit.Assert.*;
45  import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
46  import static org.kuali.rice.core.api.criteria.PredicateFactory.like;
47  
48  /**
49   * 
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   */
52  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
53  public class IdentityServiceTest extends KIMTestCase {
54  
55  	private IdentityService identityService;
56  
57  	public void setUp() throws Exception {
58  		super.setUp();
59  		if (null == identityService) {
60  			identityService = findIdSvc();
61  		}
62  	}
63  	
64  	@Test
65  	public void testGetPrincipal() {
66  		Principal principal = identityService.getPrincipal("KULUSER");
67  		assertNotNull("principal must not be null", principal);
68  		assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
69  	}
70  
71      @Test
72      public void testGetPrincipalNotFound() {
73          Principal principal = identityService.getPrincipal("DoesNotExist");
74          assertNull("principal should not be found", principal);
75      }
76  
77      @Test
78      public void testGetPrincipals() {
79          List<String> principalIds = new ArrayList<String>();
80          principalIds.add("KULUSER");
81          List<Principal> validPrincipals = identityService.getPrincipals(principalIds);
82          assertNotNull("validPrincipals must not be null", validPrincipals);
83          assertEquals("validPrincipals name did not match expected result","kuluser", validPrincipals.get(0).getPrincipalName());
84      }
85  
86  	@Test
87  	public void testGetPrincipalByPrincipalName() {
88  		Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
89  		assertNotNull("principal must not be null", principal);
90  		assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
91  	}
92  
93      @Test
94      public void testGetPrincipalByPrincipalNameNotFound() {
95          Principal principal = identityService.getPrincipalByPrincipalName("DoesNotExist");
96          assertNull("principal should not be found", principal);
97      }
98  	
99  	@Test
100 	public void testGetDefaultEntityByPrincipalId() {
101 		String principalId = "KULUSER";
102 		EntityDefault info = identityService.getEntityDefaultByPrincipalId(principalId);
103 		assertNotNull("entity must not be null", info);
104 		assertNotNull("entity principals must not be null", info.getPrincipals());
105 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
106 		for (Principal principalInfo : info.getPrincipals()) {
107 			assertEquals("Wrong principal id", principalId, principalInfo.getPrincipalId());
108 		}
109 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
110 	}
111 
112 	@Test
113 	public void testGetDefaultEntityByPrincipalName() {
114 		String principalName = "kuluser";
115 		EntityDefault info = identityService.getEntityDefaultByPrincipalName(principalName);
116 		assertNotNull("entity must not be null", info);
117 		assertNotNull("entity principals must not be null", info.getPrincipals());
118 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
119 		for (Principal principalInfo : info.getPrincipals()) {
120 			assertEquals("Wrong principal name", principalName, principalInfo.getPrincipalName());
121 		}
122 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
123 	}
124 
125 	@Test
126 	public void testGetEntityByPrincipalId() {
127 		String principalId = "KULUSER";
128 		Entity info = identityService.getEntityByPrincipalId(principalId);
129 		assertNotNull("entity must not be null", info);
130 		assertNotNull("entity principals must not be null", info.getPrincipals());
131 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
132 		for (Principal principalInfo : info.getPrincipals()) {
133 			assertEquals("Wrong principal id", principalId, principalInfo.getPrincipalId());
134 		}
135 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
136 		assertTrue("entity residencies must not be null", (info.getResidencies() == null) || info.getResidencies().isEmpty());
137 	}
138 
139 	@Test
140 	public void testGetEntityByPrincipalName() {
141 		String principalName = "kuluser";
142 		Entity info = identityService.getEntityByPrincipalName(principalName);
143 		assertNotNull("entity must not be null", info);
144 		assertNotNull("entity principals must not be null", info.getPrincipals());
145 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
146 		for (Principal principalInfo : info.getPrincipals()) {
147 			assertEquals("Wrong principal name", principalName, principalInfo.getPrincipalName());
148 		}
149 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
150 		assertTrue("entity residencies must not be null", (info.getResidencies() == null) || info.getResidencies().isEmpty());
151 	}
152 
153 	@Test
154 	public void testGetContainedAttributes() {
155 		Principal principal = identityService.getPrincipal("p1");
156 		
157 		EntityDefault entity = identityService.getEntityDefault(principal.getEntityId());
158 		assertNotNull( "Entity Must not be null", entity );
159 		EntityTypeContactInfoDefault eet = entity.getEntityType( "PERSON" );
160 		assertNotNull( "PERSON EntityTypeData must not be null", eet );
161 		assertNotNull( "EntityEntityType's default email address must not be null", eet.getDefaultEmailAddress() );
162 		assertEquals( "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
163 	}
164 
165 	protected QueryByCriteria setUpEntityLookupCriteria(String principalId) {
166 		PersonServiceImpl personServiceImpl = (PersonServiceImpl) KIMServiceLocatorInternal.getService(KimApiServiceLocator.KIM_PERSON_SERVICE);
167 		Map<String,String> criteria = new HashMap<String,String>(1);
168 		criteria.put(KIMPropertyConstants.Person.PRINCIPAL_ID, principalId);
169 		Map<String, String> entityCriteria = personServiceImpl.convertPersonPropertiesToEntityProperties(criteria);
170         entityCriteria.put("entityTypeContactInfos.entityTypeCode", "PERSON");
171         QueryByCriteria.Builder query = QueryByCriteria.Builder.create();
172         List<Predicate> predicates = new ArrayList<Predicate>();
173         for (String key : entityCriteria.keySet()) {
174             predicates.add(like(key, entityCriteria.get(key)));
175         }
176         if (!predicates.isEmpty()) {
177             query.setPredicates(and(predicates.toArray(new Predicate[] {})));
178         }
179         return query.build();
180 	}
181 
182 	@Test
183 	public void testLookupEntityDefaultInfo() {
184 		String principalIdToTest = "p1";
185 		EntityDefaultQueryResults qr = identityService.findEntityDefaults(setUpEntityLookupCriteria(principalIdToTest));
186         List<EntityDefault> results = qr.getResults();
187 		assertNotNull("Lookup results should never be null", results);
188 		assertEquals("Lookup result count is invalid", 1, results.size());
189 		for (EntityDefault kimEntityDefaultInfo : results) {
190 			assertEquals("Entity should have only one principal for this test", 1, kimEntityDefaultInfo.getPrincipals().size());
191 			assertEquals("Principal Ids should match", principalIdToTest, kimEntityDefaultInfo.getPrincipals().get(0).getPrincipalId());
192 		}
193 	}
194 
195 	@Test
196 	public void testLookupEntityInfo() {
197 		String principalIdToTest = "p1";
198 		List<Entity> results = identityService.findEntities(setUpEntityLookupCriteria(principalIdToTest)).getResults();
199 		assertNotNull("Lookup results should never be null", results);
200 		assertEquals("Lookup result count is invalid", 1, results.size());
201 		for (Entity kimEntityInfo : results) {
202 			assertEquals("Entity should have only one principal for this test", 1, kimEntityInfo.getPrincipals().size());
203 			assertEquals("Principal Ids should match", principalIdToTest, kimEntityInfo.getPrincipals().get(0).getPrincipalId());
204 		}
205 	}
206 
207     @Test
208     public void testGetEntityWithNameChangeDate() {
209         String principalName = "testuser7";
210         Entity info = identityService.getEntityByPrincipalName(principalName);
211         List<EntityName> names = info.getNames();
212         for (EntityName name : names) {
213             assertNotNull("nameChangeDate should have been set for PrincipalName " + principalName,name.getNameChangedDate());
214         }
215     }
216 
217     protected IdentityService findIdSvc() throws Exception {
218 		return (IdentityService) GlobalResourceLoader.getService(
219                 new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.IDENTITY_SERVICE_SOAP));
220 	}
221 
222 	protected void setIdentityService(IdentityService idSvc) {
223 		this.identityService = idSvc;
224 	}
225 }