package gov.nih.nci.cabig.caaers.web.study;

import static org.easymock.EasyMock.expect;
import gov.nih.nci.cabig.caaers.dao.OrganizationDao;
import gov.nih.nci.cabig.caaers.dao.StudyDao;
import gov.nih.nci.cabig.caaers.dao.AgentDao;
import gov.nih.nci.cabig.caaers.dao.CtcDao;
import gov.nih.nci.cabig.caaers.dao.ResearchStaffDao;
import gov.nih.nci.cabig.caaers.dao.SiteInvestigatorDao;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.Ctc;
import gov.nih.nci.cabig.caaers.domain.Study;
import gov.nih.nci.cabig.caaers.utils.ConfigProperty;
import gov.nih.nci.cabig.caaers.web.WebTestCase;
import gov.nih.nci.cabig.ctms.web.tabs.StaticTabConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.Errors;
import org.springframework.validation.BindingResult;
import org.easymock.classextension.EasyMock;
import static org.easymock.classextension.EasyMock.*;

/**
 * @author Kulasekaran
 * @author Rhett Sutphin
 */
public class CreateStudyControllerTest extends WebTestCase {
    private CreateStudyController controller;
    private StudyDao studyDao;
    private OrganizationDao organizationDao;
    private AgentDao agentDao;
    private ResearchStaffDao researchStaffDao;
    private SiteInvestigatorDao siteInvestigatorDao;
    private CtcDao ctcDao;
    private ConfigProperty configProperty;
    

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        studyDao = registerDaoMockFor(StudyDao.class);
        agentDao = registerDaoMockFor(AgentDao.class);
        ctcDao = registerDaoMockFor(CtcDao.class);
        researchStaffDao = registerDaoMockFor(ResearchStaffDao.class);
        siteInvestigatorDao = registerDaoMockFor(SiteInvestigatorDao.class);
        organizationDao = new OrganizationDao() {
            @Override
            public List<Organization> getAll() {
                return new ArrayList<Organization>();
            }
        };
        configProperty = registerMockFor(ConfigProperty.class);
        expect(configProperty.getMap()).andReturn(Collections.emptyMap()).anyTimes();

        StaticTabConfigurer tabConfigurer = new StaticTabConfigurer(
            ctcDao, organizationDao, studyDao, agentDao, researchStaffDao, siteInvestigatorDao);
        tabConfigurer.addBean("configurationProperty", configProperty);

        controller = new CreateStudyController();
        controller.setStudyDao(studyDao);
        controller.setOrganizationDao(organizationDao);
        controller.setAgentDao(agentDao);
        controller.setResearchStaffDao(researchStaffDao);
        controller.setSiteInvestigatorDao(siteInvestigatorDao);
        controller.setCtcDao(ctcDao);
        controller.setTabConfigurer(tabConfigurer);
    }
    
    
    //TODO: fix this 
    public void testViewOnGet() throws Exception {
        request.setMethod("GET");
        //replayMocks();
        //ModelAndView mv = controller.handleRequest(request, response);
        //verifyMocks();
        //assertEquals("study/study_details", mv.getViewName());
        
    }

    /* I think this test is failing b/c the command isn't in the session.
       No time to fix right now.  RMS20070502 
    public void testDetailsTabSubmit() throws Exception {
        request.setMethod("POST");
        request.addParameter("multiInstitutionIndicator", "true");
        request.addParameter("shortTitle", "Scott");
        request.addParameter("longTitle", "Male");
        request.addParameter("description", "Description");
        request.addParameter("primarySponsorCode", "Primary Sponsor Code");
        request.addParameter("phaseCode", "PhaseCode");
        //request.addParameter("reviewDate", "2006-01-01");
        request.setParameter("_page", "0");
        request.setParameter("_target1", "");

        replayMocks();
        ModelAndView mv = controller.handleRequest(request, response);
        verifyMocks();
        System.out.println("mv.getModel() = " + mv.getModel());
        Errors errors = (Errors) mv.getModel().get(BindingResult.class + ".command");
        assertNotNull("Errors obj. should be in model", errors);
        assertEquals("Should be no errors", 0, errors.getErrorCount());

        assertEquals("study/study_identifiers", mv.getViewName());
        Study command = (Study) mv.getModel().get("command");
        assertEquals("Scott", command.getShortTitle());
        // exhaustively testing binding is pointless
    }
    */
}

@@ -588,24 +588,68 @@
 gProperty;%0D%0A
+import gov.nih.nci.cabig.caaers.utils.Lov;%0D%0A
 import gov.n
@@ -2287,16 +2287,35 @@
 ections.
+%3CString, List%3CLov%3E%3E
 emptyMap

Correct for generified ConfigProperty