View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.util;
17  
18  import org.junit.AfterClass;
19  import org.junit.Before;
20  import org.junit.BeforeClass;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.kuali.rice.core.api.CoreConstants;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.core.api.resourceloader.ResourceLoader;
27  import org.kuali.rice.core.framework.config.property.SimpleConfig;
28  import org.kuali.rice.krad.data.metadata.MetadataRepository;
29  import org.kuali.rice.krad.service.DataDictionaryService;
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.test.context.ContextConfiguration;
32  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33  
34  import javax.xml.namespace.QName;
35  
36  import static org.junit.Assert.assertTrue;
37  import static org.mockito.Mockito.mock;
38  import static org.mockito.Mockito.when;
39  
40  /**
41   * Tests LegacyDetectionAdvice
42   */
43  @RunWith(SpringJUnit4ClassRunner.class)
44  @ContextConfiguration
45  public class LegacyDetectionAdviceTest {
46      @LegacyDataFramework
47      public static class LegacyService {
48          public boolean performDeprecatedAction() { return true; }
49      }
50      public static class NonLegacyService {
51          public boolean performAction() { return true; }
52      }
53  
54      @Autowired
55      LegacyService legacyService;
56      @Autowired
57      NonLegacyService nonLegacyService;
58  
59      @BeforeClass
60      public static void initGrl() throws Exception {
61  
62          ConfigContext.init(new SimpleConfig());
63          ConfigContext.getCurrentContextConfig().putProperty(CoreConstants.Config.APPLICATION_ID, LegacyUtilsTest.class.getName());
64  
65          // have to mock these because LegacyDetectionAdvice is calling LegacyUtils
66          MetadataRepository metadataRepository = mock(MetadataRepository.class);
67          DataDictionaryService dataDictionaryService = mock(DataDictionaryService.class);
68          ResourceLoader resourceLoader = mock(ResourceLoader.class);
69          when(resourceLoader.getName()).thenReturn(QName.valueOf(LegacyUtilsTest.class.getName()));
70          when(resourceLoader.getService(QName.valueOf("metadataRepository"))).thenReturn(metadataRepository);
71          when(resourceLoader.getService(QName.valueOf("dataDictionaryService"))).thenReturn(dataDictionaryService);
72          GlobalResourceLoader.addResourceLoader(resourceLoader);
73          GlobalResourceLoader.start();
74      }
75  
76      @AfterClass
77      public static void stopGrl() throws Exception {
78          GlobalResourceLoader.stop();
79      }
80  
81      @Before
82      public void resetConfiguration() {
83  
84          ConfigContext.getCurrentContextConfig().removeProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK);
85      }
86  
87      @Test
88      public void testNonLegacyServiceAlwaysAllowed() {
89          assertTrue(nonLegacyService.performAction());
90          ConfigContext.getCurrentContextConfig().putProperty(KRADConstants.Config.ENABLE_LEGACY_DATA_FRAMEWORK, "true");
91          assertTrue(nonLegacyService.performAction());
92      }
93  
94      @Test(expected=IllegalStateException.class)
95      public void testLegacyServiceInvocationAllowedWhenEnabled() {
96          assertTrue(nonLegacyService.performAction());
97          legacyService.performDeprecatedAction();
98      }
99  
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 }