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;
17  
18  import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
19  import org.kuali.rice.core.api.lifecycle.Lifecycle;
20  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.kim.api.type.KimType;
23  import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.test.BaselineTestCase;
26  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
27  import org.kuali.rice.test.BaselineTestCase.Mode;
28  import org.kuali.rice.test.SQLDataLoader;
29  import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
30  
31  import javax.xml.namespace.QName;
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  import static org.junit.Assert.fail;
38  
39  /**
40   * This is test base that should be used for all KIM unit tests. All non-web unit tests for KIM should extend this base
41   * class.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @BaselineMode(Mode.ROLLBACK_CLEAR_DB)
46  public abstract class KIMTestCase extends BaselineTestCase {
47  
48  	private static final String KIM_MODULE_NAME = "kim";
49  	
50  	public KIMTestCase() {
51  		super(KIM_MODULE_NAME);
52  	}
53  	
54  	@Override
55  	protected List<Lifecycle> getSuiteLifecycles() {
56  		List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
57  		suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.xml"));
58  		return suiteLifecycles;
59  	}
60  	
61  	@Override
62  	protected void loadSuiteTestData() throws Exception {
63  		super.loadSuiteTestData();
64  		new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.sql", "/").runSql();
65  		new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularRolesTestData.sql", "/").runSql();
66  		new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularGroupsTestData.sql", "/").runSql();
67  	}
68  	
69  	@Override
70  	protected Lifecycle getLoadApplicationLifecycle() {
71      	SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KIMTestHarnessApplicationResourceLoader"), "classpath:KIMTestHarnessSpringBeans.xml", null);
72      	springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
73      	return springResourceLoader;
74  	}
75  	
76  	/**
77  	 * Override the standard per-test lifecycles to prepend ClearDatabaseLifecycle and ClearCacheLifecycle
78  	 * @see org.kuali.rice.test.RiceTestCase#getPerTestLifecycles()
79  	 */
80  	@Override
81  	protected List<Lifecycle> getPerTestLifecycles() {
82  		List<Lifecycle> lifecycles = super.getPerTestLifecycles();
83  		lifecycles.add(new ClearCacheLifecycle());
84  		return lifecycles;
85  	}
86  	
87  	public class ClearCacheLifecycle extends BaseLifecycle {
88  		@Override
89  		public void stop() throws Exception {
90  			//KimApiServiceLocator.getIdentityManagementService().flushAllCaches();
91  			//KimApiServiceLocator.getRoleService().flushRoleCaches();
92  			super.stop();
93  		}
94  
95  	}
96  	
97  	protected List<String> getPerTestTablesNotToClear() {
98  		List<String> tablesNotToClear = new ArrayList<String>();
99  		tablesNotToClear.add("KRIM_.*");
100 		tablesNotToClear.add("KRNS_.*");
101         tablesNotToClear.add("KRCR_.*");
102         tablesNotToClear.add("KREW_.*");
103 		return tablesNotToClear;
104 	}
105 
106 
107 	/**
108      * @see org.kuali.rice.test.RiceTestCase#getModuleName()
109      */
110 	@Override
111 	protected String getModuleName() {
112 		return KIM_MODULE_NAME;
113 	}
114 	
115 	protected KimType getDefaultKimType() {
116 		KimType type = KimApiServiceLocator.getKimTypeInfoService().getKimType("1");
117 		if (type == null) {
118 			fail("Failed to locate the default Kim Type.");
119 		}
120 		return type;
121 	}
122 	
123 	protected PermissionTemplateBo getDefaultPermissionTemplate() {
124 		Map<String, Object> fieldValues = new HashMap<String, Object>();
125 		fieldValues.put("namespaceCode", "KUALI");
126 		fieldValues.put("name", "Default");
127 		PermissionTemplateBo template = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PermissionTemplateBo.class, fieldValues);
128 		if (template == null) {
129 			fail("Failed to locate the default Permission Template.");
130 		}
131 		return template;
132 	}
133 	
134 	protected String getNewRoleId() {
135 		return getIdFromSequence("KRIM_ROLE_ID_S");
136 	}
137 	
138 	protected String getNewRoleMemberId() {
139 		return getIdFromSequence("KRIM_ROLE_MBR_ID_S");
140 	}
141 	
142 	protected String getNewRolePermissionId() {
143 		return getIdFromSequence("KRIM_ROLE_ID_S");
144 	}
145 	
146 	protected String getIdFromSequence(String sequenceName) {
147 		Long sequenceId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(sequenceName);
148 		return "" + sequenceId;
149     }
150 }