Search in sources :

Example 1 with Session

use of net.sf.sahi.session.Session in project sakuli by ConSol.

the class ModifySahiTimerAspect method modifySahiTimer.

void modifySahiTimer(JoinPoint joinPoint, boolean beforeMethod) {
    BaseActionLoader loader = BeanLoader.loadBaseActionLoader();
    SahiProxyProperties sahiProxyProperties = loader.getSahiProxyProperties();
    if (sahiProxyProperties != null && sahiProxyProperties.isRequestDelayActive()) {
        RhinoScriptRunner rhinoScriptRunner = loader.getRhinoScriptRunner();
        if (rhinoScriptRunner != null && rhinoScriptRunner.getSession() != null) {
            getLogger(joinPoint).debug("MODIFY SAHI-TIMER for {}", getClassAndMethodAsString(joinPoint));
            Session session = rhinoScriptRunner.getSession();
            if (beforeMethod) {
                Integer delay = determineDelay(joinPoint, loader);
                session.setVariable(SahiProxyProperties.SAHI_REQUEST_DELAY_TIME_VAR, delay.toString());
                //short sleep to ensure that browser context can read out the value
                try {
                    logger.info("wait {}ms for sahi refresh", sahiProxyProperties.getRequestDelayRefreshMs());
                    Thread.sleep(sahiProxyProperties.getRequestDelayRefreshMs());
                } catch (InterruptedException e) {
                    BeanLoader.loadBaseActionLoader().getExceptionHandler().handleException(e, true);
                }
                logger.info("sahi-proxy-timer modified to {} ms", delay.toString());
            } else {
                logger.info("reset sahi-proxy-timer");
                session.setVariable(SahiProxyProperties.SAHI_REQUEST_DELAY_TIME_VAR, null);
            }
        }
    }
}
Also used : RhinoScriptRunner(net.sf.sahi.rhino.RhinoScriptRunner) BaseActionLoader(org.sakuli.loader.BaseActionLoader) SahiProxyProperties(org.sakuli.datamodel.properties.SahiProxyProperties) Session(net.sf.sahi.session.Session)

Example 2 with Session

use of net.sf.sahi.session.Session in project sakuli by ConSol.

the class ModifySahiTimerAspectTest method testDisabledModifySahiTimer.

@Test
public void testDisabledModifySahiTimer() throws Exception {
    RhinoScriptRunner runner = mock(RhinoScriptRunner.class);
    Session session = mock(Session.class);
    when(runner.getSession()).thenReturn(session);
    BaseActionLoader baseActionLoader = BeanLoader.loadBaseActionLoader();
    baseActionLoader.setRhinoScriptRunner(runner);
    when(baseActionLoader.getSahiProxyProperties().isRequestDelayActive()).thenReturn(false);
    doReturn(LoggerFactory.getLogger(this.getClass())).when(testling).getLogger(any(JoinPoint.class));
    doReturn("sig.method()").when(testling).getClassAndMethodAsString(any(JoinPoint.class));
    //test modifcation of timer is disabled
    testling.modifySahiTimer(mock(JoinPoint.class), true);
    testling.modifySahiTimer(mock(JoinPoint.class), false);
    verify(session, never()).setVariable(anyString(), anyString());
    verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class));
    //test no session available
    when(baseActionLoader.getSahiProxyProperties().isRequestDelayActive()).thenReturn(true);
    when(runner.getSession()).thenReturn(null);
    testling.modifySahiTimer(mock(JoinPoint.class), true);
    testling.modifySahiTimer(mock(JoinPoint.class), false);
    verify(session, never()).setVariable(anyString(), anyString());
    verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class));
}
Also used : RhinoScriptRunner(net.sf.sahi.rhino.RhinoScriptRunner) BaseActionLoader(org.sakuli.loader.BaseActionLoader) Session(net.sf.sahi.session.Session) JoinPoint(org.aspectj.lang.JoinPoint) Test(org.testng.annotations.Test)

Example 3 with Session

use of net.sf.sahi.session.Session in project sakuli by ConSol.

the class ModifySahiTimerAspectTest method testModifySahiTimer.

@Test
public void testModifySahiTimer() throws Exception {
    RhinoScriptRunner runner = mock(RhinoScriptRunner.class);
    Session session = mock(Session.class);
    when(runner.getSession()).thenReturn(session);
    BaseActionLoader baseActionLoader = BeanLoader.loadBaseActionLoader();
    baseActionLoader.setRhinoScriptRunner(runner);
    when(baseActionLoader.getSahiProxyProperties().isRequestDelayActive()).thenReturn(true);
    doReturn(1000).when(testling).determineDelay(any(JoinPoint.class), any(BaseActionLoader.class));
    doReturn(LoggerFactory.getLogger(this.getClass())).when(testling).getLogger(any(JoinPoint.class));
    doReturn("sig.method()").when(testling).getClassAndMethodAsString(any(JoinPoint.class));
    //test modifcation of timer to 1000ms
    testling.modifySahiTimer(mock(JoinPoint.class), true);
    verify(session).setVariable(SahiProxyProperties.SAHI_REQUEST_DELAY_TIME_VAR, "1000");
    verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class));
    assertLastLine(logFile, "sahi-proxy-timer", LogLevel.INFO, "sahi-proxy-timer modified to 1000 ms");
    //test reset timer
    testling.modifySahiTimer(mock(JoinPoint.class), false);
    verify(session).setVariable(SahiProxyProperties.SAHI_REQUEST_DELAY_TIME_VAR, null);
    verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class));
    assertLastLine(logFile, "sahi-proxy-timer", LogLevel.INFO, "reset sahi-proxy-timer");
}
Also used : RhinoScriptRunner(net.sf.sahi.rhino.RhinoScriptRunner) BaseActionLoader(org.sakuli.loader.BaseActionLoader) Session(net.sf.sahi.session.Session) JoinPoint(org.aspectj.lang.JoinPoint) Test(org.testng.annotations.Test)

Aggregations

RhinoScriptRunner (net.sf.sahi.rhino.RhinoScriptRunner)3 Session (net.sf.sahi.session.Session)3 BaseActionLoader (org.sakuli.loader.BaseActionLoader)3 JoinPoint (org.aspectj.lang.JoinPoint)2 Test (org.testng.annotations.Test)2 SahiProxyProperties (org.sakuli.datamodel.properties.SahiProxyProperties)1