Skip to content

Commit 9f56928

Browse files
justin-tayjzheaux
authored andcommitted
Add SupplierClientRegistrationRepository
Closes gh-12967
1 parent 1ff5eb6 commit 9f56928

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.Iterator;
20+
import java.util.function.Supplier;
21+
22+
import org.springframework.util.Assert;
23+
import org.springframework.util.function.SingletonSupplier;
24+
25+
/**
26+
* A {@link ClientRegistrationRepository} that lazily calls to retrieve
27+
* {@link ClientRegistration}(s) when requested.
28+
*
29+
* @author Justin Tay
30+
* @since 6.2
31+
* @see ClientRegistrationRepository
32+
* @see ClientRegistration
33+
*/
34+
public final class SupplierClientRegistrationRepository
35+
implements ClientRegistrationRepository, Iterable<ClientRegistration> {
36+
37+
private final Supplier<? extends ClientRegistrationRepository> repositorySupplier;
38+
39+
/**
40+
* Constructs an {@code SupplierClientRegistrationRepository} using the provided
41+
* parameters.
42+
* @param repositorySupplier the client registration repository supplier
43+
*/
44+
public <T extends ClientRegistrationRepository & Iterable<ClientRegistration>> SupplierClientRegistrationRepository(
45+
Supplier<T> repositorySupplier) {
46+
Assert.notNull(repositorySupplier, "repositorySupplier cannot be null");
47+
this.repositorySupplier = SingletonSupplier.of(repositorySupplier);
48+
}
49+
50+
@Override
51+
public ClientRegistration findByRegistrationId(String registrationId) {
52+
Assert.hasText(registrationId, "registrationId cannot be empty");
53+
return this.repositorySupplier.get().findByRegistrationId(registrationId);
54+
}
55+
56+
/**
57+
* Returns an {@code Iterator} of {@link ClientRegistration}.
58+
* @return an {@code Iterator<ClientRegistration>}
59+
*/
60+
@Override
61+
public Iterator<ClientRegistration> iterator() {
62+
return ((Iterable<ClientRegistration>) this.repositorySupplier.get()).iterator();
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)