package org.opencb.opencga.storage.hadoop.variant.index.sample;

import org.junit.Before;
import org.junit.Test;
import org.opencb.biodata.models.variant.StudyEntry;
import org.opencb.biodata.models.variant.Variant;
import org.opencb.biodata.models.variant.VariantBuilder;
import org.opencb.biodata.models.variant.avro.VariantType;
import org.opencb.opencga.storage.core.io.bit.BitBuffer;
import org.opencb.opencga.core.config.storage.IndexFieldConfiguration;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class VariantFileIndexEntryConverterTest {

    private VariantFileIndexConverter fileIndexConverter;
    private FileIndexSchema fileIndex;

    @Before
    public void setUp() throws Exception {
        fileIndexConverter = new VariantFileIndexConverter(SampleIndexSchema.defaultSampleIndexSchema());
        fileIndex = (SampleIndexSchema.defaultSampleIndexSchema()).getFileIndex();
    }

    @Test
    public void testConvert() {
        BitBuffer bitBuffer = new BitBuffer(fileIndex.getBitsLength());

        fileIndex.getTypeIndex().write(VariantType.SNV, bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", "0").build()));

        fileIndex.getTypeIndex().write(VariantType.INDEL, bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:-").addSample("s1", "0/1", "0").build()));

        fileIndex.getTypeIndex().write(VariantType.DELETION, bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100-200:A:<DEL>").addSample("s1", "0/1", "0").build()));

        fileIndex.getTypeIndex().write(VariantType.INSERTION, bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100-200:A:<INS>").addSample("s1", "0/1", "0").build()));

        fileIndex.getTypeIndex().write(VariantType.SNV, bitBuffer);
        fileIndex.getCustomField(IndexFieldConfiguration.Source.FILE, StudyEntry.FILTER).write("PASS", bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", "0").setFilter("PASS").build()));

        fileIndex.getCustomField(IndexFieldConfiguration.Source.FILE, StudyEntry.FILTER).write(null, bitBuffer);
        fileIndex.getCustomField(IndexFieldConfiguration.Source.FILE, StudyEntry.QUAL).write("2000.0", bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", "0").setQuality(2000.0).build()));

        fileIndex.getCustomField(IndexFieldConfiguration.Source.FILE, StudyEntry.QUAL).write("10.0", bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", "0").setQuality(10.0).build()));

        bitBuffer.clear();
        fileIndex.getTypeIndex().write(VariantType.SNV, bitBuffer);
        fileIndex.getFilePositionIndex().write(3, bitBuffer);
        fileIndex.getCustomField(IndexFieldConfiguration.Source.FILE, StudyEntry.QUAL).write("10.0", bitBuffer);
        assertEquals(bitBuffer,
                fileIndexConverter.createFileIndexValue(0, 3, v("1:100:A:C").addSample("s1", "0/1", "0").setQuality(10.0).build()));

        bitBuffer.clear();
        fileIndex.getTypeIndex().write(VariantType.SNV, bitBuffer);
        for (Integer dp : Arrays.asList(1, 5, 10, 20, 50)) {
            fileIndex.getCustomField(IndexFieldConfiguration.Source.SAMPLE, "DP").write(String.valueOf(dp), bitBuffer);
            assertEquals(bitBuffer,
                    fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", dp.toString()).build()));
            assertEquals(bitBuffer,
                    fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", dp.toString()).addFileData("DP", 10000).build()));
            assertEquals(bitBuffer,
                    fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").addSample("s1", "0/1", dp.toString()).addFileData("DP", 0).build()));
//            assertEquals(bitBuffer,
//                    fileIndexConverter.createFileIndexValue(0, 0, v("1:100:A:C").setSampleDataKeys("GT").addSample("s1", "0/1").addFileData("DP", dp).build()));
        }
    }

    private VariantBuilder v(String s) {
        return Variant.newBuilder(s).setStudyId("s").setSampleDataKeys("GT", "DP").setFileId("f1");
    }

}
@@ -355,108 +355,108 @@
 cga.
-storage.core.io.bit.BitBuffer;%0Aimport org.opencb.opencga.core.config.storage.IndexFieldConfiguration
+core.config.storage.IndexFieldConfiguration;%0Aimport org.opencb.opencga.storage.core.io.bit.BitBuffer
 ;%0A%0Ai
@@ -471,22 +471,32 @@
 va.util.
-Arrays
+stream.IntStream
 ;%0A%0Aimpor
@@ -1244,33 +1244,33 @@
 e(%22s1%22, %220/1%22, %22
-0
+.
 %22).build()));%0A%0A 
@@ -1463,33 +1463,33 @@
 e(%22s1%22, %220/1%22, %22
-0
+.
 %22).build()));%0A%0A 
@@ -1693,33 +1693,33 @@
 e(%22s1%22, %220/1%22, %22
-0
+.
 %22).build()));%0A%0A 
@@ -1932,17 +1932,17 @@
 %220/1%22, %22
-0
+.
 %22).build
@@ -2260,25 +2260,25 @@
 1%22, %220/1%22, %22
-0
+.
 %22).setFilter
@@ -2651,33 +2651,33 @@
 e(%22s1%22, %220/1%22, %22
-0
+.
 %22).setQuality(20
@@ -2932,33 +2932,33 @@
 e(%22s1%22, %220/1%22, %22
-0
+.
 %22).setQuality(10
@@ -3376,17 +3376,17 @@
 %220/1%22, %22
-0
+.
 %22).setQu
@@ -3534,38 +3534,39 @@
 p : 
-Arrays.asList(1, 5, 10, 20, 50
+IntStream.range(0, 60).toArray(
 )) %7B

Fix test VariantFileIndexEntryConverterTest