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

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.experiment.baseline.BaselineExperiment;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineProfilesList;
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.trader.cache.RnaSeqBaselineExperimentsCache;
import uk.ac.ebi.atlas.web.BaselineRequestPreferences;
import uk.ac.ebi.atlas.web.RnaSeqBaselineRequestPreferences;

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

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 {

    public static final String ORGANISM_PART = "ORGANISM_PART";

    private BaselineProfilesHeatMap subject;

    @Inject
    private RnaSeqBaselineExperimentsCache rnaSeqBaselineExperimentsCache;

    @Inject
    SolrQueryService solrQueryService;

    @Inject
    RnaSeqBaselineProfileStreamFactory inputStreamFactory;

    private BaselineRequestPreferences requestPreferences = new RnaSeqBaselineRequestPreferences();

    private BaselineRequestContext baselineRequestContext;

    BaselineExperiment baselineExperiment;

    @Inject
    ExpressionAtlasExperimentTrader experimentTrader;

    @Before
    public void initRequestContext() throws ExecutionException {

        String randomAccession = experimentTrader.getRnaSeqDifferentialExperimentAccessions().iterator().next();
        baselineExperiment = rnaSeqBaselineExperimentsCache.getExperiment(randomAccession);

        baselineRequestContext = new BaselineRequestContext(requestPreferences, baselineExperiment);

        subject = new BaselineProfilesHeatMap(inputStreamFactory);
    }

    // http://localhost:8080/gxa/experiments/E-MTAB-1733?displayLevels=true&_specific=on&geneQuery=R-HSA-73887&geneSetMatch=true
    @Test
    public void weCanGetAnyExperimentAtAll()  {
        if(Math.random() < 0.5) {
            setNotSpecific();
        }
        if(Math.random() < 0.5) {
            setGeneQuery("protein_coding");
        }

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

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

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

    private void setGeneQuery(String geneQueryString) {
        requestPreferences.setGeneQuery(SemanticQuery.create(geneQueryString));
    }

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

}

@@ -848,51 +848,38 @@
 las.
-trader.cache.RnaSeqBaselineExperimentsCache
+web.BaselineRequestPreferences
 ;%0Aim
@@ -895,32 +895,42 @@
 c.ebi.atlas.web.
+Proteomics
 BaselineRequestP
@@ -1529,96 +1529,8 @@
 t;%0A%0A
-    @Inject%0A    private RnaSeqBaselineExperimentsCache rnaSeqBaselineExperimentsCache;%0A%0A
     
@@ -1710,49 +1710,8 @@
 nces
- = new RnaSeqBaselineRequestPreferences()
 ;%0A%0A 
@@ -1957,17 +1957,16 @@
 ption %7B%0A
-%0A
         
@@ -2014,26 +2014,19 @@
 .get
-RnaSeqDifferential
+AllBaseline
 Expe
@@ -2096,14 +2096,9 @@
 t = 
-rnaSeq
+(
 Base
@@ -2115,45 +2115,337 @@
 ment
-sCache.getExperiment(randomAccession)
+) experimentTrader.getPublicExperiment(randomAccession);%0A%0A        requestPreferences =%0A                baselineExperiment.getType().isRnaSeqBaseline() ? new RnaSeqBaselineRequestPreferences() :%0A                baselineExperiment.getType().isProteomicsBaseline() ? new ProteomicsBaselineRequestPreferences() :%0A                null
 ;%0A%0A 
@@ -2942,25 +2942,22 @@
 uery
-(%22p
+P
 rotein
-_c
+C
 oding
-%22
+(
 );%0A 
@@ -3235,32 +3235,16 @@
 Context,
-%0A               
  geneQue
@@ -3350,31 +3350,22 @@
 uery
-(String geneQueryStr
+ProteinCod
 ing
+(
 ) %7B%0A
@@ -3429,23 +3429,24 @@
 ate(
-geneQueryStr
+%22protein_cod
 ing
+%22
 ));%0A

Fix test (for real)