/*
 * 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.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Collection;

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

import ubic.basecode.ontology.model.OntologyTerm;
import ubic.gemma.model.common.description.Characteristic;
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 {
        if ( !os.getMgedOntologyService().isOntologyLoaded() ) {
            os.getMgedOntologyService().startInitializationThread( true );
            int c = 0;
            while ( !os.getMgedOntologyService().isOntologyLoaded() ) {
                Thread.sleep( 5000 );
                log.info( "Waiting for Ontology to load" );
                if ( ++c > 20 ) {
                    fail( "Ontology load timeout" );
                }
            }
        }
        Collection<Characteristic> name = os.findExactTerm( "male",
                "http://mged.sourceforge.net/ontologies/MGEDOntology.owl#Sex", null );
        for ( Characteristic characteristic : name ) {
            log.info( characteristic );
        }
        assertTrue( name.size() > 0 );
    }

    @Test
    public void testObsolete() throws Exception {
        if ( !os.getDiseaseOntologyService().isOntologyLoaded() ) {
            os.getDiseaseOntologyService().startInitializationThread( true );
            int c = 0;

            while ( !os.getDiseaseOntologyService().isOntologyLoaded() ) {
                Thread.sleep( 1000 );
                log.info( "Waiting for DiseaseOntology to load" );
                if ( ++c > 100 ) {
                    fail( "Ontology load timeout" );
                }
            }
        }

        OntologyTerm t1 = os.getTerm( "http://purl.org/obo/owl/DOID#DOID_0050001" );
        assertNotNull( t1 );

        // Actinomadura madurae infectious disease
        assertTrue( os.isObsolete( "http://purl.org/obo/owl/DOID#DOID_0050001" ) );

        // inflammatory diarrhea, not obolete as of May 2012.
        assertNotNull( os.getTerm( "http://purl.org/obo/owl/DOID#DOID_0050132" ) );
        assertTrue( !os.isObsolete( "http://purl.org/obo/owl/DOID#DOID_0050132" ) );

    }
}

@@ -1540,77 +1540,8 @@
 n %7B%0A
-        if ( !os.getMgedOntologyService().isOntologyLoaded() ) %7B%0A    
     
@@ -1599,36 +1599,32 @@
 Thread( true );%0A
-    
         int c = 
@@ -1626,36 +1626,32 @@
  c = 0;%0A        
-    
 while ( !os.getM
@@ -1698,36 +1698,32 @@
 ) %7B%0A            
-    
 Thread.sleep( 50
@@ -1720,17 +1720,18 @@
 .sleep( 
-5
+10
 000 );%0A 
@@ -1733,36 +1733,32 @@
  );%0A            
-   
- 
 log.info( %22Waiti
@@ -1789,36 +1789,32 @@
  );%0A            
-    
 if ( ++c %3E 20 ) 
@@ -1808,17 +1808,17 @@
 ( ++c %3E 
-2
+1
 0 ) %7B%0A  
@@ -1823,36 +1823,32 @@
                 
- 
-   
 fail( %22Ontology 
@@ -1864,34 +1864,16 @@
 out%22 );%0A
-                %7D%0A
         
@@ -1880,32 +1880,33 @@
     %7D%0A        %7D%0A
+%0A
         Collecti
@@ -2259,80 +2259,8 @@
 n %7B%0A
-        if ( !os.getDiseaseOntologyService().isOntologyLoaded() ) %7B%0A    
     
@@ -2333,28 +2333,24 @@
  );%0A        
- 
-   
 int c = 0;%0A%0A
@@ -2349,20 +2349,16 @@
 c = 0;%0A%0A
-    
         
@@ -2424,36 +2424,32 @@
 ) %7B%0A            
-    
 Thread.sleep( 10
@@ -2450,16 +2450,17 @@
 ep( 1000
+0
  );%0A    
@@ -2459,36 +2459,32 @@
  );%0A            
-  
-  
 log.info( %22Waiti
@@ -2522,36 +2522,32 @@
  );%0A            
-    
 if ( ++c %3E 100 )
@@ -2543,17 +2543,16 @@
 ++c %3E 10
-0
  ) %7B%0A   
@@ -2556,36 +2556,32 @@
                 
- 
-   
 fail( %22Ontology 
@@ -2601,26 +2601,8 @@
  );%0A
-                %7D%0A
     

improve ontology loader so it retries.