package uk.ac.ebi.atlas.profiles;

import au.com.bytecode.opencsv.CSVReader;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.ExperimentDTOTest;
import uk.ac.ebi.atlas.experimentimport.expressiondataserializer.ExpressionSerializerService;
import uk.ac.ebi.atlas.model.experiment.ExperimentType;
import uk.ac.ebi.atlas.model.experiment.baseline.BaselineExpression;
import uk.ac.ebi.atlas.resource.DataFileHub;

import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

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

    private static final String accession = "E-MTAB-513";
    private static final int GENE_ID_INDEX = 0;
    private static final int GENE_NAME_INDEX = 1;
    private static final int FIRST_LEVEL_INDEX = 2;

    @Inject
    private ExpressionSerializerService expressionSerializerService;

    @Inject
    private DataFileHub dataFileHub;

    private BaselineExpressionsKryoReader subject;

    @Before
    public void setUp() {
        expressionSerializerService.kryoSerializeExpressionData(ExperimentDTOTest.mockDTO(accession, ExperimentType.RNASEQ_MRNA_BASELINE));
        subject = BaselineExpressionsKryoReader.create(dataFileHub.getKryoFileForReading(accession));
    }

    @Test
    public void serializedFilesAreEqualToTsvFiles() throws IOException {
        CSVReader csvReader = new CSVReader(dataFileHub.getBaselineExperimentFiles(accession).main.getReader(), '\t');

        // Read header
        String[] tsvLine = csvReader.readNext();
        List<String> assaysFromTsv = Arrays.asList(tsvLine).subList(FIRST_LEVEL_INDEX, tsvLine.length);

        while (subject.readLine()) {
            tsvLine = csvReader.readNext();

            assertThat(subject.getGeneId(), is(tsvLine[GENE_ID_INDEX]));
            assertThat(subject.getGeneName(), is(tsvLine[GENE_NAME_INDEX]));

            List<String> expressionLevelStrings = new ArrayList<>();
            for (BaselineExpression expression : subject.getExpressions()) {
                Assert.assertThat(assaysFromTsv, hasItem(expression.getDataColumnDescriptorId()));
                expressionLevelStrings.add(expression.getLevel()+"");
            }
            assertThat(expressionLevelStrings.size(), is(assaysFromTsv.size()));

            for (String expression : Arrays.asList(tsvLine).subList(FIRST_LEVEL_INDEX, tsvLine.length)) {
                assertTrue(expressionLevelStrings.containsAll(Arrays.asList(expression.split(","))));
            }
        }
    }

}
@@ -66,24 +66,243 @@
 .CSVReader;%0A
+import com.google.common.base.Function;%0Aimport com.google.common.base.Joiner;%0Aimport com.google.common.collect.FluentIterable;%0Aimport com.google.common.collect.ImmutableList;%0Aimport org.apache.commons.lang3.ArrayUtils;%0A
 import org.j
@@ -918,16 +918,50 @@
 leHub;%0A%0A
+import javax.annotation.Nullable;%0A
 import j
@@ -1138,16 +1138,62 @@
 rtThat;%0A
+import static org.hamcrest.Matchers.contains;%0A
 import s
@@ -1271,51 +1271,8 @@
 .is;
-%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A@W
@@ -2789,31 +2789,34 @@
 String%3E 
-expressionLevel
+serializedQuartile
 Strings 
@@ -3032,34 +3032,77 @@
     
-expressionLevelStrings.add
+serializedQuartileStrings.add(Joiner.on(%22,%22).join(ArrayUtils.toObject
 (exp
@@ -3116,18 +3116,21 @@
 .get
-Level()+%22%22
+Quartiles()))
 );%0A 
@@ -3134,32 +3134,33 @@
 ;%0A            %7D%0A
+%0A
             asse
@@ -3159,299 +3159,825 @@
     
-assertThat(expressionLevelStrings.size(), is(assaysFromTsv.size()));%0A%0A            for (String expression : Arrays.asList(tsvLine).subList(FIRST_LEVEL_INDEX, tsvLine.length)) %7B%0A                assertTrue(expressionLevelStrings.containsAll(Arrays.asList(expression.split(%22,%22))));%0A            %7D
+ImmutableList.Builder%3CString%3E builder = ImmutableList.builder();%0A            for (String quartilesField : Arrays.copyOfRange(tsvLine, FIRST_LEVEL_INDEX, tsvLine.length)) %7B%0A                builder.add(FluentIterable.from(quartilesField.split(%22,%22))%0A                        .transform(new Function%3CString, String%3E() %7B%0A                            @Nullable%0A                            @Override%0A                            public String apply(String str) %7B%0A                                return Double.toString(Double.parseDouble(str));%0A                            %7D%0A                        %7D).join(Joiner.on(%22,%22))%0A                );%0A            %7D%0A%0A            assertThat(serializedQuartileStrings.size(), is(assaysFromTsv.size()));%0A            assertThat(serializedQuartileStrings, contains(builder.build().toArray()));
 %0A   

Fix test (was incorrectly comparing expression values to quartiles fields)