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.beans.factory.annotation.Value;
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.ExperimentType;
import uk.ac.ebi.atlas.trader.ExperimentTrader;
import uk.ac.ebi.atlas.trader.cache.BaselineExperimentsCache;
import uk.ac.ebi.atlas.utils.CsvReaderFactory;

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

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.verify;

@Named
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:solrContextIT.xml", "classpath:oracleContext.xml"})
public class ExpressionSerializerServiceIT {

    @Inject
    public ExperimentTrader experimentTrader;

    @Inject
    public BaselineExperimentsCache baselineExperimentsCache;

    @Mock
    ExperimentChecker experimentChecker;

    private String serializedExpressionsFileTemplate;
    private String serializedExpressionLevelsFileTemplate;

    @Value("#{configuration['experiment.magetab.path.template']}")
    public String tsvFileTemplate;


    String accession = "E-MTAB-513";

    private ExpressionSerializerFactory expressionSerializerFactory;
    private ExpressionSerializerService subject;


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

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

        expressionSerializerFactory = new ExpressionSerializerFactory(
                new RnaSeqBaselineExpressionKryoSerializer(serializedExpressionsFileTemplate, tsvFileTemplate, new CsvReaderFactory()));

        subject = new ExpressionSerializerService(experimentTrader, expressionSerializerFactory,
                baselineExperimentsCache,experimentChecker);
    }

    @Test
    public void weCanSerializeTheFile() {
        assertEquals(true, fileExists(tsvFileTemplate, accession));
        assertEquals(false, fileExists(serializedExpressionsFileTemplate, accession));
        assertEquals(false, fileExists(serializedExpressionLevelsFileTemplate, accession));

        subject.kryoSerializeExpressionData(accession);

        assertEquals(true, fileExists(serializedExpressionsFileTemplate, accession));
        assertEquals(true, fileExists(serializedExpressionLevelsFileTemplate, accession));
    }

    @Test
    public void weCheckTheFileExistsBeforeWeAttemptToSerializeIt(){
        subject.kryoSerializeExpressionData(accession);

        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)));
    }

}

@@ -921,32 +921,80 @@
 org.
-junit.Assert.assertEqual
+hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.i
 s;%0Ai
@@ -1321,16 +1321,75 @@
 ceIT %7B%0A%0A
+    private static final String accession = %22E-MTAB-513%22;%0A%0A
     @Inj
@@ -1623,67 +1623,8 @@
 ate;
-%0A    private String serializedExpressionLevelsFileTemplate;
 %0A%0A  
@@ -1728,116 +1728,8 @@
 e;%0A%0A
-%0A    String accession = %22E-MTAB-513%22;%0A%0A    private ExpressionSerializerFactory expressionSerializerFactory;%0A
     
@@ -2022,24 +2022,25 @@
 .kryo%22;%0A
+%0A
         
 serializ
@@ -2035,94 +2035,35 @@
     
-serializedExpressionLevelsFileTemplate = tmp.toString() + %22/%7B0%7D_levels.kryo%22;%0A%0A       
+ExpressionSerializerFactory
  exp
@@ -2487,37 +2487,29 @@
       assert
-Equals(true, 
+That(
 fileExists(t
@@ -2526,32 +2526,42 @@
 late, accession)
+, is(true)
 );%0A        asser
@@ -2557,38 +2557,29 @@
       assert
-Equals(false, 
+That(
 fileExists(s
@@ -2626,99 +2626,18 @@
 ion)
-);%0A        assertEquals(false, fileExists(serializedExpressionLevelsFileTemplate, accession
+, is(false
 ));%0A
@@ -2704,37 +2704,29 @@
       assert
-Equals(true, 
+That(
 fileExists(s
@@ -2773,98 +2773,17 @@
 ion)
-);%0A        assertEquals(true, fileExists(serializedExpressionLevelsFileTemplate, accession
+, is(true
 ));%0A
@@ -2915,33 +2915,32 @@
 ata(accession);%0A
-%0A
         verify(e

Fix test for build http://bar:8085/browse/ATLAS-TESTDELIVERY-3816: remove deleted parameter (for real this time)