001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.kim.test;
017    
018    import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
019    import org.kuali.rice.core.api.lifecycle.Lifecycle;
020    import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
021    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
022    import org.kuali.rice.kim.api.type.KimType;
023    import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
024    import org.kuali.rice.krad.service.KRADServiceLocator;
025    import org.kuali.rice.test.BaselineTestCase;
026    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
027    import org.kuali.rice.test.BaselineTestCase.Mode;
028    import org.kuali.rice.test.SQLDataLoader;
029    import org.kuali.rice.test.lifecycles.KEWXmlDataLoaderLifecycle;
030    
031    import javax.xml.namespace.QName;
032    import java.util.ArrayList;
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Map;
036    
037    import static org.junit.Assert.fail;
038    
039    /**
040     * This is test base that should be used for all KIM unit tests. All non-web unit tests for KIM should extend this base
041     * class.
042     *
043     * @author Kuali Rice Team (rice.collab@kuali.org)
044     */
045    @BaselineMode(Mode.ROLLBACK_CLEAR_DB)
046    public abstract class KIMTestCase extends BaselineTestCase {
047    
048            private static final String KIM_MODULE_NAME = "kim";
049            
050            public KIMTestCase() {
051                    super(KIM_MODULE_NAME);
052            }
053            
054            @Override
055            protected List<Lifecycle> getSuiteLifecycles() {
056                    List<Lifecycle> suiteLifecycles = super.getSuiteLifecycles();
057                    suiteLifecycles.add(new KEWXmlDataLoaderLifecycle("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.xml"));
058                    return suiteLifecycles;
059            }
060            
061            @Override
062            protected void loadSuiteTestData() throws Exception {
063                    super.loadSuiteTestData();
064                    new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteTestData.sql", "/").runSql();
065                    new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularRolesTestData.sql", "/").runSql();
066                    new SQLDataLoader("classpath:org/kuali/rice/kim/test/CircularGroupsTestData.sql", "/").runSql();
067            new SQLDataLoader("classpath:org/kuali/rice/kim/test/DefaultSuiteLDAPTestData.sql", "/").runSql();
068            }
069            
070            @Override
071            protected Lifecycle getLoadApplicationLifecycle() {
072            SpringResourceLoader springResourceLoader = new SpringResourceLoader(new QName("KIMTestHarnessApplicationResourceLoader"), "classpath:KIMTestHarnessSpringBeans.xml", null);
073            springResourceLoader.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
074            return springResourceLoader;
075            }
076            
077            /**
078             * Override the standard per-test lifecycles to prepend ClearDatabaseLifecycle and ClearCacheLifecycle
079             * @see org.kuali.rice.test.RiceTestCase#getPerTestLifecycles()
080             */
081            @Override
082            protected List<Lifecycle> getPerTestLifecycles() {
083                    List<Lifecycle> lifecycles = super.getPerTestLifecycles();
084                    lifecycles.add(new ClearCacheLifecycle());
085                    return lifecycles;
086            }
087            
088            public class ClearCacheLifecycle extends BaseLifecycle {
089                    @Override
090                    public void stop() throws Exception {
091                            //KimApiServiceLocator.getIdentityManagementService().flushAllCaches();
092                            //KimApiServiceLocator.getRoleService().flushRoleCaches();
093                            super.stop();
094                    }
095    
096            }
097            
098            protected List<String> getPerTestTablesNotToClear() {
099                    List<String> tablesNotToClear = new ArrayList<String>();
100                    tablesNotToClear.add("KRIM_.*");
101                    tablesNotToClear.add("KRNS_.*");
102            tablesNotToClear.add("KRCR_.*");
103            tablesNotToClear.add("KREW_.*");
104                    return tablesNotToClear;
105            }
106    
107    
108            /**
109         * @see org.kuali.rice.test.RiceTestCase#getModuleName()
110         */
111            @Override
112            protected String getModuleName() {
113                    return KIM_MODULE_NAME;
114            }
115            
116            protected KimType getDefaultKimType() {
117                    KimType type = KimApiServiceLocator.getKimTypeInfoService().getKimType("1");
118                    if (type == null) {
119                            fail("Failed to locate the default Kim Type.");
120                    }
121                    return type;
122            }
123            
124            protected PermissionTemplateBo getDefaultPermissionTemplate() {
125                    Map<String, Object> fieldValues = new HashMap<String, Object>();
126                    fieldValues.put("namespaceCode", "KUALI");
127                    fieldValues.put("name", "Default");
128                    PermissionTemplateBo template = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PermissionTemplateBo.class, fieldValues);
129                    if (template == null) {
130                            fail("Failed to locate the default Permission Template.");
131                    }
132                    return template;
133            }
134            
135            protected String getNewRoleId() {
136                    return getIdFromSequence("KRIM_ROLE_ID_S");
137            }
138            
139            protected String getNewRoleMemberId() {
140                    return getIdFromSequence("KRIM_ROLE_MBR_ID_S");
141            }
142            
143            protected String getNewRolePermissionId() {
144                    return getIdFromSequence("KRIM_ROLE_ID_S");
145            }
146            
147            protected String getIdFromSequence(String sequenceName) {
148                    Long sequenceId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(sequenceName);
149                    return "" + sequenceId;
150        }
151    }