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

import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.*;
import edu.northwestern.bioinformatics.studycalendar.web.ControllerTestCase;
import edu.northwestern.bioinformatics.studycalendar.service.TemplateSkeletonCreator;
import edu.northwestern.bioinformatics.studycalendar.service.StudyService;
import static org.easymock.classextension.EasyMock.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Rhett Sutphin
 */
public class NewStudyControllerTest extends ControllerTestCase {
    private static final int ID = 81;

    private NewStudyController controller;
    private StudyService studyService;
    private NewStudyCommand command;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        request.setMethod("GET");
        studyService = registerMockFor(StudyService.class);
        command = registerMockFor(NewStudyCommand.class);

        controller = new NewStudyController() {
            @Override
            protected Object getCommand(HttpServletRequest request) throws Exception {
                return command;
            }
        };
        controller.setStudyService(studyService);
        controller.setControllerTools(controllerTools);
    }

    public void testHandle() throws Exception {
        Study study = setId(ID, new Study());
        expect(command.create()).andReturn(study);

        replayMocks();
        ModelAndView mv = controller.handleRequest(request, response);
        verifyMocks();

        assertEquals("redirectToCalendarTemplate", mv.getViewName());
        assertEquals(1, mv.getModel().size());
        assertContainsPair(mv.getModel(), "study", ID);
    }
    
    public void testBindMode() throws Exception {
        request.setParameter("base", "BLANK");
        command.setBase(TemplateSkeletonCreator.BLANK);
        expect(command.create()).andReturn(setId(ID, new Study()));

        replayMocks();
        controller.handleRequest(request, response);
        verifyMocks();
    }

}

@@ -129,16 +129,93 @@
 .Study;%0A
+import edu.northwestern.bioinformatics.studycalendar.domain.delta.Amendment;%0A
 import s
@@ -281,24 +281,24 @@
 Fixtures.*;%0A
-
 import edu.n
@@ -933,16 +933,88 @@
 command;
+%0A    private static final int AMENDMENT_ID = 4;%0A    private Study study;
 %0A%0A    @O
@@ -1559,24 +1559,146 @@
 llerTools);%0A
+        study = setId(ID, new Study());%0A        study.setDevelopmentAmendment(setId(AMENDMENT_ID, new Amendment(%22dev%22)));%0A
     %7D%0A%0A    p
@@ -1744,54 +1744,8 @@
 n %7B%0A
-        Study study = setId(ID, new Study());%0A
     
@@ -2001,17 +2001,17 @@
 tEquals(
-1
+2
 , mv.get
@@ -2083,16 +2083,86 @@
 %22, ID);%0A
+        assertContainsPair(mv.getModel(), %22amendment%22, AMENDMENT_ID);%0A
     %7D%0A  
@@ -2361,37 +2361,20 @@
 Return(s
-etId(ID, new S
 tudy
-())
 );%0A%0A    

Fix for changes in [1614]