package uk.ac.ebi.atlas.acceptance.rest.tests;

import com.google.common.collect.Lists;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.response.ResponseBody;
import org.junit.Test;
import uk.ac.ebi.atlas.acceptance.rest.EndPoint;
import uk.ac.ebi.atlas.search.SemanticQuery;
import uk.ac.ebi.atlas.search.SemanticQueryTerm;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;

public class AutocompleteControllerEIT {

    private static final String END_POINT_URL = "/gxa/json/suggestions";

    private EndPoint subject = new EndPoint(END_POINT_URL,"query=ASP&species=homo sapiens");

    @Test
    public void verifyHeader() {
        Response response = subject.getResponse();

        // http status code OK
        assertThat(response.getStatusCode(), is(200));

        // unicode encoded plain text
        assertThat(response.getContentType(), is("application/json;charset=UTF-8"));

    }

    @Test
    public void shouldReturnNonEmptyJSonObject(){
        //given
        ResponseBody responseBody = subject.getResponse().body();
        String jsonString = responseBody.asString();
        //when
        SemanticQuery query = SemanticQuery.fromJson(jsonString);
        SemanticQueryTerm firstTerm = query.iterator().next();
        //then
        assertThat(firstTerm.value(), is("ASPA"));
        assertThat(firstTerm.category(), is("symbol"));
        assertEquals(query.size(), 15);

    }

    @Test
    public void shouldReturnNonEmptyJSonObjectForBlankSpecies(){
        //given
        EndPoint emptySpeciesRequest = new EndPoint(END_POINT_URL,"query=ASP");
        ResponseBody responseBody = emptySpeciesRequest.getResponse().body();
        String jsonString = responseBody.asString();
        //when
        SemanticQuery query = SemanticQuery.fromJson(jsonString);
        List<SemanticQueryTerm> suggestionList = Lists.newArrayList(query);
        //then
        assertThat(suggestionList, hasSize(15));
        for (SemanticQueryTerm queryTerm : suggestionList) {
            assertThat(queryTerm.value().toLowerCase(), containsString("asp"));
        }
    }

}

@@ -479,16 +479,65 @@
 String;%0A
+import static org.hamcrest.Matchers.greaterThan;%0A
 import s
@@ -635,33 +635,36 @@
 org.
-junit.Assert.assertEquals
+hamcrest.Matchers.startsWith
 ;%0A%0Ap
@@ -776,16 +776,68 @@
 stions%22;
+%0A    private static final String QUERY_TEXT = %22ASP%22;
 %0A%0A    pr
@@ -895,19 +895,34 @@
 ,%22query=
-ASP
+%22 + QUERY_TEXT + %22
 &species
@@ -926,17 +926,17 @@
 ies=homo
- 
+_
 sapiens%22
@@ -1648,17 +1648,29 @@
 (), 
-is(%22ASPA%22
+startsWith(QUERY_TEXT
 ));%0A
@@ -1743,14 +1743,12 @@
 sert
-Equals
+That
 (que
@@ -1758,18 +1758,30 @@
 size(), 
-15
+greaterThan(5)
 );%0A%0A    
@@ -1856,16 +1856,17 @@
 pecies()
+ 
 %7B%0A      
@@ -1948,20 +1948,30 @@
 ,%22query=
-ASP%22
+%22 + QUERY_TEXT
 );%0A     
@@ -2315,10 +2315,22 @@
 ize(
-15
+greaterThan(0)
 ));%0A
@@ -2465,13 +2465,32 @@
 ing(
-%22asp%22
+QUERY_TEXT.toLowerCase()
 ));%0A

Make tests more robust