// Path: src/main/java/org/dmfs/oauth2/client/errors/TokenRequestError.java
// public final class TokenRequestError extends ProtocolError
// {
//     private static final long serialVersionUID = 1L;
// 
//     private final String mErrorResponse;
// 
//     /**
//      * {@link JSONObject} is not a {@link Serializable}, so we mark it transient and restore it from String if necessary.
//      */
//     private transient JSONObject mErrorObject;
// 
// 
//     public TokenRequestError(JSONObject errorObject) throws JSONException
//     {
//         super(errorObject.getString("error"));
//         mErrorObject = errorObject;
//         mErrorResponse = errorObject.toString();
//     }
// 
// 
//     /**
//      * Returns the error code token that was returned by the server.
//      *
//      * @return A String containing the error code token.
//      */
//     public String error()
//     {
//         return errorObject().optString("error", "unknown");
//     }
// 
// 
//     /**
//      * Returns the error description that was returned by the server.
//      *
//      * @return A String containing the error description.
//      */
//     public String description()
//     {
//         return errorObject().optString("error_description", "");
//     }
// 
// 
//     /**
//      * Returns the optional error URI returned by the server.
//      *
//      * @return A URI pointing to a more descriptive error page or <code>null</code>.
//      */
//     public URI uri()
//     {
//         return errorObject().has("error_uri") ? URI.create(errorObject().optString("error_uri")) : null;
//     }
// 
// 
//     private JSONObject errorObject()
//     {
//         if (mErrorObject == null)
//         {
//             try
//             {
//                 mErrorObject = new JSONObject(mErrorResponse);
//             }
//             catch (JSONException e)
//             {
//                 throw new RuntimeException(String.format("Can't restore JSONObject from String", mErrorResponse), e);
//             }
//         }
//         return mErrorObject;
//     }
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.dmfs.httpessentials.HttpStatus;
import org.dmfs.httpessentials.exceptions.ProtocolError;
import org.dmfs.httpessentials.exceptions.ProtocolException;
import org.dmfs.httpessentials.headers.EmptyHeaders;
import org.dmfs.httpessentials.mockutils.entities.StaticMockResponseEntity;
import org.dmfs.httpessentials.mockutils.responses.StaticMockResponse;
import org.dmfs.httpessentials.types.StructuredMediaType;
import org.dmfs.oauth2.client.errors.TokenRequestError;
import org.junit.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
/*
 * Copyright 2016 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.http.responsehandlers;





/**
 * Test the {@link TokenErrorResponseHandler}.
 * <p/>
 * TODO: also test some invalid responses.
 *
 * @author Marten Gajda <marten@dmfs.org>
 */
public class TokenErrorResponseHandlerTest
{

    @Test
    public void testHandleResponseNoUri() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException
    {
        final String accessTokenResponse = "{" + "\"error\":\"invalid_grant\"," + "\"error_description\":\"DESCRIPTION\"," + "}";

        try
        {
            new TokenErrorResponseHandler().handleResponse(
                    new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(
                            new StructuredMediaType("application", "json", "utf-8"), accessTokenResponse)));
            fail("No exception thrown");
        }
// Path: src/main/java/org/dmfs/oauth2/client/errors/TokenRequestError.java
// public final class TokenRequestError extends ProtocolError
// {
//     private static final long serialVersionUID = 1L;
// 
//     private final String mErrorResponse;
// 
//     /**
//      * {@link JSONObject} is not a {@link Serializable}, so we mark it transient and restore it from String if necessary.
//      */
//     private transient JSONObject mErrorObject;
// 
// 
//     public TokenRequestError(JSONObject errorObject) throws JSONException
//     {
//         super(errorObject.getString("error"));
//         mErrorObject = errorObject;
//         mErrorResponse = errorObject.toString();
//     }
// 
// 
//     /**
//      * Returns the error code token that was returned by the server.
//      *
//      * @return A String containing the error code token.
//      */
//     public String error()
//     {
//         return errorObject().optString("error", "unknown");
//     }
// 
// 
//     /**
//      * Returns the error description that was returned by the server.
//      *
//      * @return A String containing the error description.
//      */
//     public String description()
//     {
//         return errorObject().optString("error_description", "");
//     }
// 
// 
//     /**
//      * Returns the optional error URI returned by the server.
//      *
//      * @return A URI pointing to a more descriptive error page or <code>null</code>.
//      */
//     public URI uri()
//     {
//         return errorObject().has("error_uri") ? URI.create(errorObject().optString("error_uri")) : null;
//     }
// 
// 
//     private JSONObject errorObject()
//     {
//         if (mErrorObject == null)
//         {
//             try
//             {
//                 mErrorObject = new JSONObject(mErrorResponse);
//             }
//             catch (JSONException e)
//             {
//                 throw new RuntimeException(String.format("Can't restore JSONObject from String", mErrorResponse), e);
//             }
//         }
//         return mErrorObject;
//     }
// }


// Path: src/test/java/org/dmfs/oauth2/client/http/responsehandlers/TokenErrorResponseHandlerTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.dmfs.httpessentials.HttpStatus;
import org.dmfs.httpessentials.exceptions.ProtocolError;
import org.dmfs.httpessentials.exceptions.ProtocolException;
import org.dmfs.httpessentials.headers.EmptyHeaders;
import org.dmfs.httpessentials.mockutils.entities.StaticMockResponseEntity;
import org.dmfs.httpessentials.mockutils.responses.StaticMockResponse;
import org.dmfs.httpessentials.types.StructuredMediaType;
import org.dmfs.oauth2.client.errors.TokenRequestError;
import org.junit.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;

/*
 * Copyright 2016 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.http.responsehandlers;





/**
 * Test the {@link TokenErrorResponseHandler}.
 * <p/>
 * TODO: also test some invalid responses.
 *
 * @author Marten Gajda <marten@dmfs.org>
 */
public class TokenErrorResponseHandlerTest
{

    @Test
    public void testHandleResponseNoUri() throws UnsupportedEncodingException, IOException, ProtocolError, ProtocolException
    {
        final String accessTokenResponse = "{" + "\"error\":\"invalid_grant\"," + "\"error_description\":\"DESCRIPTION\"," + "}";

        try
        {
            new TokenErrorResponseHandler().handleResponse(
                    new StaticMockResponse(HttpStatus.BAD_REQUEST, EmptyHeaders.INSTANCE, new StaticMockResponseEntity(
                            new StructuredMediaType("application", "json", "utf-8"), accessTokenResponse)));
            fail("No exception thrown");
        }
        catch (TokenRequestError e)