DATACMNS-1108 - Add support for Reactive SpEL Context retrieval.

We now support reactive EvaluationContext retrieval in conjunction with ReactiveEvaluationContextExtensions. Reactive SpEL Context extensions may access Reactor's Context and provide contextual details during SpEL evaluation.

Original Pull Request: #454
This commit is contained in:
Mark Paluch
2020-06-29 16:08:47 +02:00
committed by Christoph Strobl
parent 8a9033fdd5
commit 1c354f6d4e
17 changed files with 1175 additions and 126 deletions

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2020 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.data.spel;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
/**
* Unit tests for {@link ExpressionDependencies}.
*
* @author Mark Paluch
*/
class ExpressionDependenciesUnitTests {
SpelExpressionParser PARSER = new SpelExpressionParser();
@Test
void shouldExtractDependencies() {
String expression = "hasRole('ROLE_ADMIN') ? '%' : principal.emailAddress";
SpelExpression spelExpression = (SpelExpression) PARSER.parseExpression(expression);
SpelNode ast = spelExpression.getAST();
ExpressionDependencies dependencies = ExpressionDependencies.discover(ast, true);
assertThat(dependencies).extracting(ExpressionDependencies.ExpressionDependency::getSymbol).containsOnly("hasRole",
"principal");
}
@Test
void shouldExtractDependenciesFromMethodCallArgs() {
String expression = "hasRole(principal.emailAddress)";
SpelExpression spelExpression = (SpelExpression) PARSER.parseExpression(expression);
SpelNode ast = spelExpression.getAST();
ExpressionDependencies dependencies = ExpressionDependencies.discover(ast, true);
assertThat(dependencies).extracting(ExpressionDependencies.ExpressionDependency::getSymbol).containsOnly("hasRole",
"principal");
}
@Test
void shouldExtractFirstMethodAsDependency() {
String expression = "hello().hasRole(principal.emailAddress, principal.somethingElse).somethingElse()";
SpelExpression spelExpression = (SpelExpression) PARSER.parseExpression(expression);
SpelNode ast = spelExpression.getAST();
ExpressionDependencies dependencies = ExpressionDependencies.discover(ast, true);
assertThat(dependencies).extracting(ExpressionDependencies.ExpressionDependency::getSymbol).containsOnly("hello",
"principal");
}
}

View File

@@ -0,0 +1,229 @@
/*
* Copyright 2020 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.data.spel;
import static org.assertj.core.api.Assertions.*;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.spel.spi.EvaluationContextExtension;
import org.springframework.data.spel.spi.ReactiveEvaluationContextExtension;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
/**
* Unit tests for {@link ReactiveExtensionAwareEvaluationContextProvider}.
*
* @author Mark Paluch
*/
class ReactiveExtensionAwareEvaluationContextProviderUnitTests {
SpelExpressionParser PARSER = new SpelExpressionParser();
@BeforeEach
void beforeEach() {
GenericModelRoot.creationCounter.set(0);
SecurityExpressionRoot.creationCounter.set(0);
}
@Test // DATACMNS-1108
void shouldResolveExtension() {
Expression expression = PARSER.parseExpression("hasRole('ROLE_ADMIN') ? '%' : principal.name");
ExpressionDependencies dependencies = ExpressionDependencies.discover(expression);
ReactiveExtensionAwareEvaluationContextProvider provider = new ReactiveExtensionAwareEvaluationContextProvider(
Arrays.asList(SampleReactiveExtension.INSTANCE, GenericExtension.INSTANCE));
provider.getEvaluationContext(new Object[0], dependencies).map(expression::getValue) //
.as(StepVerifier::create) //
.expectNext("Walter") //
.verifyComplete();
assertThat(GenericModelRoot.creationCounter).hasValue(1);
assertThat(SecurityExpressionRoot.creationCounter).hasValue(1);
}
@Test // DATACMNS-1108
void shouldLoadGenericExtensionOnly() {
Expression expression = PARSER.parseExpression("isKnown('FOO')");
ExpressionDependencies dependencies = ExpressionDependencies.discover(expression);
ReactiveExtensionAwareEvaluationContextProvider provider = new ReactiveExtensionAwareEvaluationContextProvider(
Arrays.asList(SampleReactiveExtension.INSTANCE, GenericExtension.INSTANCE));
provider.getEvaluationContext(new Object[0], dependencies).map(expression::getValue) //
.as(StepVerifier::create) //
.expectNext(true) //
.verifyComplete();
assertThat(GenericModelRoot.creationCounter).hasValue(1);
assertThat(SecurityExpressionRoot.creationCounter).hasValue(0);
}
@Test // DATACMNS-1108
void unknownMethodShouldLoadGenericExtensionOnly() {
Expression expression = PARSER.parseExpression("unknown('FOO')");
ExpressionDependencies dependencies = ExpressionDependencies.discover(expression);
ReactiveExtensionAwareEvaluationContextProvider provider = new ReactiveExtensionAwareEvaluationContextProvider(
Arrays.asList(SampleReactiveExtension.INSTANCE, GenericExtension.INSTANCE));
provider.getEvaluationContext(new Object[0], dependencies).map(expression::getValue) //
.as(StepVerifier::create) //
.verifyError(SpelEvaluationException.class);
assertThat(GenericModelRoot.creationCounter).hasValue(1);
assertThat(SecurityExpressionRoot.creationCounter).hasValue(0);
}
@Test // DATACMNS-1108
void genericReactiveExtensionIsAlwaysObtained() {
Expression expression = PARSER.parseExpression("1+1");
ExpressionDependencies dependencies = ExpressionDependencies.discover(expression);
ReactiveExtensionAwareEvaluationContextProvider provider = new ReactiveExtensionAwareEvaluationContextProvider(
Collections.singletonList(GenericReactiveExtension.INSTANCE));
provider.getEvaluationContext(new Object[0], dependencies).map(expression::getValue) //
.as(StepVerifier::create) //
.expectNext(2) //
.verifyComplete();
assertThat(GenericModelRoot.creationCounter).hasValue(1);
}
/**
* Sample reactive extension exposing a concrete type.
*/
enum SampleReactiveExtension implements ReactiveEvaluationContextExtension {
INSTANCE;
@Override
public Mono<ExpressiveExtension> getExtension() {
return Mono.just(ExpressiveExtension.INSTANCE);
}
@Override
public String getExtensionId() {
return "reactive";
}
}
/**
* Sample reactive extension exposing a generic type.
*/
enum GenericReactiveExtension implements ReactiveEvaluationContextExtension {
INSTANCE;
@Override
public Mono<EvaluationContextExtension> getExtension() {
return Mono.just(GenericExtension.INSTANCE);
}
@Override
public String getExtensionId() {
return "reactive";
}
}
/**
* Extension exposing a concrete root object type.
*/
enum ExpressiveExtension implements EvaluationContextExtension {
INSTANCE;
@Override
public String getExtensionId() {
return "expressive";
}
@Override
public SecurityExpressionRoot getRootObject() {
return new SecurityExpressionRoot();
}
}
/**
* Extension without exposing a concrete extension type.
*/
enum GenericExtension implements EvaluationContextExtension {
INSTANCE;
@Override
public String getExtensionId() {
return "generic";
}
@Override
public Object getRootObject() {
return new GenericModelRoot();
}
}
public static class GenericModelRoot {
static AtomicInteger creationCounter = new AtomicInteger();
GenericModelRoot() {
creationCounter.incrementAndGet();
}
public boolean isKnown(String role) {
return role.equals("FOO");
}
}
public static class SecurityExpressionRoot {
static AtomicInteger creationCounter = new AtomicInteger();
SecurityExpressionRoot() {
creationCounter.incrementAndGet();
}
public boolean hasRole(String role) {
return role.equals("ADMIN");
}
public Object getPrincipal() {
return new MyPrincipal();
}
}
static class MyPrincipal {
public String getName() {
return "Walter";
}
}
}