private Mono<Void> saveDelta() { if (this.delta.isEmpty()) { return Mono.empty(); } String sessionKey = getSessionKey(getId()); Mono<Boolean> update = ReactiveRedisOperationsSessionRepository.this.sessionRedisOperations .opsForHash().putAll(sessionKey, this.delta); Mono<Boolean> setTtl = ReactiveRedisOperationsSessionRepository.this.sessionRedisOperations .expire(sessionKey, getMaxInactiveInterval()); return update.and(setTtl).and((s) -> { this.delta.clear(); s.onComplete(); }).then(); }
@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 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 saveNewSession() { 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)); StepVerifier .create(this.repository.createSession().doOnNext(this.repository::save)) .consumeNextWith((session) -> { verify(this.redisOperations).opsForHash(); verify(this.hashOperations).putAll(anyString(), this.delta.capture()); verify(this.redisOperations).expire(anyString(), any()); verifyZeroInteractions(this.redisOperations); verifyZeroInteractions(this.hashOperations); Map<String, Object> delta = this.delta.getAllValues().get(0); assertThat(delta.size()).isEqualTo(3); assertThat(delta.get( ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY)) .isEqualTo(session.getCreationTime().toEpochMilli()); assertThat(delta.get( ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY)) .isEqualTo((int) Duration.ofSeconds( MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS) .getSeconds()); assertThat(delta.get( ReactiveRedisOperationsSessionRepository.LAST_ACCESSED_TIME_KEY)) .isEqualTo( session.getLastAccessedTime().toEpochMilli()); }).verifyComplete(); }
@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)); }