package uk.ac.ebi.atlas.geneindex;

import org.junit.Before;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.atlas.utils.Files;

import java.io.IOException;
import java.net.URISyntaxException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class IndexClientTestIT {

    private IndexClient subject;

    private GenePropertyQueryBuilder queryBuilder;

    private String jsonData;

    @Before
    public void loadTestData() throws IOException {
        jsonData = Files.readTextFileFromClasspath(this.getClass(), "solr.json");
    }

    @Before
    public void initSubject() {
        RestTemplate restTemplate = new RestTemplate();

        queryBuilder = mock(GenePropertyQueryBuilder.class);

        subject = new IndexClient(restTemplate, queryBuilder);

        //ToDo ...this is bad, requires connection to lime to run... Should we configure it like we did in selenium, setting localhost as default and a target server to be used when running integration profile (bamboo)
        subject.setServerURL("http://lime:8983/solr/");
    }

    @Test
    public void testFindGeneIdJsonValidQuery() throws URISyntaxException {
        //given
        when(queryBuilder.buildQueryString(anyString())).thenReturn("(alltext:\"GO:0008134\" OR alltext:\"p53 " +
                "binding\")");

        String result = subject.findGeneIdJson(anyString(), "homo sapiens");

        //some genes are found
        assertThat(result, containsString("[{\"identifier\":\"ENSG"));
    }


    @Test
    public void testFindGeneIdJsonNotExistingQuery() throws URISyntaxException {
        //given
        when(queryBuilder.buildQueryString(anyString())).thenReturn("(alltext:\"NOT THERE\")");

        String result = subject.findGeneIdJson(anyString(), "homo sapiens");

        //no genes are found
        assertThat(result, containsString("\"numFound\":0"));
    }


}

@@ -77,16 +77,307 @@
 t.Test;%0A
+import org.junit.runner.RunWith;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0Aimport org.springframework.test.context.web.WebAppConfiguration;%0A
 import o
@@ -420,16 +420,16 @@
 mplate;%0A
-
 import u
@@ -754,16 +754,147 @@
 .when;%0A%0A
+@RunWith(SpringJUnit4ClassRunner.class)%0A@WebAppConfiguration%0A@ContextConfiguration(locations = %22classpath:applicationContext.xml%22)%0A
 public c
@@ -1035,16 +1035,96 @@
 nData;%0A%0A
+    @Value(%22#%7Bconfiguration%5B'index.server.url'%5D%7D%22)%0A    private String solrURL;%0A%0A
     @Bef
@@ -1499,227 +1499,8 @@
 );%0A%0A
-        //ToDo ...this is bad, requires connection to lime to run... Should we configure it like we did in selenium, setting localhost as default and a target server to be used when running integration profile (bamboo)%0A
     
@@ -1528,32 +1528,15 @@
 URL(
-%22http://lime:8983/
 solr
-/%22
+URL
 );%0A 

test was failing when running it outside the intranet. Now uses index.server.url configuration that points to solr on localhost when running tests in local.integration.test profile.