1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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          
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 
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 }