/*
 *************************************************************************
 * The contents of this file are subject to the Openbravo  Public  License
 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
 * Version 1.1  with a permitted attribution clause; you may not  use this
 * file except in compliance with the License. You  may  obtain  a copy of
 * the License at http://www.openbravo.com/legal/license.html 
 * Software distributed under the License  is  distributed  on  an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific  language  governing  rights  and  limitations
 * under the License. 
 * The Original Code is Openbravo ERP. 
 * The Initial Developer of the Original Code is Openbravo SLU 
 * All portions are Copyright (C) 2017 Openbravo SLU 
 * All Rights Reserved. 
 * Contributor(s):  ______________________________________.
 ************************************************************************
 */

package com.openbravo.test.integration.erp.modules.client.application.testsuites.standardview.form;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.openbravo.test.integration.erp.data.DataObject;
import com.openbravo.test.integration.erp.data.LogInData;
import com.openbravo.test.integration.erp.data.financial.receivablespayables.transactions.financialaccount.AccountData;
import com.openbravo.test.integration.erp.data.financial.receivablespayables.transactions.financialaccount.TransactionsData;
import com.openbravo.test.integration.erp.modules.client.application.gui.GeneratedTab;
import com.openbravo.test.integration.erp.testscripts.OpenbravoERPTest;
import com.openbravo.test.integration.erp.testscripts.financial.receivablespayables.transactions.FinancialAccount;
import com.openbravo.test.integration.util.ConfigurationProperties;

/**
 * Test that the value of a date field can be set without errors right after invoking the FIC.
 * 
 * See issue https://issues.openbravo.com/view.php?id=35665
 */
@RunWith(Parameterized.class)
public class FRM0060_SetDateAfterFICCall extends OpenbravoERPTest {

  /** Log4j logger for this class. */
  private static Logger logger = Logger.getLogger(FRM0060_SetDateAfterFICCall.class);

  /* Data for this test. */

  /** The account header data. */
  AccountData accountHeaderData;
  /** The transaction data. */
  TransactionsData transactionData;

  /**
   * Class constructor.
   * 
   * @param accountHeaderData
   *          The financial account data.
   * @param transactionData
   *          The transaction data.
   */
  public FRM0060_SetDateAfterFICCall(AccountData accountHeaderData, TransactionsData transactionData) {
    this.accountHeaderData = accountHeaderData;
    this.transactionData = transactionData;
    logInData = new LogInData.Builder().userName("QAAdmin").password("QAAdmin").build();
  }

  /**
   * Test parameters.
   * 
   * @return collection of object arrays with data for the test
   * 
   */
  @Parameters
  public static Collection<Object[]> aPRDepositReconciliation001Values() {
    String date = new SimpleDateFormat(ConfigurationProperties.INSTANCE.getDateFormat())
        .format(new Date());
    Object[][] data = new Object[][] { {
        /* Parameters for [FRM0060_SetDateAfterFICCallTest] */
        new AccountData.Builder().name("Spain Bank").build(),
        new TransactionsData.Builder().transactionType("BP Deposit").gLItem("Salaries")
            .depositAmount("101.00").transactionDate(date).accountingDate(date).build() } };
    return Arrays.asList(data);
  }

  /**
   * Match Automatically a Transaction and a Bank Statement Line
   * 
   * @throws ParseException
   */
  @Test
  public void FRM0060_SetDateAfterFICCallTest() throws ParseException {
    logger.info("** Start of test case [FRM0060_SetDateAfterFICCall]");

    // Select Financial Account
    FinancialAccount financialAccount = new FinancialAccount(mainPage).open();
    financialAccount.select(accountHeaderData);

    // Create a Transaction
    FinancialAccount.Transaction transactions = financialAccount.new Transaction(mainPage);
    transactions.create(transactionData);
    transactions.assertSaved();

    // Delete the Transaction
    transactions.getTab().deleteOnGrid();

    logger.info("** End of test case [FRM0060_SetDateAfterFICCall]");
  }

  public void cleanUp(GeneratedTab<DataObject> tab) {
    tab.select(transactionData);
    tab.deleteOnGrid();
  }

  @AfterClass
  public static void tearDown() {
    OpenbravoERPTest.forceLoginRequired();
  }
}