NotificationExpirationManagement implements RedisExpirationStrategy { @Override public void sessionTouched(SessionData session) { new ExpirationManagement().manageExpiration(session); } NotificationExpirationManagement(RedisFacade redis, RedisSessionRepository redisSession, String namespace,
      String owner, String keyPrefix, boolean sticky); @Override void sessionDeleted(SessionData session); @Override void sessionTouched(SessionData session); @Override void startExpiredSessionsTask(final SessionManager sessionManager); @Override void close(); void reset(); @Override void sessionIdChange(SessionData session);  }
@Test public void testSessionTouched() { expiration.sessionTouched(session); ArgumentCaptor<byte[]> captureExpireKey = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<byte[]> captureSessionKey = ArgumentCaptor.forClass(byte[].class); verify(redis).sadd(captureExpireKey.capture(), captureSessionKey.capture()); assertEquals("prefixexpirations:60000", SafeEncoder.encode(captureExpireKey.getValue())); assertEquals("key:{1}", SafeEncoder.encode(captureSessionKey.getValue())); ArgumentCaptor<Long> captureInstant = ArgumentCaptor.forClass(Long.class); verify(redis).expireAt(captureExpireKey.capture(), captureInstant.capture()); assertEquals("prefixexpirations:60000", SafeEncoder.encode(captureExpireKey.getValue())); assertEquals(Long.valueOf(360), captureInstant.getValue()); ArgumentCaptor<byte[]> captureValue = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<Integer> captureInt = ArgumentCaptor.forClass(Integer.class); verify(redis).setex(captureExpireKey.capture(), captureInt.capture(), captureValue.capture()); assertEquals("com.amadeus.session:expire::test:{1}", SafeEncoder.encode(captureExpireKey.getValue())); assertEquals("", SafeEncoder.encode(captureValue.getValue())); assertEquals(Integer.valueOf(20), captureInt.getValue()); verify(redis).expire(captureSessionKey.capture(), captureInt.capture()); assertEquals("key:{1}", SafeEncoder.encode(captureSessionKey.getValue())); assertEquals(Integer.valueOf(320), captureInt.getValue()); }
@Test public void testSessionTouchedNotNewSessionChangeExpiryMinute() { session = new SessionData("1", 100, 70); session.setNew(false); expiration.sessionTouched(session); ArgumentCaptor<byte[]> captureExpireKey = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<byte[]> captureSessionKey = ArgumentCaptor.forClass(byte[].class); verify(redis).srem(captureExpireKey.capture(), captureSessionKey.capture()); assertEquals("prefixexpirations:60000", SafeEncoder.encode(captureExpireKey.getValue())); assertEquals("key:{1}", SafeEncoder.encode(captureSessionKey.getValue())); }
@Test public void testSessionTouchedNotNewSession() { session = new SessionData("1", 100, 10); session.setNew(false); expiration.sessionTouched(session); verify(redis, never()).srem(any(byte[].class), any(byte[].class)); }
@Test public void testSessionTouchedNotNewSessionNeverExpires() { NotificationExpirationManagement neverExpires = new NotificationExpirationManagement(redis, redisSession, "test", "this", "prefix:", false); session = new SessionData("1", 100, 0); session.setNew(false); neverExpires.sessionTouched(session); ArgumentCaptor<byte[]> captureKey = ArgumentCaptor.forClass(byte[].class); verify(redis).del(captureKey.capture()); assertEquals("com.amadeus.session:expire::test:{1}", SafeEncoder.encode(captureKey.getValue())); ArgumentCaptor<byte[]> captureKey2 = ArgumentCaptor.forClass(byte[].class); verify(redis).persist(captureKey2.capture()); assertArrayEquals(SafeEncoder.encode("key:{1}"), captureKey2.getValue()); ArgumentCaptor<byte[]> captureSessionKey = ArgumentCaptor.forClass(byte[].class); verify(redis).srem(captureKey.capture(), captureSessionKey.capture()); assertEquals("prefix:expirations:60000", SafeEncoder.encode(captureKey.getValue())); assertEquals("key:{1}", SafeEncoder.encode(captureSessionKey.getValue())); }
@Test public void testSessionTouchedSticky() { NotificationExpirationManagement sticky = new NotificationExpirationManagement(redis, redisSession, "test", "this", "prefix:", true); sticky.sessionTouched(session); ArgumentCaptor<byte[]> captureKey = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<byte[]> captureSessionKey = ArgumentCaptor.forClass(byte[].class); verify(redis, times(2)).sadd(captureKey.capture(), captureSessionKey.capture()); ArrayList<String> captured = new ArrayList<>(); for (byte[] key : captureKey.getAllValues()) { captured.add(encode(key)); } assertThat(captured, hasItem("prefix:forced-expirations:120000")); verify(redis, times(2)).expireAt(captureKey.capture(), any(long.class)); captured.clear(); for (byte[] key : captureSessionKey.getAllValues()) { captured.add(encode(key)); } assertThat(captured, hasItem("key:{1}")); }