001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.test.service;
017
018import org.junit.Test;
019import org.kuali.rice.core.api.criteria.Predicate;
020import org.kuali.rice.core.api.criteria.QueryByCriteria;
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.kim.api.KimApiConstants;
023import org.kuali.rice.kim.api.identity.IdentityService;
024import org.kuali.rice.kim.api.identity.entity.Entity;
025import org.kuali.rice.kim.api.identity.entity.EntityDefault;
026import org.kuali.rice.kim.api.identity.entity.EntityDefaultQueryResults;
027import org.kuali.rice.kim.api.identity.name.EntityName;
028import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
029import org.kuali.rice.kim.api.identity.principal.Principal;
030import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
031import org.kuali.rice.kim.api.services.KimApiServiceLocator;
032import org.kuali.rice.kim.impl.KIMPropertyConstants;
033import org.kuali.rice.kim.impl.identity.PersonServiceImpl;
034import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
035import org.kuali.rice.kim.test.KIMTestCase;
036import org.kuali.rice.test.BaselineTestCase;
037
038import javax.xml.namespace.QName;
039import java.util.ArrayList;
040import java.util.HashMap;
041import java.util.List;
042import java.util.Map;
043
044import static org.junit.Assert.*;
045import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
046import static org.kuali.rice.core.api.criteria.PredicateFactory.like;
047
048/**
049 * 
050 * @author Kuali Rice Team (rice.collab@kuali.org)
051 */
052@BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
053public class IdentityServiceTest extends KIMTestCase {
054
055        private IdentityService identityService;
056
057        public void setUp() throws Exception {
058                super.setUp();
059                if (null == identityService) {
060                        identityService = findIdSvc();
061                }
062        }
063        
064        @Test
065        public void testGetPrincipal() {
066                Principal principal = identityService.getPrincipal("KULUSER");
067                assertNotNull("principal must not be null", principal);
068                assertEquals("Principal name did not match expected result","kuluser", principal.getPrincipalName());
069        }
070
071    @Test
072    public void testGetPrincipalNotFound() {
073        Principal principal = identityService.getPrincipal("DoesNotExist");
074        assertNull("principal should not be found", principal);
075    }
076
077    @Test
078    public void testGetPrincipals() {
079        List<String> principalIds = new ArrayList<String>();
080        principalIds.add("KULUSER");
081        List<Principal> validPrincipals = identityService.getPrincipals(principalIds);
082        assertNotNull("validPrincipals must not be null", validPrincipals);
083        assertEquals("validPrincipals name did not match expected result","kuluser", validPrincipals.get(0).getPrincipalName());
084    }
085
086        @Test
087        public void testGetPrincipalByPrincipalName() {
088                Principal principal = identityService.getPrincipalByPrincipalName("kuluser");
089                assertNotNull("principal must not be null", principal);
090                assertEquals("Principal ID did not match expected result","KULUSER", principal.getPrincipalId());
091        }
092
093    @Test
094    public void testGetPrincipalByPrincipalNameNotFound() {
095        Principal principal = identityService.getPrincipalByPrincipalName("DoesNotExist");
096        assertNull("principal should not be found", principal);
097    }
098
099    @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}