package uk.ac.ebi.atlas.search.analyticsindex.differential;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import uk.ac.ebi.atlas.search.SemanticQuery;

import javax.inject.Inject;

import java.util.Map;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.isOneOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContext.xml", "/solrContext.xml", "/dbContext.xml"})
public class DifferentialAnalyticsSearchServiceIT {

    @Inject
    DifferentialAnalyticsSearchService subject;

    private SemanticQuery query = SemanticQuery.create("zinc finger");
    private SemanticQuery condition = SemanticQuery.create("pish");
    private String species = "oryza sativa";


    @Test
    public void fetchDifferentialFacetsForSearch() {
        JsonObject result = subject.fetchDifferentialFacetsForSearch(query);
        assertAboutFacets(result);
    }

    @Test
    public void fetchDifferentialResultsForSearch() {
        JsonObject result = subject.fetchDifferentialResultsForSearch(query);
        assertAboutResults(result);
    }

    @Test
    public void fetchDifferentialFacetsForQuery1() {
        JsonObject result = subject.fetchDifferentialFacetsForQuery(query);
        assertAboutFacets(result);
    }

    @Test
    public void fetchDifferentialFacetsForQuery3() {
        JsonObject result = subject.fetchDifferentialFacetsForQuery(query,condition, species);
        assertAboutFacets(result);
    }

    @Test
    public void fetchDifferentialResultsForQuery1() {
        JsonObject result = subject.fetchDifferentialResultsForQuery(query);
        assertAboutResults(result);
    }
    @Test
    public void fetchDifferentialResultsForQuery() {
        JsonObject result = subject.fetchDifferentialResultsForQuery(query, condition, species);
        assertAboutResults(result);
    }

    private void assertAboutFacets(JsonObject result){
        assertThat(result.entrySet().size(), greaterThan(0));
        for(Map.Entry<String,?> e: result.entrySet()){
            assertThat(e.getKey(),
                    isOneOf("kingdom", "species", "experimentType", "factors", "numReplicates","regulation"));
        }
    }

    private void assertAboutResults(JsonObject result){
        assertTrue(result.has("results"));
        assertThat(result.get("results").getAsJsonArray().size(), greaterThan(0));
        for(JsonElement e: result.get("results").getAsJsonArray()){
            assertTrue(e.getAsJsonObject().has("bioentityIdentifier"));
            assertTrue(e.getAsJsonObject().has("bioentityName"));
            assertTrue(e.getAsJsonObject().has("experimentAccession"));
            assertTrue(e.getAsJsonObject().has("experimentType"));
            assertTrue(e.getAsJsonObject().has("contrastId"));
            assertTrue(e.getAsJsonObject().has("foldChange"));
            assertTrue(e.getAsJsonObject().has("comparison"));
            assertTrue(e.getAsJsonObject().has("experimentName"));
        }
    }
}

@@ -424,16 +424,63 @@
 icQuery;
+%0Aimport uk.ac.ebi.atlas.species.SpeciesFactory;
 %0A%0Aimport
@@ -909,16 +909,134 @@
 ceIT %7B%0A%0A
+    static final SemanticQuery EMPTY_QUERY = SemanticQuery.create();%0A%0A    @Inject%0A    SpeciesFactory speciesFactory;%0A%0A
     @Inj
@@ -1382,41 +1382,33 @@
 etch
-DifferentialFacetsForSearch(query
+Facets(query, EMPTY_QUERY
 );%0A 
@@ -1557,42 +1557,34 @@
 etch
-DifferentialResultsForSearch(query
+Results(query, EMPTY_QUERY
 );%0A 
@@ -1733,40 +1733,33 @@
 etch
-DifferentialFacetsForQuery(query
+Facets(query, EMPTY_QUERY
 );%0A 
@@ -1907,34 +1907,14 @@
 etch
-DifferentialFacetsForQuery
+Facets
 (que
@@ -1926,32 +1926,55 @@
 ndition, species
+Factory.create(species)
 );%0A        asser
@@ -2111,41 +2111,34 @@
 etch
-DifferentialResultsForQuery(query
+Results(query, EMPTY_QUERY
 );%0A 
@@ -2286,35 +2286,15 @@
 etch
-DifferentialResultsForQuery
+Results
 (que
@@ -2315,16 +2315,39 @@
  species
+Factory.create(species)
 );%0A     

Fix tests to use renamed methods