ConjunctAuthScopeExpression extends AbstractAuthScopeExpression { @Override public boolean isAuthorized(Set<String> userScopes) { if (innerExpressions.isEmpty()) { return false; } for (AuthScopeExpression innerExpression : innerExpressions) { if (!innerExpression.isAuthorized(userScopes)) { return false; } } return true; } ConjunctAuthScopeExpression(List<SingleAuthScopeExpression> innerExpressions); @Override String[] getAllScopes(); @Override boolean isAuthorized(Set<String> userScopes); @Override String toLoggingForm(); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void testIsAuthorizedWithNoInnerExpressions() { ConjunctAuthScopeExpression expression = new ConjunctAuthScopeExpression(ImmutableList.<SingleAuthScopeExpression>of()); assertFalse(expression.isAuthorized(ImmutableSet.of("scope0"))); }
@Test public void testIsAuthorizedWithSuccess() { Set<String> scopes = ImmutableSet.of("scope0", "scope1", "scope2", "scope3"); SingleAuthScopeExpression expression1 = mock(SingleAuthScopeExpression.class); when(expression1.isAuthorized(scopes)).thenReturn(true); SingleAuthScopeExpression expression2 = mock(SingleAuthScopeExpression.class); when(expression2.isAuthorized(scopes)).thenReturn(true); SingleAuthScopeExpression expression3 = mock(SingleAuthScopeExpression.class); when(expression3.isAuthorized(scopes)).thenReturn(true); ConjunctAuthScopeExpression expression = new ConjunctAuthScopeExpression( ImmutableList.<SingleAuthScopeExpression>of(expression1, expression2, expression3)); assertTrue(expression.isAuthorized(scopes)); }
@Test public void testIsAuthorizedWithFailure() { Set<String> scopes = ImmutableSet.of("scope0", "scope1", "scope2", "scope3"); SingleAuthScopeExpression expression1 = mock(SingleAuthScopeExpression.class); when(expression1.isAuthorized(scopes)).thenReturn(true); SingleAuthScopeExpression expression2 = mock(SingleAuthScopeExpression.class); SingleAuthScopeExpression expression3 = mock(SingleAuthScopeExpression.class); ConjunctAuthScopeExpression expression = new ConjunctAuthScopeExpression( ImmutableList.<SingleAuthScopeExpression>of(expression1, expression2, expression3)); assertFalse(expression.isAuthorized(scopes)); verify(expression3, never()).isAuthorized(scopes); }