View Javadoc
1   /**
2    * Copyright 2005-2014 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 testUpdatePrincipal() {
101         String principalName = "kuluser";
102         String principalId = "KULUSER";
103         String newPrincipalName = "kuluserUpdatedName";
104 
105         Principal principal = identityService.getPrincipalByPrincipalName(principalName);
106         assertNotNull("principal must not be null", principal);
107         assertEquals("principal id should be " + principalId, principalId, principal.getPrincipalId());
108         assertEquals("principal name should be " + principalName, principalName, principal.getPrincipalName());
109 
110         Principal.Builder builder = Principal.Builder.create(principal);
111         builder.setPrincipalName(newPrincipalName);
112         Principal updatedPrincipal = identityService.updatePrincipal(builder.build());
113         assertNotNull("principal must not be null", updatedPrincipal);
114         assertEquals("principal id should be " + principalId, principalId, updatedPrincipal.getPrincipalId());
115         assertEquals("principal name should be " + newPrincipalName, newPrincipalName,
116                 updatedPrincipal.getPrincipalName());
117 
118         Principal principalById = identityService.getPrincipal(principalId);
119         assertNotNull("principal must not be null", principalById);
120         assertEquals("principal name should be " + newPrincipalName, newPrincipalName,
121                 principalById.getPrincipalName());
122 
123         // Set the princpalName back to kuluser to prevent issues with other tests
124         Principal.Builder builder2 = Principal.Builder.create(principalById);
125         builder2.setPrincipalName(principalName);
126         Principal revertedPrincipal = identityService.updatePrincipal(builder2.build());
127         assertNotNull("principal must not be null", revertedPrincipal);
128         assertEquals("principal id should be " + principalId, principalId, revertedPrincipal.getPrincipalId());
129         assertEquals("principal name should be " + principalName, principalName, revertedPrincipal.getPrincipalName());
130     }
131 
132 	@Test
133 	public void testGetDefaultEntityByPrincipalId() {
134 		String principalId = "KULUSER";
135 		EntityDefault info = identityService.getEntityDefaultByPrincipalId(principalId);
136 		assertNotNull("entity must not be null", info);
137 		assertNotNull("entity principals must not be null", info.getPrincipals());
138 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
139 		for (Principal principalInfo : info.getPrincipals()) {
140 			assertEquals("Wrong principal id", principalId, principalInfo.getPrincipalId());
141 		}
142 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
143 	}
144 
145 	@Test
146 	public void testGetDefaultEntityByPrincipalName() {
147 		String principalName = "kuluser";
148 		EntityDefault info = identityService.getEntityDefaultByPrincipalName(principalName);
149 		assertNotNull("entity must not be null", info);
150 		assertNotNull("entity principals must not be null", info.getPrincipals());
151 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
152 		for (Principal principalInfo : info.getPrincipals()) {
153 			assertEquals("Wrong principal name", principalName, principalInfo.getPrincipalName());
154 		}
155 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
156 	}
157 
158 	@Test
159 	public void testGetEntityByPrincipalId() {
160 		String principalId = "KULUSER";
161 		Entity info = identityService.getEntityByPrincipalId(principalId);
162 		assertNotNull("entity must not be null", info);
163 		assertNotNull("entity principals must not be null", info.getPrincipals());
164 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
165 		for (Principal principalInfo : info.getPrincipals()) {
166 			assertEquals("Wrong principal id", principalId, principalInfo.getPrincipalId());
167 		}
168 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
169 		assertTrue("entity residencies must not be null", (info.getResidencies() == null) || info.getResidencies().isEmpty());
170 	}
171 
172 	@Test
173 	public void testGetEntityByPrincipalName() {
174 		String principalName = "kuluser";
175 		Entity info = identityService.getEntityByPrincipalName(principalName);
176 		assertNotNull("entity must not be null", info);
177 		assertNotNull("entity principals must not be null", info.getPrincipals());
178 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
179 		for (Principal principalInfo : info.getPrincipals()) {
180 			assertEquals("Wrong principal name", principalName, principalInfo.getPrincipalName());
181 		}
182 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
183 		assertTrue("entity residencies must not be null", (info.getResidencies() == null) || info.getResidencies().isEmpty());
184 	}
185 
186 	@Test
187 	public void testGetContainedAttributes() {
188 		Principal principal = identityService.getPrincipal("p1");
189 		
190 		EntityDefault entity = identityService.getEntityDefault(principal.getEntityId());
191 		assertNotNull( "Entity Must not be null", entity );
192 		EntityTypeContactInfoDefault eet = entity.getEntityType( "PERSON" );
193 		assertNotNull( "PERSON EntityTypeData must not be null", eet );
194 		assertNotNull( "EntityEntityType's default email address must not be null", eet.getDefaultEmailAddress() );
195 		assertEquals( "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
196 	}
197 
198 	protected QueryByCriteria setUpEntityLookupCriteria(String principalId) {
199 		PersonServiceImpl personServiceImpl = (PersonServiceImpl) KIMServiceLocatorInternal.getService(KimApiServiceLocator.KIM_PERSON_SERVICE);
200 		Map<String,String> criteria = new HashMap<String,String>(1);
201 		criteria.put(KIMPropertyConstants.Person.PRINCIPAL_ID, principalId);
202 		Map<String, String> entityCriteria = personServiceImpl.convertPersonPropertiesToEntityProperties(criteria);
203         entityCriteria.put("entityTypeContactInfos.entityTypeCode", "PERSON");
204         QueryByCriteria.Builder query = QueryByCriteria.Builder.create();
205         List<Predicate> predicates = new ArrayList<Predicate>();
206         for (String key : entityCriteria.keySet()) {
207             predicates.add(like(key, entityCriteria.get(key)));
208         }
209         if (!predicates.isEmpty()) {
210             query.setPredicates(and(predicates.toArray(new Predicate[predicates.size()])));
211         }
212         return query.build();
213 	}
214 
215 	@Test
216 	public void testLookupEntityDefaultInfo() {
217 		String principalIdToTest = "p1";
218 		EntityDefaultQueryResults qr = identityService.findEntityDefaults(setUpEntityLookupCriteria(principalIdToTest));
219         List<EntityDefault> results = qr.getResults();
220 		assertNotNull("Lookup results should never be null", results);
221 		assertEquals("Lookup result count is invalid", 1, results.size());
222 		for (EntityDefault kimEntityDefaultInfo : results) {
223 			assertEquals("Entity should have only one principal for this test", 1, kimEntityDefaultInfo.getPrincipals().size());
224 			assertEquals("Principal Ids should match", principalIdToTest, kimEntityDefaultInfo.getPrincipals().get(0).getPrincipalId());
225 		}
226 	}
227 
228 	@Test
229 	public void testLookupEntityInfo() {
230 		String principalIdToTest = "p1";
231 		List<Entity> results = identityService.findEntities(setUpEntityLookupCriteria(principalIdToTest)).getResults();
232 		assertNotNull("Lookup results should never be null", results);
233 		assertEquals("Lookup result count is invalid", 1, results.size());
234 		for (Entity kimEntityInfo : results) {
235 			assertEquals("Entity should have only one principal for this test", 1, kimEntityInfo.getPrincipals().size());
236 			assertEquals("Principal Ids should match", principalIdToTest, kimEntityInfo.getPrincipals().get(0).getPrincipalId());
237 		}
238 	}
239 
240     @Test
241     public void testGetEntityWithNameChangeDate() {
242         String principalName = "testuser7";
243         Entity info = identityService.getEntityByPrincipalName(principalName);
244         List<EntityName> names = info.getNames();
245         for (EntityName name : names) {
246             assertNotNull("nameChangeDate should have been set for PrincipalName " + principalName,name.getNameChangedDate());
247         }
248     }
249 
250     protected IdentityService findIdSvc() throws Exception {
251 		return (IdentityService) GlobalResourceLoader.getService(
252                 new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.IDENTITY_SERVICE_SOAP));
253 	}
254 
255 	protected void setIdentityService(IdentityService idSvc) {
256 		this.identityService = idSvc;
257 	}
258 }