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

import com.google.common.collect.Sets;
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.MicroarrayRequestContext;
import uk.ac.ebi.atlas.model.experiment.differential.microarray.MicroarrayExperiment;
import uk.ac.ebi.atlas.profiles.differential.microarray.MicroarrayProfileStreamFactory;
import uk.ac.ebi.atlas.profiles.writer.MicroarrayProfilesWriterFactory;
import uk.ac.ebi.atlas.solr.query.SolrQueryService;
import uk.ac.ebi.atlas.trader.ExperimentTrader;
import uk.ac.ebi.atlas.web.MicroarrayRequestPreferences;

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 MicroarrayExperimentDownloadControllerTest {

    private static final String EXPERIMENT_ACCESSION = "experimentAccession";
    private static final String ARRAY_DESIGN = "arrayDesign";
    private static final String ACCESS_KEY = "hunter2";

    @Mock
    private DataWriterFactory dataWriterFactoryMock;

    @Mock
    private MicroarrayExperiment experimentMock;

    @Mock
    private MicroarrayRequestPreferences preferencesMock;

    @Mock
    private MicroarrayRequestContext requestContextMock;

    @Mock
    private HttpServletResponse responseMock;

    @Mock
    private PrintWriter printWriterMock;

    @Mock
    private ExpressionsWriter expressionsWriterMock;

    @Mock
    private ExperimentTrader experimentTrader;

    private MicroarrayExperimentDownloadController subject;

    @Mock
    MicroarrayProfileStreamFactory microarrayProfileStreamFactory;

    @Mock
    MicroarrayProfilesWriterFactory microarrayProfilesWriterFactory;

    @Mock
    SolrQueryService solrQueryService;

    @Mock
    DataWriterFactory dataWriterFactory;

    @Before
    public void setUp() throws Exception {
        subject = new MicroarrayExperimentDownloadController(experimentTrader, microarrayProfileStreamFactory,
                microarrayProfilesWriterFactory, solrQueryService, dataWriterFactory);

        when(experimentTrader.getExperiment(EXPERIMENT_ACCESSION,ACCESS_KEY)).thenReturn(experimentMock);
        when(experimentMock.getAccession()).thenReturn(EXPERIMENT_ACCESSION);
        when(experimentMock.getArrayDesignAccessions()).thenReturn(Sets.newTreeSet(Sets.newHashSet(ARRAY_DESIGN)));
        when(preferencesMock.getArrayDesignAccession()).thenReturn(ARRAY_DESIGN);

    }

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

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

    @Test
    public void testDownloadNormalizedData() throws Exception {
        when(expressionsWriterMock.write()).thenReturn(0L);
        when(dataWriterFactoryMock.getMicroarrayRawDataWriter(experimentMock, printWriterMock, ARRAY_DESIGN)).thenReturn(expressionsWriterMock);

        subject.downloadNormalizedData(EXPERIMENT_ACCESSION, ACCESS_KEY,preferencesMock, responseMock);

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

    @Test
    public void testDownloadLogFoldData() throws Exception {
        when(expressionsWriterMock.write()).thenReturn(0L);
        when(dataWriterFactoryMock.getMicroarrayLogFoldDataWriter(experimentMock, printWriterMock, ARRAY_DESIGN)).thenReturn(expressionsWriterMock);

        subject.downloadLogFoldData(EXPERIMENT_ACCESSION,ACCESS_KEY, preferencesMock, responseMock);

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

    @Test
    public void testDownloadAllAnalytics() throws Exception {
        when(expressionsWriterMock.write()).thenReturn(0L);
        when(dataWriterFactoryMock.getMicroarrayAnalyticsDataWriter(experimentMock, printWriterMock, ARRAY_DESIGN)).thenReturn(expressionsWriterMock);

        subject.downloadAllAnalytics(EXPERIMENT_ACCESSION,ACCESS_KEY, preferencesMock, responseMock);

        verify(expressionsWriterMock).write();
        verify(responseMock).setHeader("Content-Disposition", "attachment; filename=\"" + EXPERIMENT_ACCESSION + "_" + ARRAY_DESIGN + "-analytics.tsv\"");
        verify(responseMock).setContentType("text/plain; charset=utf-8");
    }
}
@@ -594,34 +594,222 @@
 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.SpeciesProperties
 ;%0Aim
@@ -988,16 +988,95 @@
 riter;%0A%0A
+import static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.eq;%0A
 import s
@@ -2206,60 +2206,8 @@
 e;%0A%0A
-    @Mock%0A    DataWriterFactory dataWriterFactory;%0A%0A
     
@@ -2261,207 +2261,8 @@
 n %7B%0A
-        subject = new MicroarrayExperimentDownloadController(experimentTrader, microarrayProfileStreamFactory,%0A                microarrayProfilesWriterFactory, solrQueryService, dataWriterFactory);%0A%0A
     
@@ -2569,82 +2569,714 @@
     
-when(preferencesMock.getArrayDesignAccession()).thenReturn(ARRAY_DESIGN);%0A
+Species species = new Species(%22%22, SpeciesProperties.UNKNOWN);%0A        when(experimentMock.getSpecies()).thenReturn(species);%0A        when(responseMock.getWriter()).thenReturn(printWriterMock);%0A%0A        when(solrQueryService.fetchResponse(any(SemanticQuery.class), eq(species.getReferenceName())))%0A                .thenReturn(new GeneQueryResponse());%0A%0A%0A        when(preferencesMock.getArrayDesignAccession()).thenReturn(ARRAY_DESIGN);%0A%0A        when(responseMock.getWriter()).thenReturn(printWriterMock);%0A%0A        subject = new MicroarrayExperimentDownloadController(experimentTrader, microarrayProfileStreamFactory,%0A                microarrayProfilesWriterFactory, solrQueryService, dataWriterFactoryMock);
 %0A   

Fix test setup