/*
 * The Gemma project
 * 
 * Copyright (c) 2007 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.ontology;

import static org.junit.Assert.assertEquals;

import java.util.Collection;

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

import ubic.basecode.ontology.model.OntologyTerm;
import ubic.basecode.ontology.providers.AbstractOntologyService;
import ubic.gemma.model.common.description.Characteristic;
import ubic.gemma.ontology.OntologyService;
import ubic.gemma.testing.BaseSpringContextTest;

/**
 * @author paul
 * @version $Id$
 */
public class OntologyServiceTest extends BaseSpringContextTest {

    @Autowired
    OntologyService os;

    /**
     * This test can fail if the db isn't initialized public void testListAvailableOntologies() throws Exception {
     * Collection<Ontology> name = OntologyService.listAvailableOntologies(); assertTrue( name.size() > 0 ); }
     */
    @Test
    public final void testFindExactMatch() throws Exception {
        loadOntology( "mgedOntologyService" );
        Collection<Characteristic> name = os.findExactTerm( "male",
                "http://mged.sourceforge.net/ontologies/MGEDOntology.owl#Sex" );
        for ( Characteristic characteristic : name ) {
            log.info( characteristic );
        }
        assertEquals( 1, name.size() );
    }

    /**
     * Test for problem with birnlex, which returns 'organ' as a _subclass_ of 'liver'.
     * 
     * @throws Exception
     */
    @Test
    public final void testFindTerm() throws Exception {
        loadOntology( "birnLexOntologyService" );
        Collection<OntologyTerm> terms = os.findTerms( "liver" );
        // assertTrue( terms.size() > 0 );
        //
        // for ( OntologyTerm term : terms ) {
        // if ( term.getLabel().contains( "Organ" ) ) {
        // assertTrue( false );
        // }
        // }
    }

    private void loadOntology( String ontology ) throws Exception {
        AbstractOntologyService os = ( AbstractOntologyService ) this.getBean( ontology );
        if ( !os.isOntologyLoaded() ) os.init( true ); // force load.
        while ( !os.isOntologyLoaded() ) {
            Thread.sleep( 1000 );
            log.info( "Waiting for Ontology to load" );
        }
    }

}

@@ -716,16 +716,96 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;
 %0A%0Aimport
@@ -969,73 +969,8 @@
 rm;%0A
-import ubic.basecode.ontology.providers.AbstractOntologyService;%0A
 impo
@@ -1591,46 +1591,217 @@
     
-loadOntology( %22mgedOntologyService%22 );
+os.getMgedOntologyService().init( true );%0A        while ( !os.getMgedOntologyService().isOntologyLoaded() ) %7B%0A            Thread.sleep( 1000 );%0A            log.info( %22Waiting for Ontology to load%22 );%0A        %7D
 %0A   
@@ -2313,50 +2313,224 @@
     
-loadOntology( %22birnLexOntologyService
+os.getBirnLexOntologyService().init( true );%0A        while ( !os.getBirnLexOntologyService().isOntologyLoaded() ) %7B%0A            Thread.sleep( 5000 );%0A            log.info( %22Waiting for Ontology to load
 %22 );%0A
+        %7D%0A
     
@@ -2590,35 +2590,32 @@
 iver%22 );%0A       
- //
  assertTrue( ter
@@ -2649,19 +2649,16 @@
 %0A       
- //
  for ( O
@@ -2694,18 +2694,19 @@
         
-//
+   
  if ( te
@@ -2755,426 +2755,77 @@
     
-// assertTrue( false );%0A        // %7D%0A        // %7D%0A    %7D%0A%0A    private void loadOntology( String ontology ) throws Exception %7B%0A        AbstractOntologyService os = ( AbstractOntologyService ) this.getBean( ontology );%0A        if ( !os.isOntologyLoaded() ) os.init( true ); // force load.%0A        while ( !os.isOntologyLoaded() ) %7B%0A            Thread.sleep( 1000 );%0A            log.info( %22Waiting for Ontology to load%22 );
+        fail( %22Should get organ as a child of liver%22 );%0A            %7D
 %0A   

fix tests