|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.security.oauth2.client.registration; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 29 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 30 | +import static org.mockito.BDDMockito.given; |
| 31 | +import static org.mockito.Mockito.times; |
| 32 | +import static org.mockito.Mockito.verify; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link SupplierClientRegistrationRepository}. |
| 36 | + * |
| 37 | + * @author Justin Tay |
| 38 | + * @since 6.2 |
| 39 | + */ |
| 40 | +@ExtendWith(MockitoExtension.class) |
| 41 | +public class SupplierClientRegistrationRepositoryTests { |
| 42 | + |
| 43 | + private ClientRegistration registration = TestClientRegistrations.clientRegistration().build(); |
| 44 | + |
| 45 | + private SupplierClientRegistrationRepository clients = new SupplierClientRegistrationRepository( |
| 46 | + () -> new InMemoryClientRegistrationRepository(this.registration)); |
| 47 | + |
| 48 | + @Mock |
| 49 | + Supplier<InMemoryClientRegistrationRepository> clientRegistrationRepositorySupplier; |
| 50 | + |
| 51 | + @Test |
| 52 | + public void constructorMapClientRegistrationWhenNullThenIllegalArgumentException() { |
| 53 | + assertThatIllegalArgumentException().isThrownBy(() -> new SupplierClientRegistrationRepository(null)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void constructorMapClientRegistrationWhenEmptyMapThenRepositoryIsEmpty() { |
| 58 | + SupplierClientRegistrationRepository clients = new SupplierClientRegistrationRepository( |
| 59 | + () -> new InMemoryClientRegistrationRepository(Collections.emptyMap())); |
| 60 | + assertThat(clients).isEmpty(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void findByRegistrationIdWhenFoundThenFound() { |
| 65 | + String id = this.registration.getRegistrationId(); |
| 66 | + assertThat(this.clients.findByRegistrationId(id)).isEqualTo(this.registration); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void findByRegistrationIdWhenNotFoundThenNull() { |
| 71 | + String id = this.registration.getRegistrationId() + "MISSING"; |
| 72 | + assertThat(this.clients.findByRegistrationId(id)).isNull(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void findByRegistrationIdWhenNullIdThenIllegalArgumentException() { |
| 77 | + assertThatIllegalArgumentException().isThrownBy(() -> this.clients.findByRegistrationId(null)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void findByRegistrationIdThenSingletonSupplierCached() { |
| 82 | + SupplierClientRegistrationRepository test = new SupplierClientRegistrationRepository( |
| 83 | + this.clientRegistrationRepositorySupplier); |
| 84 | + given(this.clientRegistrationRepositorySupplier.get()) |
| 85 | + .willReturn(new InMemoryClientRegistrationRepository(this.registration)); |
| 86 | + String id = this.registration.getRegistrationId(); |
| 87 | + assertThat(test.findByRegistrationId(id)).isEqualTo(this.registration); |
| 88 | + id = this.registration.getRegistrationId(); |
| 89 | + assertThat(test.findByRegistrationId(id)).isEqualTo(this.registration); |
| 90 | + verify(this.clientRegistrationRepositorySupplier, times(1)).get(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void iteratorWhenRemoveThenThrowsUnsupportedOperationException() { |
| 95 | + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(this.clients.iterator()::remove); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void iteratorWhenGetThenContainsAll() { |
| 100 | + assertThat(this.clients).containsOnly(this.registration); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments