package org.intermine.bio.dataconversion;

/*
 * Copyright (C) 2002-2010 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.intermine.bio.dataconversion.GFF3RecordHandler;
import org.intermine.metadata.Model;
import org.intermine.xml.full.Item;
import org.intermine.xml.full.ItemFactory;
import org.intermine.xml.full.ReferenceList;

import junit.framework.TestCase;


public class GFF3RecordHandlerTest extends TestCase
{
    private Model tgtModel;
    private ItemFactory itemFactory;

    public void setUp() throws Exception {
        tgtModel = Model.getInstanceByName("genomic");
        itemFactory = new ItemFactory(tgtModel);
    }

    public void testSetReference() throws Exception {
        GFF3RecordHandler handler = new GFF3RecordHandler(tgtModel);

        Item threePrimeUTR = itemFactory.makeItem(null, "ThreePrimeUTR", "");
        handler.setFeature(threePrimeUTR);

        Item gene = itemFactory.makeItem(null, "Gene", "");

        Item relation = itemFactory.makeItem(null, "SimpleRelation", "");
        relation.setReference("subject", threePrimeUTR.getIdentifier());
        relation.setReference("object", gene.getIdentifier());
        handler.addParentRelation(relation);

        Item expected = itemFactory.makeItem(threePrimeUTR.getIdentifier(), "ThreePrimeUTR", "");
        expected.setReference("gene", gene.getIdentifier());

        Map refs = new HashMap();
        refs.put("ThreePrimeUTR", "gene");

        handler.setReferences(refs);
        assertEquals(expected, handler.getFeature());
    }

    public void testSetCollection() throws Exception {
        GFF3RecordHandler handler = new GFF3RecordHandler(tgtModel);

        Item exon = itemFactory.makeItem(null, "Exon", "");
        handler.setFeature(exon);

        Item transcript1 = itemFactory.makeItem(null, "Transcript", "");
        Item transcript2 = itemFactory.makeItem(null, "Transcript", "");

        Item relation1 = itemFactory.makeItem(null, "SimpleRelation", "");
        relation1.setReference("subject", exon.getIdentifier());
        relation1.setReference("object", transcript1.getIdentifier());
        handler.addParentRelation(relation1);
        Item relation2 = itemFactory.makeItem(null, "SimpleRelation", "");
        relation2.setReference("subject", exon.getIdentifier());
        relation2.setReference("object", transcript2.getIdentifier());
        handler.addParentRelation(relation2);

        Item expected = itemFactory.makeItem(exon.getIdentifier(), "Exon", "");
        ReferenceList transcripts = new ReferenceList("transcripts", Arrays.asList(new String[] {transcript1.getIdentifier(),
                                                                                                 transcript2.getIdentifier()}));
        expected.addCollection(transcripts);

        Map refs = new HashMap();
        refs.put("Exon", "transcripts");

        handler.setReferences(refs);
        assertEquals(expected, handler.getFeature());
    }


}

@@ -1237,32 +1237,34 @@
 , %22Gene%22, %22%22);%0A%0A
+//
         Item rel
@@ -1313,32 +1313,34 @@
 Relation%22, %22%22);%0A
+//
         relation
@@ -1388,32 +1388,34 @@
 tIdentifier());%0A
+//
         relation
@@ -1486,33 +1486,37 @@
 ddParent
-Relation(relation
+(gene.getIdentifier()
 );%0A%0A    
@@ -2210,24 +2210,26 @@
 ipt%22, %22%22);%0A%0A
+//
         Item
@@ -2283,32 +2283,34 @@
 Relation%22, %22%22);%0A
+//
         relation
@@ -2350,32 +2350,34 @@
 tIdentifier());%0A
+//
         relation
@@ -2460,29 +2460,41 @@
 rent
-Relation(relation1
+(transcript1.getIdentifier()
 );%0A
+//
     
@@ -2556,32 +2556,34 @@
 Relation%22, %22%22);%0A
+//
         relation
@@ -2623,32 +2623,34 @@
 tIdentifier());%0A
+//
         relation
@@ -2733,26 +2733,36 @@
 rent
-Relation(relation2
+(transcript2.getIdentifier()
 );%0A%0A

test fix