package uk.ac.ebi.atlas.widget;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.hamcrest.Matchers;
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.acceptance.rest.fixtures.RestAssuredFixture;
import uk.ac.ebi.atlas.search.SemanticQuery;
import uk.ac.ebi.atlas.search.analyticsindex.baseline.BaselineAnalyticsSearchService;
import uk.ac.ebi.atlas.search.analyticsindex.differential.DifferentialAnalyticsSearchService;
import uk.ac.ebi.atlas.trader.SpeciesFactory;

import javax.inject.Inject;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:solrContextIT.xml", "classpath:oracleContext.xml"})
public class AnalyticsSearchServiceIT extends RestAssuredFixture {

    private static final String BASELINE_GENE = "ENSG00000000003";
    private static final String DIFFERENTIAL_GENE = "AT2G06310";
    private static final String NON_EXISTENT_GENE = "FOOBAR";

    @Inject
    private BaselineAnalyticsSearchService baselineAnalyticsSearchService;

    @Inject
    private DifferentialAnalyticsSearchService differentialAnalyticsSearchService;

    @Test
    public void geneExpressedInBaselineAndDifferentialExperimentsReturnsTrue() {
        JsonObject result = baselineAnalyticsSearchService.findFacetsForTreeSearch(SemanticQuery.create(BASELINE_GENE),
                SemanticQuery.create(), SpeciesFactory.NULL);
        assertThat(result.entrySet(), not(Matchers.empty()));
        assertTrue("This Ensembl gene has a homo sapiens result", result.has("homo sapiens"));

    }

    @Test
    public void geneExpressedInDifferentialExperimentsOnlyReturnsFalse() {
        assertThat(
                baselineAnalyticsSearchService.findFacetsForTreeSearch(SemanticQuery.create(DIFFERENTIAL_GENE), SemanticQuery.create(), SpeciesFactory.NULL),
                is(new JsonObject()));
        assertThat(
                differentialAnalyticsSearchService.fetchDifferentialFacetsForSearch(SemanticQuery.create
                        (DIFFERENTIAL_GENE)).entrySet(), not(Matchers.<Map.Entry<String,JsonElement>>empty()));
    }

    @Test
    public void nonExistentGeneReturnsFalse() {
        assertThat(
                baselineAnalyticsSearchService.findFacetsForTreeSearch(SemanticQuery.create(NON_EXISTENT_GENE), SemanticQuery.create(), SpeciesFactory.NULL),
                is(new JsonObject()));
        assertThat(
                differentialAnalyticsSearchService.fetchDifferentialResultsForSearch(SemanticQuery.create
                        (NON_EXISTENT_GENE)).get("results").getAsJsonArray().size(),
                is(0));
    }
}

@@ -26,16 +26,93 @@
 idget;%0A%0A
+import com.google.common.collect.ImmutableList;%0Aimport com.google.gson.Gson;%0A
 import c
@@ -903,16 +903,65 @@
 rtThat;%0A
+import static org.hamcrest.Matchers.greaterThan;%0A
 import s
@@ -1714,24 +1714,101 @@
 chService;%0A%0A
+    //These tests are really the assertions for ExpressionDataControllerEIT.%0A
     @Test%0A  
@@ -1874,19 +1874,8 @@
 ents
-ReturnsTrue
 () %7B
@@ -2290,28 +2290,16 @@
 entsOnly
-ReturnsFalse
 () %7B%0A   
@@ -2801,20 +2801,8 @@
 Gene
-ReturnsFalse
 () %7B
@@ -3259,11 +3259,1239 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void differentialAnalyticsSearchServiceHasTheRightReturnFormat()%7B%0A        JsonObject result = differentialAnalyticsSearchService.fetchDifferentialResultsForSearch(SemanticQuery.create(%22GO:0008150%22));%0A        testDifferentialResultsAreInRightFormat(result);%0A    %7D%0A%0A    private ImmutableList%3CString%3E fieldsNeededInDifferentialResults = ImmutableList.of(%0A            %22species%22,%0A            %22kingdom%22,%0A            %22experimentType%22,%0A            %22numReplicates%22,%0A            %22regulation%22,%0A            %22factors%22,%0A            %22bioentityIdentifier%22,%0A            %22experimentAccession%22,%0A            %22experimentName%22,%0A            %22contrastId%22,%0A            %22comparison%22,%0A            %22foldChange%22,%0A            %22colour%22,%0A            %22id%22);%0A%0A    void testDifferentialResultsAreInRightFormat(JsonObject result)%7B%0A        assertTrue(new Gson().toJson(result), result.has(%22results%22));%0A        assertThat(result.get(%22results%22).getAsJsonArray().size(), greaterThan(0));%0A        for(JsonElement e: result.get(%22results%22).getAsJsonArray())%7B%0A            for(String fieldName: fieldsNeededInDifferentialResults) %7B%0A                assertTrue(%22result has %22+fieldName, e.getAsJsonObject().has(fieldName));%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Test that shows us which fields we have in analytics search service