|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.session;
|
18 | 18 |
|
| 19 | +import org.junit.jupiter.api.Nested; |
19 | 20 | import org.junit.jupiter.api.Test;
|
20 | 21 |
|
| 22 | +import org.springframework.boot.actuate.session.ReactiveSessionsEndpoint; |
21 | 23 | import org.springframework.boot.actuate.session.SessionsEndpoint;
|
22 | 24 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
23 |
| -import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 25 | +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; |
| 26 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
24 | 27 | import org.springframework.context.annotation.Bean;
|
25 | 28 | import org.springframework.context.annotation.Configuration;
|
26 | 29 | import org.springframework.session.FindByIndexNameSessionRepository;
|
| 30 | +import org.springframework.session.ReactiveSessionRepository; |
| 31 | +import org.springframework.session.SessionRepository; |
27 | 32 |
|
28 | 33 | import static org.assertj.core.api.Assertions.assertThat;
|
29 | 34 | import static org.mockito.Mockito.mock;
|
|
35 | 40 | */
|
36 | 41 | class SessionsEndpointAutoConfigurationTests {
|
37 | 42 |
|
38 |
| - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
39 |
| - .withConfiguration(AutoConfigurations.of(SessionsEndpointAutoConfiguration.class)) |
40 |
| - .withUserConfiguration(SessionConfiguration.class); |
| 43 | + @Nested |
| 44 | + class ServletSessionEndpointConfigurationTests { |
41 | 45 |
|
42 |
| - @Test |
43 |
| - void runShouldHaveEndpointBean() { |
44 |
| - this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include=sessions") |
45 |
| - .run((context) -> assertThat(context).hasSingleBean(SessionsEndpoint.class)); |
46 |
| - } |
| 46 | + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() |
| 47 | + .withConfiguration(AutoConfigurations.of(SessionsEndpointAutoConfiguration.class)) |
| 48 | + .withUserConfiguration(IndexedSessionRepositoryConfiguration.class); |
47 | 49 |
|
48 |
| - @Test |
49 |
| - void runWhenNotExposedShouldNotHaveEndpointBean() { |
50 |
| - this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(SessionsEndpoint.class)); |
51 |
| - } |
| 50 | + @Test |
| 51 | + void runShouldHaveEndpointBean() { |
| 52 | + this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include=sessions") |
| 53 | + .run((context) -> assertThat(context).hasSingleBean(SessionsEndpoint.class)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void runWhenNoIndexedSessionRepositoryShouldHaveEndpointBean() { |
| 58 | + new WebApplicationContextRunner() |
| 59 | + .withConfiguration(AutoConfigurations.of(SessionsEndpointAutoConfiguration.class)) |
| 60 | + .withUserConfiguration(SessionRepositoryConfiguration.class) |
| 61 | + .withPropertyValues("management.endpoints.web.exposure.include=sessions") |
| 62 | + .run((context) -> assertThat(context).hasSingleBean(SessionsEndpoint.class)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void runWhenNotExposedShouldNotHaveEndpointBean() { |
| 67 | + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(SessionsEndpoint.class)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() { |
| 72 | + this.contextRunner.withPropertyValues("management.endpoint.sessions.enabled:false") |
| 73 | + .run((context) -> assertThat(context).doesNotHaveBean(SessionsEndpoint.class)); |
| 74 | + } |
| 75 | + |
| 76 | + @Configuration(proxyBeanMethods = false) |
| 77 | + static class IndexedSessionRepositoryConfiguration { |
| 78 | + |
| 79 | + @Bean |
| 80 | + FindByIndexNameSessionRepository<?> sessionRepository() { |
| 81 | + return mock(FindByIndexNameSessionRepository.class); |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Configuration(proxyBeanMethods = false) |
| 87 | + static class SessionRepositoryConfiguration { |
| 88 | + |
| 89 | + @Bean |
| 90 | + SessionRepository<?> sessionRepository() { |
| 91 | + return mock(SessionRepository.class); |
| 92 | + } |
| 93 | + |
| 94 | + } |
52 | 95 |
|
53 |
| - @Test |
54 |
| - void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() { |
55 |
| - this.contextRunner.withPropertyValues("management.endpoint.sessions.enabled:false") |
56 |
| - .run((context) -> assertThat(context).doesNotHaveBean(SessionsEndpoint.class)); |
57 | 96 | }
|
58 | 97 |
|
59 |
| - @Configuration(proxyBeanMethods = false) |
60 |
| - static class SessionConfiguration { |
| 98 | + @Nested |
| 99 | + class ReactiveSessionEndpointConfigurationTests { |
| 100 | + |
| 101 | + private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner() |
| 102 | + .withConfiguration(AutoConfigurations.of(SessionsEndpointAutoConfiguration.class)) |
| 103 | + .withUserConfiguration(ReactiveSessionRepositoryConfiguration.class); |
| 104 | + |
| 105 | + @Test |
| 106 | + void runShouldHaveEndpointBean() { |
| 107 | + this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include=sessions") |
| 108 | + .run((context) -> assertThat(context).hasSingleBean(ReactiveSessionsEndpoint.class)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void runWhenNotExposedShouldNotHaveEndpointBean() { |
| 113 | + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ReactiveSessionsEndpoint.class)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() { |
| 118 | + this.contextRunner.withPropertyValues("management.endpoint.sessions.enabled:false") |
| 119 | + .run((context) -> assertThat(context).doesNotHaveBean(ReactiveSessionsEndpoint.class)); |
| 120 | + } |
| 121 | + |
| 122 | + @Configuration(proxyBeanMethods = false) |
| 123 | + static class ReactiveSessionRepositoryConfiguration { |
| 124 | + |
| 125 | + @Bean |
| 126 | + ReactiveSessionRepository<?> sessionRepository() { |
| 127 | + return mock(ReactiveSessionRepository.class); |
| 128 | + } |
61 | 129 |
|
62 |
| - @Bean |
63 |
| - FindByIndexNameSessionRepository<?> sessionRepository() { |
64 |
| - return mock(FindByIndexNameSessionRepository.class); |
65 | 130 | }
|
66 | 131 |
|
67 | 132 | }
|
|
0 commit comments