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

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

Introduction

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

Prototype

@Override
    public Object getAttribute(String name) 

Source Link

Usage

From source file:com.healthcit.cacure.web.controller.LogoutControllerTest.java

@Test
public void testProcessLogout() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    UserCredentials userCredentials = new UserCredentials();
    userCredentials.setUserName("Testing");
    userCredentials.setPassword("TestPassword");
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(Constants.CREDENTIALS, userCredentials);
    request.setSession(session);/*from  www  .  j  a v a  2 s  . c  om*/
    RedirectView actual = (RedirectView) logoutController.processLogout(request);
    RedirectView expected = new RedirectView(Constants.HOME_URI, true);
    Assert.assertNotNull(actual);
    Assert.assertNull(session.getAttribute(Constants.CREDENTIALS));
    Assert.assertEquals(expected.getUrl(), actual.getUrl());
}

From source file:fragment.web.AuthenticationControllerTest.java

@Test
public void testReset() {
    MockHttpSession mockSession = new MockHttpSession();
    User user = userDAO.find(3L);/*  ww  w  .j ava 2s.  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:fragment.web.AuthenticationControllerTest.java

@Test(expected = UserAuthorizationInvalidException.class)
public void testBadReset() {
    MockHttpSession mockSession = new MockHttpSession();
    User user = userDAO.find(3L);/* w w w .  j a  va  2  s.  c o m*/
    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:org.jasig.cas.support.oauth.web.flow.OAuthActionTests.java

@Test
public void testStartAuthentication() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameter(OAuthConstants.THEME, MY_THEME);
    mockRequest.setParameter(OAuthConstants.LOCALE, MY_LOCALE);
    mockRequest.setParameter(OAuthConstants.METHOD, MY_METHOD);

    final MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
    when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);

    final MockRequestContext mockRequestContext = new MockRequestContext();
    mockRequestContext.setExternalContext(servletExternalContext);
    mockRequestContext.getFlowScope().put(OAuthConstants.SERVICE,
            new SimpleWebApplicationServiceImpl(MY_SERVICE));

    final OAuthAction oAuthAction = new OAuthAction();
    oAuthAction.setConfiguration(newConfiguration());
    final Event event = oAuthAction.execute(mockRequestContext);
    assertEquals("error", event.getId());
    assertEquals(MY_THEME, mockSession.getAttribute(OAuthConstants.THEME));
    assertEquals(MY_LOCALE, mockSession.getAttribute(OAuthConstants.LOCALE));
    assertEquals(MY_METHOD, mockSession.getAttribute(OAuthConstants.METHOD));
    final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
    assertTrue(((String) flowScope.get("FacebookProviderUrl")).startsWith(
            "https://www.facebook.com/dialog/oauth?client_id=my_key&redirect_uri=http%3A%2F%2Fcasserver%2Flogin%3Foauth_provider%3DFacebookProvider&state="));
    assertEquals("/oauth10login?oauth_provider=TwitterProvider", flowScope.get("TwitterProviderUrl"));
}

From source file:org.jasig.cas.client.authentication.AuthenticationFilterTests.java

@Test
public void testGateway() throws Exception {
    final MockHttpSession session = new MockHttpSession();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final FilterChain filterChain = new FilterChain() {

        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
            // nothing to do
        }//from  w ww . java2s  . c  om
    };

    request.setSession(session);
    this.filter.setRenew(true);
    this.filter.setGateway(true);
    this.filter.doFilter(request, response, filterChain);
    assertNotNull(session.getAttribute(DefaultGatewayResolverImpl.CONST_CAS_GATEWAY));
    assertNotNull(response.getRedirectedUrl());

    final MockHttpServletResponse response2 = new MockHttpServletResponse();
    this.filter.doFilter(request, response2, filterChain);
    assertNull(session.getAttribute(DefaultGatewayResolverImpl.CONST_CAS_GATEWAY));
    assertNull(response2.getRedirectedUrl());
}

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

/**
 * @see RadiologyOrderFormController#postSaveRadiologyOrder(HttpServletRequest, Integer, Order,
 *      BindingResult)//from w  w w .j  a  va2 s  . c  o  m
 */
@Test
@Verifies(value = "should set http session attribute openmrs message to order saved and redirect to radiology order list when save study was successful", method = "postSaveRadiologyOrder(HttpServletRequest, Integer, RadiologyOrder, BindingResult)")
public void postSaveRadiologyOrder_shouldSetHttpSessionAttributeOpenmrsMessageToOrderSavedAndRedirectToRadiologyOrderListWhenSaveStudyWasSuccessful()
        throws Exception {

    //given
    RadiologyOrder mockRadiologyOrder = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrder.getStudy().setMwlStatus(MwlStatus.SAVE_OK);

    when(radiologyService.placeRadiologyOrder(mockRadiologyOrder)).thenReturn(mockRadiologyOrder);

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

    BindingResult orderErrors = mock(BindingResult.class);
    when(orderErrors.hasErrors()).thenReturn(false);

    ModelAndView modelAndView = radiologyOrderFormController.postSaveRadiologyOrder(mockRequest, null,
            mockRadiologyOrder, mockRadiologyOrder, orderErrors);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is("redirect:/module/radiology/radiologyOrder.list"));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR), is("Order.saved"));
}

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

/**
 * @see RadiologyOrderFormController#postSaveRadiologyOrder(HttpServletRequest, Integer, Order,
 *      BindingResult)//w w w . j ava2 s .  c o  m
 */
@Test
@Verifies(value = "should set http session attribute openmrs message to study performed when study performed status is in progress and request was issued by radiology scheduler", method = "postSaveRadiologyOrder(HttpServletRequest, Integer, RadiologyOrder, BindingResult)")
public void postSaveRadiologyOrder_shouldSetHttpSessionAttributeOpenmrsMessageToStudyPerformedWhenStudyPerformedStatusIsInProgressAndRequestWasIssuedByRadiologyScheduler()
        throws Exception {

    //given
    RadiologyOrder mockRadiologyOrder = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrder.getStudy().setPerformedStatus(PerformedProcedureStepStatus.IN_PROGRESS);
    User mockRadiologyScheduler = RadiologyTestData.getMockRadiologyScheduler();

    when(userContext.getAuthenticatedUser()).thenReturn(mockRadiologyScheduler);
    when(radiologyService.placeRadiologyOrder(mockRadiologyOrder)).thenReturn(mockRadiologyOrder);

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

    BindingResult orderErrors = mock(BindingResult.class);
    when(orderErrors.hasErrors()).thenReturn(false);

    ModelAndView modelAndView = radiologyOrderFormController.postSaveRadiologyOrder(mockRequest,
            mockRadiologyOrder.getPatient().getPatientId(), mockRadiologyOrder, mockRadiologyOrder,
            orderErrors);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is("module/radiology/radiologyOrderForm"));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("radiology.studyPerformed"));
}

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

/**
 * @see RadiologyOrderFormController#saveRadiologyOrder(HttpServletRequest, RadiologyOrder, BindingResult)
 * @verifies save given radiology order if valid and set http session attribute openmrs message to order saved and
 *           redirect to the new radiology order
 *///from  w  ww  .  jav  a 2 s.  co m
@Test
public void saveRadiologyOrder_shouldSaveGivenRadiologyOrderIfValidAndSetHttpSessionAttributeOpenmrsMessageToOrderSavedAndRedirectToNewRadiologyOrder()
        throws Exception {

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

    when(radiologyOrderService.placeRadiologyOrder(mockRadiologyOrder)).thenReturn(mockRadiologyOrder);

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

    BindingResult orderErrors = mock(BindingResult.class);
    when(orderErrors.hasErrors()).thenReturn(false);

    ModelAndView modelAndView = radiologyOrderFormController.saveRadiologyOrder(mockRequest, mockRadiologyOrder,
            orderErrors);

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

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

/**
 * @see RadiologyOrderFormController#postSaveRadiologyOrder(HttpServletRequest, Integer, Order,
 *      BindingResult)/*from   w w w  .  ja v a  2s. c o m*/
 */
@Test
@Verifies(value = "should set http session attribute openmrs message to order saved and redirect to patient dashboard when save study was successful and given patient id", method = "postSaveRadiologyOrder(HttpServletRequest, Integer, RadiologyOrder, BindingResult)")
public void postSaveRadiologyOrder_shouldSetHttpSessionAttributeOpenmrsMessageToOrderSavedAndRedirectToPatientDashboardWhenSaveStudyWasSuccessfulAndGivenPatientId()
        throws Exception {

    //given
    RadiologyOrder mockRadiologyOrder = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrder.getStudy().setMwlStatus(MwlStatus.SAVE_OK);

    when(radiologyService.placeRadiologyOrder(mockRadiologyOrder)).thenReturn(mockRadiologyOrder);

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

    BindingResult orderErrors = mock(BindingResult.class);
    when(orderErrors.hasErrors()).thenReturn(false);

    ModelAndView modelAndView = radiologyOrderFormController.postSaveRadiologyOrder(mockRequest,
            mockRadiologyOrder.getPatient().getPatientId(), mockRadiologyOrder, mockRadiologyOrder,
            orderErrors);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(),
            is("redirect:/patientDashboard.form?patientId=" + mockRadiologyOrder.getPatient().getPatientId()));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR), is("Order.saved"));
}

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

/**
 * @see RadiologyOrderFormController#postSaveRadiologyOrder(HttpServletRequest, Integer, Order,
 *      BindingResult)/*from w w w .  j  a v a  2s  .c o  m*/
 */
@Test
@Verifies(value = "should set http session attribute openmrs message to saved fail worklist and redirect to patient dashboard when save study was not successful and given patient id", method = "postSaveRadiologyOrder(HttpServletRequest, Integer, RadiologyOrder, BindingResult)")
public void postSaveRadiologyOrder_shouldSetHttpSessionAttributeOpenmrsMessageToSavedFailWorklistAndRedirectToPatientDashboardWhenSaveStudyWasNotSuccessfulAndGivenPatientId()
        throws Exception {

    //given
    RadiologyOrder mockRadiologyOrder = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrder.getStudy().setMwlStatus(MwlStatus.SAVE_ERR);

    when(radiologyService.placeRadiologyOrder(mockRadiologyOrder)).thenReturn(mockRadiologyOrder);

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

    BindingResult orderErrors = mock(BindingResult.class);
    when(orderErrors.hasErrors()).thenReturn(false);

    ModelAndView modelAndView = radiologyOrderFormController.postSaveRadiologyOrder(mockRequest,
            mockRadiologyOrder.getPatient().getPatientId(), mockRadiologyOrder, mockRadiologyOrder,
            orderErrors);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(),
            is("redirect:/patientDashboard.form?patientId=" + mockRadiologyOrder.getPatient().getPatientId()));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR),
            is("radiology.savedFailWorklist"));

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

    mockRadiologyOrder.getStudy().setMwlStatus(MwlStatus.SAVE_ERR);
    when(radiologyService.placeRadiologyOrder(mockRadiologyOrder)).thenReturn(mockRadiologyOrder);

    modelAndView = radiologyOrderFormController.postSaveRadiologyOrder(mockRequest,
            mockRadiologyOrder.getPatient().getPatientId(), mockRadiologyOrder, mockRadiologyOrder,
            orderErrors);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(),
            is("redirect:/patientDashboard.form?patientId=" + mockRadiologyOrder.getPatient().getPatientId()));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR),
            is("radiology.savedFailWorklist"));
}