package edu.northwestern.bioinformatics.studycalendar.web;

import edu.northwestern.bioinformatics.studycalendar.dao.SiteDao;
import edu.northwestern.bioinformatics.studycalendar.dao.UserRoleDao;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.*;
import edu.northwestern.bioinformatics.studycalendar.domain.*;
import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import static org.easymock.EasyMock.expect;

import static java.util.Arrays.asList;
import java.util.List;
import java.util.Map;

public class SiteCoordinatorDashboardCommandTest extends StudyCalendarTestCase {
    private SiteDao siteDao;
    private UserRoleDao userRoleDao;
    private Study study;
    private List<User> users;
    private List<Site> sites;
    private List<UserRole> userRoles;


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

        siteDao     = registerDaoMockFor(SiteDao.class);
        userRoleDao = registerDaoMockFor(UserRoleDao.class);

        study = createNamedInstance("Study A", Study.class);

        Site  site0  = createNamedInstance("Mayo Clinic" , Site.class);
        Site  site1  = createNamedInstance("Northwestern", Site.class);

        User  user0  = createNamedInstance("John", User.class);
        User  user1  = createNamedInstance("Jake", User.class);

        UserRole role0 = createUserRole(user0, Role.PARTICIPANT_COORDINATOR, site0, site1);
        role0.setStudySites(asList(createStudySite(study, site0)));

        UserRole role1 = createUserRole(user1, Role.PARTICIPANT_COORDINATOR, site0);

        user0.addUserRole(role0);
        user1.addUserRole(role1);

        sites     = asList(site0, site1);
        users     = asList(user0, user1);
        userRoles = asList(role0, role1);
    }

    public void testBuildStudyAssignmentGrid() throws Exception {
        expect(userRoleDao.getAllParticipantCoordinatorUserRoles()).andReturn(userRoles);
        expect(siteDao.getAll()).andReturn(sites);

        replayMocks();
        SiteCoordinatorDashboardCommand newCommand = createCommand();
        verifyMocks();

        Map<User, Map<Site,SiteCoordinatorDashboardCommand.StudyAssignmentCell>> studyAssignmentGrid = newCommand.getStudyAssignmentGrid();

        assertTrue("No User", studyAssignmentGrid.containsKey(users.get(0)));
        assertTrue("No User", studyAssignmentGrid.containsKey(users.get(1)));

        assertTrue("No Site", studyAssignmentGrid.get(users.get(0)).containsKey(sites.get(0)));
        assertTrue("No Site", studyAssignmentGrid.get(users.get(1)).containsKey(sites.get(0)));

        assertTrue("Should be selected"     , studyAssignmentGrid.get(users.get(0)).get(sites.get(0)).isSelected());
        assertFalse("Should not be selected", studyAssignmentGrid.get(users.get(0)).get(sites.get(1)).isSelected());
        assertFalse("Should not be selected", studyAssignmentGrid.get(users.get(1)).get(sites.get(0)).isSelected());
        assertFalse("Should not be selected", studyAssignmentGrid.get(users.get(1)).get(sites.get(1)).isSelected());

        assertTrue("Site Access should be allowed"     , studyAssignmentGrid.get(users.get(0)).get(sites.get(0)).isSiteAccessAllowed());
        assertTrue("Site Access should be allowed"     , studyAssignmentGrid.get(users.get(0)).get(sites.get(1)).isSiteAccessAllowed());
        assertTrue("Site Access should be allowed"     , studyAssignmentGrid.get(users.get(1)).get(sites.get(0)).isSiteAccessAllowed());
        assertFalse("Site Access should not be allowed", studyAssignmentGrid.get(users.get(1)).get(sites.get(1)).isSiteAccessAllowed());
    }

    private SiteCoordinatorDashboardCommand createCommand() {
        return new SiteCoordinatorDashboardCommand(siteDao, userRoleDao, study);
    }
}

@@ -721,38 +721,8 @@
 dy;%0A
-    private List%3CUser%3E users;%0A
     
@@ -785,16 +785,78 @@
 rRoles;%0A
+    private User user0, user1;%0A    private Site site0, site1;%0A
 %0A%0A    pr
@@ -1104,30 +1104,24 @@
 );%0A%0A        
-Site  
 site0  = cre
@@ -1178,14 +1178,8 @@
     
-Site  
 site
@@ -1237,30 +1237,24 @@
 );%0A%0A        
-User  
 user0  = cre
@@ -1303,14 +1303,8 @@
     
-User  
 user
@@ -1712,50 +1712,8 @@
 1);%0A
-        users     = asList(user0, user1);%0A
     
@@ -2289,24 +2289,17 @@
 Key(user
-s.get(0)
+0
 ));%0A    
@@ -2360,24 +2360,17 @@
 Key(user
-s.get(1)
+1
 ));%0A%0A   
@@ -2424,25 +2424,18 @@
 get(user
-s.get(
 0)
-)
 .contain
@@ -2439,32 +2439,25 @@
 ainsKey(site
-s.get(0)
+0
 ));%0A        
@@ -2506,24 +2506,17 @@
 get(user
-s.get(1)
+1
 ).contai
@@ -2525,24 +2525,17 @@
 Key(site
-s.get(0)
+0
 ));%0A%0A   
@@ -2601,32 +2601,25 @@
 rid.get(user
-s.get(0)
+0
 ).get(sites.
@@ -2612,32 +2612,25 @@
 r0).get(site
-s.get(0)
+0
 ).isSelected
@@ -2704,32 +2704,25 @@
 rid.get(user
-s.get(0)
+0
 ).get(sites.
@@ -2715,32 +2715,25 @@
 r0).get(site
-s.get(1)
+1
 ).isSelected
@@ -2807,32 +2807,25 @@
 rid.get(user
-s.get(1)
+1
 ).get(sites.
@@ -2818,32 +2818,25 @@
 r1).get(site
-s.get(0)
+0
 ).isSelected
@@ -2910,32 +2910,25 @@
 rid.get(user
-s.get(1)
+1
 ).get(sites.
@@ -2921,33 +2921,26 @@
 r1).get(site
-s.get(
 1)
-)
 .isSelected(
@@ -3025,32 +3025,25 @@
 rid.get(user
-s.get(0)
+0
 ).get(sites.
@@ -3036,32 +3036,25 @@
 r0).get(site
-s.get(0)
+0
 ).isSiteAcce
@@ -3152,24 +3152,17 @@
 get(user
-s.get(0)
+0
 ).get(si
@@ -3159,32 +3159,25 @@
 r0).get(site
-s.get(1)
+1
 ).isSiteAcce
@@ -3271,32 +3271,25 @@
 rid.get(user
-s.get(1)
+1
 ).get(sites.
@@ -3290,16 +3290,9 @@
 site
-s.get(0)
+0
 ).is
@@ -3398,24 +3398,17 @@
 get(user
-s.get(1)
+1
 ).get(si
@@ -3413,16 +3413,9 @@
 site
-s.get(1)
+1
 ).is

Simplify Site coordinator dashboard command test.