package edu.duke.cabig.c3pr.web;

import static org.easymock.EasyMock.expect;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.ModelAndView;

import edu.duke.cabig.c3pr.dao.HealthcareSiteDao;
import edu.duke.cabig.c3pr.dao.ParticipantDao;
import edu.duke.cabig.c3pr.domain.HealthcareSite;
import edu.duke.cabig.c3pr.domain.Participant;
import edu.duke.cabig.c3pr.domain.validator.ParticipantValidator;
import edu.duke.cabig.c3pr.utils.ConfigurationProperty;
import edu.duke.cabig.c3pr.utils.ContextTools;
import edu.duke.cabig.c3pr.utils.Lov;
import edu.duke.cabig.c3pr.web.participant.ParticipantTab;

/**
 * @author Ramakrishna
 */

public class CreateParticipantControllerTest extends ControllerTestCase {

    private CreateParticipantController controller = new CreateParticipantController();

    private ParticipantDao participantDao;

    private HealthCareSiteDaoMock healthcareSiteDao;

    private ParticipantValidator participantValidator;

    private ConfigurationProperty configurationProperty;

    private ApplicationContext context;

    private Participant participant;

    protected void setUp() throws Exception {
        super.setUp();
        context = ContextTools.createConfigPropertiesApplicationContext();
        participant = registerMockFor(Participant.class);
        participantDao = registerMockFor(ParticipantDao.class);
        controller.setParticipantDao(participantDao);
        healthcareSiteDao = registerMockFor(HealthCareSiteDaoMock.class);
        controller.setHealthcareSiteDao(healthcareSiteDao);
        participantValidator = registerMockFor(ParticipantValidator.class);
        controller.setParticipantValidator(participantValidator);
        configurationProperty = registerMockFor(ConfigurationProperty.class);
        controller.setConfigurationProperty(new ConfigurationProperty());
        configurationProperty = (ConfigurationProperty) context.getBean("configurationProperty");
        controller.setConfigurationProperty(configurationProperty);
        ((ParticipantTab)controller.getFlow().getTab(0)).setConfigurationProperty(configurationProperty);
        ((ParticipantTab)controller.getFlow().getTab(0)).setHealthcareSiteDao(healthcareSiteDao);

    }

    public void testReferenceData() throws Exception {

      
        Map<String, Object> refdata = controller.getFlow().getTab(0).referenceData(participant);
        List<Lov> genders = (List<Lov>) refdata.get("administrativeGenderCode");
        System.out.println(" Size of ref data : " + refdata.size());
        Iterator<Lov> genderIter = genders.iterator();
        Lov gender;
        while (genderIter.hasNext()) {
            gender = genderIter.next();
            if (gender.getCode() == "Male") {
                assertEquals("Genders missing or wrong", "Male", gender.getDesc());

            }
        }

    }

    public void testViewOnGet() throws Exception {
    	
    	//expect(healthcareSiteDao.getAll()).andReturn(null).times(2);
        replayMocks();
        ModelAndView mv = controller.handleRequest(request, response);
        assertNotNull("Command not present in model: ", mv);
        assertEquals("participant/participant", mv.getViewName());
        request.setMethod("GET");
        verifyMocks();
    }

    public void testViewOnGoodSubmit() throws Exception {
    	
    	request.addParameter("firstName", "John");
        request.addParameter("lastName", "Doe");
        request.addParameter("birthDate", "02/11/1967");
        request.addParameter("administrativeGenderCode", "Male");
        request.addParameter("ethnicGroupCode", "Non Hispanic or Latino");
        request.addParameter("raceCode", "Not Reported");
        request.setParameter("_target1", "");
    	
    	//expect(healthcareSiteDao.getAll()).andReturn(null).times(2);
        replayMocks();
        ModelAndView mv = controller.handleRequest(request, response);
        assertNotNull("Command not present in model: ", mv);
        assertEquals("participant/participant", mv.getViewName());
        request.setMethod("GET");
        verifyMocks();
    }

    public class HealthCareSiteDaoMock extends HealthcareSiteDao {

        public List<HealthcareSite> getAll() {
            List list = new ArrayList();
            list = healthcareSiteDao.getAll();
            return list;
        }
    }
}

@@ -31,53 +31,8 @@
 b;%0A%0A
-import static org.easymock.EasyMock.expect;%0A%0A
 impo
@@ -692,19 +692,148 @@
 ticipant
-Tab
+DetailsTab;%0Aimport edu.duke.cabig.c3pr.web.participant.ParticipantTab;%0Aimport edu.duke.cabig.c3pr.web.participant.ParticipantWrapper
 ;%0A%0A/**%0A 
@@ -1312,16 +1312,99 @@
 icipant;
+%0A    %0A    private ParticipantWrapper participantWrapper = new ParticipantWrapper();
 %0A%0A    pr
@@ -1591,32 +1591,88 @@
 icipant.class);%0A
+        participantWrapper.setParticipant(participant);%0A
         particip
@@ -2680,16 +2680,40 @@
 fdata = 
+((ParticipantDetailsTab)
 controll
@@ -2734,16 +2734,17 @@
 etTab(0)
+)
 .referen
@@ -2750,16 +2750,24 @@
 nceData(
+request,
 particip
@@ -2761,32 +2761,39 @@
 uest,participant
+Wrapper
 );%0A        List%3C
@@ -3234,25 +3234,24 @@
 %7D%0A        %7D%0A
-%0A
     %7D%0A%0A    p

test case fix