Search in sources :

Example 1 with UserSessions

use of com.haulmont.cuba.security.app.UserSessions in project cuba by cuba-platform.

the class ServiceInterceptorTest method testNewThread.

@Test
public void testNewThread() throws Exception {
    ServiceInterceptorTestService service = AppBeans.get(ServiceInterceptorTestService.class);
    UserSessions userSessions = AppBeans.get(UserSessions.class);
    // workaround for test security setup
    Field startedField = AppContext.class.getDeclaredField("started");
    startedField.setAccessible(true);
    startedField.set(null, true);
    AppContext.setSecurityContext(AppContext.NO_USER_CONTEXT);
    UserSession userSession = new UserSession(AppContext.NO_USER_CONTEXT.getSessionId(), new User(), Collections.emptyList(), Locale.ENGLISH, true);
    userSessions.add(userSession);
    try {
        appender.getMessages().clear();
        service.declarativeTransactionNewThread();
        assertEquals(0, appender.getMessages().stream().filter(s -> s.contains("from another service")).count());
        appender.getMessages().clear();
        try {
            service.executeWithExceptionNewThread();
        } catch (Exception e) {
            assertTrue(e instanceof RemoteException && ((RemoteException) e).getFirstCauseException() instanceof TestingService.TestException);
        }
    } finally {
        userSessions.remove(userSession);
        startedField.set(null, false);
    }
}
Also used : Field(java.lang.reflect.Field) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession) TestingService(com.haulmont.cuba.core.app.TestingService) RemoteException(com.haulmont.cuba.core.global.RemoteException) UserSessions(com.haulmont.cuba.security.app.UserSessions) RemoteException(com.haulmont.cuba.core.global.RemoteException) Test(org.junit.Test)

Aggregations

TestingService (com.haulmont.cuba.core.app.TestingService)1 RemoteException (com.haulmont.cuba.core.global.RemoteException)1 UserSessions (com.haulmont.cuba.security.app.UserSessions)1 User (com.haulmont.cuba.security.entity.User)1 UserSession (com.haulmont.cuba.security.global.UserSession)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1