|
| 1 | +/* |
| 2 | + * Copyright 2014-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.hazelcast; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import com.hazelcast.core.HazelcastInstance; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 29 | +import org.springframework.security.core.Authentication; |
| 30 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 31 | +import org.springframework.security.core.context.SecurityContext; |
| 32 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 33 | +import org.springframework.session.FlushMode; |
| 34 | +import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession; |
| 35 | +import org.springframework.test.context.ContextConfiguration; |
| 36 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 37 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +/** |
| 42 | + * Integration tests for {@link HazelcastIndexedSessionRepository} using embedded |
| 43 | + * topology, with flush mode set to immediate. |
| 44 | + * |
| 45 | + * @author Eleftheria Stein |
| 46 | + */ |
| 47 | +@ExtendWith(SpringExtension.class) |
| 48 | +@ContextConfiguration |
| 49 | +@WebAppConfiguration |
| 50 | +class FlushImmediateHazelcastIndexedSessionRepositoryITests { |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private HazelcastIndexedSessionRepository repository; |
| 54 | + |
| 55 | + @Test |
| 56 | + void createSessionWithSecurityContextAndFindByPrincipalName() { |
| 57 | + String username = "saves-" + System.currentTimeMillis(); |
| 58 | + |
| 59 | + HazelcastIndexedSessionRepository.HazelcastSession session = this.repository.createSession(); |
| 60 | + String sessionId = session.getId(); |
| 61 | + |
| 62 | + Authentication authentication = new UsernamePasswordAuthenticationToken(username, "password", |
| 63 | + AuthorityUtils.createAuthorityList("ROLE_USER")); |
| 64 | + SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); |
| 65 | + securityContext.setAuthentication(authentication); |
| 66 | + session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); |
| 67 | + |
| 68 | + this.repository.save(session); |
| 69 | + |
| 70 | + Map<String, HazelcastIndexedSessionRepository.HazelcastSession> findByPrincipalName = this.repository |
| 71 | + .findByPrincipalName(username); |
| 72 | + |
| 73 | + assertThat(findByPrincipalName).hasSize(1); |
| 74 | + assertThat(findByPrincipalName.keySet()).containsOnly(sessionId); |
| 75 | + |
| 76 | + this.repository.deleteById(sessionId); |
| 77 | + } |
| 78 | + |
| 79 | + @EnableHazelcastHttpSession(flushMode = FlushMode.IMMEDIATE) |
| 80 | + @Configuration |
| 81 | + static class HazelcastSessionConfig { |
| 82 | + |
| 83 | + @Bean |
| 84 | + HazelcastInstance hazelcastInstance() { |
| 85 | + return HazelcastITestUtils.embeddedHazelcastServer(); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments