
package uk.ac.ebi.atlas.experimentpage.differential.download;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import uk.ac.ebi.atlas.experimentpage.context.RnaSeqRequestContext;
import uk.ac.ebi.atlas.model.experiment.differential.DifferentialExperiment;
import uk.ac.ebi.atlas.profiles.differential.rnaseq.RnaSeqProfileStreamFactory;
import uk.ac.ebi.atlas.profiles.writer.RnaSeqDifferentialProfilesWriterFactory;
import uk.ac.ebi.atlas.solr.query.SolrQueryService;
import uk.ac.ebi.atlas.trader.ExperimentTrader;
import uk.ac.ebi.atlas.web.DifferentialRequestPreferences;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RnaSeqExperimentDownloadControllerTest {

    public static final String EXPERIMENT_ACCESSION = "experimentAccession";
    
    @Mock
    HttpServletRequest requestMock;

    @Mock
    DifferentialRequestPreferences preferencesMock;

    @Mock
    HttpServletResponse responseMock;

    @Mock
    DifferentialExperiment experimentMock;

    @Mock
    RnaSeqRequestContext requestContextMock;

    @Mock
    PrintWriter printWriterMock;

    @Mock
    ExpressionsWriter expressionsWriterMock;

    @Mock
    ExperimentTrader experimentTrader;

    @Mock
    RnaSeqProfileStreamFactory rnaSeqProfileStreamFactory;
    
    @Mock
    RnaSeqDifferentialProfilesWriterFactory rnaSeqDifferentialProfilesWriterFactory;
    
    @Mock
    SolrQueryService solrQueryService;
    
    @Mock
    DataWriterFactory dataWriterFactory;

    RnaSeqExperimentDownloadController subject;

    @Before
    public void setUp() throws Exception {
                
        subject = new RnaSeqExperimentDownloadController(rnaSeqProfileStreamFactory, rnaSeqDifferentialProfilesWriterFactory,
                solrQueryService,dataWriterFactory, experimentTrader);
        when(experimentTrader.getExperiment(EXPERIMENT_ACCESSION,"")).thenReturn(experimentMock);
        when(experimentMock.getAccession()).thenReturn(EXPERIMENT_ACCESSION);
        when(responseMock.getWriter()).thenReturn(printWriterMock);
    }

    @Test
    public void testDownloadGeneProfiles() throws Exception {
        subject.downloadGeneProfiles(EXPERIMENT_ACCESSION,"", preferencesMock, responseMock);

        verify(responseMock).setHeader("Content-Disposition", "attachment; filename=\"" + EXPERIMENT_ACCESSION + "-query-results.tsv\"");
        verify(responseMock).setContentType("text/plain; charset=utf-8");

    }

    @Test
    public void testDownloadRawCounts() throws Exception {
        when(dataWriterFactory.getRnaSeqRawDataWriter(experimentMock, printWriterMock)).thenReturn(expressionsWriterMock);
        when(expressionsWriterMock.write()).thenReturn(0L);

        subject.downloadRawCounts(EXPERIMENT_ACCESSION,"",requestMock, responseMock);

        verify(expressionsWriterMock).write();
        verify(responseMock).setHeader("Content-Disposition", "attachment; filename=\"" + EXPERIMENT_ACCESSION + "-raw-counts.tsv\"");
        verify(responseMock).setContentType("text/plain; charset=utf-8");
    }

    @Test
    public void testDownloadAllAnalytics() throws Exception {

        when(dataWriterFactory.getRnaSeqAnalyticsDataWriter(experimentMock, printWriterMock)).thenReturn(expressionsWriterMock);
        when(expressionsWriterMock.write()).thenReturn(0L);

        subject.downloadAllAnalytics(EXPERIMENT_ACCESSION,"",requestMock, responseMock);

        verify(expressionsWriterMock).write();
        verify(responseMock).setHeader("Content-Disposition", "attachment; filename=\"" + EXPERIMENT_ACCESSION + "-analytics.tsv\"");
        verify(responseMock).setContentType("text/plain; charset=utf-8");
    }
}
@@ -206,24 +206,84 @@
 UnitRunner;%0A
+import org.springframework.mock.web.MockHttpServletRequest;%0A
 import uk.ac
@@ -603,34 +603,269 @@
 as.s
-olr.query.SolrQueryService
+earch.SemanticQuery;%0Aimport uk.ac.ebi.atlas.solr.query.GeneQueryResponse;%0Aimport uk.ac.ebi.atlas.solr.query.SolrQueryService;%0Aimport uk.ac.ebi.atlas.species.Species;%0Aimport uk.ac.ebi.atlas.species.SpeciesFactory;%0Aimport uk.ac.ebi.atlas.species.SpeciesProperties
 ;%0Aim
@@ -1092,16 +1092,95 @@
 riter;%0A%0A
+import static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.eq;%0A
 import s
@@ -1422,34 +1422,24 @@
 sion%22;%0A    %0A
-    @Mock%0A
     HttpServ
@@ -1456,36 +1456,91 @@
  requestMock
-;%0A%0A    @Mock
+ = new MockHttpServletRequest(%22GET%22, %22https://www.ebi.ac.uk/gxa%22);%0A
 %0A    Differe
@@ -1574,24 +1574,63 @@
 ferencesMock
+ = new DifferentialRequestPreferences()
 ;%0A%0A    @Mock
@@ -2325,455 +2325,728 @@
     
-        %0A        subject = new RnaSeqExperimentDownloadController(rnaSeqProfileStreamFactory, rnaSeqDifferentialProfilesWriterFactory,%0A                solrQueryService,dataWriterFactory, experimentTrader);%0A        when(experimentTrader.getExperiment(EXPERIMENT_ACCESSION,%22%22)).thenReturn(experimentMock);%0A        when(experimentMock.getAccession()).thenReturn(EXPERIMENT_ACCESSION);%0A        when(responseMock.getWriter()).thenReturn(printWriterMock
+Species species = new Species(%22%22, SpeciesProperties.UNKNOWN);%0A        when(experimentTrader.getExperiment(EXPERIMENT_ACCESSION,%22%22)).thenReturn(experimentMock);%0A        when(experimentMock.getAccession()).thenReturn(EXPERIMENT_ACCESSION);%0A        when(experimentMock.getSpecies()).thenReturn(species);%0A        when(responseMock.getWriter()).thenReturn(printWriterMock);%0A        when(solrQueryService.fetchResponse(any(SemanticQuery.class), eq(species.getReferenceName())))%0A                .thenReturn(new GeneQueryResponse());%0A        subject = new RnaSeqExperimentDownloadController(rnaSeqProfileStreamFactory, rnaSeqDifferentialProfilesWriterFactory,%0A                solrQueryService,dataWriterFactory, experimentTrader
 );%0A 

Fix test setup