# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1450953926 -19800
# Node ID c3f6d6cce2902f1f69eda77fccf9d57adab529af
# Parent  9bd63f2d466ebe65661625ea4ffda80180591f4c
Verifies issue 31718: Added automated test 'I31718_CheckGiftCardPaymentWithCustomPayment'

diff --git a/src-test/org/openbravo/test/mobile/common/selenium/javascript/TestId.java b/src-test/org/openbravo/test/mobile/common/selenium/javascript/TestId.java
--- a/src-test/org/openbravo/test/mobile/common/selenium/javascript/TestId.java
+++ b/src-test/org/openbravo/test/mobile/common/selenium/javascript/TestId.java
@@ -872,7 +872,7 @@
   BUTTON_MORE_PAYMENTS_DIV("toolbarPayment_btnMore", EnyoKind.BUTTON), //
   BUTTON_MORE_PAYMENTS("btnMore_btn", EnyoKind.BUTTON), //
   BUTTON_PAYMENT_GIFTCARD("PaymentGiftCard_button", EnyoKind.BUTTON), //
-  BUTTON_PAYMENT_VOUCHER("PaymentGiftVoucher_button", EnyoKind.BUTTON), //
+  BUTTON_PAYMENT_GIFTVOUCHER("PaymentGiftVoucher_button", EnyoKind.BUTTON), //
   FIELD_GIFTCARDPAYMENT_CARDID("bodyContent_cardentry"), //
   BUTTON_GIFTCARDPAYMENT_APPLY("bodyButtons_okbutton", EnyoKind.BUTTON), //
   LABEL_ZERO_PAYMENT_NEEDED("paymentTabContent_donezerolbl"), //
diff --git a/src-test/org/openbravo/test/mobile/retail/extmodules/selenium/tests/giftcards/I31718_CheckGiftCardPaymentWithCustomPayment.java b/src-test/org/openbravo/test/mobile/retail/extmodules/selenium/tests/giftcards/I31718_CheckGiftCardPaymentWithCustomPayment.java
new file mode 100644
--- /dev/null
+++ b/src-test/org/openbravo/test/mobile/retail/extmodules/selenium/tests/giftcards/I31718_CheckGiftCardPaymentWithCustomPayment.java
@@ -0,0 +1,179 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (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 S.L.U.
+ * All portions are Copyright (C) 2015 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author RAN
+ *
+ */
+
+package org.openbravo.test.mobile.retail.extmodules.selenium.tests.giftcards;
+
+import java.util.UUID;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.common.selenium.SeleniumHelper;
+import org.openbravo.test.mobile.common.selenium.database.DatabaseHelperInsertUpdateOrDelete;
+import org.openbravo.test.mobile.common.selenium.javascript.TestId;
+import org.openbravo.test.mobile.common.selenium.terminals.WebPOSExtModulesTerminalHelper;
+
+/**
+ * 
+ * Verify the Payment type Gift Card/Voucher will display if we add any custom Payment types
+ */
+public class I31718_CheckGiftCardPaymentWithCustomPayment extends WebPOSExtModulesTerminalHelper {
+
+  private final String paymentMethodCash = "45A202BF44884F05B8A1BF741E2063B6";
+  private final String posTerminalTypePaymentMethodCash = "BD225CB17C674DAB81D8D86FD6A574CB";
+
+  private final int newPaymentTypeCount = 2;
+  private final String[] arrFinAccountIDList = new String[newPaymentTypeCount];
+  private final String[] arrFinAccountPaymentMethodIDList = new String[newPaymentTypeCount];
+  private final String[] arrPaymentTypeIDList = new String[newPaymentTypeCount];
+
+  @Test
+  public void test() {
+    boolean isTestSuccessful = false;
+    try {
+      final String clientId = (String) SeleniumHelper
+          .executeScriptWithReturn("OB.POS.modelterminal.get('terminal').client");
+      final String orgId = (String) SeleniumHelper
+          .executeScriptWithReturn("OB.POS.modelterminal.get('terminal').organization");
+      final String userId = "100";
+
+      final String posTerminalId = (String) SeleniumHelper
+          .executeScriptWithReturn("OB.POS.modelterminal.get('terminal').id");
+
+      // Create database changes
+      createDatabaseChanges(clientId, orgId, userId, posTerminalId);
+
+      // Update Terminal with Database Changes
+      updateTerminalAfterDatabaseChange();
+
+      // Verify Payment Types
+      verifyGiftPaymentType();
+
+      // Restore database changes
+      restoreDatabaseChanges();
+
+      // Update Terminal with Database Changes
+      updateTerminalAfterDatabaseChange();
+
+      isTestSuccessful = true;
+    } finally {
+      if (!isTestSuccessful) {
+        // Restore database changes
+        restoreDatabaseChanges();
+      }
+    }
+  }
+
+  private void createDatabaseChanges(final String clientId, final String orgId,
+      final String userId, final String posTerminalId) {
+    // Add Financial Account, Payment Method for FinAccount & Payment Type for POS Terminal
+    for (int i = 0; i < newPaymentTypeCount; i++) {
+      arrFinAccountIDList[i] = getUUID();
+      arrFinAccountPaymentMethodIDList[i] = getUUID();
+      arrPaymentTypeIDList[i] = getUUID();
+
+      final String strStaticAccountName = "VBS Credit Account " + (i + 1);
+      final String strStaticPaymentTypeName = "VBS Credit " + (i + 1);
+      final String strStaticPaymentTypeValue = "OBPOS_vbscredit" + (i + 1);
+
+      // Add to Financial Account
+      final String insertSQL2 = String
+          .format(
+              "INSERT INTO fin_financial_account(fin_financial_account_id, ad_client_id, ad_org_id, createdby, updatedby, c_currency_id, name, c_country_id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
+              arrFinAccountIDList[i], clientId, orgId, userId, userId, "102", strStaticAccountName,
+              "100");
+      new DatabaseHelperInsertUpdateOrDelete().execute(insertSQL2, 1);
+
+      // Add Payment Method to Financial Account
+      final String insertSQL3 = String
+          .format(
+              "INSERT INTO fin_finacc_paymentmethod(fin_finacc_paymentmethod_id, ad_client_id, ad_org_id, createdby, updatedby, fin_paymentmethod_id, fin_financial_account_id, automatic_receipt, automatic_payment, automatic_deposit, automatic_withdrawn, upondeposituse, inuponclearinguse, uponwithdrawaluse, outuponclearinguse)"
+                  + "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
+              arrFinAccountPaymentMethodIDList[i], clientId, orgId, userId, userId,
+              paymentMethodCash, arrFinAccountIDList[i], "N", "N", "Y", "Y", "DEP", "CLE", "WIT",
+              "CLE");
+      new DatabaseHelperInsertUpdateOrDelete().execute(insertSQL3, 1);
+
+      // Add Payment Type to POS Terminal
+      final String insertSQL4 = String
+          .format(
+              "INSERT INTO obpos_app_payment(obpos_app_payment_id, ad_client_id, ad_org_id, createdby, updatedby, value, name, line, fin_financial_account_id, obpos_applications_id, obpos_app_payment_type_id)"
+                  + "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
+              arrPaymentTypeIDList[i], clientId, orgId, userId, userId, strStaticPaymentTypeValue,
+              strStaticPaymentTypeName, ((i + 1) * 10), arrFinAccountIDList[i], posTerminalId,
+              posTerminalTypePaymentMethodCash);
+      new DatabaseHelperInsertUpdateOrDelete().execute(insertSQL4, 1);
+    }
+
+  }
+
+  private void restoreDatabaseChanges() {
+    // Delete Financial Account, Payment Method for FinAccount & Payment Type for POS Terminal
+    for (int i = 0; i < newPaymentTypeCount; i++) {
+      // Delete Payment Type to POS Terminal
+      new DatabaseHelperInsertUpdateOrDelete().execute(String.format(
+          "delete from obpos_app_payment where obpos_app_payment_id = '%s'",
+          arrPaymentTypeIDList[i]), 1);
+
+      // Delete Payment Method to Financial Account
+      new DatabaseHelperInsertUpdateOrDelete().execute(String.format(
+          "delete from fin_finacc_paymentmethod where fin_finacc_paymentmethod_id = '%s'",
+          arrFinAccountPaymentMethodIDList[i]), 1);
+
+      // Delete Financial Account
+      new DatabaseHelperInsertUpdateOrDelete().execute(String.format(
+          "delete from fin_financial_account_acct where fin_financial_account_id = '%s'",
+          arrFinAccountIDList[i]), 1);
+
+      new DatabaseHelperInsertUpdateOrDelete().execute(String.format(
+          "delete from fin_financial_account where fin_financial_account_id = '%s'",
+          arrFinAccountIDList[i]), 1);
+    }
+  }
+
+  private void verifyGiftPaymentType() {
+    // Add Product
+    tap(TestId.BUTTON_SEARCH);
+    write(TestId.FIELD_SEARCH_TEXT, "Baby carrier");
+    tap(TestId.BUTTON_SEARCH_EXECUTE);
+    tap(TestId.BUTTON_SEARCHPRODUCT_BACKPACKSANDTRAVEL_BABYCARRIER_SHOWPRODSK);
+    verify(TestId.LABEL_TOTALTOPAY, "110.50");
+    tap(TestId.BUTTON_PAY);
+
+    // Open Payments Method Popup
+    tap(TestId.BUTTON_MORE_PAYMENTS_DIV);
+    isVisible(TestId.POPUP_PAYMENTMETHOD, true);
+    isVisible(TestId.BUTTON_PAYMENT_GIFTCARD, true);
+    isVisible(TestId.BUTTON_PAYMENT_GIFTVOUCHER, true);
+
+    // Set Gift Card as Payment type
+    tap(TestId.BUTTON_PAYMENT_GIFTCARD);
+
+    // Delete the Receipt
+    tap(TestId.BUTTON_DELETERECEIPT);
+    tap(TestId.BUTTON_DELETERECEIPT_CONFIRM);
+  }
+
+  private String getUUID() {
+    String randomUUID = UUID.randomUUID().toString();
+    randomUUID = randomUUID.toUpperCase();
+    return randomUUID.replace("-", "");
+  }
+}
\ No newline at end of file
diff --git a/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/system/I30879_CheckMorePaymentMethodDisplay.java b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/system/I30879_CheckMorePaymentMethodDisplay.java
--- a/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/system/I30879_CheckMorePaymentMethodDisplay.java
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/system/I30879_CheckMorePaymentMethodDisplay.java
@@ -77,14 +77,17 @@
       // Verify Payment Types
       verifyPaymentType();
 
-      isTestSuccessful = true;
-    } finally {
       // Restore database changes
       restoreDatabaseChanges();
 
-      if (isTestSuccessful) {
-        // Update Terminal with Database Changes
-        updateTerminalAfterDatabaseChange();
+      // Update Terminal with Database Changes
+      updateTerminalAfterDatabaseChange();
+
+      isTestSuccessful = true;
+    } finally {
+      if (!isTestSuccessful) {
+        // Restore database changes
+        restoreDatabaseChanges();
       }
     }
   }
