package com.autonomy.abc.find;

import com.autonomy.abc.base.FindTestBase;
import com.autonomy.abc.selenium.find.FindPage;
import com.autonomy.abc.selenium.find.FindService;
import com.autonomy.abc.selenium.find.results.ResultsView;
import com.autonomy.abc.selenium.query.Query;
import com.hp.autonomy.frontend.selenium.config.TestConfig;
import com.hp.autonomy.frontend.selenium.framework.logging.ActiveBug;
import com.hp.autonomy.frontend.selenium.framework.logging.ResolvedBug;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.hp.autonomy.frontend.selenium.framework.state.TestStateAssert.verifyThat;
import static com.hp.autonomy.frontend.selenium.matchers.ElementMatchers.containsText;
import static com.hp.autonomy.frontend.selenium.matchers.ElementMatchers.hasTagName;
import static com.hp.autonomy.frontend.selenium.matchers.StringMatchers.containsString;
import static org.hamcrest.Matchers.*;

public class ResultsITCase extends FindTestBase {
    private FindPage findPage;
    private FindService findService;

    public ResultsITCase(final TestConfig config) {
        super(config);
    }

    @Before
    public void setUp() {
        findPage = getElementFactory().getFindPage();
        findService = getApplication().findService();
    }

    @Test
    @ResolvedBug("CSA-1665")
    public void testSearchTermInResults() {
        final String searchTerm = "tiger";

        findService.search(searchTerm);

        for (final WebElement searchElement : getDriver().findElements(By.xpath("//*[contains(@class,'search-text') and contains(text(),'" + searchTerm + "')]"))) {
            if (searchElement.isDisplayed()) {        //They can become hidden if they're too far in the summary
                verifyThat(searchElement.getText().toLowerCase(), containsString(searchTerm));
            }
            verifyThat(searchElement, not(hasTagName("a")));
        }
    }

    //Both correctly failing last part because some titles are duplicates
    @Test
    @ActiveBug("CSA-2082")
    public void testAutoScroll() {
        ResultsView results = findService.search("nightmare");

        verifyThat(results.getResults().size(), lessThanOrEqualTo(30));

        findPage.scrollToBottom();
        verifyThat(results.getResults(), hasSize(allOf(greaterThanOrEqualTo(30), lessThanOrEqualTo(60))));

        findPage.scrollToBottom();
        verifyThat(results.getResults(), hasSize(allOf(greaterThanOrEqualTo(60), lessThanOrEqualTo(90))));

        final List<String> titles = results.getResultTitles();
        final Set<String> titlesSet = new HashSet<>(titles);

        verifyThat("No duplicate titles", titles, hasSize(titlesSet.size()));
    }

    @Test
    @ResolvedBug("CCUK-3647")
    public void testNoMoreResultsFoundAtEnd() {
        ResultsView results = findService.search(new Query("oesophageal"));

        verifyThat(results.getResults().size(), lessThanOrEqualTo(30));

        findPage.scrollToBottom();
        verifyThat(results.resultsDiv(), containsText("No more results found"));
    }

    @Test
    @ResolvedBug("FIND-93")
    public void testNoResults() {
        ResultsView results = findService.search("thissearchwillalmostcertainlyreturnnoresults");

        verifyThat(results.resultsDiv(), containsText("No results found"));

        findPage.scrollToBottom();

        final int occurrences = StringUtils.countMatches(results.resultsDiv().getText(), "results found");
        verifyThat("Only one message showing at the bottom of search results", occurrences, is(1));
    }

}

@@ -167,16 +167,74 @@
 ervice;%0A
+import com.autonomy.abc.selenium.find.results.FindResult;%0A
 import c
@@ -394,78 +394,8 @@
 ig;%0A
-import com.hp.autonomy.frontend.selenium.framework.logging.ActiveBug;%0A
 impo
@@ -622,24 +622,52 @@
 ebElement;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -2109,82 +2109,8 @@
  %7D%0A%0A
-    //Both correctly failing last part because some titles are duplicates%0A
     
@@ -2124,14 +2124,16 @@
    @
-Acti
+Resol
 ve
+d
 Bug(
@@ -2610,22 +2610,16 @@
         
-final 
 List%3CStr
@@ -2627,57 +2627,165 @@
 ng%3E 
-titles = results.getResultTitles();%0A        final
+references = new ArrayList%3C%3E();%0A%0A        for(FindResult result : results.getResults())%7B%0A            references.add(result.getReference());%0A        %7D%0A%0A       
  Set
@@ -2793,20 +2793,24 @@
 String%3E 
-titl
+referenc
 esSet = 
@@ -2827,17 +2827,161 @@
 t%3C%3E(
-titles);%0A
+references);%0A%0A        /* References apparently may not be unique, but they're definitely ~more unique%0A                than titles within our data set  */
 %0A   
@@ -3014,21 +3014,29 @@
 ate 
-titles%22, titl
+references%22, referenc
 es, 
@@ -3043,20 +3043,24 @@
 hasSize(
-titl
+referenc
 esSet.si

Update testAutoScroll to look for duplicate references (much less likely than titles)