Example usage for org.springframework.mock.web MockHttpSession MockHttpSession

List of usage examples for org.springframework.mock.web MockHttpSession MockHttpSession

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpSession MockHttpSession.

Prototype

public MockHttpSession() 

Source Link

Document

Create a new MockHttpSession with a default MockServletContext .

Usage

From source file:fragment.web.UsersControllerTest.java

@Test
public void testCreateUserStep2() throws Exception {
    User user = userDAO.find(3L);/*from   w w  w .ja  va  2s.c  om*/
    asUser(user);
    UserForm form = new UserForm();
    form.setCountryList(countryService.getCountries(null, null, null, null, null, null, null));
    com.citrix.cpbm.access.User newUser = form.getUser();
    newUser.setEmail("test@test.com");
    newUser.setUsername("testuser");
    newUser.setFirstName("firstName");
    newUser.setLastName("lastName");
    Profile profile = profileDAO.findByName("User");
    form.setUserProfile(profile.getId());
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("submitButtonEmail", "Finish");
    BindingResult bindingResult = validate(form);
    com.citrix.cpbm.access.Tenant proxyTenant = (com.citrix.cpbm.access.Tenant) CustomProxy
            .newInstance(controller.getTenant());
    String view = controller.createUserStepTwo(form, bindingResult, proxyTenant, map, mockRequest,
            new MockHttpSession());
    Assert.assertEquals("users.newuserregistration.finish", view);
    Assert.assertNotNull(map.get("user"));
    Assert.assertEquals(form, map.get("user"));
}

From source file:org.openmrs.module.radiology.order.web.RadiologyOrderFormControllerTest.java

/**
 * @see RadiologyOrderFormController#discontinueRadiologyOrder(HttpServletRequest,HttpServletResponse,RadiologyOrder,DiscontinuationOrderRequest,BindingResult)
 * @verifies not discontinue given radiology order and not redirect if discontinuation order request is not valid
 *///from   w  ww .  j  a v a2s. com
@Test
public void discontinueRadiologyOrder_shouldNotDiscontinueGivenRadiologyOrderAndNotRedirectIfDiscontinuationOrderRequestIsNotValid()
        throws Exception {

    // given
    RadiologyOrder mockRadiologyOrderToDiscontinue = RadiologyTestData.getMockRadiologyOrder1();

    DiscontinuationOrderRequest discontinuationOrderRequest = new DiscontinuationOrderRequest();
    discontinuationOrderRequest.setOrderer(mockRadiologyOrderToDiscontinue.getOrderer());
    discontinuationOrderRequest.setReasonNonCoded("");

    Order mockDiscontinuationOrder = new Order();
    mockDiscontinuationOrder.setOrderId(2);
    mockDiscontinuationOrder.setAction(Order.Action.DISCONTINUE);
    mockDiscontinuationOrder.setOrderer(discontinuationOrderRequest.getOrderer());
    mockDiscontinuationOrder.setOrderReasonNonCoded(discontinuationOrderRequest.getReasonNonCoded());
    mockDiscontinuationOrder.setPreviousOrder(mockRadiologyOrderToDiscontinue);

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("discontinueOrder", "discontinueOrder");
    MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    when(radiologyOrderService.getRadiologyOrder(mockRadiologyOrderToDiscontinue.getOrderId()))
            .thenReturn(mockRadiologyOrderToDiscontinue);
    when(radiologyOrderService.discontinueRadiologyOrder(mockRadiologyOrderToDiscontinue,
            mockDiscontinuationOrder.getOrderer(), mockDiscontinuationOrder.getOrderReasonNonCoded()))
                    .thenReturn(mockDiscontinuationOrder);

    BindingResult resultDiscontinueOrderRequest = mock(BindingResult.class);
    when(resultDiscontinueOrderRequest.hasErrors()).thenReturn(true);

    assertThat(mockRadiologyOrderToDiscontinue.getAction(), is(Order.Action.NEW));
    ModelAndView modelAndView = radiologyOrderFormController.discontinueRadiologyOrder(mockRequest,
            mockRadiologyOrderToDiscontinue, discontinuationOrderRequest, resultDiscontinueOrderRequest);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is(RadiologyOrderFormController.RADIOLOGY_ORDER_FORM_VIEW));

    assertThat(modelAndView.getModelMap(), hasKey("order"));
    Order order = (Order) modelAndView.getModelMap().get("order");
    assertThat(order, is(mockRadiologyOrderToDiscontinue));

    assertThat(modelAndView.getModelMap(), hasKey("radiologyOrder"));
    RadiologyOrder radiologyOrder = (RadiologyOrder) modelAndView.getModelMap().get("radiologyOrder");
    assertThat(radiologyOrder, is(mockRadiologyOrderToDiscontinue));
}

From source file:org.openmrs.module.radiology.web.controller.RadiologyOrderFormControllerTest.java

/**
 * @see RadiologyOrderFormController#postDiscontinueRadiologyOrder(HttpServletRequest,
 *      HttpServletResponse, Order, String, Date)
 *//*  w  ww .jav  a2 s. c  o  m*/
@Test
@Verifies(value = "should discontinue non discontinued order and redirect to discontinuation order", method = "postDiscontinueRadiologyOrder(HttpServletRequest, HttpServletResponse, Order, String, Date)")
public void postDiscontinueRadiologyOrder_shouldDiscontinueNonDiscontinuedOrderAndRedirectToDiscontinuationOrder()
        throws Exception {
    //given
    RadiologyOrder mockRadiologyOrderToDiscontinue = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrderToDiscontinue.getStudy().setMwlStatus(MwlStatus.DISCONTINUE_OK);
    String discontinueReason = "Wrong Procedure";
    Date discontinueDate = new GregorianCalendar(2015, Calendar.JANUARY, 01).getTime();

    Order mockDiscontinuationOrder = new Order();
    mockDiscontinuationOrder.setOrderId(2);
    mockDiscontinuationOrder.setAction(Order.Action.DISCONTINUE);
    mockDiscontinuationOrder.setOrderer(mockRadiologyOrderToDiscontinue.getOrderer());
    mockDiscontinuationOrder.setOrderReasonNonCoded(discontinueReason);
    mockDiscontinuationOrder.setDateActivated(discontinueDate);
    mockDiscontinuationOrder.setPreviousOrder(mockRadiologyOrderToDiscontinue);

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("discontinueOrder", "discontinueOrder");
    MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    when(radiologyService.getRadiologyOrderByOrderId(mockRadiologyOrderToDiscontinue.getOrderId()))
            .thenReturn(mockRadiologyOrderToDiscontinue);
    when(radiologyService.discontinueRadiologyOrder(mockRadiologyOrderToDiscontinue,
            mockDiscontinuationOrder.getOrderer(), mockDiscontinuationOrder.getDateActivated(),
            mockDiscontinuationOrder.getOrderReasonNonCoded())).thenReturn(mockDiscontinuationOrder);

    assertThat(mockRadiologyOrderToDiscontinue.getAction(), is(Order.Action.NEW));
    ModelAndView modelAndView = radiologyOrderFormController.postDiscontinueRadiologyOrder(mockRequest, null,
            mockRadiologyOrderToDiscontinue, mockDiscontinuationOrder);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is(
            "redirect:/module/radiology/radiologyOrder.form?orderId=" + mockDiscontinuationOrder.getOrderId()));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR),
            is("Order.discontinuedSuccessfully"));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackActionControllerTests.java

@Test
public void verifyResponseIsCodeWithoutState() throws Exception {
    final AuthorizationCode authorizationCode = mock(AuthorizationCode.class);
    when(authorizationCode.getId()).thenReturn(AC_ID);

    final Set<String> scopes = new HashSet<>();
    scopes.add(NAME1);//w  w  w . j a va 2 s. c  om
    scopes.add(NAME2);

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.grantAuthorizationCode(TokenType.OFFLINE, CLIENT_ID, TICKET_GRANTING_TICKET_ID,
            REDIRECT_URI, scopes)).thenReturn(authorizationCode);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_REDIRECT_URI, REDIRECT_URI);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE_SET, scopes);
    mockRequest.setSession(mockSession);
    mockRequest.setParameter(OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION,
            OAuthConstants.OAUTH20_APPROVAL_PROMPT_ACTION_ALLOW);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertEquals(redirectView.getUrl(), REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + AC_ID);

    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_RESPONSE_TYPE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_CLIENT_ID));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_STATE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_REDIRECT_URI));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_TOKEN_TYPE));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_LOGIN_TICKET_ID));
    assertNull(mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:fragment.web.AuthenticationControllerTest.java

@Test
public void testReset() {
    MockHttpSession mockSession = new MockHttpSession();
    User user = userDAO.find(3L);//from   ww  w .j a v a 2  s.  c om
    long genTime = System.currentTimeMillis();
    String auth = user.getAuthorization(genTime);
    String view = controller.reset(auth, genTime, user.getParam(), mockSession, map);
    Assert.assertEquals("auth.reset", view);
    Assert.assertNotNull(mockSession.getAttribute(AbstractBaseController.RESET_USER_KEY));
    Assert.assertEquals(user.getUsername(), mockSession.getAttribute(AbstractBaseController.RESET_USER_KEY));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20AuthorizeCallbackControllerTests.java

@Test
public void verifyAutoPromptWithExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.OFFLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(false);//  w w  w .j  a  v  a2 s .com
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(true);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, "auto");
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl()
            .endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL + "?action=allow"));

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:fragment.web.UsersControllerTest.java

@Test
public void testCreateUserStep2WithSuffix() throws Exception {
    asRoot();//from   w ww.jav  a2  s.co m
    com.vmops.model.Configuration configuration = configurationService
            .locateConfigurationByName(Names.com_citrix_cpbm_username_duplicate_allowed);
    configuration.setValue("true");
    configurationService.update(configuration);

    User user = userDAO.find(3L);

    Tenant userTenant = user.getTenant();
    String suffix = "testSuffix";
    userTenant.setUsernameSuffix(suffix);
    tenantService.update(userTenant);

    asUser(user);
    UserForm form = new UserForm();
    form.setCountryList(countryService.getCountries(null, null, null, null, null, null, null));
    com.citrix.cpbm.access.User newUser = form.getUser();
    newUser.setEmail("test@test.com");
    newUser.setUsername("testuser");
    newUser.setFirstName("firstName");
    newUser.setLastName("lastName");
    Profile profile = profileDAO.findByName("User");
    form.setUserProfile(profile.getId());
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.addParameter("submitButtonEmail", "Finish");
    BindingResult bindingResult = validate(form);
    com.citrix.cpbm.access.Tenant proxyTenant = (com.citrix.cpbm.access.Tenant) CustomProxy
            .newInstance(controller.getTenant());
    String view = controller.createUserStepTwo(form, bindingResult, proxyTenant, map, mockRequest,
            new MockHttpSession());
    Assert.assertEquals("users.newuserregistration.finish", view);
    Assert.assertNotNull(map.get("user"));
    Assert.assertEquals(form, map.get("user"));

    User found = userService.getUserByParam("username", "testuser@" + suffix, false);
    Assert.assertNotNull(found);
    Assert.assertEquals("firstName", found.getFirstName());
    Assert.assertEquals("lastName", found.getLastName());

}

From source file:fragment.web.AuthenticationControllerTest.java

@Test(expected = UserAuthorizationInvalidException.class)
public void testBadReset() {
    MockHttpSession mockSession = new MockHttpSession();
    User user = userDAO.find(3L);//from w  ww .  ja v  a2  s.c om
    long genTime = System.currentTimeMillis();
    String auth = user.getAuthorization(genTime);
    String view = controller.reset(auth, genTime - 100, user.getParam(), mockSession, map);
    Assert.assertEquals("auth.reset", view);
    Assert.assertNotNull(mockSession.getAttribute(AbstractBaseController.RESET_USER_KEY));
    Assert.assertEquals(user.getUsername(), mockSession.getAttribute(AbstractBaseController.RESET_USER_KEY));
}

From source file:fragment.web.AuthenticationControllerTest.java

@Test
public void testResetPassword() {
    MockHttpSession mockSession = new MockHttpSession();
    HttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    User user = userDAO.find(3L);//  w  w  w .java  2 s. co  m
    mockSession.setAttribute(AbstractBaseController.RESET_USER_KEY, user.getUsername());
    String newpass = "newPassw0rd";
    String view = controller.reset(newpass, mockSession, mockHttpServletRequest);
    Assert.assertEquals("redirect:/portal/login", view);
    userDAO.flush();
    userDAO.refresh(user);
    Assert.assertTrue(user.authenticate(newpass));
}

From source file:fragment.web.AuthenticationControllerTest.java

@Test(expected = NoSuchUserException.class)
public void testBadResetPassword() {
    MockHttpSession mockSession = new MockHttpSession();
    String newpass = "newPassw0rd";
    HttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    controller.reset(newpass, mockSession, mockHttpServletRequest);
}