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

import java.util.ArrayList;
import java.util.List;
import org.easymock.classextension.EasyMock;
import org.springframework.web.servlet.ModelAndView;

import edu.northwestern.bioinformatics.studycalendar.dao.SiteDao;
import edu.northwestern.bioinformatics.studycalendar.dao.StudyDao;
import edu.northwestern.bioinformatics.studycalendar.domain.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.domain.Site;
import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.service.TemplateService;
import edu.northwestern.bioinformatics.studycalendar.web.ControllerTestCase;

/**
 * @author Jaron Sampson
 *
 */
public class ReportBuilderSelectSitesControllerTest extends ControllerTestCase {
	private ReportBuilderSelectSitesController controller;
    private SiteDao siteDao;


	protected void setUp() throws Exception {
		super.setUp();

		siteDao = registerDaoMockFor(SiteDao.class);
		
		controller = new ReportBuilderSelectSitesController();
		
		controller.setSiteDao(siteDao);

	}
	
	public void testHandleRequest() throws Exception {
		List<Site> sites = new ArrayList<Site>();
		sites.add(Fixtures.createNamedInstance("New York", Site.class));
		sites.add(Fixtures.createNamedInstance("Chicago", Site.class));
		sites.add(Fixtures.createNamedInstance("Boston", Site.class));
		
		EasyMock.expect(siteDao.getById(1)).andReturn(sites.get(0));
		EasyMock.expect(siteDao.getById(2)).andReturn(sites.get(1));;
		EasyMock.expect(siteDao.getById(3)).andReturn(sites.get(2));;
		
		replayMocks();
		String[] siteIds = {"1","2","3"};
		request.addParameter("sites", siteIds);
		ModelAndView actual = controller.handleRequest(request, response);
		
		verifyMocks();
		
		assertEquals("Wrong view name", "reporting/ajax/studiesBySites", actual.getViewName());
	}

}

@@ -114,24 +114,49 @@
 util.List;%0D%0A
+import java.util.Set;%0D%0A%0D%0A
 import org.e
@@ -316,76 +316,8 @@
 o;%0D%0A
-import edu.northwestern.bioinformatics.studycalendar.dao.StudyDao;%0D%0A
 impo
@@ -575,30 +575,23 @@
 dar.
-service.TemplateServic
+domain.StudySit
 e;%0D%0A
@@ -1317,15 +1317,14 @@
 ce(%22
-Chicago
+Boston
 %22, S
@@ -1335,34 +1335,45 @@
 class));%0D%0A%09%09
-sites.add(
+Site westCoastSite = 
 Fixtures.cre
@@ -1382,38 +1382,39 @@
 eNamedInstance(%22
-Boston
+Seattle
 %22, Site.class));
@@ -1407,25 +1407,24 @@
  Site.class)
-)
 ;%0D%0A%09%09%0D%0A%09%09Eas
@@ -1548,74 +1548,794 @@
 1));
-;%0D%0A%09%09EasyMock.expect(siteDao.getById(3)).andReturn(sites.get(2));;
+%0D%0A%09%09%0D%0A%09%09List%3CStudy%3E studies = new ArrayList%3CStudy%3E();%0D%0A%09%09String%5B%5D armNames = %7B%22Arm1%22, %22Arm2%22%7D;%0D%0A%09%09studies.add(Fixtures.createSingleEpochStudy(%22Frank%22, %22Treatment%22, armNames));%0D%0A%09%09studies.add(Fixtures.createSingleEpochStudy(%22Norbert%22, %22Treatment%22, armNames));%0D%0A%09%09studies.add(Fixtures.createSingleEpochStudy(%22Lucy%22, %22Treatment%22, armNames));%0D%0A%09%09studies.add(Fixtures.createSingleEpochStudy(%22Tom%22, %22Treatment%22, armNames));%0D%0A%09%09%0D%0A%09%09List%3CStudySite%3E studySites = new ArrayList%3CStudySite%3E();%0D%0A%09%09studySites.add(Fixtures.createStudySite(studies.get(0), sites.get(0)));%0D%0A%09%09studySites.add(Fixtures.createStudySite(studies.get(1), sites.get(0)));%0D%0A%09%09studySites.add(Fixtures.createStudySite(studies.get(2), westCoastSite));%0D%0A%09%09studySites.add(Fixtures.createStudySite(studies.get(3), sites.get(1)));%0D%0A%09%09
 %0D%0A%09%09
@@ -2387,12 +2387,8 @@
 ,%222%22
-,%223%22
 %7D;%0D%0A
@@ -2526,16 +2526,214 @@
 );%0D%0A%09%09%0D%0A
+%09%09Set%3CStudy%3E studiesInModel = (Set%3CStudy%3E) actual.getModel().get(%22studies%22);%0D%0A%09%09for(Study study : studiesInModel) %7B%0D%0A%09%09%09assertNotSame(%22Found a study from wrong site.%22, studies.get(2), study);%0D%0A%09%09%7D%0D%0A
 %09%09assert

Test for report builder controller