package uk.ac.ebi.atlas.experimentpage.baseline;

import com.google.common.collect.ImmutableList;
import org.junit.Before;
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.experimentpage.context.BaselineRequestContext;
import uk.ac.ebi.atlas.model.ExpressionUnit;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExperiment;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineProfilesList;
import uk.ac.ebi.atlas.profiles.baseline.BaselineProfileStreamOptions;
import uk.ac.ebi.atlas.profiles.stream.ProteomicsBaselineProfileStreamFactory;
import uk.ac.ebi.atlas.profiles.stream.RnaSeqBaselineProfileStreamFactory;
import uk.ac.ebi.atlas.search.SemanticQuery;
import uk.ac.ebi.atlas.solr.query.GeneQueryResponse;
import uk.ac.ebi.atlas.solr.query.SolrQueryService;
import uk.ac.ebi.atlas.trader.ExpressionAtlasExperimentTrader;
import uk.ac.ebi.atlas.web.BaselineRequestPreferences;
import uk.ac.ebi.atlas.web.ProteomicsBaselineRequestPreferences;
import uk.ac.ebi.atlas.web.RnaSeqBaselineRequestPreferences;

import javax.inject.Inject;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:solrContext.xml", "classpath:dbContext.xml"})
public class BaselineProfilesHeatMapIT {

    BaselineProfilesHeatMap<BaselineProfileStreamOptions<? extends ExpressionUnit.Absolute>> subject;

    @Inject
    SolrQueryService solrQueryService;

    @Inject
    RnaSeqBaselineProfileStreamFactory rnaSeqBaselineProfileStreamFactory;

    @Inject
    ProteomicsBaselineProfileStreamFactory proteomicsBaselineProfileStreamFactory;

    BaselineRequestPreferences<? extends ExpressionUnit.Absolute> requestPreferences;

    BaselineRequestContext<? extends ExpressionUnit.Absolute> baselineRequestContext;

    BaselineExperiment baselineExperiment;

    @Inject
    ExpressionAtlasExperimentTrader experimentTrader;

    @Before
    public void initRequestContext() throws ExecutionException {
        ImmutableList<String> experimentAccessions = ImmutableList.copyOf(experimentTrader.getAllBaselineExperimentAccessions());
        int randomIndex = ThreadLocalRandom.current().nextInt(0, experimentAccessions.size());
        baselineExperiment = (BaselineExperiment) experimentTrader.getPublicExperiment(experimentAccessions.get(randomIndex));

        if (baselineExperiment.getType().isRnaSeqBaseline()) {
            requestPreferences = new RnaSeqBaselineRequestPreferences();
            baselineRequestContext = new BaselineRequestContext<>(requestPreferences, baselineExperiment);
            subject = new BaselineProfilesHeatMap(rnaSeqBaselineProfileStreamFactory);
        } else { //if (baselineExperiment.getType().isProteomicsBaseline()) {
            requestPreferences = new ProteomicsBaselineRequestPreferences();
            baselineRequestContext = new BaselineRequestContext<>(requestPreferences, baselineExperiment);
            subject = new BaselineProfilesHeatMap(proteomicsBaselineProfileStreamFactory);
        }
    }

    @Test
    public void weCanGetAnyExperimentAtAll()  {
        if(Math.random() < 0.5) {
            setNotSpecific();
        }
        if(Math.random() < 0.5) {
            setGeneQueryProteinCoding();
        }

        GeneQueryResponse geneQueryResponse = solrQueryService.fetchResponse
                (baselineRequestContext.getGeneQuery(), baselineRequestContext.getSpecies());

        BaselineProfilesList profiles = subject.fetch(baselineExperiment, baselineRequestContext, geneQueryResponse);

        assertThat(profiles.size(), greaterThan(0));
    }

    private void setGeneQueryProteinCoding() {
        requestPreferences.setGeneQuery(SemanticQuery.create("protein_coding"));
    }

    private void setNotSpecific() {
        requestPreferences.setSpecific(false);
    }

}

@@ -87,24 +87,71 @@
 utableList;%0A
+import com.google.common.collect.ImmutableSet;%0A
 import org.j
@@ -163,16 +163,16 @@
 Before;%0A
-
 import o
@@ -902,53 +902,8 @@
 ry;%0A
-import uk.ac.ebi.atlas.search.SemanticQuery;%0A
 impo
@@ -1706,16 +1706,170 @@
 apIT %7B%0A%0A
+    static final ImmutableSet%3CString%3E EXPERIMENTS_ACCESSIONS_WITH_NO_PROTEIN_CODING_RESULTS =%0A            ImmutableSet.of(%22E-MTAB-2037%22, %22E-MTAB-3028%22);%0A%0A
     Base
@@ -2782,24 +2782,26 @@
 ns.size());%0A
+//
         base
@@ -2914,16 +2914,119 @@
 Index));
+%0A        baselineExperiment = (BaselineExperiment) experimentTrader.getPublicExperiment(%22E-MTAB-2037%22);
 %0A%0A      
@@ -3853,33 +3853,147 @@
     %7D%0A        if
-(
+ (EXPERIMENTS_ACCESSIONS_WITH_NO_PROTEIN_CODING_RESULTS.contains(baselineExperiment.getAccession()) %7C%7C%0A            
 Math.random() %3C 
@@ -4015,33 +4015,30 @@
     
-setGeneQueryProteinCoding
+removeDefaultGeneQuery
 ();%0A
@@ -4420,33 +4420,30 @@
 oid 
-setGeneQueryProteinCoding
+removeDefaultGeneQuery
 () %7B
@@ -4487,46 +4487,12 @@
 ery(
-SemanticQuery.create(%22protein_coding%22)
+null
 );%0A 

Fix flakey test Some experiments don’t have any genes which match protein_coding, which also is the default query that should be unset sometimes in any case Fixes http://bar:8085/browse/ATLAS-ATLASGXA-556