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