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     */
016    package org.kuali.rice.krad.util;
017    
018    import org.junit.AfterClass;
019    import org.junit.Before;
020    import org.junit.BeforeClass;
021    import org.junit.Test;
022    import org.junit.runner.RunWith;
023    import org.kuali.rice.core.api.CoreConstants;
024    import org.kuali.rice.core.api.config.property.ConfigContext;
025    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
026    import org.kuali.rice.core.api.resourceloader.ResourceLoader;
027    import org.kuali.rice.core.framework.config.property.SimpleConfig;
028    import org.kuali.rice.krad.data.metadata.MetadataRepository;
029    import org.kuali.rice.krad.service.DataDictionaryService;
030    import org.springframework.beans.factory.annotation.Autowired;
031    import org.springframework.test.context.ContextConfiguration;
032    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
033    
034    import javax.xml.namespace.QName;
035    
036    import static org.junit.Assert.assertTrue;
037    import static org.mockito.Mockito.mock;
038    import static org.mockito.Mockito.when;
039    
040    /**
041     * Tests LegacyDetectionAdvice
042     */
043    @RunWith(SpringJUnit4ClassRunner.class)
044    @ContextConfiguration
045    public class LegacyDetectionAdviceTest {
046        @LegacyDataFramework
047        public static class LegacyService {
048            public boolean performDeprecatedAction() { return true; }
049        }
050        public static class NonLegacyService {
051            public boolean performAction() { return true; }
052        }
053    
054        @Autowired
055        LegacyService legacyService;
056        @Autowired
057        NonLegacyService nonLegacyService;
058    
059        @BeforeClass
060        public static void initGrl() throws Exception {
061    
062            ConfigContext.init(new SimpleConfig());
063            ConfigContext.getCurrentContextConfig().putProperty(CoreConstants.Config.APPLICATION_ID, LegacyUtilsTest.class.getName());
064    
065            // have to mock these because LegacyDetectionAdvice is calling LegacyUtils
066            MetadataRepository metadataRepository = mock(MetadataRepository.class);
067            DataDictionaryService dataDictionaryService = mock(DataDictionaryService.class);
068            ResourceLoader resourceLoader = mock(ResourceLoader.class);
069            when(resourceLoader.getName()).thenReturn(QName.valueOf(LegacyUtilsTest.class.getName()));
070            when(resourceLoader.getService(QName.valueOf("kd-metadataRepository"))).thenReturn(metadataRepository);
071            when(resourceLoader.getService(QName.valueOf("dataDictionaryService"))).thenReturn(dataDictionaryService);
072            GlobalResourceLoader.addResourceLoader(resourceLoader);
073            GlobalResourceLoader.start();
074        }
075    
076        @AfterClass
077        public static void stopGrl() throws Exception {
078            GlobalResourceLoader.stop();
079        }
080    
081        @Before
082        public void resetConfiguration() {
083    
084            ConfigContext.getCurrentContextConfig().removeProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK);
085        }
086    
087        @Test
088        public void testNonLegacyServiceAlwaysAllowed() {
089            assertTrue(nonLegacyService.performAction());
090            ConfigContext.getCurrentContextConfig().putProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK, "true");
091            assertTrue(nonLegacyService.performAction());
092        }
093    
094        @Test(expected=IllegalStateException.class)
095        public void testLegacyServiceInvocationAllowedWhenEnabled() {
096            assertTrue(nonLegacyService.performAction());
097            legacyService.performDeprecatedAction();
098        }
099    
100        @Test // no exception is thrown
101        public void testLegacyServiceInvocationDisallowedWhenDisabled() {
102            ConfigContext.getCurrentContextConfig().putProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK, "true");
103            assertTrue(nonLegacyService.performAction());
104            assertTrue(legacyService.performDeprecatedAction());
105        }
106    }