Allow in-memory client registration repos to be constructed with a map

Fixes gh-5918
This commit is contained in:
Vedran Pavic 2018-10-04 07:41:35 +02:00 committed by Joe Grandja
parent 8f49ca850a
commit e1b095df32
4 changed files with 65 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -34,6 +34,7 @@ import static java.util.stream.Collectors.toConcurrentMap;
*
* @author Joe Grandja
* @author Rob Winch
* @author Vedran Pavic
* @since 5.0
* @see ClientRegistrationRepository
* @see ClientRegistration
@ -56,11 +57,26 @@ public final class InMemoryClientRegistrationRepository implements ClientRegistr
* @param registrations the client registration(s)
*/
public InMemoryClientRegistrationRepository(List<ClientRegistration> registrations) {
this(createRegistrationsMap(registrations));
}
private static Map<String, ClientRegistration> createRegistrationsMap(List<ClientRegistration> registrations) {
Assert.notEmpty(registrations, "registrations cannot be empty");
Collector<ClientRegistration, ?, ConcurrentMap<String, ClientRegistration>> collector =
toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity());
this.registrations = registrations.stream()
.collect(collectingAndThen(collector, Collections::unmodifiableMap));
return registrations.stream().collect(collectingAndThen(collector, Collections::unmodifiableMap));
}
/**
* Constructs an {@code InMemoryClientRegistrationRepository} using the provided {@code Map}
* of {@link ClientRegistration#getRegistrationId() registration id} to {@link ClientRegistration}.
*
* @since 5.2
* @param registrations the {@code Map} of client registration(s)
*/
public InMemoryClientRegistrationRepository(Map<String, ClientRegistration> registrations) {
Assert.notNull(registrations, "registrations cannot be null");
this.registrations = registrations;
}
@Override

View File

@ -30,6 +30,7 @@ import reactor.core.publisher.Mono;
* A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory.
*
* @author Rob Winch
* @author Vedran Pavic
* @since 5.1
* @see ClientRegistrationRepository
* @see ClientRegistration
@ -64,6 +65,18 @@ public final class InMemoryReactiveClientRegistrationRepository
.collect(Collectors.toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity()));
}
/**
* Constructs an {@code InMemoryReactiveClientRegistrationRepository} using the provided {@code Map}
* of {@link ClientRegistration#getRegistrationId() registration id} to {@link ClientRegistration}.
* <b>NOTE:</b> The supplied {@code Map} must be a non-blocking {@code Map}.
*
* @since 5.2
* @param registrations the {@code Map} of client registration(s)
*/
public InMemoryReactiveClientRegistrationRepository(Map<String, ClientRegistration> registrations) {
Assert.notNull(registrations, "registrations cannot be null");
this.clientIdToClientRegistration = registrations;
}
@Override
public Mono<ClientRegistration> findByRegistrationId(String registrationId) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -20,7 +20,9 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
@ -28,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link InMemoryClientRegistrationRepository}.
*
* @author Rob Winch
* @author Vedran Pavic
* @since 5.0
*/
public class InMemoryClientRegistrationRepositoryTests {
@ -53,6 +56,17 @@ public class InMemoryClientRegistrationRepositoryTests {
new InMemoryClientRegistrationRepository(registrations);
}
@Test(expected = IllegalArgumentException.class)
public void constructorMapClientRegistrationWhenNullThenIllegalArgumentException() {
new InMemoryClientRegistrationRepository((Map<String, ClientRegistration>) null);
}
@Test
public void constructorMapClientRegistrationWhenEmptyMapThenRepositoryIsEmpty() {
InMemoryClientRegistrationRepository clients = new InMemoryClientRegistrationRepository(new HashMap<>());
assertThat(clients).isEmpty();
}
@Test
public void findByRegistrationIdWhenFoundThenFound() {
String id = this.registration.getRegistrationId();

View File

@ -19,7 +19,9 @@ package org.springframework.security.oauth2.client.registration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
@ -28,6 +30,7 @@ import reactor.test.StepVerifier;
/**
* @author Rob Winch
* @author Vedran Pavic
* @since 5.1
*/
public class InMemoryReactiveClientRegistrationRepositoryTests {
@ -68,6 +71,20 @@ public class InMemoryReactiveClientRegistrationRepositoryTests {
.isInstanceOf(IllegalArgumentException.class);
}
@Test
public void constructorWhenClientRegistrationMapIsNullThenIllegalArgumentException() {
Map<String, ClientRegistration> registrations = null;
assertThatThrownBy(() -> new InMemoryReactiveClientRegistrationRepository(registrations))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
public void constructorWhenClientRegistrationMapIsEmptyThenRepositoryIsEmpty() {
InMemoryReactiveClientRegistrationRepository repository = new InMemoryReactiveClientRegistrationRepository(
new HashMap<>());
assertThat(repository).isEmpty();
}
@Test
public void findByRegistrationIdWhenValidIdThenFound() {
StepVerifier.create(this.repository.findByRegistrationId(this.registration.getRegistrationId()))