Skip to content

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

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,6 +34,7 @@
*
* @author Joe Grandja
* @author Rob Winch
* @author Vedran Pavic
* @since 5.0
* @see ClientRegistrationRepository
* @see ClientRegistration
Expand All @@ -56,11 +57,25 @@ public InMemoryClientRegistrationRepository(ClientRegistration... registrations)
* @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));
toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity());
return registrations.stream().collect(collectingAndThen(collector, Collections::unmodifiableMap));
}

/**
* Constructs an {@code InMemoryReactiveClientRegistrationRepository} backed by a {@link Map} of client ids to
* {@link ClientRegistration}s.
*
* @param registrations the map of client registration(s)
*/
public InMemoryClientRegistrationRepository(Map<String, ClientRegistration> registrations) {
Assert.notNull(registrations, "registrations cannot be null");
this.registrations = registrations;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory.
*
* @author Rob Winch
* @author Vedran Pavic
* @since 5.1
* @see ClientRegistrationRepository
* @see ClientRegistration
Expand Down Expand Up @@ -64,6 +65,16 @@ public InMemoryReactiveClientRegistrationRepository(List<ClientRegistration> reg
.collect(Collectors.toConcurrentMap(ClientRegistration::getRegistrationId, Function.identity()));
}

/**
* Constructs an {@code InMemoryReactiveClientRegistrationRepository} backed by a {@link Map} of client ids to
* {@link ClientRegistration}s. Note that the supplied map must be a non-blocking map.
*
* @param registrations the 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,14 +20,17 @@

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;

/**
* Tests for {@link InMemoryClientRegistrationRepository}.
*
* @author Rob Winch
* @author Vedran Pavic
* @since 5.0
*/
public class InMemoryClientRegistrationRepositoryTests {
Expand All @@ -53,6 +56,17 @@ public void constructorListClientRegistrationWhenDuplicateIdThenIllegalArgumentE
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
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;
Expand All @@ -28,6 +30,7 @@

/**
* @author Rob Winch
* @author Vedran Pavic
* @since 5.1
*/
public class InMemoryReactiveClientRegistrationRepositoryTests {
Expand Down Expand Up @@ -68,6 +71,20 @@ public void constructorWhenClientRegistrationIsNullThenIllegalArgumentException(
.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()))
Expand Down