/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.cdancy.bitbucket.rest.filters;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.annotations.Test;

import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpRequest;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import static com.google.common.io.BaseEncoding.base64;

import org.testng.annotations.BeforeMethod;

/**
 * Unit tests for the {@link BitbucketAuthentication} class.
 */
@Test(groups = "unit", testName = "BitbucketAuthenticationMockTest")
public class BitbucketAuthenticationMockTest {

    private final String admin = "admin";
    private final String authorization = "authorization";
    private final String basic = "Basic ";

    private HttpRequest httpRequest;

    public BitbucketAuthenticationMockTest() { }

    @SuppressWarnings("unused")
    @BeforeMethod
    private void initHttpRequest() {
        httpRequest = HttpRequest.builder()
            .endpoint("http://domain.org")
            .method("GET")
            .build();
    }

    @Test
    public void testBasicAt() {
        final String credential = "admin:password";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,"basic@" + credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        httpRequest = bitbucketAuthentication.filter(httpRequest);
        assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(basic + base64().encode(credential.getBytes()));
    }

    @Test
    public void testBasicAtWithAtInPassword() {
        final String credential = "admin:pass@word";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,"basic@" + credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        httpRequest = bitbucketAuthentication.filter(httpRequest);
        assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(basic + base64().encode(credential.getBytes()));
    }

    @Test
    public void testBasic() {
        final String credential = "admin:password";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        httpRequest = bitbucketAuthentication.filter(httpRequest);
        assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(basic + base64().encode(credential.getBytes()));
    }

    @Test
    public void testBase64() {
        final String credential = "YWRtaW46cGFzc3dvcmQ=";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        httpRequest = bitbucketAuthentication.filter(httpRequest);
        assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(basic + credential);
    }

    @Test
    public void testBasicBase64() {
        final String credential = "YWRtaW46cGFzc3dvcmQ=";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,"basic@" + credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        httpRequest = bitbucketAuthentication.filter(httpRequest);
        assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(basic + credential);
    }

    @Test
    public void testBearer() {
        final String credential = "9DfK3AF9Jeke1O0dkKX5kDswps43FEDlf5Frkspma21M";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,"bearer@" + credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        httpRequest = bitbucketAuthentication.filter(httpRequest);
        assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo("Bearer " + credential);
    }

    @Test
    public void testBadAuthentication() {
        final String credential = "Bearer 9DfK3AF9Jeke1O0dkKX5kDswps43FEDlf5Frkspma21M";
        final Supplier<Credentials> creds = Suppliers.ofInstance(new Credentials(admin,credential));
        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);
        try {
            httpRequest = bitbucketAuthentication.filter(httpRequest);
        } catch (IllegalArgumentException exception) {
            assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(null);
        }
    }
}

@@ -1524,16 +1524,61 @@
 Basic %22;
+%0A    private final String basicAt = %22basic@%22;
 %0A%0A    pr
@@ -2066,32 +2066,31 @@
 tials(admin,
-%22
 basic
-@%22
+At
  + credentia
@@ -2387,32 +2387,574 @@
 tes()));%0A    %7D%0A%0A
+    @Test%0A    public void testBasicAtEmpty() %7B%0A        final String credential = %22%22;%0A        final Supplier%3CCredentials%3E creds = Suppliers.ofInstance(new Credentials(admin,basicAt + credential));%0A        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);%0A        try %7B%0A            httpRequest = bitbucketAuthentication.filter(httpRequest);%0A        %7D catch (IllegalArgumentException exception) %7B%0A            assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(null);%0A        %7D%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3131,32 +3131,31 @@
 tials(admin,
-%22
 basic
-@%22
+At
  + credentia
@@ -4618,24 +4618,23 @@
 s(admin,
-%22
 basic
-@%22
+At
  + crede
@@ -5420,32 +5420,575 @@
 ential);%0A    %7D%0A%0A
+    @Test%0A    public void testBearerEmpty() %7B%0A        final String credential = %22%22;%0A        final Supplier%3CCredentials%3E creds = Suppliers.ofInstance(new Credentials(admin,%22bearer@%22 + credential));%0A        final BitbucketAuthentication bitbucketAuthentication = new BitbucketAuthentication(creds);%0A        try %7B%0A            httpRequest = bitbucketAuthentication.filter(httpRequest);%0A        %7D catch (IllegalArgumentException exception) %7B%0A            assertThat(httpRequest.getFirstHeaderOrNull(authorization)).isEqualTo(null);%0A        %7D%0A    %7D%0A%0A
     @Test%0A    pu

Test for emptiness after @