### Question:
OAuth2ScopeCondition extends EntitlementConditionAdaptor { @Override public ConditionDecision evaluate(String realm, Subject subject, String resourceName, Map<String, Set<String>> env) throws EntitlementException { if (debug.messageEnabled()) { debug.message("Entering OAuth2ScopeCondition.getConditionDecision(). Required scopes=" + requiredScopes + ". Request scopes=" + (env == null ? "<missing>" : env.get(REQUEST_SCOPE_ATTRIBUTE))); } Set<String> scopeParams = (env == null ? null : env.get(REQUEST_SCOPE_ATTRIBUTE)); String scopes = ""; if (scopeParams != null && !scopeParams.isEmpty()) { scopes = scopeParams.iterator().next(); } boolean allowed = false; try { Set<String> requestScopes = toScopeSet(scopes); allowed = requestScopes.containsAll(requiredScopes); } catch (EntitlementException e) { if (debug.messageEnabled()) { debug.message("Invalid scope in request: " + e.getMessage(), e); } } if (debug.messageEnabled()) { debug.message("OAuth2ScopeCondition decision: " + allowed); } return new ConditionDecision(allowed, Collections.<String, Set<String>>emptyMap()); } OAuth2ScopeCondition(); @Override void setState(String state); @Override String getState(); @Override ConditionDecision evaluate(String realm, Subject subject, String resourceName, Map<String, Set<String>> env); @Override String toString(); Set<String> getRequiredScopes(); void setRequiredScopes(Set<String> requiredScopes); @Override void validate(); @Override boolean equals(Object obj); @Override int hashCode(); static final String REQUEST_SCOPE_ATTRIBUTE; }### Answer:
@Test public void conditionShouldEvaluateToFalseWhenNoRequiredScopesSetAndNoneSetInEnvironment() throws EntitlementException { String realm = "REALM"; Subject subject = new Subject(); String resourceName = "RESOURCE_NAME"; Map<String, Set<String>> env = new HashMap<String, Set<String>>(); ConditionDecision decision = condition.evaluate(realm, subject, resourceName, env); assertThat(decision.isSatisfied()).isFalse(); assertThat(decision.getAdvice()).isEmpty(); }
@Test public void conditionShouldEvaluateToFalseWhenNoRequiredScopesSetAndEmptyScopeSetInEnvironment() throws EntitlementException { String realm = "REALM"; Subject subject = new Subject(); String resourceName = "RESOURCE_NAME"; Map<String, Set<String>> env = new HashMap<String, Set<String>>(); env.put("scope", Collections.singleton("")); ConditionDecision decision = condition.evaluate(realm, subject, resourceName, env); assertThat(decision.isSatisfied()).isFalse(); assertThat(decision.getAdvice()).isEmpty(); }
@Test public void conditionShouldEvaluateToTrueWhenNoRequiredScopeSetAndSingleScopeSetInEnvironment() throws EntitlementException { String realm = "REALM"; Subject subject = new Subject(); String resourceName = "RESOURCE_NAME"; Map<String, Set<String>> env = new HashMap<String, Set<String>>(); env.put("scope", Collections.singleton("cn")); ConditionDecision decision = condition.evaluate(realm, subject, resourceName, env); assertThat(decision.isSatisfied()).isTrue(); assertThat(decision.getAdvice()).isEmpty(); }