// Path: src/main/java/org/dmfs/oauth2/client/OAuth2Scope.java
// public interface OAuth2Scope
// {
//     /**
//      * Returns whether this scope is empty.
//      *
//      * @return <code>true</code> if this scope is empty, <code>false</code> otherwise.
//      */
//     boolean isEmpty();
// 
//     /**
//      * Returns whether this scope contains the given scope token or not.
//      *
//      * @param token
//      *         The scope token to check for.
//      *
//      * @return <code>true</code> if the scope token is contained in this scope, <code>false</code> otherwise.
//      */
//     boolean hasToken(String token);
// 
//     /**
//      * Returns the number of tokens in this {@link OAuth2Scope}.
//      *
//      * @return The number of tokens in this {@link OAuth2Scope}.
//      */
//     int tokenCount();
// 
//     /**
//      * Returns a string version of this scope as described in <a href="https://tools.ietf.org/html/rfc6749#section-3.3">RFC 6749, section 3.3</a>, i.e. a list
//      * of scope tokens separated by spaces.
//      *
//      * @return A String containing a list scope tokens, separated by spaces, or an empty String if {@link #isEmpty()} returns <code>true</code>.
//      */
//     String toString();
// }
// 
// Path: src/main/java/org/dmfs/oauth2/client/scope/StringScope.java
// public final class StringScope implements OAuth2Scope
// {
//     private final String mScope;
// 
// 
//     /**
//      * Creates an {@link OAuth2Scope} from the given space separated token list.
//      *
//      * @param scope
//      */
//     public StringScope(String scope)
//     {
//         mScope = scope;
//     }
// 
// 
//     @Override
//     public boolean hasToken(String token)
//     {
//         Iterator<CharSequence> tokenIterator = new UnquotedSplit(mScope, ' ');
//         while (tokenIterator.hasNext())
//         {
//             if (tokenIterator.next().toString().equals(token))
//             {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
// 
//     @Override
//     public int tokenCount()
//     {
//         if (mScope.isEmpty())
//         {
//             return 0;
//         }
//         int count = 0;
//         for (CharSequence token : new Split(mScope, ' '))
//         {
//             count += 1;
//         }
//         return count;
//     }
// 
// 
//     @Override
//     public boolean isEmpty()
//     {
//         return mScope.isEmpty();
//     }
// 
// 
//     @Override
//     public String toString()
//     {
//         return mScope;
//     }
// 
// 
//     @Override
//     public boolean equals(Object obj)
//     {
//         if (!(obj instanceof OAuth2Scope))
//         {
//             return false;
//         }
// 
//         OAuth2Scope other = (OAuth2Scope) obj;
//         if (isEmpty() && other.isEmpty())
//         {
//             return true;
//         }
// 
//         if (tokenCount() != other.tokenCount())
//         {
//             return false;
//         }
// 
//         Iterator<CharSequence> tokens = new UnquotedSplit(mScope, ' ');
//         while (tokens.hasNext())
//         {
//             if (!other.hasToken(tokens.next().toString()))
//             {
//                 return false;
//             }
//         }
//         return true;
//     }
// }


import org.dmfs.jems.hamcrest.matchers.optional.AbsentMatcher;
import org.dmfs.oauth2.client.OAuth2Scope;
import org.dmfs.oauth2.client.scope.StringScope;
import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.junit.Test;
import static org.dmfs.jems.hamcrest.matchers.optional.PresentMatcher.present;
import static org.dmfs.jems.mockito.doubles.TestDoubles.dummy;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/*
 * Copyright 2017 dmfs GmbH
 *
 * 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
 *
 *     http://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.dmfs.oauth2.client.tokens;




/**
 * @author Marten Gajda
 */
public class JsonAccessTokenTest
{
    @Test
    public void testNoScope() throws Exception
    {
        OAuth2Scope dummyScope = dummy(OAuth2Scope.class);
        JSONObject jsonObject = new JSONObject();
        assertThat(new JsonAccessToken(jsonObject, dummyScope).scope(), sameInstance(dummyScope));
    }


    @Test
    public void testScope() throws Exception
    {
        OAuth2Scope dummyScope = dummy(OAuth2Scope.class);
        // note: we don't use a mock here, because there are multiple ways to get a String value and we don't want to make assumptions on that
        JSONObject jsonObject = new JSONObject("{\"scope\": \"scope1 scope2\"}");

// Path: src/main/java/org/dmfs/oauth2/client/OAuth2Scope.java
// public interface OAuth2Scope
// {
//     /**
//      * Returns whether this scope is empty.
//      *
//      * @return <code>true</code> if this scope is empty, <code>false</code> otherwise.
//      */
//     boolean isEmpty();
// 
//     /**
//      * Returns whether this scope contains the given scope token or not.
//      *
//      * @param token
//      *         The scope token to check for.
//      *
//      * @return <code>true</code> if the scope token is contained in this scope, <code>false</code> otherwise.
//      */
//     boolean hasToken(String token);
// 
//     /**
//      * Returns the number of tokens in this {@link OAuth2Scope}.
//      *
//      * @return The number of tokens in this {@link OAuth2Scope}.
//      */
//     int tokenCount();
// 
//     /**
//      * Returns a string version of this scope as described in <a href="https://tools.ietf.org/html/rfc6749#section-3.3">RFC 6749, section 3.3</a>, i.e. a list
//      * of scope tokens separated by spaces.
//      *
//      * @return A String containing a list scope tokens, separated by spaces, or an empty String if {@link #isEmpty()} returns <code>true</code>.
//      */
//     String toString();
// }
// 
// Path: src/main/java/org/dmfs/oauth2/client/scope/StringScope.java
// public final class StringScope implements OAuth2Scope
// {
//     private final String mScope;
// 
// 
//     /**
//      * Creates an {@link OAuth2Scope} from the given space separated token list.
//      *
//      * @param scope
//      */
//     public StringScope(String scope)
//     {
//         mScope = scope;
//     }
// 
// 
//     @Override
//     public boolean hasToken(String token)
//     {
//         Iterator<CharSequence> tokenIterator = new UnquotedSplit(mScope, ' ');
//         while (tokenIterator.hasNext())
//         {
//             if (tokenIterator.next().toString().equals(token))
//             {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
// 
//     @Override
//     public int tokenCount()
//     {
//         if (mScope.isEmpty())
//         {
//             return 0;
//         }
//         int count = 0;
//         for (CharSequence token : new Split(mScope, ' '))
//         {
//             count += 1;
//         }
//         return count;
//     }
// 
// 
//     @Override
//     public boolean isEmpty()
//     {
//         return mScope.isEmpty();
//     }
// 
// 
//     @Override
//     public String toString()
//     {
//         return mScope;
//     }
// 
// 
//     @Override
//     public boolean equals(Object obj)
//     {
//         if (!(obj instanceof OAuth2Scope))
//         {
//             return false;
//         }
// 
//         OAuth2Scope other = (OAuth2Scope) obj;
//         if (isEmpty() && other.isEmpty())
//         {
//             return true;
//         }
// 
//         if (tokenCount() != other.tokenCount())
//         {
//             return false;
//         }
// 
//         Iterator<CharSequence> tokens = new UnquotedSplit(mScope, ' ');
//         while (tokens.hasNext())
//         {
//             if (!other.hasToken(tokens.next().toString()))
//             {
//                 return false;
//             }
//         }
//         return true;
//     }
// }


// Path: src/test/java/org/dmfs/oauth2/client/tokens/JsonAccessTokenTest.java
import org.dmfs.jems.hamcrest.matchers.optional.AbsentMatcher;
import org.dmfs.oauth2.client.OAuth2Scope;
import org.dmfs.oauth2.client.scope.StringScope;
import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.junit.Test;
import static org.dmfs.jems.hamcrest.matchers.optional.PresentMatcher.present;
import static org.dmfs.jems.mockito.doubles.TestDoubles.dummy;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/*
 * Copyright 2017 dmfs GmbH
 *
 * 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
 *
 *     http://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.dmfs.oauth2.client.tokens;




/**
 * @author Marten Gajda
 */
public class JsonAccessTokenTest
{
    @Test
    public void testNoScope() throws Exception
    {
        OAuth2Scope dummyScope = dummy(OAuth2Scope.class);
        JSONObject jsonObject = new JSONObject();
        assertThat(new JsonAccessToken(jsonObject, dummyScope).scope(), sameInstance(dummyScope));
    }


    @Test
    public void testScope() throws Exception
    {
        OAuth2Scope dummyScope = dummy(OAuth2Scope.class);
        // note: we don't use a mock here, because there are multiple ways to get a String value and we don't want to make assumptions on that
        JSONObject jsonObject = new JSONObject("{\"scope\": \"scope1 scope2\"}");

        assertThat(new JsonAccessToken(jsonObject, dummyScope).scope(), Matchers.<OAuth2Scope>is(new StringScope("scope1 scope2")));