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

import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.domain.Epoch;
import edu.northwestern.bioinformatics.studycalendar.domain.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.domain.Named;
import edu.northwestern.bioinformatics.studycalendar.domain.PlannedCalendar;
import edu.northwestern.bioinformatics.studycalendar.dao.StudyDao;
import org.easymock.classextension.EasyMock;

/**
 * @author Rhett Sutphin
 */
public class RenameCommandTest extends StudyCalendarTestCase {
    private static final String NEW_NAME = "new name";
    private static final Study STUDY = new Study();
    private StudyDao studyDao;
    private RenameCommand command;

    protected void setUp() throws Exception {
        super.setUp();
        studyDao = registerDaoMockFor(StudyDao.class);
        command = new RenameCommand();
        command.setStudyDao(studyDao);
        command.setValue(NEW_NAME);
    }

    public void testRenameStudy() throws Exception {
        command.setStudy(STUDY);

        doApply();
        assertRenamed("Study", STUDY);
    }

    public void testRenameMultiArmEpoch() throws Exception {
        Epoch epoch = createEpoch("E1", "A", "B");
        command.setEpoch(epoch);

        doApply();
        assertRenamed("Epoch", epoch);
        assertName("Arm 0 should not be renamed", "A", epoch.getArms().get(0));
        assertName("Arm 1 should not be renamed", "B", epoch.getArms().get(1));
    }

    public void testRenameNoArmEpoch() throws Exception {
        Epoch epoch = createEpoch("E1");
        command.setEpoch(epoch);

        doApply();
        assertRenamed("Epoch", epoch);
        assertRenamed("Sole arm", epoch.getArms().get(0));
    }

    public void testRenameArmFromMultiArmEpoch() throws Exception {
        Epoch epoch = createEpoch("E1", "A", "B");
        command.setArm(epoch.getArms().get(0));

        doApply();
        assertName("Epoch should not be renamed", "E1", epoch);
        assertRenamed("Arm 0", epoch.getArms().get(0));
        assertName("Arm 1 should not be renamed", "B", epoch.getArms().get(1));
    }

    public void testRenameArmOfNoArmEpoch() throws Exception {
        Epoch epoch = createEpoch("E1");
        command.setArm(epoch.getArms().get(0));

        doApply();
        assertRenamed("Epoch", epoch);
        assertRenamed("Sole arm", epoch.getArms().get(0));
    }
    
    private static Epoch createEpoch(String epochName, String... armNames) {
        PlannedCalendar cal = new PlannedCalendar();
        cal.setStudy(STUDY);

        Epoch epoch = Fixtures.createEpoch(epochName, armNames);
        epoch.setPlannedCalendar(cal);
        return epoch;
    }

    private void doApply() {
        studyDao.save(STUDY);
        replayMocks();
        command.apply();
        verifyMocks();
    }

    private static void assertRenamed(String desc, Named named) {
        assertName(desc + " not renamed", NEW_NAME, named);
    }

    private static void assertName(String message, String expectedName, Named named) {
        assertEquals(message, expectedName, named.getName());
    }
}

@@ -564,53 +564,8 @@
 Dao;
-%0Aimport org.easymock.classextension.EasyMock;
 %0A%0A/*
@@ -729,46 +729,19 @@
 ate 
-static final Study STUDY = new S
+Study s
 tudy
-()
 ;%0A  
@@ -1042,16 +1042,86 @@
 W_NAME);
+%0A%0A        study = Fixtures.createSingleEpochStudy(%22Study 1234%22, %22E1%22);
 %0A    %7D%0A%0A
@@ -1190,37 +1190,37 @@
 ommand.setStudy(
-STUDY
+study
 );%0A%0A        doAp
@@ -1257,21 +1257,21 @@
 Study%22, 
-STUDY
+study
 );%0A    %7D
@@ -2587,23 +2587,16 @@
 private 
-static 
 Epoch cr
@@ -2649,91 +2649,8 @@
 ) %7B%0A
-        PlannedCalendar cal = new PlannedCalendar();%0A        cal.setStudy(STUDY);%0A%0A
     
@@ -2671,28 +2671,20 @@
 h = 
-Fixtures
+Epoch
 .create
-Epoch
 (epo
@@ -2714,15 +2714,15 @@
     
-epoch.s
+study.g
 etPl
@@ -2735,19 +2735,32 @@
 alendar(
-cal
+).addEpoch(epoch
 );%0A     
@@ -2838,13 +2838,13 @@
 ave(
-STUDY
+study
 );%0A 

Correct for changes to EditCommand