package com.atlassian.jira.plugins.dvcs.spi.bitbucket.clientlibrary.restpoints;

import com.atlassian.jira.plugins.dvcs.spi.bitbucket.clientlibrary.client.BitbucketRemoteClient;
import com.atlassian.jira.plugins.dvcs.spi.bitbucket.clientlibrary.request.BitbucketRequestException;
import com.atlassian.jira.plugins.dvcs.spi.bitbucket.clientlibrary.request.scribe.ThreeLegged10aOauthProvider;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * This test is about getting rid of unnecessary "java.net.ProtocolException: Server redirected too many  times (20)".
 * That was caused by not properly encoded URL paths together with OAuth authentication.
 *
 * @author Martin Skurla
 */
public class URLPathsShouldBeCorrectlyEncodedTest
{
    private static final String OAUTH_KEY          = "PEruYUbd3vTdnSPYez";
    private static final String OAUTH_SECRET       = "hwP4CP9ZtAPMR9UJH59q8mnAWx7NSRpp";
    private static final String OAUTH_ACCESS_TOKEN = "qUeN7w3cGVwFhgcDtu&T7Ex3kMQ6TzAg8GeskdeD3ERLPQc65sV";

    private static AccountRemoteRestpoint accountRemoteRestpoint;


    @BeforeClass
    public static void initializeAccountREST()
    {
        BitbucketRemoteClient bitbucketRemoteClientWithOAuthAuth =
                new BitbucketRemoteClient(new ThreeLegged10aOauthProvider(BitbucketRemoteClient.BITBUCKET_URL,
                                                                          OAUTH_KEY,
                                                                          OAUTH_SECRET,
                                                                          OAUTH_ACCESS_TOKEN));

        accountRemoteRestpoint = bitbucketRemoteClientWithOAuthAuth.getAccountRest();
    }


    @Test(expectedExceptions = BitbucketRequestException.NotFound_404.class)
    public void performingGETRequestWithOAuthAuthentication_ShouldNotThrowProtocolException()
    {
        // performing GET with not properly encoded URL used to throw ProtocolException
        accountRemoteRestpoint.getUser("fake User");
    }

    @Test(expectedExceptions = BitbucketRequestException.NotFound_404.class)
    public void performingPOSTRequestWithOAuthAuthentication_ShouldNotThrowProtocolException()
    {
        // performing POST with not properly encoded URL used to return HTTP 302 status code
        accountRemoteRestpoint.inviteUser("a", "b", "c", "d e");
    }
}

@@ -72,24 +72,107 @@
 tpoints;%0D%0A%0D%0A
+import org.testng.annotations.BeforeClass;%0D%0Aimport org.testng.annotations.Test;%0D%0A%0D%0A
 import com.a
@@ -478,91 +478,8 @@
 %0D%0A%0D%0A
-import org.testng.annotations.BeforeClass;%0D%0Aimport org.testng.annotations.Test;%0D%0A%0D%0A
 /**%0D
@@ -2140,34 +2140,103 @@
 ception.
-NotFound_404.class
+class, expectedExceptionsMessageRegExp = %22Error response code during the request : 400%22
 )%0D%0A    p

Change in bitbucket response? Adjusting test