package edu.northwestern.bioinformatics.studycalendar.web.schedule;

import edu.northwestern.bioinformatics.studycalendar.dao.ArmDao;
import edu.northwestern.bioinformatics.studycalendar.dao.ScheduledCalendarDao;
import edu.northwestern.bioinformatics.studycalendar.domain.Arm;
import edu.northwestern.bioinformatics.studycalendar.domain.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledCalendar;
import edu.northwestern.bioinformatics.studycalendar.web.ControllerTestCase;
import static org.easymock.classextension.EasyMock.expect;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * @author Rhett Sutphin
 */
public class ScheduleNextArmControllerTest extends ControllerTestCase {
    private ArmDao armDao;
    private ScheduledCalendarDao scheduledCalendarDao;

    private ScheduleNextArmCommand command;
    private ScheduleNextArmController controller;

    protected void setUp() throws Exception {
        super.setUp();
        armDao = registerDaoMockFor(ArmDao.class);
        scheduledCalendarDao = registerDaoMockFor(ScheduledCalendarDao.class);

        command = registerMockFor(ScheduleNextArmCommand.class, ScheduleNextArmCommand.class.getMethod("schedule"));

        controller = new ScheduleNextArmController() {
            @Override protected Object getCommand(HttpServletRequest request) throws Exception {
                return command;
            }
        };
        controller.setArmDao(armDao);
        controller.setScheduledCalendarDao(scheduledCalendarDao);
    }
    
    public void testBindArm() throws Exception {
        int id = 44;
        Arm expectedArm = Fixtures.setId(id, Fixtures.createNamedInstance("Baker", Arm.class));
        expect(armDao.getById(id)).andReturn(expectedArm);
        request.addParameter("arm", Integer.toString(id));

        executeRequest();

        assertSame(expectedArm, command.getArm());
    }

    public void testBindCalendar() throws Exception {
        int id = 17;
        ScheduledCalendar expectedCalendar = Fixtures.setId(id, new ScheduledCalendar());
        expect(scheduledCalendarDao.getById(id)).andReturn(expectedCalendar);
        request.addParameter("calendar", Integer.toString(id));

        executeRequest();

        assertSame(expectedCalendar, command.getCalendar());
    }

    public void testBindDate() throws Exception {
        request.addParameter("startDate", "08/05/2003");

        executeRequest();

        assertDayOfDate(2003, Calendar.AUGUST, 5, command.getStartDate());
    }

    private void executeRequest() throws Exception {
        command.schedule();
        replayMocks();
        ModelAndView mv = controller.handleRequest(request, response);
        verifyMocks();

        BindingResult result = (BindingResult) mv.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");
        assertEquals("There were errors in the request: " + result.getAllErrors(), 0, result.getErrorCount());
    }
}

@@ -420,16 +420,90 @@
 lendar;%0A
+import edu.northwestern.bioinformatics.studycalendar.domain.ScheduledArm;%0A
 import e
@@ -1092,16 +1092,90 @@
 troller;
+%0A    private static final ScheduledArm SCHEDULED_ARM = new ScheduledArm();
 %0A%0A    pr
@@ -2809,19 +2809,308 @@
    p
-rivate void
+ublic void testScheduledArmInModel() throws Exception %7B%0A        ModelAndView actual = executeRequest();%0A        assertTrue(%22Missing scheduledArm%22, actual.getModel().containsKey(%22scheduledArm%22));%0A        assertSame(SCHEDULED_ARM, actual.getModel().get(%22scheduledArm%22));%0A    %7D%0A%0A    private ModelAndView
  exe
@@ -3146,24 +3146,31 @@
 n %7B%0A        
+expect(
 command.sche
@@ -3175,16 +3175,42 @@
 hedule()
+).andReturn(SCHEDULED_ARM)
 ;%0A      
@@ -3431,16 +3431,16 @@
 mand%22);%0A
-
         
@@ -3538,20 +3538,39 @@
 orCount());%0A
+        return mv;%0A
     %7D%0A%7D%0A

Test that scheduledArm is passed into the model