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();
}
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();
}
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();
}
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();
}
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());
}
Aggregations