/*
 * The Gemma project
 * 
 * Copyright (c) 2006 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.expression.experiment;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;

import java.util.Collection;
import java.util.HashSet;

import junit.framework.TestCase;
import ubic.gemma.model.common.auditAndSecurity.Person;
import ubic.gemma.model.expression.arrayDesign.ArrayDesign;
import ubic.gemma.model.expression.bioAssay.BioAssay;

/**
 * @author daq2101
 * @author paul
 * @version $Id$
 */
public class ExpressionExperimentServiceImplTest extends TestCase {
    Collection<ExpressionExperiment> c;
    Collection<ExpressionExperiment> cJustTwelve;
    private ExpressionExperiment ee = null;
    private Person nobody = null;
    private Person admin = null;
    private ExpressionExperimentServiceImpl svc = null;

    private ExpressionExperimentDao eeDao;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        svc = new ExpressionExperimentServiceImpl();

        eeDao = createMock( ExpressionExperimentDao.class );
        svc.setExpressionExperimentDao( eeDao );

        nobody = Person.Factory.newInstance();
        admin = Person.Factory.newInstance();

        ee = ExpressionExperiment.Factory.newInstance();
        ee.setDescription( "From test" );
        ee.setName( "Test experiment" );
        ee.setOwner( nobody );

        ArrayDesign ad1 = ArrayDesign.Factory.newInstance();
        ArrayDesign ad2 = ArrayDesign.Factory.newInstance();

        for ( int i = 0; i < 10; i++ ) {
            BioAssay ba = BioAssay.Factory.newInstance();

            if ( i % 2 == 0 ) {
              ba.setArrayDesignUsed( ad1 );
            } else {
              ba.setArrayDesignUsed( ad2 );
            }
            ee.getBioAssays().add( ba );
        }

        ee.getInvestigators().add( admin );

        c = new HashSet<ExpressionExperiment>();
        ExpressionExperiment numberTwelve = ExpressionExperiment.Factory.newInstance();
        numberTwelve.setId( new Long( 12 ) );

        c.add( numberTwelve );
        c.add( ExpressionExperiment.Factory.newInstance() );
        c.add( ExpressionExperiment.Factory.newInstance() );

        cJustTwelve = new HashSet<ExpressionExperiment>();
        cJustTwelve.add( numberTwelve );

    }

    @SuppressWarnings("unchecked")
    public void testGetArrayDesignsUsed() throws Exception {
        assert ee != null;
        Collection<ArrayDesign> ads = svc.getArrayDesignsUsed( ee );
        assertTrue( ads.size() == 2 );
    }

    public void testExpressionExperimentFindByInvestigator() {
        reset( eeDao );
        eeDao.findByInvestigator( nobody );
        expectLastCall().andReturn( cJustTwelve );
        replay( eeDao );
        svc.findByInvestigator( nobody );
        verify( eeDao );
    }

    public void testExpressionExperimentFindAll() {
        reset( eeDao );
        eeDao.loadAll();
        expectLastCall().andReturn( c );
        replay( eeDao );
        svc.loadAll();
        verify( eeDao );
    }
    
 

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        ee = null;
    }
}
@@ -2181,32 +2181,67 @@
 .newInstance();%0A
+        ad1.setShortName( %22foo%22 );%0A
         ArrayDes
@@ -2276,32 +2276,67 @@
 y.newInstance();
+%0A        ad2.setShortName( %22bar%22 );
 %0A%0A        for ( 
@@ -2339,11 +2339,12 @@
 r ( 
-int
+long
  i =
@@ -2413,32 +2413,62 @@
 .newInstance();%0A
+            ba.setId( i + 1 );
 %0A            if 
@@ -2490,32 +2490,34 @@
 %7B%0A              
+  
 ba.setArrayDesig
@@ -2547,24 +2547,26 @@
    %7D else %7B%0A
+  
             
@@ -3273,24 +3273,98 @@
 ee != null;%0A
+        // this is a method implemented by the service using just a thaw.%0A
         Coll
@@ -3438,13 +3438,18 @@
 sert
-True(
+Equals( 2,
  ads
@@ -3459,13 +3459,8 @@
 ze()
- == 2
  );%0A
@@ -3971,15 +3971,8 @@
    %7D
-%0A    %0A 
 %0A%0A  

fix broken test