/*
 * The aspiredb project
 * 
 * Copyright (c) 2013 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 ubc.pavlab.aspiredb.server.dao;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

import java.util.Collection;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ubc.pavlab.aspiredb.server.BaseSpringContextTest;
import ubc.pavlab.aspiredb.server.model.Label;
import ubc.pavlab.aspiredb.server.model.Project;
import ubc.pavlab.aspiredb.server.model.Subject;
import ubc.pavlab.aspiredb.server.model.Variant;
import ubc.pavlab.aspiredb.server.util.PersistentTestObjectHelper;

@RunWith(SpringJUnit4ClassRunner.class)
public class LabelDaoTest extends BaseSpringContextTest {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private ProjectDao projectDao;
    
    @Autowired
    private SubjectDao subjectDao;

    @Autowired
    private VariantDao variantDao;

    @Autowired
    PersistentTestObjectHelper testObjectHelper;

    @Autowired
    HibernateTransactionManager txManager;

    String testSubjectId;
    Subject testSubject;
    Variant testVariant;
    Project testProject;
    
    @Before
    public void init() {
        testSubjectId = RandomStringUtils.randomAlphanumeric( 5 );
        testSubject = testObjectHelper.createPersistentTestSubjectObjectWithCNV(testSubjectId);
        testVariant = testSubject.getVariants().iterator().next();
        
        testProject = new Project();
        testProject.setName(RandomStringUtils.randomAlphabetic( 4 ));
        testProject.getSubjects().add(testSubject);

        testProject = testObjectHelper.createPersistentProject(testProject);

    }

    @Test
    public void addSubjectLabel() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testSubject.addLabel( label );
        subjectDao.update( testSubject );

        new InlineTransaction() {
            @Override
            public void instructions() {
                Subject s = subjectDao.findByPatientId( testSubjectId );
                assertTrue(s.getLabels().size() > 0);
            }
        }.execute();
    }

    @Test
    public void addVariantLabel() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testVariant.addLabel( label );
        variantDao.update( testVariant );

        new InlineTransaction() {
            @Override
            public void instructions() {
                Subject s = subjectDao.findByPatientId( testSubjectId );
                Variant v = s.getVariants().iterator().next();
                assertTrue(v.getLabels().size() > 0);
            }
        }.execute();
     }

    @Test
    public void labelSuggestion() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testSubject.addLabel( label );
        subjectDao.update( testSubject );

        Collection<Label> labels = labelDao.getLabelsMatching( name.substring(0, 3) );
        Label l = labels.iterator().next();
        assertEquals( name, l.getName() );
    }
    
    @Test
    public void labelSuggestionByContext() {
        String name = RandomStringUtils.randomAlphabetic( 4 );
        Label label = new Label( name, "FF0000" );
        labelDao.create( label );
        testSubject.addLabel( label );
        subjectDao.update( testSubject );

        Collection<Label> labels = labelDao.getSubjectLabelsByProjectId( 0L );
        assertEquals(0, labels.size());
        
        labels = labelDao.getSubjectLabelsByProjectId( testProject.getId() );
        assertNotNull(labels);
        assertEquals(1, labels.size());
        assertEquals( name, labels.iterator().next().getName() );
    }
}

@@ -2447,51 +2447,8 @@
     
-testProject.getSubjects().add(testSubject);
 %0A%0A  
@@ -2526,62 +2526,269 @@
 t);%0A
-%0A    %7D%0A%0A    @Test%0A    public void addSubjectLabel() %7B%0A
+        %0A        testObjectHelper.addSubjectToProject( testSubject, testProject );%0A        %0A%0A    %7D%0A%0A    @Test%0A    public void addSubjectLabel() %7B%0A        %0A%0A        new InlineTransaction() %7B%0A            @Override%0A            public void instructions() %7B%0A        
     
@@ -2846,32 +2846,40 @@
 c( 4 );%0A        
+   
+     
 Label label = ne
@@ -2897,32 +2897,40 @@
 me, %22FF0000%22 );%0A
+        
         labelDao
@@ -2947,32 +2947,40 @@
 abel );%0A        
+      
+  
 testSubject.addL
@@ -2986,32 +2986,40 @@
 Label( label );%0A
+        
         subjectD
@@ -3040,33 +3040,32 @@
 tSubject );%0A
-%0A
         
 new InlineTr
@@ -3056,96 +3056,33 @@
     
-new InlineTransaction() %7B%0A            @Override%0A            public void instructions() %7B
+        %0A                
 %0A   

fix test