<commit_before>package com.braintreegateway.integrationtest;

import java.net.URLEncoder;
import java.util.List;

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.braintreegateway.testhelpers.HttpHelper;
import com.braintreegateway.testhelpers.TestHelper;
import com.braintreegateway.util.Http;
import com.braintreegateway.util.QueryString;
import com.braintreegateway.CreditCardRequest;
import com.braintreegateway.CustomerRequest;
import com.braintreegateway.CreditCard;
import com.braintreegateway.Customer;
import com.braintreegateway.Result;
import com.braintreegateway.Environment;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.util.NodeWrapper;
import com.braintreegateway.ClientTokenRequest;
import com.braintreegateway.ClientTokenOptionsRequest;

public class ClientTokenIT {
    private BraintreeGateway gateway;

    private String urlencode(String string) {
        String encodedString = "";
        try {
            encodedString = URLEncoder.encode(string, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return encodedString;
    }

    private int postResponseCode(String url, QueryString payload) {
        int responseCode = -1;
        try {
          responseCode = HttpHelper.postResponseCode(url, payload.toString());
        } catch (java.net.MalformedURLException e) {
          throw new RuntimeException(e);
        } catch (java.io.IOException e) {
          throw new RuntimeException(e);
        }

        return responseCode;
    }

    private String _getFingerprint(String rawClientToken) {
        String decodedClientToken = TestHelper.decodeClientToken(rawClientToken);
        return TestHelper.extractParamFromJson("authorizationFingerprint", decodedClientToken);
    }

    @Before
    public void createGateway() {
        this.gateway = new BraintreeGateway(
            Environment.DEVELOPMENT,
            "integration_merchant_id",
            "integration_public_key",
            "integration_private_key"
        );
    }

    @Test
    public void fingerprintIsAcceptedByTheGateway() {
        String clientToken = gateway.clientToken().generate();
        String authorizationFingerprint = _getFingerprint(clientToken);
        String encodedFingerprint = "";
        try {
            encodedFingerprint = URLEncoder.encode(authorizationFingerprint, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        String url = gateway.baseMerchantURL()  + "/client_api/nonces.json?";
        url += "authorizationFingerprint=" + encodedFingerprint;
        url += "&sharedCustomerIdentifierType=testing&sharedCustomerIdentifier=test-identifier";

        int responseCode = -1;
        try {
          responseCode = HttpHelper.getResponseCode(url);
        } catch (java.net.MalformedURLException e) {
          fail();
        } catch (java.io.IOException e) {
          fail();
        }
        assertEquals(200, responseCode);
    }

    @Test
    public void versionOptionSupported() {
        ClientTokenRequest clientTokenRequest = new ClientTokenRequest().version(1);
        String clientToken = gateway.clientToken().generate(clientTokenRequest);
        int version = TestHelper.extractIntParamFromJson("version", clientToken);
        assertEquals(1, version);
    }

    @Test
    public void versionDefaultsToTwo() {
        ClientTokenRequest clientTokenRequest = new ClientTokenRequest();
        String encodedClientToken = gateway.clientToken().generate(clientTokenRequest);
        String decodedClientToken = TestHelper.decodeClientToken(encodedClientToken);
        String version = TestHelper.extractParamFromJson("version", decodedClientToken);
        assertEquals("2", version);
    }

    @Test
    public void versionDefaultsToTwoWithoutRequest() {
        String encodedClientToken = gateway.clientToken().generate();
        String decodedClientToken = TestHelper.decodeClientToken(encodedClientToken);
        String version = TestHelper.extractParamFromJson("version", decodedClientToken);
        assertEquals("2", version);
    }

    @Test
    public void fingerprintCanContainCustomerId() {
        CustomerRequest customerRequest = new CustomerRequest();
        Result<Customer> result = gateway.customer().create(customerRequest);<commit_msg>Fix client token version type<commit_after>package com.braintreegateway.integrationtest;

import java.net.URLEncoder;
import java.util.List;

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.braintreegateway.testhelpers.HttpHelper;
import com.braintreegateway.testhelpers.TestHelper;
import com.braintreegateway.util.Http;
import com.braintreegateway.util.QueryString;
import com.braintreegateway.CreditCardRequest;
import com.braintreegateway.CustomerRequest;
import com.braintreegateway.CreditCard;
import com.braintreegateway.Customer;
import com.braintreegateway.Result;
import com.braintreegateway.Environment;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.util.NodeWrapper;
import com.braintreegateway.ClientTokenRequest;
import com.braintreegateway.ClientTokenOptionsRequest;

public class ClientTokenIT {
    private BraintreeGateway gateway;

    private String urlencode(String string) {
        String encodedString = "";
        try {
            encodedString = URLEncoder.encode(string, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return encodedString;
    }

    private int postResponseCode(String url, QueryString payload) {
        int responseCode = -1;
        try {
          responseCode = HttpHelper.postResponseCode(url, payload.toString());
        } catch (java.net.MalformedURLException e) {
          throw new RuntimeException(e);
        } catch (java.io.IOException e) {
          throw new RuntimeException(e);
        }

        return responseCode;
    }

    private String _getFingerprint(String rawClientToken) {
        String decodedClientToken = TestHelper.decodeClientToken(rawClientToken);
        return TestHelper.extractParamFromJson("authorizationFingerprint", decodedClientToken);
    }

    @Before
    public void createGateway() {
        this.gateway = new BraintreeGateway(
            Environment.DEVELOPMENT,
            "integration_merchant_id",
            "integration_public_key",
            "integration_private_key"
        );
    }

    @Test
    public void fingerprintIsAcceptedByTheGateway() {
        String clientToken = gateway.clientToken().generate();
        String authorizationFingerprint = _getFingerprint(clientToken);
        String encodedFingerprint = "";
        try {
            encodedFingerprint = URLEncoder.encode(authorizationFingerprint, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        String url = gateway.baseMerchantURL()  + "/client_api/nonces.json?";
        url += "authorizationFingerprint=" + encodedFingerprint;
        url += "&sharedCustomerIdentifierType=testing&sharedCustomerIdentifier=test-identifier";

        int responseCode = -1;
        try {
          responseCode = HttpHelper.getResponseCode(url);
        } catch (java.net.MalformedURLException e) {
          fail();
        } catch (java.io.IOException e) {
          fail();
        }
        assertEquals(200, responseCode);
    }

    @Test
    public void versionOptionSupported() {
        ClientTokenRequest clientTokenRequest = new ClientTokenRequest().version(1);
        String clientToken = gateway.clientToken().generate(clientTokenRequest);
        int version = TestHelper.extractIntParamFromJson("version", clientToken);
        assertEquals(1, version);
    }

    @Test
    public void versionDefaultsToTwo() {
        ClientTokenRequest clientTokenRequest = new ClientTokenRequest();
        String encodedClientToken = gateway.clientToken().generate(clientTokenRequest);
        String decodedClientToken = TestHelper.decodeClientToken(encodedClientToken);
        int version = TestHelper.extractIntParamFromJson("version", decodedClientToken);
        assertEquals(2, version);
    }

    @Test
    public void versionDefaultsToTwoWithoutRequest() {
        String encodedClientToken = gateway.clientToken().generate();
        String decodedClientToken = TestHelper.decodeClientToken(encodedClientToken);
        int version = TestHelper.extractIntParamFromJson("version", decodedClientToken);
        assertEquals(2, version);
    }

    @Test
    public void fingerprintCanContainCustomerId() {
        CustomerRequest customerRequest = new CustomerRequest();
        Result<Customer> result = gateway.customer().create(customerRequest);
{'fraction_non_alphanumeric': 0.08803912850155625, 'fraction_numerical': 0.002445531347265451, 'mean_word_length': 3.390922401171303, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '982955', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2320, 'n_words': 463}