package edu.northwestern.bioinformatics.studycalendar.security;

import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import edu.northwestern.bioinformatics.studycalendar.domain.Role;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.easymock.classextension.EasyMock;
import static org.easymock.classextension.EasyMock.*;

/**
 * @author Rhett Sutphin
 */
public class WebSSOAuthoritiesPopulatorTest extends StudyCalendarTestCase {
    private WebSSOAuthoritiesPopulator populator;
    private UserDetailsService userDetailsService;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        userDetailsService = registerMockFor(UserDetailsService.class);
        populator = new WebSSOAuthoritiesPopulator();
        populator.setUserDetailsService(userDetailsService);
    }

    public void testPopulation() throws Exception {
        String expectedSsoUserId = "CAGRID_SSO_EMAIL_ID^jo@org.org$CAGRID_SSO_FIRST_NAME^Josephine$CAGRID_SSO_LAST_NAME^Miller$CAGRID_SSO_GRID_IDENTITY^ablablab";
        String expectedAddress = "jo@org.org";
        expect(userDetailsService.loadUserByUsername(expectedAddress)).andReturn(
            new User(expectedAddress, "ignored", true, true, true, true, new Role[] { Role.STUDY_ADMIN }));
        replayMocks();

        UserDetails details = populator.getUserDetails(expectedSsoUserId);
        assertTrue("Returned details not an instance of WebSSOUser", details instanceof WebSSOUser);
        WebSSOUser ssoDetails = (WebSSOUser) details;
        assertEquals("First name not preserved", "Josephine", ssoDetails.getFirstName());
        assertEquals("Last name not preserved", "Miller", ssoDetails.getLastName());
        assertEquals("User has wrong number of authorities", 1, ssoDetails.getAuthorities().length);
        assertEquals("User has wrong authority", Role.STUDY_ADMIN, ssoDetails.getAuthorities()[0]);
    }
}

@@ -115,36 +115,18 @@
 dar.
-testing.StudyCalendarTestCas
+domain.Rol
 e;%0Ai
@@ -181,75 +181,36 @@
 dar.
-domain.Role;%0Aimport org.acegisecurity.userdetails.UserDetailsServic
+testing.StudyCalendarTestCas
 e;%0Ai
@@ -316,40 +316,52 @@
 org.
-easymock.classextension.EasyMock
+acegisecurity.userdetails.UserDetailsService
 ;%0Aim
@@ -409,17 +409,22 @@
 asyMock.
-*
+expect
 ;%0A%0A/**%0A 

Clean up imports