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

import com.google.common.collect.ImmutableList;
import com.google.gson.JsonObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import uk.ac.ebi.atlas.experimentpage.context.BaselineRequestContext;
import uk.ac.ebi.atlas.model.experiment.ExperimentDisplayDefaults;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExperiment;
import uk.ac.ebi.atlas.profiles.baseline.BaselineProfileStreamOptions;
import uk.ac.ebi.atlas.profiles.stream.RnaSeqBaselineProfileStreamFactory;
import uk.ac.ebi.atlas.resource.MockDataFileHub;
import uk.ac.ebi.atlas.trader.ExperimentTrader;
import uk.ac.ebi.atlas.web.RnaSeqBaselineRequestPreferences;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HistogramServiceTest {
    MockDataFileHub dataFileHub;
    @Spy
    RnaSeqBaselineProfileStreamFactory rnaSeqBaselineProfileStreamFactory = new RnaSeqBaselineProfileStreamFactory(dataFileHub);

    @Mock
    ExperimentTrader experimentTrader;

    @Mock
    BaselineExperiment experiment;

    CutoffScale.Scaled cutoffScale = new CutoffScale.Scaled();

    HistogramService subject;

    @Before
    public void setUp() throws Exception {
        MockDataFileHub dataFileHub = new MockDataFileHub();

        subject = new HistogramService(rnaSeqBaselineProfileStreamFactory, experimentTrader, cutoffScale.get());

        when(experiment.getAccession()).thenReturn("accession");
        when(experiment.getDisplayDefaults()).thenReturn(ExperimentDisplayDefaults.simpleDefaults());

        when(experimentTrader.getExperiment("accession", "")).thenReturn(experiment);

        dataFileHub.addTpmsExpressionFile("accession", ImmutableList.of( new String[]{"Gene ID", "Gene name"},
                new String[]{"id_1", "name_1"}));
    }

    @Test
    public void testGet() throws Exception {
        BaselineProfileStreamOptions baselineProfileStreamOptions = new BaselineRequestContext<>(new RnaSeqBaselineRequestPreferences(), experiment);
        JsonObject result = subject.get("accession", "", baselineProfileStreamOptions).asJson();

        assertThat(result.has("bins"), is(true));
        Mockito.verify(rnaSeqBaselineProfileStreamFactory).histogram(experiment, baselineProfileStreamOptions, cutoffScale.get());
    }

    @Test
    public void cachingWorks() throws Exception {
        subject.get("accession", "", new BaselineRequestContext<>(new RnaSeqBaselineRequestPreferences(), experiment)).asJson();
        subject.get("accession", "", new BaselineRequestContext<>(new RnaSeqBaselineRequestPreferences(), experiment)).asJson();

        Mockito.verify(rnaSeqBaselineProfileStreamFactory, times(1)).histogram(eq(experiment), ArgumentMatchers.any(), eq(cutoffScale.get()));
    }
}
@@ -318,32 +318,8 @@
 to;%0A
-import org.mockito.Spy;%0A
 impo
@@ -429,16 +429,61 @@
 ontext;%0A
+import uk.ac.ebi.atlas.model.ExpressionUnit;%0A
 import u
@@ -1235,16 +1235,8 @@
 ub;%0A
-    @Spy
 %0A   
@@ -1309,62 +1309,8 @@
 tory
- = new RnaSeqBaselineProfileStreamFactory(dataFileHub)
 ;%0A%0A 
@@ -1488,16 +1488,95 @@
 mService
+%3CBaselineProfileStreamOptions%3CExpressionUnit.Absolute.Rna%3E, BaselineExperiment%3E
  subject
@@ -1637,29 +1637,25 @@
 n %7B%0A        
-MockD
+d
 ataFileHub d
@@ -1657,31 +1657,130 @@
 Hub 
-dataFileHub = new MockD
+= new MockDataFileHub();%0A        rnaSeqBaselineProfileStreamFactory = Mockito.spy(new RnaSeqBaselineProfileStreamFactory(d
 ataF
@@ -1785,17 +1785,17 @@
 aFileHub
-(
+)
 );%0A%0A    
@@ -1828,16 +1828,18 @@
 mService
+%3C%3E
 (rnaSeqB
@@ -2420,16 +2420,45 @@
 mOptions
+%3CExpressionUnit.Absolute.Rna%3E
  baselin
@@ -2480,16 +2480,32 @@
 ptions =
+%0A               
  new Bas
@@ -2785,16 +2785,33 @@
 Factory)
+%0A                
 .histogr
@@ -2962,32 +2962,65 @@
 ect.get(
-%22accession%22, %22%22,
+%0A                %22accession%22, %22%22,%0A               
  new Bas
@@ -3124,32 +3124,65 @@
 ect.get(
-%22accession%22, %22%22,
+%0A                %22accession%22, %22%22,%0A               
  new Bas
@@ -3335,16 +3335,33 @@
 imes(1))
+%0A                
 .histogr

Fix set up of tests - fixes build http://bar:8085/browse/ATLAS-GXACI-56