View Javadoc

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