Polish Logging and Tests

Removing debug statements which would have prematurely terminated the
stream, changing to AssertJ, and adding another test.

Issue: gh-3743
This commit is contained in:
Karl Goffin 2018-10-29 23:54:55 -04:00 committed by Josh Cummings
parent 92e68a589a
commit 50d26c9d28
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
2 changed files with 21 additions and 15 deletions

View File

@ -175,21 +175,12 @@ public class DefaultMethodSecurityExpressionHandler extends
if (filterTarget instanceof Stream) { if (filterTarget instanceof Stream) {
final Stream<?> original = (Stream<?>) filterTarget; final Stream<?> original = (Stream<?>) filterTarget;
if (debug) {
logger.debug("Filtering stream with " + original.count() + " elements");
}
Stream<?> filtered = original.filter(filterObject -> { return original.filter(filterObject -> {
rootObject.setFilterObject(filterObject); rootObject.setFilterObject(filterObject);
return ExpressionUtils.evaluateAsBoolean(filterExpression, ctx); return ExpressionUtils.evaluateAsBoolean(filterExpression, ctx);
}) })
.onClose(original::close); .onClose(original::close);
if (debug) {
logger.debug("Retaining elements: " + filtered.collect(Collectors.toList()));
}
return filtered;
} }
throw new IllegalArgumentException( throw new IllegalArgumentException(

View File

@ -15,11 +15,13 @@
*/ */
package org.springframework.security.access.expression.method; package org.springframework.security.access.expression.method;
import static org.mockito.Mockito.verify; import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -85,9 +87,22 @@ public class DefaultMethodSecurityExpressionHandlerTests {
Object filtered = handler.filter(stream, expression, context); Object filtered = handler.filter(stream, expression, context);
Assert.assertTrue("response was wrong type", Stream.class.isAssignableFrom(filtered.getClass())); assertThat(filtered).isInstanceOf(Stream.class);
List<String> list = ((Stream<String>) filtered).collect(Collectors.toList()); List<String> list = ((Stream<String>) filtered).collect(Collectors.toList());
Assert.assertEquals(2, list.size()); assertThat(list).containsExactly("1", "3");
Assert.assertFalse("contains filtered element", list.contains("2")); }
@Test
public void filterStreamWhenClosedThenUpstreamGetsClosed() {
final Stream<?> upstream = mock(Stream.class);
doReturn(Stream.<String>empty()).when(upstream).filter(any());
Expression expression = handler.getExpressionParser().parseExpression("true");
EvaluationContext context = handler.createEvaluationContext(authentication,
methodInvocation);
((Stream) handler.filter(upstream, expression, context)).close();
verify(upstream).close();
} }
} }