View Javadoc

1   /*
2    * Copyright 2007-2010 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.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
20  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
21  import org.kuali.rice.kim.api.type.KimType;
22  import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
23  import org.kuali.rice.kim.framework.type.KimTypeService;
24  import org.kuali.rice.kim.test.KIMTestCase;
25  import org.kuali.rice.kim.util.KimConstants;
26  import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceBase;
27  
28  import javax.xml.namespace.QName;
29  
30  import static org.junit.Assert.*;
31  
32  /**
33   * Tests the {@link org.kuali.rice.kim.impl.services.KIMServiceLocatorInternal} class.
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class KIMServiceLocatorInternalTest extends KIMTestCase {
39  
40  	@Test
41  	public void testGetKimTypeService_KimType() {
42  		
43  		// test by passing null
44  		try {
45  		    KimTypeService typeService1 = KimFrameworkServiceLocator.getKimTypeService((KimType) null);
46              assertTrue("should have thrown exception and never got here", false);
47          } catch (IllegalArgumentException e) {
48          }
49  		
50  		// test by passing a KimType with a null service name
51  		
52  		KimType.Builder nullKimType = KimType.Builder.create();
53  		nullKimType.setServiceName(null);
54  		KimTypeService typeService2 = KimFrameworkServiceLocator.getKimTypeService(nullKimType.build());
55  		assertNotNull("type service shoudl have been found", typeService2);
56  		assertEquals("should be the default kim type", DataDictionaryTypeServiceBase.class, typeService2.getClass());
57  		
58  		// test by passing a KimType with an empty service name
59  		
60  		KimType.Builder emptyKimType = KimType.Builder.create();
61  		nullKimType.setServiceName("");
62  		KimTypeService typeService3 = KimFrameworkServiceLocator.getKimTypeService(emptyKimType.build());
63  		assertNotNull("type service should have been found", typeService3);
64  		assertEquals("should be the default kim type", DataDictionaryTypeServiceBase.class, typeService3.getClass());
65  		
66  		// test by passing a KimType that refers to the Permission TypeService
67  		
68  		KimType permissionKimType = KimApiServiceLocator.getKimTypeInfoService().findKimTypeByNameAndNamespace(KimConstants.NAMESPACE_CODE, "Permission");
69  		assertNotNull("The KR-IDM:Permission KimType should exist.", permissionKimType);
70  		
71  		KimTypeService typeService4 = KimFrameworkServiceLocator.getKimTypeService(permissionKimType);
72  		assertNotNull("type service should have been found", typeService4);
73  		
74  	}
75  	
76  	@Test
77  	public void testGetKimTypeService_QName() {
78  		
79  		// test by passing null
80  		
81  		try {
82  			KimFrameworkServiceLocator.getKimTypeService((QName) null);
83  			fail("getKimTypeService with a null QName should have thrown an IllegalArgumentException");
84  		} catch (IllegalArgumentException e) {}
85  		
86  		// test by passing an invalid QName
87  		
88  		KimTypeService typeService1 = KimFrameworkServiceLocator.getKimTypeService(new QName("badNamespace", "badServiceName"));
89  		assertNull("A null KimTypeService should have been returned.", typeService1);
90  		
91  		// test by passing a QName for a valid service, but not one which is a KimTypeService, null should be returned
92  		
93  		// fetch the group service instead
94  		KimTypeService typeService2 = KimFrameworkServiceLocator.getKimTypeService(new QName(KimApiServiceLocator.KIM_GROUP_SERVICE));
95  		assertNull("A null KimTypeService should have been returned.", typeService2);
96  
97  		// test by passing the QName for the Permission TypeService
98  		
99  		QName permissionServiceName = new QName("permissionPermissionTypeService");
100 		KimTypeService typeService3 = KimFrameworkServiceLocator.getKimTypeService(permissionServiceName);
101 		assertNotNull("permission type service should have been found", typeService3);
102 		
103 	}
104 	
105 }