public static OAuthBearerValidationResult validateScope(OAuthBearerToken token, List<String> requiredScope) {
        final Set<String> tokenScope = token.scope();
        if (requiredScope == null || requiredScope.isEmpty())
            return OAuthBearerValidationResult.newSuccess();
        for (String requiredScopeElement : requiredScope) {
            if (!tokenScope.contains(requiredScopeElement))
                return OAuthBearerValidationResult.newFailure(String.format(
                        "The provided scope (%s) was missing a required scope (%s).  All required scope elements: %s",
                        tokenScope, requiredScopeElement, requiredScope),
                        requiredScope.toString(), null);
        }
        return OAuthBearerValidationResult.newSuccess();
    }
@SuppressWarnings({"unchecked", "rawtypes"})
    @Test
    public void validateScope() {
        long nowMs = TIME.milliseconds();
        double nowClaimValue = ((double) nowMs) / 1000;
        final List<String> noScope = Collections.emptyList();
        final List<String> scope1 = Collections.singletonList("scope1");
        final List<String> scope1And2 = Arrays.asList("scope1", "scope2");
        for (boolean actualScopeExists : new boolean[] {true, false}) {
            List<? extends List> scopes = !actualScopeExists ? Collections.singletonList((List) null)
                    : Arrays.asList(noScope, scope1, scope1And2);
            for (List<String> actualScope : scopes) {
                for (boolean requiredScopeExists : new boolean[] {true, false}) {
                    List<? extends List> requiredScopes = !requiredScopeExists ? Collections.singletonList((List) null)
                            : Arrays.asList(noScope, scope1, scope1And2);
                    for (List<String> requiredScope : requiredScopes) {
                        StringBuilder sb = new StringBuilder("{");
                        appendJsonText(sb, "exp", nowClaimValue);
                        appendCommaJsonText(sb, "sub", "principalName");
                        if (actualScope != null)
                            sb.append(',').append(scopeJson(actualScope));
                        sb.append("}");
                        String compactSerialization = HEADER_COMPACT_SERIALIZATION + Base64.getUrlEncoder()
                                .withoutPadding().encodeToString(sb.toString().getBytes(StandardCharsets.UTF_8)) + ".";
                        OAuthBearerUnsecuredJws testJwt = new OAuthBearerUnsecuredJws(compactSerialization, "sub",
                                "scope");
                        OAuthBearerValidationResult result = OAuthBearerValidationUtils.validateScope(testJwt,
                                requiredScope);
                        if (!requiredScopeExists || requiredScope.isEmpty())
                            assertTrue(isSuccess(result));
                        else if (!actualScopeExists || actualScope.size() < requiredScope.size())
                            assertTrue(isFailureWithMessageAndFailureScope(result));
                        else
                            assertTrue(isSuccess(result));
                    }
                }
            }
        }
    }