/*
 * The Gemma project
 * 
 * Copyright (c) 2009 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.image.aba;

import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import ubic.gemma.testing.BaseSpringContextTest;

/**
 * Alan brain Atlas service test.
 * 
 * @version $Id$
 * @author kelsey
 */
public class AllenBrainAtlasServiceTest extends BaseSpringContextTest {

    @Autowired
    private AllenBrainAtlasService abaService = null;

    private static Log log = LogFactory.getLog( AllenBrainAtlasServiceTest.class.getName() );

    /**
     * @throws Exception
     */
    @SuppressWarnings("null")
    @Test
    public void testGetGene() throws Exception {
        AbaGene grin1 = null;

        try {
            grin1 = abaService.getGene( "Grin1" );
        } catch ( IOException e ) {
            if ( e.getMessage().contains( "502" ) || e.getMessage().contains( "503" ) ) {
                log.warn( "Server error from Allen Atlas: skipping test" );
                return;
            }
        }

        assertNotNull( grin1 );

        Collection<ImageSeries> representativeSaggitalImages = new HashSet<ImageSeries>();

        for ( ImageSeries is : grin1.getImageSeries() ) {
            if ( is.getPlane().equalsIgnoreCase( "sagittal" ) ) {

                Collection<Image> images = abaService.getImageseries( is.getImageSeriesId() );
                Collection<Image> representativeImages = new HashSet<Image>();

                for ( Image img : images ) {
                    if ( ( 2600 > img.getPosition() ) && ( img.getPosition() > 2200 ) ) {
                        representativeImages.add( img );
                    }
                }

                if ( representativeImages.isEmpty() ) continue;

                // Only add if there is something to add
                is.setImages( representativeImages );
                representativeSaggitalImages.add( is );
            }
        }
        grin1.setImageSeries( representativeSaggitalImages );

        // log.info( grin1 );
    }

}

@@ -1341,24 +1341,970 @@
 tName() );%0A%0A
+    /**%0A     * Not all ABA genes have the only the first letter capatalized%0A     * %0A     * @throws Exception%0A     */%0A    @SuppressWarnings(%22null%22)%0A    @Test%0A    public void testGetGeneCapitals() throws Exception %7B%0A        AbaGene gene = null;%0A%0A        try %7B%0A            gene = abaService.getGene( %22BC004044%22 );%0A        %7D catch ( IOException e ) %7B%0A            if ( e.getMessage().contains( %22502%22 ) %7C%7C e.getMessage().contains( %22503%22 ) ) %7B%0A                log.warn( %22Server error from Allen Atlas: skipping test%22 );%0A                return;%0A            %7D%0A        %7D%0A        assertNotNull( gene );%0A%0A        try %7B%0A            gene = abaService.getGene( %22grin1%22 );%0A        %7D catch ( IOException e ) %7B%0A            if ( e.getMessage().contains( %22502%22 ) %7C%7C e.getMessage().contains( %22503%22 ) ) %7B%0A                log.warn( %22Server error from Allen Atlas: skipping test%22 );%0A                return;%0A            %7D%0A        %7D%0A%0A        assertNotNull( gene );%0A    %7D%0A%0A
     /**%0A    

Test for all upper case genes