package uk.ac.ebi.atlas.experimentimport.expressiondataserializer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
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.experimentimport.ExperimentChecker;
import uk.ac.ebi.atlas.model.experiment.ExperimentType;
import uk.ac.ebi.atlas.resource.DataFileHub;
import uk.ac.ebi.atlas.trader.ExperimentTrader;

import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContext.xml", "/solrContext.xml", "/oracleContext.xml"})
public class ExpressionSerializerServiceIT {

    private static final String accession = "E-MTAB-513";

    @Inject
    public ExperimentTrader experimentTrader;

    @Mock
    ExperimentChecker experimentChecker;

    @Inject
    DataFileHub dataFileHub;

    private String serializedExpressionsFileTemplate;

    private ExpressionSerializerService subject;


    @Before
    public void setUp() throws IOException {
        MockitoAnnotations.initMocks(this);

        Path tmp = Files.createTempDirectory("serialized_expression");
        serializedExpressionsFileTemplate = tmp.toString() + "/{0}.kryo";

        subject = new ExpressionSerializerService(new RnaSeqBaselineExpressionKryoSerializer(dataFileHub,
                serializedExpressionsFileTemplate),
                experimentChecker);
    }

    @Test
    public void weCanSerializeTheFile() {
        assertThat(dataFileHub.getBaselineExperimentFiles(accession).main.exists(), is(true));
        assertThat(fileExists(serializedExpressionsFileTemplate, accession), is(false));

        subject.kryoSerializeExpressionData(accession, ExperimentType.RNASEQ_MRNA_BASELINE);

        assertThat(fileExists(serializedExpressionsFileTemplate, accession), is(true));
    }

    @Test
    public void weCheckTheFileExistsBeforeWeAttemptToSerializeIt(){
        subject.kryoSerializeExpressionData(accession, ExperimentType.RNASEQ_MRNA_BASELINE);
        verify(experimentChecker).checkAllFiles(eq(accession), any(ExperimentType.class));
    }

    private boolean fileExists(String template, String experimentAccession) {
        return Files.exists(FileSystems.getDefault().getPath(template.replaceAll("\\{0\\}", experimentAccession)));
    }

    @Test
    public void skipIffNotRnaSeqBaseline(){
        for(ExperimentType experimentType : ExperimentType.values()){
            String result = subject.kryoSerializeExpressionData(accession, experimentType);
            assertEquals(result=="skipped", experimentType == ExperimentType.RNASEQ_MRNA_BASELINE);
        }
    }

}

@@ -568,56 +568,8 @@
 Hub;
-%0Aimport uk.ac.ebi.atlas.trader.ExperimentTrader;
 %0A%0Aim
@@ -809,53 +809,8 @@
 is;%0A
-import static org.junit.Assert.assertEquals;%0A
 impo
@@ -1131,24 +1131,16 @@
  %7B%0A%0A    
-private 
 static f
@@ -1182,67 +1182,8 @@
 %22;%0A%0A
-    @Inject%0A    public ExperimentTrader experimentTrader;%0A%0A
     
@@ -1276,24 +1276,16 @@
 b;%0A%0A    
-private 
 String s
@@ -1323,24 +1323,16 @@
 e;%0A%0A    
-private 
 Expressi
@@ -2945,34 +2945,42 @@
 sert
-Equals(result==%22skipped%22, 
+That(%22skipped%22.equals(result), is(
 expe
@@ -2990,17 +2990,17 @@
 entType 
-=
+!
 = Experi
@@ -3021,32 +3021,33 @@
 Q_MRNA_BASELINE)
+)
 ;%0A        %7D%0A    

Fix test by doing the right comparison; do the package-private field thing in test; remove unnecessary collaborators