Polish "Provide an Actuator endpoint for non-indexed session repositories"

See gh-32046
This commit is contained in:
Moritz Halbritter
2024-01-18 10:52:58 +01:00
parent 6a9eb7754f
commit de76ef1b3b
3 changed files with 12 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,8 @@
package org.springframework.boot.actuate.session;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
@@ -97,56 +95,4 @@ public class SessionsEndpoint {
}
/**
* Description of user's {@link Session session}.
*/
public static final class SessionDescriptor implements OperationResponseBody {
private final String id;
private final Set<String> attributeNames;
private final Instant creationTime;
private final Instant lastAccessedTime;
private final long maxInactiveInterval;
private final boolean expired;
public SessionDescriptor(Session session) {
this.id = session.getId();
this.attributeNames = session.getAttributeNames();
this.creationTime = session.getCreationTime();
this.lastAccessedTime = session.getLastAccessedTime();
this.maxInactiveInterval = session.getMaxInactiveInterval().getSeconds();
this.expired = session.isExpired();
}
public String getId() {
return this.id;
}
public Set<String> getAttributeNames() {
return this.attributeNames;
}
public Instant getCreationTime() {
return this.creationTime;
}
public Instant getLastAccessedTime() {
return this.lastAccessedTime;
}
public long getMaxInactiveInterval() {
return this.maxInactiveInterval;
}
public boolean isExpired() {
return this.expired;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.session;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -53,21 +55,23 @@ class ReactiveSessionsEndpointTests {
assertThat(result.getLastAccessedTime()).isEqualTo(session.getLastAccessedTime());
assertThat(result.getMaxInactiveInterval()).isEqualTo(session.getMaxInactiveInterval().getSeconds());
assertThat(result.isExpired()).isEqualTo(session.isExpired());
}).verifyComplete();
}).expectComplete().verify(Duration.ofSeconds(1));
then(this.sessionRepository).should().findById(session.getId());
}
@Test
void getSessionWithIdNotFound() {
given(this.sessionRepository.findById("not-found")).willReturn(Mono.empty());
StepVerifier.create(this.endpoint.getSession("not-found")).verifyComplete();
StepVerifier.create(this.endpoint.getSession("not-found")).expectComplete().verify(Duration.ofSeconds(1));
then(this.sessionRepository).should().findById("not-found");
}
@Test
void deleteSession() {
given(this.sessionRepository.deleteById(session.getId())).willReturn(Mono.empty());
StepVerifier.create(this.endpoint.deleteSession(session.getId())).verifyComplete();
StepVerifier.create(this.endpoint.deleteSession(session.getId()))
.expectComplete()
.verify(Duration.ofSeconds(1));
then(this.sessionRepository).should().deleteById(session.getId());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ class SessionsEndpointTests {
void sessionsForUsername() {
given(this.indexedSessionRepository.findByPrincipalName("user"))
.willReturn(Collections.singletonMap(session.getId(), session));
List<SessionsEndpoint.SessionDescriptor> result = this.endpoint.sessionsForUsername("user").getSessions();
List<SessionDescriptor> result = this.endpoint.sessionsForUsername("user").getSessions();
assertThat(result).hasSize(1);
assertThat(result.get(0).getId()).isEqualTo(session.getId());
assertThat(result.get(0).getAttributeNames()).isEqualTo(session.getAttributeNames());
@@ -74,7 +74,7 @@ class SessionsEndpointTests {
@Test
void getSession() {
given(this.sessionRepository.findById(session.getId())).willReturn(session);
SessionsEndpoint.SessionDescriptor result = this.endpoint.getSession(session.getId());
SessionDescriptor result = this.endpoint.getSession(session.getId());
assertThat(result.getId()).isEqualTo(session.getId());
assertThat(result.getAttributeNames()).isEqualTo(session.getAttributeNames());
assertThat(result.getCreationTime()).isEqualTo(session.getCreationTime());