# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1538373545 -19800
#      Mon Oct 01 11:29:05 2018 +0530
# Node ID 333729c400b39951e8a278c66866484eaffc346c
# Parent  4969a550f4682b783c991e12b4f039b7759c176c
Verifies issue 39352 : Added Automated Test 'I39352_VerifyCancelReplaceWithSaveRemovePref'

diff -r 4969a550f468 -r 333729c400b3 src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/cancelandreplace/I39352_VerifyCancelReplaceWithSaveRemovePref.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/cancelandreplace/I39352_VerifyCancelReplaceWithSaveRemovePref.java	Mon Oct 01 11:29:05 2018 +0530
@@ -0,0 +1,121 @@
+/*
+ *************************************************************************
+ * 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) 2018 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author RAN
+ *
+ */
+
+package org.openbravo.test.mobile.retail.pack.selenium.tests.cancelandreplace;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.retail.mobilecore.database.DatabaseHelperInsertUpdateOrDelete;
+import org.openbravo.test.mobile.retail.mobilecore.database.DatabaseHelperSelect;
+import org.openbravo.test.mobile.retail.mobilecore.javascript.BackboneHelper;
+import org.openbravo.test.mobile.retail.mobilecore.utils.RetailUtils;
+import org.openbravo.test.mobile.retail.pack.selenium.TestIdPack;
+import org.openbravo.test.mobile.retail.pack.selenium.terminals.WebPOSTerminalHelper;
+
+public class I39352_VerifyCancelReplaceWithSaveRemovePref extends WebPOSTerminalHelper {
+
+  private String preferenceId1 = RetailUtils.getRandomUUID();
+
+  @Test
+  public void test() {
+    // Create receipt
+    tap(TestIdPack.BUTTON_SEARCH);
+    write(TestIdPack.FIELD_SEARCH_TEXT, "Avalanche transceiver");
+    tap(TestIdPack.BUTTON_SEARCH_EXECUTE);
+    tap(TestIdPack.BUTTON_SEARCHPRODUCT_AVALANCHETRANSCEIVER);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+    final String receiptNo = BackboneHelper.getDocumentNo();
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    // Retrieve created receipt
+    tap(TestIdPack.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_MENU_OPENRECEIPT);
+    write(TestIdPack.FIELD_OPEN_RECEIPT_FILTER_TXT, receiptNo);
+    tap(TestIdPack.BUTTON_OPEN_RECEIPT_SEARCH);
+    tap(TestIdPack.TABLE_OPEN_RECEIPT, receiptNo);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+
+    // Do cancel and replace
+    tap(TestIdPack.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_MENU_CANCELANDREPLACE);
+    verify(TestIdPack.LABEL_RECEIPT_TYPE, "'Cancel and Replace' of  " + receiptNo);
+
+    // Add Line and delete
+    tap(TestIdPack.BUTTON_SEARCH);
+    write(TestIdPack.FIELD_SEARCH_TEXT, "Headlamp ultralight");
+    tap(TestIdPack.BUTTON_SEARCH_EXECUTE);
+    tap(TestIdPack.BUTTON_SEARCHPRODUCT_HEADLAMPULTRALIGHT);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "169.40");
+    tap(TestIdPack.BUTTON_RECEIPT_ROW2);
+    tap(TestIdPack.BUTTON_EDIT_DELETELINE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+    final String cancelReplaceReceiptNo = BackboneHelper.getDocumentNo();
+
+    // Delete Receipt
+    tap(TestIdPack.BUTTON_DELETERECEIPT);
+    tap(TestIdPack.BUTTON_DELETERECEIPT_CONFIRM);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    final String sql = String.format("select em_obpos_isdeleted FROM c_order "
+        + "where documentno = '%s'", cancelReplaceReceiptNo);
+    new DatabaseHelperSelect() {
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+        assertThat("Y", equalTo(rs.getString("em_obpos_isdeleted")));
+      }
+    }.execute(sql, 1);
+  }
+
+  @Override
+  public void beforeWithReload() {
+    createPreference(true);
+  }
+
+  @Override
+  public void afterWithReload() {
+    createPreference(false);
+  }
+
+  private void createPreference(final boolean isForTest) {
+    if (isForTest) {
+      final String clientId = BackboneHelper.getTerminalValue("client");
+      final String removeTicket = "OBPOS_remove_ticket";
+      // add preference
+      final String preferenceSQL = "insert into ad_preference(ad_preference_id, ad_client_id, ad_org_id, createdby, updatedby, value, property, ispropertylist, selected) "
+          + "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
+      new DatabaseHelperInsertUpdateOrDelete().execute(String.format(preferenceSQL, preferenceId1,
+          clientId, "0", "100", "100", "Y", removeTicket, "Y", "Y"), 1);
+    } else {
+      // delete preference
+      new DatabaseHelperInsertUpdateOrDelete().execute(String.format("delete from ad_preference "
+          + "where ad_preference_id = '%s'", preferenceId1), 1);
+    }
+  }
+}
\ No newline at end of file
