<commit_before>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2013 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

package org.openlmis.functional;

import org.openlmis.UiUtils.TestCaseHelper;
import org.openlmis.pageobjects.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.thoughtworks.selenium.SeleneseTestBase.assertFalse;
import static java.util.Arrays.asList;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

public class ManageProduct extends TestCaseHelper {

  LoginPage loginPage;
  ProductPage productPage;

  public static final String ADMIN = "admin";
  public static final String PASSWORD = "password";

  public Map<String, String> testData = new HashMap<String, String>() {{
    put(PASSWORD, "<PASSWORD>");
    put(ADMIN, "Admin123");
  }};

  @BeforeMethod(groups = {"admin"})
  public void setUp() throws InterruptedException, SQLException, IOException {
    super.setup();
    dbWrapper.removeAllExistingRights("Admin");
    dbWrapper.assignRight("Admin", "MANAGE_FACILITY");
    dbWrapper.insertProductCategoryWithDisplayOrder("Antibiotic", "Antibiotics", 1);
    dbWrapper.insertProductCategoryWithDisplayOrder("anaesthetics", "anaesthetics", 1);
    dbWrapper.insertProductCategoryWithDisplayOrder("category3", "category3", 2);
    loginPage = PageObjectFactory.getLoginPage(testWebDriver, baseUrlGlobal);
    productPage = PageObjectFactory.getProductPage(testWebDriver);
  }

  @Test(groups = {"admin"})
  public void testRightsNotPresent() throws SQLException {
    HomePage homePage = loginPage.loginAs(testData.get(ADMIN), testData.get(PASSWORD));
    homePage.navigateManageFacility();

    assertFalse(homePage.isProductTabDisplayed());
    homePage.logout();

    dbWrapper.assignRight("Admin", "MANAGE_PRODUCT");
    loginPage.loginAs(testData.get(ADMIN), testData.get(PASSWORD));
    homePage.navigateManageFacility();
    assertTrue(homePage.isProductTabDisplayed());
    productPage = homePage.navigateToProductPage();

    assertEquals("Search product", productPage.getSearchProductHeader());
    assertTrue(productPage.isSearchIconDisplayed());
    assertTrue(homePage.isProductTabDisplayed());
    assertEquals("Product", productPage.getSelectSearchOption());
    assertFalse(productPage.isResultDisplayed());
    productPage.clickSearchOptionButton();
    assertFalse(productPage.isCloseSearchResultsButtonDisplayed());
  }

  @Test(groups = {"admin"})
  public void testProductSearchSortAndPagination() throws SQLException {
    dbWrapper.assignRight("Admin", "MANAGE_PRODUCT");
    dbWrapper.assignRight("Admin", "UPLOADS");
    dbWrapper.updateFieldValue("programs", "name", "hiv", "code", "HIV");

    HomePage homePage = loginPage.loginAs(testData.get(ADMIN), testData.get(PASSWORD));
    productPage = homePage.navigateToProductPage();
    searchProduct("P");
    assertEquals("No matches found for 'P'", productPage.getNoResultsMessage());

    UploadPage uploadPage = homePage.navigateUploads();
    uploadPage.uploadProducts("QA_Products_For_Products_CRUD21.csv");
    uploadPage.verifySuccessMessageOnUploadScreen();
    uploadPage.uploadProgramProductMapping("QA_Program_Products15.csv");
    uploadPage.verifySuccessMessageOnUploadScreen();

    homePage.navigateToProductPage();
    testWebDriver.waitForAjax();
    searchProduct("P");
    assertEquals("21 matches found for 'P'", productPage.getNResultsMessage());

    verifyNumberOFPageLinksDisplayed(21, 10);
    verifyPageNumberLinksDisplayed();
    verifyPageNumberSelected(1);
    verifyNextAndLastPageLinksEnabled();
    verifyPreviousAndFirstPageLinksDisabled();
    verifyNumberOfLineItemsVisibleOnPage(10);

//    verifyCategoryOrderOnPage(asList("anaesthetics", "", "", "", "", "", "", "", "Antibiotics", ""));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets19", "Indinavir 40mg Tablets8", "Indinavir 400mg Tablets13",
      "Indinavir 400mg Tablets17", "Indinavir 400mg Tablets21", "Indinavir 400mg Tablets4", "Indinavir 400mg Tablets6",
      "Indinavir 400mg Tablets15", "AIndinavir 400mg Tablets2", "indinavir 400mg Tablets1"));
    verifyProgramOrderOnPage(asList("ESSENTIAL MEDICINES", "ESSENTIAL MEDICINES", "hiv", "hiv", "hiv", "hiv", "hiv", "TB", "MALARIA", "TB"));

    navigateToPage(2);
    verifyPageNumberSelected(2);
    verifyNextAndLastPageLinksEnabled();
    verifyPreviousAndFirstPageLinksEnabled();
    verifyNumberOfLineItemsVisibleOnPage(10);
//    verifyCategoryOrderOnPage(asList("Antibiotics", "category3", "", "", "", "Uncategorised", "", "", "", ""));
    verifyProgramOrderOnPage(asList("TB", "hiv", "hiv", "MALARIA", "VACCINES", "", "", "", "", ""));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets3", "Indinavir 400mg Tablets10", "Indinavir 400mg Tablets11",
      "Indinavir 400mg Tablets12", "Indinavir 400mg Tablets16", "Indinavir 400mg Tablets14", "Indinavir 400mg Tablets18",
      "Indinavir 400mg Tablets20", "Indinavir 400mg Tablets5", "Indinavir 400mg Tablets7"));
    assertEquals("NA", productPage.getUndefinedActive(8));

    navigateToNextPage();
    verifyPageNumberSelected(3);
    verifyNextAndLastPageLinksDisabled();
    verifyPreviousAndFirstPageLinksEnabled();
    verifyNumberOfLineItemsVisibleOnPage(1);
//    verifyCategoryOrderOnPage(asList("Uncategorised"));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets9"));
    verifyProgramOrderOnPage(asList(""));

    navigateToFirstPage();
    verifyPageNumberSelected(1);
    verifyNextAndLastPageLinksEnabled();
    verifyPreviousAndFirstPageLinksDisabled();
    verifyNumberOfLineItemsVisibleOnPage(10);
//    verifyCategoryOrderOnPage(asList("anaesthetics", "", "", "", "", "", "", "", "Antibiotics", ""));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets19", "Indinavir 40mg Tablets8", "Indinavir 400mg Tablets13",
      "Indinavir 400mg Tablets17", "Indinavir 400mg Tablets21", "Indinavir 400mg Tablets4", "Indinavir 400mg Tablets6",
      "Indinavir 400mg Tablets15", "AIndinavir 400mg Tablets2", "indinavir 400mg Tablets1"));
    verifyProgramOrderOnPage(asList("ESSENTIAL MEDICINES", "ESSENTIAL MEDICINES", "hiv", "hiv", "hiv", "hiv", "hiv", "TB", "MALARIA", "TB"));

    navigateToLastPage();
    verifyPageNumberSelected(3);
    verifyNextAndLastPageLinksDisabled();
    verifyPreviousAndFirstPageLinksEnabled();
    verifyNumberOfLineItemsVisibleOnPage(1);

    navigateToPreviousPage();
    verifyPageNumberSelected(2);
    verifyNextAndLastPageLinksEnabled();<commit_msg>|#1446| adding tests for category on product search<commit_after>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2013 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

package org.openlmis.functional;

import org.openlmis.UiUtils.TestCaseHelper;
import org.openlmis.pageobjects.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.thoughtworks.selenium.SeleneseTestBase.assertFalse;
import static java.util.Arrays.asList;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

public class ManageProduct extends TestCaseHelper {

  LoginPage loginPage;
  ProductPage productPage;

  public static final String ADMIN = "admin";
  public static final String PASSWORD = "password";

  public Map<String, String> testData = new HashMap<String, String>() {{
    put(PASSWORD, "<PASSWORD>");
    put(ADMIN, "Admin123");
  }};

  @BeforeMethod(groups = {"admin"})
  public void setUp() throws InterruptedException, SQLException, IOException {
    super.setup();
    dbWrapper.removeAllExistingRights("Admin");
    dbWrapper.assignRight("Admin", "MANAGE_FACILITY");
    dbWrapper.insertProductCategoryWithDisplayOrder("Antibiotic", "Antibiotics", 1);
    dbWrapper.insertProductCategoryWithDisplayOrder("anaesthetics", "anaesthetics", 1);
    dbWrapper.insertProductCategoryWithDisplayOrder("category3", "category3", 2);
    loginPage = PageObjectFactory.getLoginPage(testWebDriver, baseUrlGlobal);
    productPage = PageObjectFactory.getProductPage(testWebDriver);
  }

  @Test(groups = {"admin"})
  public void testRightsNotPresent() throws SQLException {
    HomePage homePage = loginPage.loginAs(testData.get(ADMIN), testData.get(PASSWORD));
    homePage.navigateManageFacility();

    assertFalse(homePage.isProductTabDisplayed());
    homePage.logout();

    dbWrapper.assignRight("Admin", "MANAGE_PRODUCT");
    loginPage.loginAs(testData.get(ADMIN), testData.get(PASSWORD));
    homePage.navigateManageFacility();
    assertTrue(homePage.isProductTabDisplayed());
    productPage = homePage.navigateToProductPage();

    assertEquals("Search product", productPage.getSearchProductHeader());
    assertTrue(productPage.isSearchIconDisplayed());
    assertTrue(homePage.isProductTabDisplayed());
    assertEquals("Product", productPage.getSelectSearchOption());
    assertFalse(productPage.isResultDisplayed());
    productPage.clickSearchOptionButton();
    assertFalse(productPage.isCloseSearchResultsButtonDisplayed());
  }

  @Test(groups = {"admin"})
  public void testProductSearchSortAndPagination() throws SQLException {
    dbWrapper.assignRight("Admin", "MANAGE_PRODUCT");
    dbWrapper.assignRight("Admin", "UPLOADS");
    dbWrapper.updateFieldValue("programs", "name", "hiv", "code", "HIV");

    HomePage homePage = loginPage.loginAs(testData.get(ADMIN), testData.get(PASSWORD));
    productPage = homePage.navigateToProductPage();
    searchProduct("P");
    assertEquals("No matches found for 'P'", productPage.getNoResultsMessage());

    UploadPage uploadPage = homePage.navigateUploads();
    uploadPage.uploadProducts("QA_Products_For_Products_CRUD21.csv");
    uploadPage.verifySuccessMessageOnUploadScreen();
    uploadPage.uploadProgramProductMapping("QA_Program_Products15.csv");
    uploadPage.verifySuccessMessageOnUploadScreen();

    homePage.navigateToProductPage();
    testWebDriver.waitForAjax();
    searchProduct("P");
    assertEquals("21 matches found for 'P'", productPage.getNResultsMessage());

    verifyNumberOFPageLinksDisplayed(21, 10);
    verifyPageNumberLinksDisplayed();
    verifyPageNumberSelected(1);
    verifyNextAndLastPageLinksEnabled();
    verifyPreviousAndFirstPageLinksDisabled();
    verifyNumberOfLineItemsVisibleOnPage(10);

    verifyCategoryOrderOnPage(asList("anaesthetics", "", "", "", "", "", "", "", "Antibiotics", ""));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets19", "Indinavir 40mg Tablets8", "Indinavir 400mg Tablets13",
      "Indinavir 400mg Tablets17", "Indinavir 400mg Tablets21", "Indinavir 400mg Tablets4", "Indinavir 400mg Tablets6",
      "Indinavir 400mg Tablets15", "AIndinavir 400mg Tablets2", "indinavir 400mg Tablets1"));
    verifyProgramOrderOnPage(asList("ESSENTIAL MEDICINES", "ESSENTIAL MEDICINES", "hiv", "hiv", "hiv", "hiv", "hiv", "TB", "MALARIA", "TB"));

    navigateToPage(2);
    verifyPageNumberSelected(2);
    verifyNextAndLastPageLinksEnabled();
    verifyPreviousAndFirstPageLinksEnabled();
    verifyNumberOfLineItemsVisibleOnPage(10);
    verifyCategoryOrderOnPage(asList("Antibiotics", "category3", "", "", "", "Uncategorised", "", "", "", ""));
    verifyProgramOrderOnPage(asList("TB", "hiv", "hiv", "MALARIA", "VACCINES", "", "", "", "", ""));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets3", "Indinavir 400mg Tablets10", "Indinavir 400mg Tablets11",
      "Indinavir 400mg Tablets12", "Indinavir 400mg Tablets16", "Indinavir 400mg Tablets14", "Indinavir 400mg Tablets18",
      "Indinavir 400mg Tablets20", "Indinavir 400mg Tablets5", "Indinavir 400mg Tablets7"));
    assertEquals("NA", productPage.getUndefinedActive(8));

    navigateToNextPage();
    verifyPageNumberSelected(3);
    verifyNextAndLastPageLinksDisabled();
    verifyPreviousAndFirstPageLinksEnabled();
    verifyNumberOfLineItemsVisibleOnPage(1);
    verifyCategoryOrderOnPage(asList("Uncategorised"));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets9"));
    verifyProgramOrderOnPage(asList(""));

    navigateToFirstPage();
    verifyPageNumberSelected(1);
    verifyNextAndLastPageLinksEnabled();
    verifyPreviousAndFirstPageLinksDisabled();
    verifyNumberOfLineItemsVisibleOnPage(10);
    verifyCategoryOrderOnPage(asList("anaesthetics", "", "", "", "", "", "", "", "Antibiotics", ""));
    verifyProductNameOrderOnPage(asList("Indinavir 400mg Tablets19", "Indinavir 40mg Tablets8", "Indinavir 400mg Tablets13",
      "Indinavir 400mg Tablets17", "Indinavir 400mg Tablets21", "Indinavir 400mg Tablets4", "Indinavir 400mg Tablets6",
      "Indinavir 400mg Tablets15", "AIndinavir 400mg Tablets2", "indinavir 400mg Tablets1"));
    verifyProgramOrderOnPage(asList("ESSENTIAL MEDICINES", "ESSENTIAL MEDICINES", "hiv", "hiv", "hiv", "hiv", "hiv", "TB", "MALARIA", "TB"));

    navigateToLastPage();
    verifyPageNumberSelected(3);
    verifyNextAndLastPageLinksDisabled();
    verifyPreviousAndFirstPageLinksEnabled();
    verifyNumberOfLineItemsVisibleOnPage(1);

    navigateToPreviousPage();
    verifyPageNumberSelected(2);
    verifyNextAndLastPageLinksEnabled();
{'fraction_non_alphanumeric': 0.11858631747728487, 'fraction_numerical': 0.02418492784607162, 'mean_word_length': 6.060849056603773, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093597', 'n_tokens_mistral': 4698, 'n_tokens_neox': 4306, 'n_words': 922}