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.krad.bo;
017
018import org.junit.Before;
019import org.junit.Test;
020import org.junit.runner.RunWith;
021import org.kuali.rice.core.api.CoreConstants;
022import org.kuali.rice.core.api.config.property.ConfigContext;
023import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
024import org.kuali.rice.core.api.resourceloader.ResourceLoader;
025import org.kuali.rice.core.framework.config.property.SimpleConfig;
026import org.kuali.rice.core.framework.resourceloader.BeanFactoryResourceLoader;
027import org.kuali.rice.krad.bo.test.TestEBOInterface;
028import org.kuali.rice.krad.data.provider.PersistenceProvider;
029import org.kuali.rice.krad.data.provider.Provider;
030import org.kuali.rice.krad.data.provider.ProviderRegistry;
031import org.kuali.rice.krad.service.KRADServiceLocator;
032import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
033import org.kuali.rice.krad.service.KualiModuleService;
034import org.kuali.rice.krad.service.ModuleService;
035import org.kuali.rice.krad.service.impl.ModuleServiceBase;
036import org.mockito.Mock;
037import org.mockito.runners.MockitoJUnitRunner;
038import org.springframework.beans.factory.support.StaticListableBeanFactory;
039
040import javax.xml.namespace.QName;
041import java.util.ArrayList;
042import java.util.Arrays;
043import java.util.List;
044
045import static org.junit.Assert.assertFalse;
046import static org.junit.Assert.assertTrue;
047import static org.mockito.Mockito.mock;
048import static org.mockito.Mockito.when;
049
050/**
051 * Tests ModulesService base implementation
052 */
053@RunWith(MockitoJUnitRunner.class)
054public class ModuleServiceBaseTest {
055    // concrete EBO class that is NOT in the package prefix
056    // but the interface it implements IS
057    private static class ModuleServiceBaseTestEBO implements TestEBOInterface {
058        @Override public void refresh() {}
059    }
060
061    // local EBO interface outside of package prefixes
062    private static interface ModuleServiceBaseTestEBOInterface extends TestEBOInterface {
063    }
064
065    private static class DataObject {}
066
067    private ModuleServiceBase module = new ModuleServiceBase();
068    private ModuleConfiguration config = new ModuleConfiguration();
069    @Mock
070    private KualiModuleService kualiModuleService;
071    private List<ModuleService> installedModuleServices = new ArrayList<ModuleService>();
072
073    @Before
074    public void initGRL() throws Exception {
075        GlobalResourceLoader.stop();
076        SimpleConfig config = new SimpleConfig();
077        config.putProperty(CoreConstants.Config.APPLICATION_ID, "APPID");
078        ConfigContext.init(config);
079
080        StaticListableBeanFactory testBf = new StaticListableBeanFactory();
081        when(kualiModuleService.getInstalledModuleServices()).thenReturn(installedModuleServices);
082
083        testBf.addBean(KRADServiceLocator.PROVIDER_REGISTRY, mock(ProviderRegistry.class));
084        testBf.addBean(KRADServiceLocatorWeb.KUALI_MODULE_SERVICE, kualiModuleService);
085
086        ResourceLoader rl = new BeanFactoryResourceLoader(new QName("moduleservicebase-unittest"), testBf);
087        GlobalResourceLoader.addResourceLoader(rl);
088        GlobalResourceLoader.start();
089    }
090
091    @Before
092    public void setup() {
093        config.setNamespaceCode("moduleconfiguration-unittest");
094        config.setDatabaseRepositoryFilePaths(Arrays.asList(new String[]{"path1", "path2"}));
095        config.setDataDictionaryPackages(Arrays.asList(new String[]{"ddpackage1", "ddpackage2"}));
096
097        // set package prefix to a nested package
098        config.setPackagePrefixes(Arrays.asList(new String[]{ TestEBOInterface.class.getPackage().getName(), getClass().getPackage().getName() }));
099
100        PersistenceProvider pp = mock(PersistenceProvider.class);
101        when(pp.handles(DataObject.class)).thenReturn(true);
102        config.setProviders(Arrays.asList(new Provider[] { pp }));
103
104        module.setModuleConfiguration(config);
105    }
106
107    @Test
108    public void testRegistersWithKualiModuleService() throws Exception {
109        module.afterPropertiesSet();
110        assertTrue(installedModuleServices.contains(module));
111    }
112
113    @Test
114    public void testNotResponsibleForNullClass() {
115        assertFalse(module.isResponsibleFor(null));
116    }
117
118    @Test
119    public void testIsResponsibleForClassByPackagePrefix() {
120        assertTrue(module.isResponsibleFor(TestEBOInterface.class));
121    }
122
123    @Test
124    public void testIsResponsibleForClassByProvider() {
125        assertTrue(module.isResponsibleFor(DataObject.class));
126    }
127
128    @Test
129    public void testIsResponsibleForEBOSubclass() {
130        assertTrue(module.isResponsibleFor(ModuleServiceBaseTestEBO.class));
131    }
132
133    @Test
134    public void testIsResponsibleForEBOInterface() {
135        assertTrue(module.isResponsibleFor(ModuleServiceBaseTestEBOInterface.class));
136    }
137}