-
Couldn't load subscription status.
- Fork 6.2k
Add Support SupplierReactiveClientRegistrationRepository #16770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||
| /* | ||||||||
| * Copyright 2002-2025 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 | ||||||||
| * | ||||||||
| * https://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.security.oauth2.client.registration; | ||||||||
|
|
||||||||
| import java.util.Iterator; | ||||||||
| import java.util.function.Supplier; | ||||||||
|
|
||||||||
| import reactor.core.publisher.Mono; | ||||||||
|
|
||||||||
| import org.springframework.util.Assert; | ||||||||
| import org.springframework.util.function.SingletonSupplier; | ||||||||
|
|
||||||||
| /** | ||||||||
| * A {@link ReactiveClientRegistrationRepository} that lazily calls to retrieve | ||||||||
| * {@link ClientRegistration}(s) when requested. | ||||||||
| * | ||||||||
| * @author Max Batischev | ||||||||
| * @since 6.5 | ||||||||
| * @see ReactiveClientRegistrationRepository | ||||||||
| * @see ClientRegistration | ||||||||
| */ | ||||||||
| public final class SupplierReactiveClientRegistrationRepository | ||||||||
| implements ReactiveClientRegistrationRepository, Iterable<ClientRegistration> { | ||||||||
|
|
||||||||
| private final Supplier<? extends ReactiveClientRegistrationRepository> repositorySupplier; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| /** | ||||||||
| * Constructs an {@code SupplierReactiveClientRegistrationRepository} using the | ||||||||
| * provided parameters. | ||||||||
| * @param repositorySupplier the client registration repository supplier | ||||||||
| */ | ||||||||
| public <T extends ReactiveClientRegistrationRepository & Iterable<ClientRegistration>> SupplierReactiveClientRegistrationRepository( | ||||||||
| Supplier<? extends ReactiveClientRegistrationRepository> repositorySupplier) { | ||||||||
|
Comment on lines
+46
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| Assert.notNull(repositorySupplier, "repositorySupplier cannot be null"); | ||||||||
| this.repositorySupplier = SingletonSupplier.of(repositorySupplier); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you able to change this logic to reflect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @franticticktick The problem is that if |
||||||||
| } | ||||||||
|
|
||||||||
| @Override | ||||||||
| public Mono<ClientRegistration> findByRegistrationId(String registrationId) { | ||||||||
| Assert.hasText(registrationId, "registrationId cannot be empty"); | ||||||||
| return this.repositorySupplier.get().findByRegistrationId(registrationId); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns an {@code Iterator} of {@link ClientRegistration}. | ||||||||
| * @return an {@code Iterator<ClientRegistration>} | ||||||||
| */ | ||||||||
| @Override | ||||||||
| public Iterator<ClientRegistration> iterator() { | ||||||||
| return ((Iterable<ClientRegistration>) this.repositorySupplier.get()).iterator(); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the generic type is done correctly, then this should not need to be cast. I've added hints in the code how to do this |
||||||||
| } | ||||||||
|
|
||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Copyright 2002-2025 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 | ||
| * | ||
| * https://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.security.oauth2.client.registration; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
| import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| /** | ||
| * Tests for {@link SupplierReactiveClientRegistrationRepository}. | ||
| * | ||
| * @author Max Batischev | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| public class SupplierReactiveClientRegistrationRepositoryTests { | ||
|
|
||
| private final ClientRegistration registration = TestClientRegistrations.clientRegistration().build(); | ||
|
|
||
| private final SupplierReactiveClientRegistrationRepository registrationRepository = new SupplierReactiveClientRegistrationRepository( | ||
| () -> new InMemoryReactiveClientRegistrationRepository(this.registration)); | ||
|
|
||
| @Mock | ||
| private Supplier<InMemoryReactiveClientRegistrationRepository> clientRegistrationRepositorySupplier; | ||
|
|
||
| @Test | ||
| void constructWhenNullThenIllegalArgumentException() { | ||
| assertThatIllegalArgumentException().isThrownBy(() -> new SupplierReactiveClientRegistrationRepository(null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void findRegistrationWhenRegistrationIsPresentThenReturns() { | ||
| String id = this.registration.getRegistrationId(); | ||
| assertThat(this.registrationRepository.findByRegistrationId(id).block()).isEqualTo(this.registration); | ||
| } | ||
|
|
||
| @Test | ||
| public void findRegistrationWhenRegistrationIsNotPresentThenNull() { | ||
| String id = this.registration.getRegistrationId() + "MISSING"; | ||
| assertThat(this.registrationRepository.findByRegistrationId(id).block()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void findRegistrationWhenNullIdIsPresentThenThrowsException() { | ||
| assertThatIllegalArgumentException() | ||
| .isThrownBy(() -> this.registrationRepository.findByRegistrationId(null).block()); | ||
| } | ||
|
|
||
| @Test | ||
| public void findRegistrationWhenIdIsPresentThenSingletonSupplierCached() { | ||
| given(this.clientRegistrationRepositorySupplier.get()) | ||
| .willReturn(new InMemoryReactiveClientRegistrationRepository(this.registration)); | ||
| SupplierReactiveClientRegistrationRepository test = new SupplierReactiveClientRegistrationRepository( | ||
| this.clientRegistrationRepositorySupplier); | ||
|
|
||
| String id = this.registration.getRegistrationId(); | ||
| assertThat(test.findByRegistrationId(id).block()).isEqualTo(this.registration); | ||
|
|
||
| id = this.registration.getRegistrationId(); | ||
| assertThat(test.findByRegistrationId(id).block()).isEqualTo(this.registration); | ||
| verify(this.clientRegistrationRepositorySupplier, times(1)).get(); | ||
| } | ||
|
|
||
| @Test | ||
| public void iteratorWhenRemoveThenThrowsUnsupportedOperationException() { | ||
| assertThatExceptionOfType(UnsupportedOperationException.class) | ||
| .isThrownBy(this.registrationRepository.iterator()::remove); | ||
| } | ||
|
|
||
| @Test | ||
| public void iteratorWhenGetThenContainsAll() { | ||
| assertThat(this.registrationRepository).containsOnly(this.registration); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.