<commit_before>package io.paymenthighway;

import io.paymenthighway.model.Splitting;
import io.paymenthighway.model.Token;
import io.paymenthighway.model.request.*;
import io.paymenthighway.model.request.sca.*;
import io.paymenthighway.model.response.*;
import io.paymenthighway.model.response.transaction.ChargeCitResponse;
import io.paymenthighway.model.response.transaction.ChargeMitResponse;
import io.paymenthighway.test.ExternalServiceTest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpCoreContext;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

/**
 * PaymentHighway Payment API tests
 */
public class PaymentAPITest {

  Properties props;
  private String serviceUrl;
  private String signatureKeyId;
  private String signatureSecret;
  private String account;
  private String merchant;

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
  }

  @AfterClass
  public static void tearDownAfterClass() throws Exception {
  }

  @Before
  public void setUp() throws Exception {
    // required system information and authentication credentials
    // we read this from file, but can be from everywhere
    this.props = null;
    try {
      this.props = PaymentHighwayUtility.getProperties();
    } catch (IOException e) {
      e.printStackTrace();
    }
    this.serviceUrl = this.props.getProperty("service_url");
    this.signatureKeyId = this.props.getProperty("signature_key_id");
    this.signatureSecret = this.props.getProperty("signature_secret");
    this.account = this.props.getProperty("sph-account");
    this.merchant = this.props.getProperty("sph-merchant");
  }

  @After
  public void tearDown() throws Exception {
  }

  private PaymentAPI createPaymentAPI() {
    return new PaymentAPI(serviceUrl, signatureKeyId, signatureSecret, account, merchant);
  }

  static private final String RESULT_CODE_OK = "100";
  static private final String RESULT_CODE_SOFT_DECLINE = "400";

  private static Card validTestCard = new Card("4153013999700321", "2023", "11", "321");
  private static Card softDeclineCard = new Card("4153013999701170", "2023", "11", "170");

  private UUID initTransaction(PaymentAPI paymentAPI) throws IOException {
    InitTransactionResponse response = paymentAPI.initTransaction();
    assertApiResponseSuccessful(response);
    return response.getId();
  }

  private void assertApiResponseSuccessful(Response response)  {
    assertNotNull(response);
    assertEquals(response.getResult().getMessage(), "OK");
    assertEquals(response.getResult().getCode(), "100");
  }

  @Test
  public void testInitTransaction() {

    // create the payment highway service
    PaymentAPI paymentAPI = createPaymentAPI();

    InitTransactionResponse response = null;

    try {
      response = paymentAPI.initTransaction();
    } catch (IOException e) {
      e.printStackTrace();
    }

    assertNotNull(response);
    assertEquals(response.getResult().getCode(), "100");
    assertEquals(response.getResult().getMessage(), "OK");
    assertEquals(response.getId().toString().length(), 36);
  }

  @Test
  public void testDebitWithCard() {

    // create the payment highway service
    PaymentAPI paymentAPI = createPaymentAPI();

    InitTransactionResponse response = null;

    try {
      response = paymentAPI.initTransaction();
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    assertNotNull(response);
    UUID transactionId = response.getId();

    String pan = "4153013999700024";
    String cvc = "024";
    String expiryYear = "2023";
    String expiryMonth = "11";
    Card card = new Card(pan, expiryYear, expiryMonth, cvc);
    TransactionRequest transaction = new TransactionRequest(card, "9999", "EUR");

    TransactionResponse transactionResponse = null;

    try {
      transactionResponse = paymentAPI.debitTransaction(transactionId, transaction);
    } catch (IOException e) {
      e.printStackTrace();
    }

    assertNotNull(transactionResponse);
    assertEquals(transactionResponse.getResult().getCode(), "100");
    assertEquals(transactionResponse.getResult().getMessage(), "OK");
  }


  private ChargeCitResponse performCustomerInitiatedTransaction(
      Long amount,
      Card card,
      StrongCustomerAuthentication strongCustomerAuthentication
  ) throws IOException {
    PaymentAPI paymentAPI = createPaymentAPI();
    UUID transactionId = initTransaction(paymentAPI);

    ChargeCitRequest request = new ChargeCitRequest.Builder(
        card, amount, "EUR", strongCustomerAuthentication
    ).build();

    return  paymentAPI.chargeCustomerInitiatedTransaction(transactionId, request);
  }

  @Test
  public void testCitWithCardNoSoftDecline() throws IOException {

    Urls urls = Urls.Builder("https://success.example.com", "https://failure.example.com", "https://cancel.example.com").build();
    StrongCustomerAuthentication strongCustomerAuthentication = StrongCustomerAuthentication.Builder(urls).build();

    ChargeCitResponse citResponse = performCustomerInitiatedTransaction(99L, validTestCard, strongCustomerAuthentication);

    assertNotNull(citResponse);
    assertEquals(RESULT_CODE_OK, citResponse.getResult().getCode());
    assertEquals("OK", citResponse.getResult().getMessage());<commit_msg>Group an auxiliary method with the other ones<commit_after>package io.paymenthighway;

import io.paymenthighway.model.Splitting;
import io.paymenthighway.model.Token;
import io.paymenthighway.model.request.*;
import io.paymenthighway.model.request.sca.*;
import io.paymenthighway.model.response.*;
import io.paymenthighway.model.response.transaction.ChargeCitResponse;
import io.paymenthighway.model.response.transaction.ChargeMitResponse;
import io.paymenthighway.test.ExternalServiceTest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpCoreContext;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

/**
 * PaymentHighway Payment API tests
 */
public class PaymentAPITest {

  Properties props;
  private String serviceUrl;
  private String signatureKeyId;
  private String signatureSecret;
  private String account;
  private String merchant;

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
  }

  @AfterClass
  public static void tearDownAfterClass() throws Exception {
  }

  @Before
  public void setUp() throws Exception {
    // required system information and authentication credentials
    // we read this from file, but can be from everywhere
    this.props = null;
    try {
      this.props = PaymentHighwayUtility.getProperties();
    } catch (IOException e) {
      e.printStackTrace();
    }
    this.serviceUrl = this.props.getProperty("service_url");
    this.signatureKeyId = this.props.getProperty("signature_key_id");
    this.signatureSecret = this.props.getProperty("signature_secret");
    this.account = this.props.getProperty("sph-account");
    this.merchant = this.props.getProperty("sph-merchant");
  }

  @After
  public void tearDown() throws Exception {
  }

  private PaymentAPI createPaymentAPI() {
    return new PaymentAPI(serviceUrl, signatureKeyId, signatureSecret, account, merchant);
  }

  static private final String RESULT_CODE_OK = "100";
  static private final String RESULT_CODE_SOFT_DECLINE = "400";

  private static Card validTestCard = new Card("4153013999700321", "2023", "11", "321");
  private static Card softDeclineCard = new Card("4153013999701170", "2023", "11", "170");

  private UUID initTransaction(PaymentAPI paymentAPI) throws IOException {
    InitTransactionResponse response = paymentAPI.initTransaction();
    assertApiResponseSuccessful(response);
    return response.getId();
  }

  private void assertApiResponseSuccessful(Response response)  {
    assertNotNull(response);
    assertEquals(response.getResult().getMessage(), "OK");
    assertEquals(response.getResult().getCode(), "100");
  }

  private ChargeCitResponse performCustomerInitiatedTransaction(
      Long amount,
      Card card,
      StrongCustomerAuthentication strongCustomerAuthentication
  ) throws IOException {
    PaymentAPI paymentAPI = createPaymentAPI();
    UUID transactionId = initTransaction(paymentAPI);

    ChargeCitRequest request = new ChargeCitRequest.Builder(
        card, amount, "EUR", strongCustomerAuthentication
    ).build();

    return  paymentAPI.chargeCustomerInitiatedTransaction(transactionId, request);
  }

  @Test
  public void testInitTransaction() {

    // create the payment highway service
    PaymentAPI paymentAPI = createPaymentAPI();

    InitTransactionResponse response = null;

    try {
      response = paymentAPI.initTransaction();
    } catch (IOException e) {
      e.printStackTrace();
    }

    assertNotNull(response);
    assertEquals(response.getResult().getCode(), "100");
    assertEquals(response.getResult().getMessage(), "OK");
    assertEquals(response.getId().toString().length(), 36);
  }

  @Test
  public void testDebitWithCard() {

    // create the payment highway service
    PaymentAPI paymentAPI = createPaymentAPI();

    InitTransactionResponse response = null;

    try {
      response = paymentAPI.initTransaction();
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    assertNotNull(response);
    UUID transactionId = response.getId();

    String pan = "4153013999700024";
    String cvc = "024";
    String expiryYear = "2023";
    String expiryMonth = "11";
    Card card = new Card(pan, expiryYear, expiryMonth, cvc);
    TransactionRequest transaction = new TransactionRequest(card, "9999", "EUR");

    TransactionResponse transactionResponse = null;

    try {
      transactionResponse = paymentAPI.debitTransaction(transactionId, transaction);
    } catch (IOException e) {
      e.printStackTrace();
    }

    assertNotNull(transactionResponse);
    assertEquals(transactionResponse.getResult().getCode(), "100");
    assertEquals(transactionResponse.getResult().getMessage(), "OK");
  }

  @Test
  public void testCitWithCardNoSoftDecline() throws IOException {

    Urls urls = Urls.Builder("https://success.example.com", "https://failure.example.com", "https://cancel.example.com").build();
    StrongCustomerAuthentication strongCustomerAuthentication = StrongCustomerAuthentication.Builder(urls).build();

    ChargeCitResponse citResponse = performCustomerInitiatedTransaction(99L, validTestCard, strongCustomerAuthentication);

    assertNotNull(citResponse);
    assertEquals(RESULT_CODE_OK, citResponse.getResult().getCode());
    assertEquals("OK", citResponse.getResult().getMessage());
{'fraction_non_alphanumeric': 0.10562494760667282, 'fraction_numerical': 0.016765864699471875, 'mean_word_length': 4.683658885183421, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, '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': '4327418', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3157, 'n_words': 714}