package uk.ac.ebi.atlas.web.controllers.rest;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import uk.ac.ebi.atlas.experimentpage.context.BaselineRequestContext;
import uk.ac.ebi.atlas.solr.query.MultiTermSuggestionService;
import uk.ac.ebi.atlas.solr.query.SuggestionService;
import uk.ac.ebi.atlas.search.SemanticQueryTerm;

import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class AutoCompleteControllerTest {

    private static final String QUERY_STRING = "This is a query";

    private static final String HOMO_SAPIENS = "homo sapiens";

    private AutoCompleteController subject;

    @Mock
    private BaselineRequestContext requestContextMock;

    @Mock
    private SuggestionService suggestionServiceMock;

    @Mock
    private MultiTermSuggestionService multiTermSuggestionServiceMock;

    @Before
    public void setUp() throws Exception {
        SemanticQueryTerm termSource1 = SemanticQueryTerm.create("Value1");
        SemanticQueryTerm termSource2 = SemanticQueryTerm.create("Value2");

        List<SemanticQueryTerm> suggestions = Lists.newArrayList(termSource1, termSource2);

        when(suggestionServiceMock.fetchTopSuggestions(QUERY_STRING, HOMO_SAPIENS)).thenReturn(suggestions);

        when(requestContextMock.getFilteredBySpecies()).thenReturn(HOMO_SAPIENS);

        subject = new AutoCompleteController(suggestionServiceMock);

    }

    @Test
    public void fetchTopSuggestions() throws Exception {
        //given
        String jsonResponse = subject.fetchTopSuggestions(QUERY_STRING, HOMO_SAPIENS);

        //then
        assertThat(jsonResponse, is("[{\"value\":\"Value1\",\"source\":\"\"},{\"value\":\"Value2\",\"source\":\"\"}]"));
    }

    @Test
    public void fetchTermSource() throws Exception {

        SemanticQueryTerm termSource1 = SemanticQueryTerm.create("TERM1", "SOURCE1");
        SemanticQueryTerm termSource2 = SemanticQueryTerm.create("TERM2", "SOURCE2");

        SemanticQueryTerm termSource3 = SemanticQueryTerm.create("TERM3", "SOURCE3");
        SemanticQueryTerm termSource4 = SemanticQueryTerm.create("TERM4", "SOURCE4");

        List<SemanticQueryTerm> termSourceList = Lists.newArrayList(termSource1,termSource2,termSource3,termSource4);

        Gson gson = new Gson();

        String suggestions = gson.toJson(termSourceList);

        assertThat(suggestions, is("[{\"value\":\"TERM1\",\"source\":\"SOURCE1\"},{\"value\":\"TERM2\",\"source\":\"SOURCE2\"},{\"value\":\"TERM3\",\"source\":\"SOURCE3\"},{\"value\":\"TERM4\",\"source\":\"SOURCE4\"}]"));
    }

}

@@ -1192,34 +1192,33 @@
 icQueryTerm 
-termSource
+queryTerm
 1 = Semantic
@@ -1267,34 +1267,33 @@
 icQueryTerm 
-termSource
+queryTerm
 2 = Semantic
@@ -1390,31 +1390,29 @@
 ist(
-termSource1, termSource
+queryTerm1, queryTerm
 2);%0A
@@ -1926,30 +1926,32 @@
 %22Value1%5C%22,%5C%22
-source
+category
 %5C%22:%5C%22%5C%22%7D,%7B%5C%22
@@ -1967,30 +1967,32 @@
 %22Value2%5C%22,%5C%22
-source
+category
 %5C%22:%5C%22%5C%22%7D%5D%22))
@@ -2090,26 +2090,25 @@
 eryTerm 
-termSource
+queryTerm
 1 = Sema
@@ -2138,22 +2138,24 @@
 ERM1%22, %22
-SOURCE
+CATEGORY
 1%22);%0A   
@@ -2177,26 +2177,25 @@
 eryTerm 
-termSource
+queryTerm
 2 = Sema
@@ -2225,22 +2225,24 @@
 ERM2%22, %22
-SOURCE
+CATEGORY
 2%22);%0A%0A  
@@ -2265,26 +2265,25 @@
 eryTerm 
-termSource
+queryTerm
 3 = Sema
@@ -2313,22 +2313,24 @@
 ERM3%22, %22
-SOURCE
+CATEGORY
 3%22);%0A   
@@ -2352,26 +2352,25 @@
 eryTerm 
-termSource
+queryTerm
 4 = Sema
@@ -2400,22 +2400,24 @@
 ERM4%22, %22
-SOURCE
+CATEGORY
 4%22);%0A%0A  
@@ -2486,54 +2486,53 @@
 ist(
-termSource1,termSource2,termSource3,termSource
+queryTerm1, queryTerm2, queryTerm3, queryTerm
 4);%0A
@@ -2655,24 +2655,41 @@
 estions, is(
+%0A                
 %22%5B%7B%5C%22value%5C%22
@@ -2701,33 +2701,37 @@
 RM1%5C%22,%5C%22
-source%5C%22:%5C%22SOURCE
+category%5C%22:%5C%22CATEGORY
 1%5C%22%7D,%7B%5C%22
@@ -2754,30 +2754,56 @@
 %22,%5C%22
-source%5C%22:%5C%22SOURCE2%5C%22%7D,
+category%5C%22:%5C%22CATEGORY2%5C%22%7D,%22 +%0A                 %22
 %7B%5C%22v
@@ -2821,33 +2821,37 @@
 RM3%5C%22,%5C%22
-source%5C%22:%5C%22SOURCE
+category%5C%22:%5C%22CATEGORY
 3%5C%22%7D,%7B%5C%22
@@ -2874,25 +2874,29 @@
 %22,%5C%22
-source%5C%22:%5C%22SOURCE
+category%5C%22:%5C%22CATEGORY
 4%5C%22%7D

Fix tests in http://bar:8085/browse/ATLAS-TESTDELIVERY-3913