Search in sources :

Example 1 with CxfFunctionGuardInterceptor

use of org.jaffa.rules.interceptors.CxfFunctionGuardInterceptor in project jaffa-framework by jaffa-projects.

the class CxfFunctionGuardInterceptorTest method testInterceptor.

@Test
public void testInterceptor() throws NoSuchMethodException, FrameworkException, ApplicationExceptions {
    // Mock the security manager
    PowerMockito.mockStatic(SecurityManager.class);
    PowerMockito.when(SecurityManager.checkFunctionAccess("Test")).thenReturn(true);
    // Mock the Rule helper factory and the rule it returns
    mockStatic(RuleHelperFactory.class);
    IRuleHelper ruleHelperFactory = mock(IRuleHelper.class);
    when(RuleHelperFactory.instance(Mockito.anyString())).thenReturn(ruleHelperFactory);
    // Mock the metadata repository
    mockStatic(MetaDataRepository.class);
    MetaDataRepository repository = mock(MetaDataRepository.class);
    when(MetaDataRepository.instance()).thenReturn(repository);
    // Create a real instance of an interceptor
    CxfFunctionGuardInterceptor interceptor = new CxfFunctionGuardInterceptor();
    // Successful access - matching method and name
    updateRules("Test", "Test", repository, ruleHelperFactory);
    interceptor.handleMessage(getMockMessage());
    // Failed access - matching method not valid name
    Fault fault = null;
    try {
        updateRules("Fail", "Test", repository, ruleHelperFactory);
        interceptor.handleMessage(getMockMessage());
    } catch (Fault f) {
        fault = f;
    }
    Assert.assertNotNull("Fault should have been thrown du to failed access", fault);
    // Successful access - not matching method, so rules not enforced
    updateRules("xxxxx", "NoMatch", repository, ruleHelperFactory);
    interceptor.handleMessage(getMockMessage());
}
Also used : CxfFunctionGuardInterceptor(org.jaffa.rules.interceptors.CxfFunctionGuardInterceptor) IRuleHelper(org.jaffa.rules.rulemeta.IRuleHelper) MetaDataRepository(org.jaffa.rules.meta.MetaDataRepository) Fault(org.apache.cxf.interceptor.Fault) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Fault (org.apache.cxf.interceptor.Fault)1 CxfFunctionGuardInterceptor (org.jaffa.rules.interceptors.CxfFunctionGuardInterceptor)1 MetaDataRepository (org.jaffa.rules.meta.MetaDataRepository)1 IRuleHelper (org.jaffa.rules.rulemeta.IRuleHelper)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1