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