package io.github.programminglife2016.pl1_2016.server.parser;

import io.github.programminglife2016.pl1_2016.parser.Segment;
import io.github.programminglife2016.pl1_2016.parser.SegmentMap;
import io.github.programminglife2016.pl1_2016.parser.SimpleParser;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
//CHECKSTYLE.OFF: MagicNumber

/**
 * Tests for the BasicServer class
 */
public class SimpleParserTest {
    private SimpleParser parser;

    /**
     * Create parser object.
     */
    @Before
    public void setUp() {
        parser = new SimpleParser();
    }

    /**
     * Test parsing a segment line to produce the correct segment.
     */
    @Test
    public void testSegmentLine() {
        parser.parse(stringToInputStream("S	1	TTGA	*	ORI:Z:MT_H37RV_BRD_V5.ref.fasta;"));
        SegmentMap segmentMap = parser.getSegmentMap();
        Segment segment = segmentMap.get(1);
        assertEquals(1, segment.getId());
        assertEquals("TTGA", segment.getData());
    }

    /**
     * Test parsing a link line to produce the correct links.
     */
    @Test
    public void testLinkLine() {
        final String gfaFile = "H	VN:Z:1.0\n"
                               + "S 1 AGAT\n"
                               + "S 2 TTGC\n"
                               + "S 3 ATGC\n"
                               + "L	1	+	2	+	0M";
        parser.parse(stringToInputStream(gfaFile));
        SegmentMap segmentMap = parser.getSegmentMap();
        Segment segment1 = segmentMap.get(1);
        Segment segment3 = segmentMap.get(3);
        assertEquals(2, segment1.getLinks().get(0).getId());
        assertTrue(segment3.getLinks().isEmpty());
    }

    @Test
    public void testParseZIndex() {
        final String gfaFile = "H	VN:Z:1.0\n" +
                "S 1 AGAT\n" +
                "S 2 TTGC\n" +
                "S	3	C	*	ORI:Z:TKK_02_0008.fasta	CRD:Z:TKK_02_0008.fasta	CRDCTG:Z:NZ_KK327777.1	CTG:Z:NZ_KK327777.1	START:Z:1451\n" +
                "L	1	+	2	+	0M";
        parser.parse(stringToInputStream(gfaFile));
        SegmentMap segmentMap = parser.getSegmentMap();
        Segment segment3 = segmentMap.get(3);
        assertEquals(1451, segment3.getColumn());
    }

    /**
     * Converts a String to an InputStream
     * @param s String
     * @return InputStream of that String.
     */
    public InputStream stringToInputStream(String s) {
        return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
    }
}

@@ -1849,24 +1849,101 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Test if the z-index of a segment is parsed correctly.%0A     */%0A
     @Test%0A  
@@ -2021,18 +2021,16 @@
 Z:1.0%5Cn%22
- +
 %0A       
@@ -2033,24 +2033,26 @@
             
+ +
  %22S 1 AGAT%5Cn
@@ -2052,19 +2052,18 @@
  AGAT%5Cn%22
- +
 %0A
+ 
         
@@ -2069,16 +2069,17 @@
         
++
  %22S 2 TT
@@ -2083,19 +2083,18 @@
  TTGC%5Cn%22
- +
 %0A
+ 
         
@@ -2100,16 +2100,17 @@
         
++
  %22S%093%09C%09
@@ -2165,32 +2165,53 @@
 a%09CRDCTG:Z:NZ_KK
+%22%0A                + %22
 327777.1%09CTG:Z:N
@@ -2238,18 +2238,16 @@
 :1451%5Cn%22
- +
 %0A       
@@ -2254,16 +2254,18 @@
         
+ +
  %22L%091%09+%09

Fix checkstyle