|
| 1 | +/* |
| 2 | + * Copyright 2014-2016 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 | + * http://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; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.rules.ExpectedException; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.runners.MockitoJUnitRunner; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.mockito.BDDMockito.given; |
| 31 | +import static org.mockito.Matchers.any; |
| 32 | +import static org.mockito.Matchers.anyString; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.times; |
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for {@link SafeRetrievingSessionRepository}. |
| 40 | + * |
| 41 | + * @author Vedran Pavic |
| 42 | + */ |
| 43 | +@RunWith(MockitoJUnitRunner.class) |
| 44 | +public class SafeRetrievingSessionRepositoryTests { |
| 45 | + |
| 46 | + @Rule |
| 47 | + public ExpectedException thrown = ExpectedException.none(); |
| 48 | + |
| 49 | + @Mock |
| 50 | + private FindByIndexNameSessionRepository<ExpiringSession> delegate; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void createWithNullDelegateFails() { |
| 54 | + this.thrown.expect(IllegalArgumentException.class); |
| 55 | + this.thrown.expectMessage("Delegate must not be null"); |
| 56 | + new SafeRetrievingSessionRepository<Session>(null, |
| 57 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void createWithNullIgnoredExceptionsFails() { |
| 62 | + this.thrown.expect(IllegalArgumentException.class); |
| 63 | + this.thrown.expectMessage("Ignored exceptions must not be empty"); |
| 64 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, null); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void createWithEmptyIgnoredExceptionsFails() { |
| 69 | + this.thrown.expect(IllegalArgumentException.class); |
| 70 | + this.thrown.expectMessage("Ignored exceptions must not be empty"); |
| 71 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 72 | + Collections.<Class<? extends RuntimeException>>emptySet()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void createSession() { |
| 77 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 78 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 79 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 80 | + repository.createSession(); |
| 81 | + verify(this.delegate, times(1)).createSession(); |
| 82 | + verifyZeroInteractions(this.delegate); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void saveSession() { |
| 87 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 88 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 89 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 90 | + repository.save(new MapSession()); |
| 91 | + verify(this.delegate, times(1)).save(any(ExpiringSession.class)); |
| 92 | + verifyZeroInteractions(this.delegate); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void getSession() { |
| 97 | + ExpiringSession session = mock(ExpiringSession.class); |
| 98 | + given(this.delegate.getSession(anyString())).willReturn(session); |
| 99 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 100 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 101 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 102 | + assertThat(repository.getSession(UUID.randomUUID().toString())) |
| 103 | + .isEqualTo(session); |
| 104 | + verify(this.delegate, times(1)).getSession(anyString()); |
| 105 | + verifyZeroInteractions(this.delegate); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + public void getSessionThrowsIgnoredException() { |
| 111 | + given(this.delegate.getSession(anyString())).willThrow(RuntimeException.class); |
| 112 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 113 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 114 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 115 | + assertThat(repository.getSession(UUID.randomUUID().toString())).isNull(); |
| 116 | + verify(this.delegate, times(1)).getSession(anyString()); |
| 117 | + verify(this.delegate, times(1)).delete(anyString()); |
| 118 | + verifyZeroInteractions(this.delegate); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + public void getSessionThrowsIgnoredExceptionWithDeletionDisabled() { |
| 124 | + given(this.delegate.getSession(anyString())).willThrow(RuntimeException.class); |
| 125 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 126 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 127 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 128 | + repository.setDeleteOnIgnoredException(false); |
| 129 | + assertThat(repository.getSession(UUID.randomUUID().toString())).isNull(); |
| 130 | + verify(this.delegate, times(1)).getSession(anyString()); |
| 131 | + verifyZeroInteractions(this.delegate); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @SuppressWarnings("unchecked") |
| 136 | + public void getSessionThrowsNotIgnoredException() { |
| 137 | + this.thrown.expect(RuntimeException.class); |
| 138 | + given(this.delegate.getSession(anyString())).willThrow(RuntimeException.class); |
| 139 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 140 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 141 | + Collections.<Class<? extends RuntimeException>>singleton(IllegalStateException.class)); |
| 142 | + repository.getSession(UUID.randomUUID().toString()); |
| 143 | + verify(this.delegate, times(1)).getSession(anyString()); |
| 144 | + verifyZeroInteractions(this.delegate); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void deleteSession() { |
| 149 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 150 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 151 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 152 | + repository.delete(UUID.randomUUID().toString()); |
| 153 | + verify(this.delegate, times(1)).delete(anyString()); |
| 154 | + verifyZeroInteractions(this.delegate); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void findByIndexNameAndIndexValue() { |
| 159 | + ExpiringSession session = mock(ExpiringSession.class); |
| 160 | + given(this.delegate.findByIndexNameAndIndexValue(anyString(), anyString())) |
| 161 | + .willReturn(Collections.singletonMap("name", session)); |
| 162 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 163 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 164 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 165 | + assertThat(repository.findByIndexNameAndIndexValue("name", "value").get("name")) |
| 166 | + .isEqualTo(session); |
| 167 | + verify(this.delegate, times(1)).findByIndexNameAndIndexValue(anyString(), |
| 168 | + anyString()); |
| 169 | + verifyZeroInteractions(this.delegate); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + @SuppressWarnings("unchecked") |
| 174 | + public void findByIndexNameAndIndexValueThrowsIgnoredException() { |
| 175 | + given(this.delegate.findByIndexNameAndIndexValue(anyString(), anyString())) |
| 176 | + .willThrow(RuntimeException.class); |
| 177 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 178 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 179 | + Collections.<Class<? extends RuntimeException>>singleton(RuntimeException.class)); |
| 180 | + assertThat(repository.findByIndexNameAndIndexValue("name", "value")).isEmpty(); |
| 181 | + verify(this.delegate, times(1)).findByIndexNameAndIndexValue(anyString(), |
| 182 | + anyString()); |
| 183 | + verifyZeroInteractions(this.delegate); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + @SuppressWarnings("unchecked") |
| 188 | + public void findByIndexNameAndIndexValueThrowsNotIgnoredException() { |
| 189 | + this.thrown.expect(RuntimeException.class); |
| 190 | + given(this.delegate.findByIndexNameAndIndexValue(anyString(), anyString())) |
| 191 | + .willThrow(RuntimeException.class); |
| 192 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 193 | + new SafeRetrievingSessionRepository<ExpiringSession>(this.delegate, |
| 194 | + Collections.<Class<? extends RuntimeException>>singleton(IllegalStateException.class)); |
| 195 | + repository.findByIndexNameAndIndexValue("name", "value"); |
| 196 | + verify(this.delegate, times(1)).findByIndexNameAndIndexValue(anyString(), |
| 197 | + anyString()); |
| 198 | + verifyZeroInteractions(this.delegate); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + @SuppressWarnings("unchecked") |
| 203 | + public void findByIndexNameAndIndexValueOnSessionRepositoryThrowsException() { |
| 204 | + this.thrown.expect(UnsupportedOperationException.class); |
| 205 | + SessionRepository delegate = mock(SessionRepository.class); |
| 206 | + SafeRetrievingSessionRepository<ExpiringSession> repository = |
| 207 | + new SafeRetrievingSessionRepository<ExpiringSession>(delegate, |
| 208 | + Collections.<Class<? extends RuntimeException>>singleton(IllegalStateException.class)); |
| 209 | + repository.findByIndexNameAndIndexValue("name", "value"); |
| 210 | + verifyZeroInteractions(delegate); |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments