package uk.ac.ebi.atlas.web;

import com.google.common.collect.ImmutableMap;
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.search.SemanticQuery;
import uk.ac.ebi.atlas.trader.ArrayDesignTrader;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ApplicationPropertiesTest {
    private static final String FEEDBACK_EMAIL_PROPERTY_KEY = "feedback.email";
    private static final String FEEDBACK_EMAIL_VALUE = "abc@abc.com";
    private static final String ARRAYEXPRESS_URL = "https://www.ebi.ac.uk/arrayexpress/";
    private static final String EXPERIMENT_ARRAYEXPRESS_URL_TEMPLATE = "experiment.arrayexpress.url.template";
    private static final String EXPERIMENT_ACCESSION = "EXPERIMENT_ACCESSION";
    private static final String A_AFFY_35 = "A-AFFY-35";
    private static final String A_AFFY_35_NAME = "A-AFFY-35-NAME";
    private static final String EXPERIMENT_ARRAYEXPRESS_ARRAYS_URL_TEMPLATE = "experiment.arrayexpress.arrays.url.template";
    private static final String ARRAYEXPRESS_ARRAYS_URL = "https://www.ebi.ac.uk/arrayexpress/arrays/";
    private static final String EXPERIMENT_PUBMED_URL_TEMPLATE = "experiment.pubmed.url.template";
    private static final String PUBMED_URL = "https://europepmc.org/abstract/MED/";
    private static final String PUB_MED_ID = "123456";

    private static final String EXPERIMENT_URL = "http://x.y/z/experiments/X";
    private static final String CONTEXT_PATH = "/z";

    private static final Map<String, String[]> REQUEST_PARAMETERS_MAP =
            ImmutableMap.of("p1",new String[]{"1"},"p2",new String[]{"2"});
    private static final String REQUEST_PARAMETERS = "p2=2&p1=1";
    private static final String DOWNLOAD_URL =
            "/experiments/X.tsv?" + REQUEST_PARAMETERS+"&geneQuery="+SemanticQuery.create().toUrlEncodedJson();

    @Mock
    private HttpServletRequest httpServletRequestMock;

    @Mock
    private Properties configurationPropertiesMock;

    @Mock
    private ArrayDesignTrader arrayDesignTraderMock;

    private ApplicationProperties subject;

    @Before
    public void setUp() throws Exception {
        when(configurationPropertiesMock.getProperty(
                EXPERIMENT_ARRAYEXPRESS_URL_TEMPLATE)).thenReturn(ARRAYEXPRESS_URL + "{0}");
        when(configurationPropertiesMock.getProperty(
                FEEDBACK_EMAIL_PROPERTY_KEY)).thenReturn(FEEDBACK_EMAIL_VALUE);

        when(configurationPropertiesMock.getProperty(
                EXPERIMENT_ARRAYEXPRESS_ARRAYS_URL_TEMPLATE)).thenReturn(ARRAYEXPRESS_ARRAYS_URL + "{0}");
        when(configurationPropertiesMock.getProperty(EXPERIMENT_PUBMED_URL_TEMPLATE)).thenReturn(PUBMED_URL + "{0}");

        //given
        when(httpServletRequestMock.getContextPath()).thenReturn(CONTEXT_PATH);
        when(httpServletRequestMock.getRequestURI()).thenReturn(EXPERIMENT_URL);
        when(httpServletRequestMock.getParameterMap()).thenReturn(REQUEST_PARAMETERS_MAP);

        when(arrayDesignTraderMock.getArrayDesignAccession(A_AFFY_35_NAME)).thenReturn(A_AFFY_35);

        subject = new ApplicationProperties(configurationPropertiesMock, arrayDesignTraderMock);
    }

    @Test
    public void testGetArrayExpressURL() throws Exception {
        assertThat(subject.getArrayExpressURL(EXPERIMENT_ACCESSION), is(ARRAYEXPRESS_URL + EXPERIMENT_ACCESSION));
    }

    @Test
    public void testGetArrayExpressArrayURL() throws Exception {
        assertThat(subject.getArrayExpressArrayURL(A_AFFY_35_NAME), is(ARRAYEXPRESS_ARRAYS_URL + A_AFFY_35));
    }

    @Test
    public void testGetPubMedURL() throws Exception {
        assertThat(subject.getPubMedURL(PUB_MED_ID), is(PUBMED_URL + PUB_MED_ID));
    }

    @Test
    public void testGetFeedbackEmailAddress() throws Exception {
        assertThat(subject.getFeedbackEmailAddress(), is(FEEDBACK_EMAIL_VALUE));
    }

    @Test
    public void buildDownloadUrl() {
        //when
        String downloadUrl = subject.buildDownloadURL(SemanticQuery.create(),httpServletRequestMock);

        //then
        assertThat(downloadUrl, is(DOWNLOAD_URL));
    }

    @Test
    public void buildServerUrl() throws Exception {

        when(httpServletRequestMock.getServerName()).thenReturn("localhost");
        when(httpServletRequestMock.getServerPort()).thenReturn(9090);
        when(httpServletRequestMock.getContextPath()).thenReturn("/gxa");
        when(httpServletRequestMock.isSecure()).thenReturn(false);

        assertThat(ApplicationProperties.buildServerURL(httpServletRequestMock), is("http://localhost:9090/gxa"));
    }
}

@@ -268,16 +268,106 @@
 cQuery;%0A
+import uk.ac.ebi.atlas.species.Species;%0Aimport uk.ac.ebi.atlas.species.SpeciesProperties;%0A
 import u
@@ -407,16 +407,16 @@
 Trader;%0A
-
 %0Aimport 
@@ -450,24 +450,47 @@
 letRequest;%0A
+import java.util.List;%0A
 import java.
@@ -582,16 +582,16 @@
 rtThat;%0A
-
 import s
@@ -622,16 +622,111 @@
 ers.is;%0A
+import static org.hamcrest.Matchers.nullValue;%0Aimport static org.hamcrest.Matchers.startsWith;%0A
 import s
@@ -5104,10 +5104,906 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void getBaselineReferenceExperimentAccession() throws Exception %7B%0A        ImmutableMap%3CString, List%3CString%3E%3E emptySpeciesResources = ImmutableMap.of();%0A        SpeciesProperties homoSapiensProperties =%0A                SpeciesProperties.create(%22Homo_sapiens%22, %22ORGANISM_PART%22, %22animals%22, emptySpeciesResources);%0A        Species species = new Species(%22Homo sapiens%22, homoSapiensProperties);%0A%0A        assertThat(ApplicationProperties.getBaselineReferenceExperimentAccession(species), startsWith(%22E-%22));%0A    %7D%0A%0A    @Test%0A    public void getUnknownSpeciesBaselineReferenceExperimentAccession() throws Exception %7B%0A        Species unknownSpecies = new Species(%22foobar%22, SpeciesProperties.UNKNOWN);%0A%0A        assertThat(unknownSpecies.isUnknown(), is (true));%0A        assertThat(ApplicationProperties.getBaselineReferenceExperimentAccession(unknownSpecies), is(nullValue()));%0A    %7D%0A%0A
 %7D%0A

Test ApplicationProperties.getBaselineReferenceExperimentAccession