private HttpSessionEvent createHttpSessionEvent(AbstractSessionEvent event) { Session session = event.getSession(); HttpSession httpSession = new HttpSessionAdapter<>(session, this.context); return new HttpSessionEvent(httpSession); }
@Test // gh-309 public void onMessageCreatedCustomSerializer() { MapSession session = this.cached; byte[] pattern = "".getBytes(StandardCharsets.UTF_8); byte[] body = new byte[0]; String channel = "spring:session:event:0:created:" + session.getId(); given(this.defaultSerializer.deserialize(body)) .willReturn(new HashMap<String, Object>()); DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, pattern); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId()); verify(this.defaultSerializer).deserialize(body); }
public static void assertSessionContent(Supplier<Stream<ApplicationEvent>> eventCollector, Class<? extends AbstractSessionEvent> eventClass, String sessionId, String attName, String attValue, int timeout, TimeUnit timeoutUnit) { long stopTime = System.currentTimeMillis() + timeoutUnit.toMillis(timeout); List<Session> sessions = null; while (System.currentTimeMillis() < stopTime) { sessions = eventCollector.get() .filter(e -> e.getClass() == eventClass) .map(e -> eventClass.cast(e)) .filter(e -> e.getSessionId().equals(sessionId)) .map(e -> (Session) e.getSession()) .collect(Collectors.toList()); if (sessions.size() > 0) { break; } } assertEquals(1, sessions.size()); assertEquals(attValue, sessions.get(0).getAttribute(attName)); }
@Test public void onMessageCreated() { MapSession session = this.cached; byte[] pattern = "".getBytes(StandardCharsets.UTF_8); String channel = "spring:session:event:0:created:" + session.getId(); JdkSerializationRedisSerializer defaultSerailizer = new JdkSerializationRedisSerializer(); this.redisRepository.setDefaultSerializer(defaultSerailizer); byte[] body = defaultSerailizer.serialize(new HashMap()); DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, pattern); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(session.getId()); }
@Test public void onMessageDeletedSessionFound() { String deletedId = "deleted-id"; given(this.redisOperations.boundHashOps(getKey(deletedId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 0, RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5)); given(this.boundHashOperations.entries()).willReturn(map); String channel = "__keyevent@0__:del"; String body = "spring:session:sessions:expires:" + deletedId; DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body.getBytes(StandardCharsets.UTF_8)); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8)); verify(this.redisOperations).boundHashOps(eq(getKey(deletedId))); verify(this.boundHashOperations).entries(); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(deletedId); verifyZeroInteractions(this.defaultSerializer); verifyZeroInteractions(this.publisher); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.boundHashOperations); }
@Test public void onMessageExpiredSessionFound() { String expiredId = "expired-id"; given(this.redisOperations.boundHashOps(getKey(expiredId))) .willReturn(this.boundHashOperations); Map map = map(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, 1, RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5)); given(this.boundHashOperations.entries()).willReturn(map); String channel = "__keyevent@0__:expired"; String body = "spring:session:sessions:expires:" + expiredId; DefaultMessage message = new DefaultMessage( channel.getBytes(StandardCharsets.UTF_8), body.getBytes(StandardCharsets.UTF_8)); this.redisRepository.setApplicationEventPublisher(this.publisher); this.redisRepository.onMessage(message, "".getBytes(StandardCharsets.UTF_8)); verify(this.redisOperations).boundHashOps(eq(getKey(expiredId))); verify(this.boundHashOperations).entries(); verify(this.publisher).publishEvent(this.event.capture()); assertThat(this.event.getValue().getSessionId()).isEqualTo(expiredId); verifyZeroInteractions(this.defaultSerializer); verifyZeroInteractions(this.publisher); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.boundHashOperations); }