/*
 * The Gemma project
 * 
 * Copyright (c) 2012 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ubic.gemma.model.genome.gene;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import ubic.gemma.genome.gene.GeneSetValueObject;
import ubic.gemma.genome.gene.GeneSetValueObjectHelper;
import ubic.gemma.genome.gene.service.GeneSetService;
import ubic.gemma.model.genome.Gene;
import ubic.gemma.model.genome.Taxon;
import ubic.gemma.security.authentication.UserManager;
import ubic.gemma.testing.BaseSpringContextTest;


/**
 * TODO Document Me
 * 
 * @author tvrossum
 * @version $Id$
 */
public class GeneSetValueObjectHelperTest extends BaseSpringContextTest {

    private Gene g1 = null;
    private Taxon tax1;
    
    @Autowired
    GeneSetService geneSetService;
    
    @Autowired
    UserManager userManager;

    @Autowired
    GeneSetValueObjectHelper geneSetValueObjectHelper;
    
    @Before
    public void setUp() throws Exception {

        g1 = this.getTestPeristentGene();
        tax1 = this.getTaxon( "human" );
        g1.setTaxon( tax1 );

    }
    
    @Test
    public void testConvertToValueObject() {

        GeneSetMember gmember = GeneSetMember.Factory.newInstance();
        gmember.setGene( g1 );
        gmember.setScore( 0.22 );

        GeneSet gset = GeneSet.Factory.newInstance();
        gset.setName( "CreateTest" );
        gset.getMembers().add( gmember );

        gset = geneSetService.create( gset );
        

        Long id = gset.getId();
        assertNotNull( id );
        
        GeneSetValueObject gsvo = geneSetValueObjectHelper.convertToValueObject( gset );
                
        assertEquals( gset.getId(), gsvo.getId());
        assertEquals( gset.getMembers().size(), gsvo.getSize().intValue() );
        assertEquals( gset.getName(), gsvo.getName() );
        // the method for setting the taxon Id uses a db call with the geneSet's id 
        // assertEquals( gmember.getGene().getTaxon().getId(), gsvo.getTaxonId() );
        

   /*     geneSetService.remove( gset );

        assertNull( geneSetService.load( id ) );*/

    }
    
    @Test
    public void testConvertToLightValueObject() {

        GeneSetMember gmember = GeneSetMember.Factory.newInstance();
        gmember.setGene( g1 );
        gmember.setScore( 0.22 );

        GeneSet gset = GeneSet.Factory.newInstance();
        gset.setName( "CreateTest" );
        gset.getMembers().add( gmember );

        gset = geneSetService.create( gset );

        Long id = gset.getId();
        assertNotNull( id );
        
        GeneSetValueObject gsvo = geneSetValueObjectHelper.convertToLightValueObject( gset );
        
        assertNull( gsvo.getGeneIds() );
                
        assertEquals( gset.getId(), gsvo.getId());
        assertEquals( gset.getMembers().size(), gsvo.getSize().intValue() );
        assertEquals( gset.getName(), gsvo.getName() );

        // the method for setting the taxon Id uses a db call with the geneSet's id 
        // assertEquals( gmember.getGene().getTaxon().getId(), gsvo.getTaxonId() );
        
        /*
        geneSetService.remove( gset );
        assertNull( geneSetService.load( id ) );*/

    }
}

@@ -1464,18 +1464,58 @@
 xon tax1
+ = null;%0A    private GeneSet gset = null
 ;%0A
-
     %0A   
@@ -1859,75 +1859,8 @@
 );%0A%0A
-    %7D%0A    %0A    @Test%0A    public void testConvertToValueObject() %7B%0A%0A
     
@@ -1994,32 +1994,24 @@
 );%0A%0A        
-GeneSet 
 gset = GeneS
@@ -2163,28 +2163,85 @@
 gset );%0A    
+%7D%0A
     
+%0A    @Test%0A    public void testConvertToValueObject() %7B
 %0A%0A        Lo
@@ -2876,32 +2876,32 @@
 %0A    %0A    @Test%0A
+
     public void 
@@ -2939,325 +2939,8 @@
  %7B%0A%0A
-        GeneSetMember gmember = GeneSetMember.Factory.newInstance();%0A        gmember.setGene( g1 );%0A        gmember.setScore( 0.22 );%0A%0A        GeneSet gset = GeneSet.Factory.newInstance();%0A        gset.setName( %22CreateTest%22 );%0A        gset.getMembers().add( gmember );%0A%0A        gset = geneSetService.create( gset );%0A%0A
     

Test refactoring