@Override public Mono<Void> save(RedisSession session) { Mono<Void> result = session.saveChangeSessionId().and(session.saveDelta()) .and((s) -> { session.isNew = false; s.onComplete(); }); if (session.isNew) { return result; } else { String sessionKey = getSessionKey( session.hasChangedSessionId() ? session.originalSessionId : session.getId()); return this.sessionRedisOperations.hasKey(sessionKey) .flatMap((exists) -> exists ? result : Mono.error(new IllegalStateException( "Session was invalidated"))); } }
@Test public void saveSessionNothingChanged() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); RedisSession session = this.repository.new RedisSession( new MapSession(this.cached)); StepVerifier.create(this.repository.save(session)).verifyComplete(); verify(this.redisOperations).hasKey(anyString()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); }
@Test public void saveLastAccessChanged() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); RedisSession session = this.repository.new RedisSession(this.cached); session.setLastAccessedTime(Instant.ofEpochMilli(12345678L)); Mono.just(session).subscribe(this.repository::save); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); assertThat(this.delta.getAllValues().get(0)) .isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli())); }
@Test public void saveSetAttribute() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); String attrName = "attrName"; RedisSession session = this.repository.new RedisSession(this.cached); session.setAttribute(attrName, "attrValue"); Mono.just(session).subscribe(this.repository::save); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); assertThat(this.delta.getAllValues().get(0)).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@Test public void saveRemoveAttribute() { given(this.redisOperations.hasKey(anyString())).willReturn(Mono.just(true)); given(this.redisOperations.opsForHash()).willReturn(this.hashOperations); given(this.hashOperations.putAll(anyString(), any())).willReturn(Mono.just(true)); given(this.redisOperations.expire(anyString(), any())) .willReturn(Mono.just(true)); String attrName = "attrName"; RedisSession session = this.repository.new RedisSession(new MapSession()); session.removeAttribute(attrName); Mono.just(session).subscribe(this.repository::save); verify(this.redisOperations).hasKey(anyString()); verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); assertThat(this.delta.getAllValues().get(0)).isEqualTo(map( RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), null)); }