View Javadoc

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