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 testGetPrincipals() {
73          List<String> principalIds = new ArrayList<String>();
74          principalIds.add("KULUSER");
75          List<Principal> validPrincipals = identityService.getPrincipals(principalIds);
76          assertNotNull("validPrincipals must not be null", validPrincipals);
77          assertEquals("validPrincipals name did not match expected result","kuluser", validPrincipals.get(0).getPrincipalName());
78      }
79  
80  	@Test
81  	public void testGetPrincipalByPrincipalName() {
82  		Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
83  		assertNotNull("principal must not be null", principal);
84  		assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
85  	}
86  	
87  	@Test
88  	public void testGetDefaultEntityByPrincipalId() {
89  		String principalId = "KULUSER";
90  		EntityDefault info = identityService.getEntityDefaultByPrincipalId(principalId);
91  		assertNotNull("entity must not be null", info);
92  		assertNotNull("entity principals must not be null", info.getPrincipals());
93  		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
94  		for (Principal principalInfo : info.getPrincipals()) {
95  			assertEquals("Wrong principal id", principalId, principalInfo.getPrincipalId());
96  		}
97  		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
98  	}
99  
100 	@Test
101 	public void testGetDefaultEntityByPrincipalName() {
102 		String principalName = "kuluser";
103 		EntityDefault info = identityService.getEntityDefaultByPrincipalName(principalName);
104 		assertNotNull("entity must not be null", info);
105 		assertNotNull("entity principals must not be null", info.getPrincipals());
106 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
107 		for (Principal principalInfo : info.getPrincipals()) {
108 			assertEquals("Wrong principal name", principalName, principalInfo.getPrincipalName());
109 		}
110 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
111 	}
112 
113 	@Test
114 	public void testGetEntityByPrincipalId() {
115 		String principalId = "KULUSER";
116 		Entity info = identityService.getEntityByPrincipalId(principalId);
117 		assertNotNull("entity must not be null", info);
118 		assertNotNull("entity principals must not be null", info.getPrincipals());
119 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
120 		for (Principal principalInfo : info.getPrincipals()) {
121 			assertEquals("Wrong principal id", principalId, principalInfo.getPrincipalId());
122 		}
123 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
124 		assertTrue("entity residencies must not be null", (info.getResidencies() == null) || info.getResidencies().isEmpty());
125 	}
126 
127 	@Test
128 	public void testGetEntityByPrincipalName() {
129 		String principalName = "kuluser";
130 		Entity info = identityService.getEntityByPrincipalName(principalName);
131 		assertNotNull("entity must not be null", info);
132 		assertNotNull("entity principals must not be null", info.getPrincipals());
133 		assertEquals("entity must have exactly 1 principal", 1, info.getPrincipals().size());
134 		for (Principal principalInfo : info.getPrincipals()) {
135 			assertEquals("Wrong principal name", principalName, principalInfo.getPrincipalName());
136 		}
137 		assertTrue("entity external identifiers must not be null", (info.getExternalIdentifiers() == null) || info.getExternalIdentifiers().isEmpty());
138 		assertTrue("entity residencies must not be null", (info.getResidencies() == null) || info.getResidencies().isEmpty());
139 	}
140 
141 	@Test
142 	public void testGetContainedAttributes() {
143 		Principal principal = identityService.getPrincipal("p1");
144 		
145 		EntityDefault entity = identityService.getEntityDefault(principal.getEntityId());
146 		assertNotNull( "Entity Must not be null", entity );
147 		EntityTypeContactInfoDefault eet = entity.getEntityType( "PERSON" );
148 		assertNotNull( "PERSON EntityTypeData must not be null", eet );
149 		assertNotNull( "EntityEntityType's default email address must not be null", eet.getDefaultEmailAddress() );
150 		assertEquals( "p1@kuali.org", eet.getDefaultEmailAddress().getEmailAddressUnmasked() );
151 	}
152 
153 	protected QueryByCriteria setUpEntityLookupCriteria(String principalId) {
154 		PersonServiceImpl personServiceImpl = (PersonServiceImpl) KIMServiceLocatorInternal.getService(KimApiServiceLocator.KIM_PERSON_SERVICE);
155 		Map<String,String> criteria = new HashMap<String,String>(1);
156 		criteria.put(KIMPropertyConstants.Person.PRINCIPAL_ID, principalId);
157 		Map<String, String> entityCriteria = personServiceImpl.convertPersonPropertiesToEntityProperties(criteria);
158         entityCriteria.put("entityTypeContactInfos.entityTypeCode", "PERSON");
159         QueryByCriteria.Builder query = QueryByCriteria.Builder.create();
160         List<Predicate> predicates = new ArrayList<Predicate>();
161         for (String key : entityCriteria.keySet()) {
162             predicates.add(like(key, entityCriteria.get(key)));
163         }
164         if (!predicates.isEmpty()) {
165             query.setPredicates(and(predicates.toArray(new Predicate[] {})));
166         }
167         return query.build();
168 	}
169 
170 	@Test
171 	public void testLookupEntityDefaultInfo() {
172 		String principalIdToTest = "p1";
173 		EntityDefaultQueryResults qr = identityService.findEntityDefaults(setUpEntityLookupCriteria(principalIdToTest));
174         List<EntityDefault> results = qr.getResults();
175 		assertNotNull("Lookup results should never be null", results);
176 		assertEquals("Lookup result count is invalid", 1, results.size());
177 		for (EntityDefault kimEntityDefaultInfo : results) {
178 			assertEquals("Entity should have only one principal for this test", 1, kimEntityDefaultInfo.getPrincipals().size());
179 			assertEquals("Principal Ids should match", principalIdToTest, kimEntityDefaultInfo.getPrincipals().get(0).getPrincipalId());
180 		}
181 	}
182 
183 	@Test
184 	public void testLookupEntityInfo() {
185 		String principalIdToTest = "p1";
186 		List<Entity> results = identityService.findEntities(setUpEntityLookupCriteria(principalIdToTest)).getResults();
187 		assertNotNull("Lookup results should never be null", results);
188 		assertEquals("Lookup result count is invalid", 1, results.size());
189 		for (Entity kimEntityInfo : results) {
190 			assertEquals("Entity should have only one principal for this test", 1, kimEntityInfo.getPrincipals().size());
191 			assertEquals("Principal Ids should match", principalIdToTest, kimEntityInfo.getPrincipals().get(0).getPrincipalId());
192 		}
193 	}
194 
195     @Test
196     public void testGetEntityWithNameChangeDate() {
197         String principalName = "testuser7";
198         Entity info = identityService.getEntityByPrincipalName(principalName);
199         List<EntityName> names = info.getNames();
200         for (EntityName name : names) {
201             assertNotNull("nameChangeDate should have been set for PrincipalName " + principalName,name.getNameChangedDate());
202         }
203     }
204 
205     protected IdentityService findIdSvc() throws Exception {
206 		return (IdentityService) GlobalResourceLoader.getService(
207                 new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.IDENTITY_SERVICE_SOAP));
208 	}
209 
210 	protected void setIdentityService(IdentityService idSvc) {
211 		this.identityService = idSvc;
212 	}
213 }