parent
714c533509
commit
cf151b1717
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.session;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||||
|
import org.springframework.boot.actuate.session.SessionsEndpoint;
|
||||||
|
import org.springframework.boot.actuate.session.SessionsWebEndpointExtension;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
|
import org.springframework.session.Session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link EnableAutoConfiguration Auto-configuration} for {@link SessionsEndpoint}.
|
||||||
|
*
|
||||||
|
* @author Vedran Pavic
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(FindByIndexNameSessionRepository.class)
|
||||||
|
@AutoConfigureAfter(SessionAutoConfiguration.class)
|
||||||
|
public class SessionsEndpointAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnBean(FindByIndexNameSessionRepository.class)
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnEnabledEndpoint
|
||||||
|
public SessionsEndpoint sessionEndpoint(
|
||||||
|
FindByIndexNameSessionRepository<? extends Session> sessionRepository) {
|
||||||
|
return new SessionsEndpoint(sessionRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnEnabledEndpoint
|
||||||
|
@ConditionalOnBean(SessionsEndpoint.class)
|
||||||
|
public SessionsWebEndpointExtension sessionsWebEndpointExtension(
|
||||||
|
SessionsEndpoint sessionsEndpoint) {
|
||||||
|
return new SessionsWebEndpointExtension(sessionsEndpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-configuration for actuator Spring Sessions concerns.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.session;
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.session;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.session.SessionsEndpoint;
|
||||||
|
import org.springframework.boot.actuate.session.SessionsWebEndpointExtension;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link SessionsEndpointAutoConfiguration}.
|
||||||
|
*
|
||||||
|
* @author Vedran Pavic
|
||||||
|
*/
|
||||||
|
public class SessionsEndpointAutoConfigurationTests {
|
||||||
|
|
||||||
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(
|
||||||
|
AutoConfigurations.of(SessionsEndpointAutoConfiguration.class))
|
||||||
|
.withUserConfiguration(SessionConfiguration.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void runShouldHaveEndpointBean() {
|
||||||
|
this.contextRunner.run(
|
||||||
|
(context) -> assertThat(context).hasSingleBean(SessionsEndpoint.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void runShouldHaveWebExtensionBean() {
|
||||||
|
this.contextRunner.run((context) -> assertThat(context)
|
||||||
|
.hasSingleBean(SessionsWebEndpointExtension.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointOrExtensionBean()
|
||||||
|
throws Exception {
|
||||||
|
this.contextRunner.withPropertyValues("endpoints.sessions.enabled:false").run(
|
||||||
|
(context) -> assertThat(context).doesNotHaveBean(SessionsEndpoint.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class SessionConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FindByIndexNameSessionRepository sessionRepository() {
|
||||||
|
return mock(FindByIndexNameSessionRepository.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.session;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
|
||||||
|
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||||
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||||
|
import org.springframework.boot.actuate.endpoint.annotation.Selector;
|
||||||
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
|
import org.springframework.session.Session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Endpoint} to expose a user's {@link Session}s.
|
||||||
|
*
|
||||||
|
* @author Vedran Pavic
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@Endpoint(id = "sessions")
|
||||||
|
public class SessionsEndpoint {
|
||||||
|
|
||||||
|
private final FindByIndexNameSessionRepository<? extends Session> sessionRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link SessionsEndpoint} instance.
|
||||||
|
* @param sessionRepository the session repository
|
||||||
|
*/
|
||||||
|
public SessionsEndpoint(
|
||||||
|
FindByIndexNameSessionRepository<? extends Session> sessionRepository) {
|
||||||
|
this.sessionRepository = sessionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReadOperation
|
||||||
|
public SessionsReport sessionsForUsername(String username) {
|
||||||
|
Map<String, ? extends Session> sessions = this.sessionRepository
|
||||||
|
.findByIndexNameAndIndexValue(
|
||||||
|
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME,
|
||||||
|
username);
|
||||||
|
return new SessionsReport(sessions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReadOperation
|
||||||
|
public SessionDescriptor getSession(@Selector String sessionId) {
|
||||||
|
Session session = this.sessionRepository.findById(sessionId);
|
||||||
|
return new SessionDescriptor(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteOperation
|
||||||
|
public void deleteSession(@Selector String sessionId) {
|
||||||
|
this.sessionRepository.deleteById(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A report of user's {@link Session sessions}. Primarily intended for serialization
|
||||||
|
* to JSON.
|
||||||
|
*/
|
||||||
|
public static final class SessionsReport {
|
||||||
|
|
||||||
|
private final List<SessionDescriptor> sessions;
|
||||||
|
|
||||||
|
public SessionsReport(Map<String, ? extends Session> sessions) {
|
||||||
|
this.sessions = sessions.entrySet().stream()
|
||||||
|
.map(s -> new SessionDescriptor(s.getValue()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SessionDescriptor> getSessions() {
|
||||||
|
return this.sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A description of user's {@link Session session}. Primarily intended for
|
||||||
|
* serialization to JSON.
|
||||||
|
*/
|
||||||
|
public static final class SessionDescriptor {
|
||||||
|
|
||||||
|
private final String id;
|
||||||
|
|
||||||
|
private final Set<String> attributeNames;
|
||||||
|
|
||||||
|
private final long creationTime;
|
||||||
|
|
||||||
|
private final long 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().toEpochMilli();
|
||||||
|
this.lastAccessedTime = session.getLastAccessedTime().toEpochMilli();
|
||||||
|
this.maxInactiveInterval = session.getMaxInactiveInterval().getSeconds();
|
||||||
|
this.expired = session.isExpired();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getAttributeNames() {
|
||||||
|
return this.attributeNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreationTime() {
|
||||||
|
return this.creationTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLastAccessedTime() {
|
||||||
|
return this.lastAccessedTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getMaxInactiveInterval() {
|
||||||
|
return this.maxInactiveInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isExpired() {
|
||||||
|
return this.expired;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.session;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||||
|
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
||||||
|
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
|
||||||
|
import org.springframework.boot.actuate.session.SessionsEndpoint.SessionsReport;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link WebEndpointExtension} for the {@link SessionsEndpoint}.
|
||||||
|
*
|
||||||
|
* @author Vedran Pavic
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@WebEndpointExtension(endpoint = SessionsEndpoint.class)
|
||||||
|
public class SessionsWebEndpointExtension {
|
||||||
|
|
||||||
|
private final SessionsEndpoint delegate;
|
||||||
|
|
||||||
|
public SessionsWebEndpointExtension(SessionsEndpoint delegate) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReadOperation
|
||||||
|
public WebEndpointResponse<SessionsReport> sessionsForUsername(String username) {
|
||||||
|
if (username == null) {
|
||||||
|
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
|
||||||
|
}
|
||||||
|
SessionsReport sessions = this.delegate.sessionsForUsername(username);
|
||||||
|
return new WebEndpointResponse<>(sessions);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actuator support for Spring Session.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.actuate.session;
|
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.session;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.session.SessionsEndpoint.SessionDescriptor;
|
||||||
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
|
import org.springframework.session.MapSession;
|
||||||
|
import org.springframework.session.Session;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link SessionsEndpoint}.
|
||||||
|
*
|
||||||
|
* @author Vedran Pavic
|
||||||
|
*/
|
||||||
|
public class SessionsEndpointTests {
|
||||||
|
|
||||||
|
private static final Session session = new MapSession();
|
||||||
|
|
||||||
|
private final FindByIndexNameSessionRepository repository = mock(
|
||||||
|
FindByIndexNameSessionRepository.class);
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private final SessionsEndpoint endpoint = new SessionsEndpoint(this.repository);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sessionsForUsername() {
|
||||||
|
given(this.repository.findByIndexNameAndIndexValue(
|
||||||
|
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "user"))
|
||||||
|
.willReturn(Collections.singletonMap(session.getId(), session));
|
||||||
|
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());
|
||||||
|
assertThat(result.get(0).getCreationTime())
|
||||||
|
.isEqualTo(session.getCreationTime().toEpochMilli());
|
||||||
|
assertThat(result.get(0).getLastAccessedTime())
|
||||||
|
.isEqualTo(session.getLastAccessedTime().toEpochMilli());
|
||||||
|
assertThat(result.get(0).getMaxInactiveInterval())
|
||||||
|
.isEqualTo(session.getMaxInactiveInterval().getSeconds());
|
||||||
|
assertThat(result.get(0).isExpired()).isEqualTo(session.isExpired());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getSession() {
|
||||||
|
given(this.repository.findById(session.getId())).willReturn(session);
|
||||||
|
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().toEpochMilli());
|
||||||
|
assertThat(result.getLastAccessedTime())
|
||||||
|
.isEqualTo(session.getLastAccessedTime().toEpochMilli());
|
||||||
|
assertThat(result.getMaxInactiveInterval())
|
||||||
|
.isEqualTo(session.getMaxInactiveInterval().getSeconds());
|
||||||
|
assertThat(result.isExpired()).isEqualTo(session.isExpired());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deleteSession() {
|
||||||
|
this.endpoint.deleteSession(session.getId());
|
||||||
|
verify(this.repository).deleteById(session.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.session;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import net.minidev.json.JSONArray;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.endpoint.web.test.WebEndpointRunners;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||||
|
import org.springframework.session.MapSession;
|
||||||
|
import org.springframework.session.Session;
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for {@link SessionsEndpoint} exposed by Jersey, Spring MVC, and
|
||||||
|
* WebFlux.
|
||||||
|
*
|
||||||
|
* @author Vedran Pavic
|
||||||
|
*/
|
||||||
|
@RunWith(WebEndpointRunners.class)
|
||||||
|
public class SessionsEndpointWebIntegrationTests {
|
||||||
|
|
||||||
|
private static final Session session = new MapSession();
|
||||||
|
|
||||||
|
private static final FindByIndexNameSessionRepository repository = mock(
|
||||||
|
FindByIndexNameSessionRepository.class);
|
||||||
|
|
||||||
|
private static WebTestClient client;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sessionsForUsernameWithoutUsernameParam() throws Exception {
|
||||||
|
client.get().uri((builder) -> builder.path("/application/sessions").build())
|
||||||
|
.exchange().expectStatus().isBadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sessionsForUsernameNoResults() throws Exception {
|
||||||
|
given(repository.findByIndexNameAndIndexValue(
|
||||||
|
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "user"))
|
||||||
|
.willReturn(Collections.emptyMap());
|
||||||
|
client.get()
|
||||||
|
.uri((builder) -> builder.path("/application/sessions")
|
||||||
|
.queryParam("username", "user").build())
|
||||||
|
.exchange().expectStatus().isOk().expectBody().jsonPath("sessions")
|
||||||
|
.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sessionsForUsernameFound() throws Exception {
|
||||||
|
given(repository.findByIndexNameAndIndexValue(
|
||||||
|
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "user"))
|
||||||
|
.willReturn(Collections.singletonMap(session.getId(), session));
|
||||||
|
client.get()
|
||||||
|
.uri((builder) -> builder.path("/application/sessions")
|
||||||
|
.queryParam("username", "user").build())
|
||||||
|
.exchange().expectStatus().isOk().expectBody().jsonPath("sessions.[*].id")
|
||||||
|
.isEqualTo(new JSONArray().appendElement(session.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class TestConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public SessionsEndpoint sessionsEndpoint() {
|
||||||
|
return new SessionsEndpoint(repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SessionsWebEndpointExtension sessionsWebEndpointExtension(
|
||||||
|
SessionsEndpoint delegate) {
|
||||||
|
return new SessionsWebEndpointExtension(delegate);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
server.session.timeout=5
|
endpoints.sessions.web.enabled=true
|
||||||
|
Loading…
Reference in New Issue