package org.cbioportal.genome_nexus.web;

import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
    properties = {
        "spring.data.mongodb.uri=mongodb://localhost/integration",
        "server.port=38892"
    }
)
public class MyVariantInfoIntegrationTest
{
    private final static String BASE_URL = "http://localhost:38892/my_variant_info/variant/";

    private RestTemplate restTemplate = new RestTemplate();

    private Map<String, HashMap> fetchMyVariantInfoAnnotationGET(String variant)
    {
        String response = this.restTemplate.getForObject(BASE_URL + variant, String.class);
        // parse response string
        JsonParser springParser = JsonParserFactory.getJsonParser();
        // cast to Map<String, HashMap>
        Map<String, HashMap> map = (Map<String, HashMap>)(Map) springParser.parseMap(response);
        return map;

    }
    private List<Map<String, HashMap>> fetchMyVariantInfoAnnotationPOST(String[] variants)
    {
        String responses = this.restTemplate.postForObject(BASE_URL, variants, String.class);
        // parse response string
        JsonParser springParser = JsonParserFactory.getJsonParser();
        // cast to List<Map<String, HashMap>>
        List<Map<String, HashMap>> lists = (List<Map<String, HashMap>>)(List<?>) springParser.parseList(responses);
        return lists;
    }


    @Test
    public void testMyVariantInfoAnnotation()
    {
        String[] variants = {
            "7:g.140453136A>T",
            "rs12190874",
            "17:g.41276045_41276046delCT",
            "INVALID"
        };

        //////////////////
        // GET requests //
        //////////////////

        String chrom = this.fetchMyVariantInfoAnnotationGET(variants[0]).get("mutdb").get("chrom").toString();
        // the chrom shout be 7
        assertEquals("7", chrom);

        Object alleleNumber = ((HashMap) this.fetchMyVariantInfoAnnotationGET(variants[0]).get("gnomadExome").get("alleleNumber")).get("an");
        // the allele number should be 246028
        assertEquals(246028, alleleNumber);

        Integer variantId = (Integer)this.fetchMyVariantInfoAnnotationGET(variants[0]).get("clinVar").get("variantId");
        // test clinVar, the variantId should be 13961
        assertEquals((Integer)13961, variantId);

        String alt = this.fetchMyVariantInfoAnnotationGET(variants[1]).get("vcf").get("alt").toString();
        // the alt should be A
        assertEquals("A", alt);

        Object alleleCount = ((HashMap) this.fetchMyVariantInfoAnnotationGET(variants[1]).get("gnomadGenome").get("alleleCount")).get("ac");
        // the allele count should be 3239
        assertEquals(3239, alleleCount);

        String vartype = this.fetchMyVariantInfoAnnotationGET(variants[2]).get("dbsnp").get("vartype").toString();
        // the vartype shout be indel
        assertEquals("indel", vartype);


        //////////////////
        // POST request //
        //////////////////

        List<Map<String, HashMap>> postResponses = this.fetchMyVariantInfoAnnotationPOST(variants);

        // for each pdbId we should have one matching PdbHeader instance, except the invalid one
        assertEquals(postResponses.size(), variants.length - 1);

        String chrom0 = postResponses.get(0).get("mutdb").get("chrom").toString();
        // the Get and Post should have same result
        assertEquals(chrom, chrom0);

        Object alleleNumber0 = ((HashMap) postResponses.get(0).get("gnomadExome").get("alleleNumber")).get("an");
        assertEquals(alleleNumber, alleleNumber0);

        Integer variantId0 = (Integer)postResponses.get(0).get("clinVar").get("variantId");
        assertEquals((Integer)variantId, variantId0);

        String alt1 = postResponses.get(1).get("vcf").get("alt").toString();
        assertEquals(alt, alt1);

        Object alleleCount1 = ((HashMap) postResponses.get(1).get("gnomadGenome").get("alleleCount")).get("ac");
        assertEquals(alleleCount, alleleCount1);

        String vartype2 = postResponses.get(2).get("dbsnp").get("vartype").toString();
        assertEquals(vartype, vartype2);

    }
}

@@ -2569,21 +2569,21 @@
 uld be 2
-46028
+51260
 %0A       
@@ -2601,13 +2601,13 @@
 ls(2
-46028
+51260
 , al
@@ -3195,18 +3195,18 @@
 ld be 32
-39
+82
 %0A       
@@ -3221,18 +3221,18 @@
 quals(32
-39
+82
 , allele
@@ -3255,23 +3255,19 @@
  String 
-vartype
+ref
  = this.
@@ -3312,37 +3312,35 @@
 ts%5B2%5D).get(%22
-dbsnp
+vcf
 %22).get(%22
 vartype%22).to
@@ -3319,39 +3319,35 @@
 get(%22vcf%22).get(%22
-vartype
+ref
 %22).toString();%0A 
@@ -3360,23 +3360,19 @@
  // the 
-vartype
+ref
  shout b
@@ -3373,21 +3373,19 @@
 hout be 
-indel
+ACF
 %0A       
@@ -3403,23 +3403,17 @@
 ls(%22
-indel%22, vartype
+ACT%22, ref
 );%0A%0A
@@ -4534,23 +4534,19 @@
  String 
-vartype
+ref
 2 = post
@@ -4571,21 +4571,19 @@
 et(%22
-dbsnp
+vcf
 %22).get(%22
 vart
@@ -4578,23 +4578,19 @@
 %22).get(%22
-vartype
+ref
 %22).toStr
@@ -4621,24 +4621,16 @@
 als(
-vartype, vartype
+ref, ref
 2);%0A

fix myvariantinfo integration test