Search in sources :

Example 6 with SessionListeners

use of io.undertow.server.session.SessionListeners in project wildfly by wildfly.

the class DistributableSessionTestCase method setAttribute.

@Test
public void setAttribute() {
    String name = "name";
    Integer value = Integer.valueOf(1);
    this.validate(session -> session.setAttribute(name, value));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = new Object();
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.setAttribute(name, value)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener, never()).attributeAdded(this.adapter, name, value);
    verify(listener).attributeUpdated(this.adapter, name, value, expected);
    verify(listener, never()).attributeRemoved(same(this.adapter), same(name), any());
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 7 with SessionListeners

use of io.undertow.server.session.SessionListeners in project wildfly by wildfly.

the class DistributableSessionTestCase method changeSessionId.

@Test
public void changeSessionId() {
    HttpServerExchange exchange = new HttpServerExchange(null);
    SessionConfig config = mock(SessionConfig.class);
    this.validate(session -> session.changeSessionId(exchange, config));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    Session<LocalSessionContext> session = mock(Session.class);
    SessionAttributes oldAttributes = mock(SessionAttributes.class);
    SessionAttributes newAttributes = mock(SessionAttributes.class);
    SessionMetaData oldMetaData = mock(SessionMetaData.class);
    SessionMetaData newMetaData = mock(SessionMetaData.class);
    LocalSessionContext oldContext = mock(LocalSessionContext.class);
    LocalSessionContext newContext = mock(LocalSessionContext.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    String oldSessionId = "old";
    String newSessionId = "new";
    String name = "name";
    Object value = new Object();
    Instant now = Instant.now();
    Duration interval = Duration.ofSeconds(10L);
    AuthenticatedSession authenticatedSession = new AuthenticatedSession(null, null);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    when(manager.createIdentifier()).thenReturn(newSessionId);
    when(manager.createSession(newSessionId)).thenReturn(session);
    when(this.session.getAttributes()).thenReturn(oldAttributes);
    when(this.session.getMetaData()).thenReturn(oldMetaData);
    when(session.getAttributes()).thenReturn(newAttributes);
    when(session.getMetaData()).thenReturn(newMetaData);
    when(oldAttributes.getAttributeNames()).thenReturn(Collections.singleton(name));
    when(oldAttributes.getAttribute(name)).thenReturn(value);
    when(newAttributes.setAttribute(name, value)).thenReturn(null);
    when(oldMetaData.getLastAccessedTime()).thenReturn(now);
    when(oldMetaData.getMaxInactiveInterval()).thenReturn(interval);
    when(this.session.getId()).thenReturn(oldSessionId);
    when(session.getId()).thenReturn(newSessionId);
    when(this.session.getLocalContext()).thenReturn(oldContext);
    when(session.getLocalContext()).thenReturn(newContext);
    when(oldContext.getAuthenticatedSession()).thenReturn(authenticatedSession);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    String result = this.adapter.changeSessionId(exchange, config);
    assertSame(newSessionId, result);
    verify(newMetaData).setLastAccessedTime(now);
    verify(newMetaData).setMaxInactiveInterval(interval);
    verify(config).setSessionId(exchange, newSessionId);
    verify(newContext).setAuthenticatedSession(same(authenticatedSession));
    verify(listener).sessionIdChanged(this.adapter, oldSessionId);
    verify(context).close();
}
Also used : AuthenticatedSession(io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession) Instant(java.time.Instant) SessionConfig(io.undertow.server.session.SessionConfig) BatchContext(org.wildfly.clustering.ee.BatchContext) Duration(java.time.Duration) HttpServerExchange(io.undertow.server.HttpServerExchange) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionMetaData(org.wildfly.clustering.web.session.SessionMetaData) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 8 with SessionListeners

use of io.undertow.server.session.SessionListeners in project wildfly by wildfly.

the class DistributableSessionTestCase method invalidate.

@Test
public void invalidate() {
    HttpServerExchange exchange = new HttpServerExchange(null);
    this.validate(session -> session.invalidate(exchange));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    String sessionId = "session";
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.session.getId()).thenReturn(sessionId);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    this.adapter.invalidate(exchange);
    verify(this.session).invalidate();
    verify(this.config).clearSession(exchange, sessionId);
    verify(listener).sessionDestroyed(this.adapter, exchange, SessionDestroyedReason.INVALIDATED);
    verify(this.batch).close();
    verify(context).close();
    verify(this.closeTask).run();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 9 with SessionListeners

use of io.undertow.server.session.SessionListeners in project wildfly by wildfly.

the class DistributableSessionTestCase method removeAttribute.

@Test
public void removeAttribute() {
    String name = "name";
    this.validate(session -> session.removeAttribute(name));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = new Object();
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.removeAttribute(name)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.removeAttribute(name);
    assertSame(expected, result);
    verify(listener).attributeRemoved(this.adapter, name, expected);
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 10 with SessionListeners

use of io.undertow.server.session.SessionListeners in project wildfly by wildfly.

the class UndertowSessionExpirationListenerTestCase method sessionExpired.

@Test
public void sessionExpired() {
    Deployment deployment = mock(Deployment.class);
    UndertowSessionManager manager = mock(UndertowSessionManager.class);
    SessionManager<LocalSessionContext, Batch> delegateManager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    SessionListener listener = mock(SessionListener.class);
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    ArgumentCaptor<Session> capturedSession = ArgumentCaptor.forClass(Session.class);
    String expectedSessionId = "session";
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, listeners);
    when(deployment.getSessionManager()).thenReturn(manager);
    when(manager.getSessionManager()).thenReturn(delegateManager);
    when(delegateManager.getBatcher()).thenReturn(batcher);
    when(batcher.suspendBatch()).thenReturn(batch);
    when(session.getId()).thenReturn(expectedSessionId);
    when(session.getAttributes()).thenReturn(attributes);
    when(attributes.getAttributeNames()).thenReturn(Collections.emptySet());
    when(session.getMetaData()).thenReturn(metaData);
    when(metaData.getCreationTime()).thenReturn(Instant.now());
    when(metaData.getLastAccessedTime()).thenReturn(Instant.now());
    when(metaData.getMaxInactiveInterval()).thenReturn(Duration.ZERO);
    expirationListener.sessionExpired(session);
    verify(batcher).suspendBatch();
    verify(listener).sessionDestroyed(capturedSession.capture(), isNull(HttpServerExchange.class), same(SessionListener.SessionDestroyedReason.TIMEOUT));
    verify(batcher).resumeBatch(batch);
    assertSame(expectedSessionId, capturedSession.getValue().getId());
    assertSame(manager, capturedSession.getValue().getSessionManager());
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Deployment(io.undertow.servlet.api.Deployment) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionListener(io.undertow.server.session.SessionListener) Session(io.undertow.server.session.Session) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Test(org.junit.Test)

Aggregations

SessionListeners (io.undertow.server.session.SessionListeners)10 Batch (org.wildfly.clustering.ee.Batch)10 SessionListener (io.undertow.server.session.SessionListener)9 Test (org.junit.Test)9 BatchContext (org.wildfly.clustering.ee.BatchContext)9 SessionAttributes (org.wildfly.clustering.web.session.SessionAttributes)7 HttpServerExchange (io.undertow.server.HttpServerExchange)4 ImmutableSession (org.wildfly.clustering.web.session.ImmutableSession)2 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)2 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)1 Session (io.undertow.server.session.Session)1 SessionConfig (io.undertow.server.session.SessionConfig)1 Deployment (io.undertow.servlet.api.Deployment)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ImmutableSessionAttributes (org.wildfly.clustering.web.session.ImmutableSessionAttributes)1 ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)1 SessionManagerConfiguration (org.wildfly.clustering.web.session.SessionManagerConfiguration)1