Example usage for org.springframework.mock.web MockHttpServletRequest setSession

List of usage examples for org.springframework.mock.web MockHttpServletRequest setSession

Introduction

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

Prototype

public void setSession(HttpSession session) 

Source Link

Usage

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

/**
 * @see RadiologyOrderFormController#postSaveRadiologyOrder(HttpServletRequest, Integer, Order,
 *      BindingResult)/*  w  ww.j av a 2  s  .  co 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"));
}

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

/**
 * @see RadiologyOrderFormController#postDiscontinueRadiologyOrder(HttpServletRequest,
 *      HttpServletResponse, Order, String, Date)
 */// w w  w . jav  a  2s  .  com
@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.flow.OAuthActionTests.java

@Test
public void testFinishAuthentication() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameter(OAuthConstants.OAUTH_PROVIDER, "FacebookProvider");

    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute(OAuthConstants.THEME, MY_THEME);
    mockSession.setAttribute(OAuthConstants.LOCALE, MY_LOCALE);
    mockSession.setAttribute(OAuthConstants.METHOD, MY_METHOD);
    final Service service = new SimpleWebApplicationServiceImpl(MY_SERVICE);
    mockSession.setAttribute(OAuthConstants.SERVICE, service);
    mockRequest.setSession(mockSession);

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

    final MockRequestContext mockRequestContext = new MockRequestContext();
    mockRequestContext.setExternalContext(servletExternalContext);

    final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class);

    final OAuthAction oAuthAction = new OAuthAction();
    oAuthAction.setConfiguration(newConfiguration());
    oAuthAction.setCentralAuthenticationService(centralAuthenticationService);
    final Event event = oAuthAction.execute(mockRequestContext);
    assertEquals("success", event.getId());
    assertEquals(MY_THEME, mockRequest.getAttribute(OAuthConstants.THEME));
    assertEquals(MY_LOCALE, mockRequest.getAttribute(OAuthConstants.LOCALE));
    assertEquals(MY_METHOD, mockRequest.getAttribute(OAuthConstants.METHOD));
    final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
    assertEquals(service, flowScope.get(OAuthConstants.SERVICE));
}

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

/**
 * @see RadiologyOrderFormController#postDiscontinueRadiologyOrder(HttpServletRequest,
 *      HttpServletResponse, Order, String, Date)
 *///from  w  w w . ja v  a 2s .co m
@Test
@Verifies(value = "should not redirect if discontinuation failed in pacs", method = "postDiscontinueRadiologyOrder(HttpServletRequest, HttpServletResponse, Order, String, Date)")
public void postDiscontinueRadiologyOrder_shouldNotRedirectIfDiscontinuationFailedInPacs() throws Exception {
    //given
    RadiologyOrder mockRadiologyOrderToDiscontinue = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrderToDiscontinue.getStudy().setMwlStatus(MwlStatus.DISCONTINUE_ERR);
    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.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(orderService.discontinueOrder(mockRadiologyOrderToDiscontinue, discontinueReason, discontinueDate,
            mockRadiologyOrderToDiscontinue.getOrderer(), mockRadiologyOrderToDiscontinue.getEncounter()))
                    .thenReturn(mockDiscontinuationOrder);

    ModelAndView modelAndView = radiologyOrderFormController.postDiscontinueRadiologyOrder(mockRequest, null,
            mockRadiologyOrderToDiscontinue, mockDiscontinuationOrder);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is("module/radiology/radiologyOrderForm"));

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

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

    assertNotNull(mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("radiology.failWorklist"));
}

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

/**
 * @see RadiologyOrderFormController#postDiscontinueRadiologyOrder(HttpServletRequest,
 *      HttpServletResponse, Order, String, Date)
 *///from  ww w  .  ja  v  a  2s . c  o  m
@Test
@Verifies(value = "should not redirect if discontinuation failed through date in the future", method = "postDiscontinueRadiologyOrder(HttpServletRequest, HttpServletResponse, Order, String, Date)")
public void postDiscontinueRadiologyOrder_shouldNotRedirectIfDiscontinuationFailedThroughDateInTheFuture()
        throws Exception {
    //given
    RadiologyOrder mockRadiologyOrderToDiscontinue = RadiologyTestData.getMockRadiologyOrder1();
    mockRadiologyOrderToDiscontinue.getStudy().setMwlStatus(MwlStatus.DISCONTINUE_OK);
    String discontinueReason = "Wrong Procedure";
    Date discontinueDate = new Date();
    APIException apiException = new APIException("Discontinue date cannot be in the future");

    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())).thenThrow(apiException);

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

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(), is("module/radiology/radiologyOrderForm"));

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

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

    assertNotNull(mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR));
    assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("Discontinue date cannot be in the future"));
}

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

@Test
public void testIgnorePatternsWithExactMatching() throws Exception {
    final AuthenticationFilter f = new AuthenticationFilter();
    final MockServletContext context = new MockServletContext();
    context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL);

    final URL url = new URL(CAS_SERVICE_URL + "?param=valueToIgnore");

    context.addInitParameter("ignorePattern", url.toExternalForm());
    context.addInitParameter("ignoreUrlPatternType", "EXACT");
    context.addInitParameter("service", CAS_SERVICE_URL);
    f.init(new MockFilterConfig(context));

    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme(url.getProtocol());
    request.setServerName(url.getHost());
    request.setServerPort(url.getPort());
    request.setQueryString(url.getQuery());
    request.setRequestURI(url.getPath());

    final MockHttpSession session = new MockHttpSession();
    request.setSession(session);

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final FilterChain filterChain = new FilterChain() {
        public void doFilter(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
        }/*from w  w w  .ja  va2  s.c om*/
    };

    f.doFilter(request, response, filterChain);
    assertNull(response.getRedirectedUrl());
}

From source file:org.jasig.cas.support.pac4j.web.flow.ClientActionTests.java

@Test
public void verifyFinishAuthentication() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");

    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.setAttribute(ClientAction.THEME, MY_THEME);
    mockSession.setAttribute(ClientAction.LOCALE, MY_LOCALE);
    mockSession.setAttribute(ClientAction.METHOD, MY_METHOD);
    final Service service = new SimpleWebApplicationServiceImpl(MY_SERVICE);
    mockSession.setAttribute(ClientAction.SERVICE, service);
    mockRequest.setSession(mockSession);

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

    final MockRequestContext mockRequestContext = new MockRequestContext();
    mockRequestContext.setExternalContext(servletExternalContext);

    final FacebookClient facebookClient = new MockFacebookClient();
    final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);

    final TicketGrantingTicket tgt = new TicketGrantingTicketImpl(TGT_ID, mock(Authentication.class),
            mock(ExpirationPolicy.class));
    final CentralAuthenticationService casImpl = mock(CentralAuthenticationService.class);
    when(casImpl.createTicketGrantingTicket(any(Credential.class))).thenReturn(tgt);
    final ClientAction action = new ClientAction(casImpl, clients);
    final Event event = action.execute(mockRequestContext);
    assertEquals("success", event.getId());
    assertEquals(MY_THEME, mockRequest.getAttribute(ClientAction.THEME));
    assertEquals(MY_LOCALE, mockRequest.getAttribute(ClientAction.LOCALE));
    assertEquals(MY_METHOD, mockRequest.getAttribute(ClientAction.METHOD));
    assertEquals(MY_SERVICE, mockRequest.getAttribute(ClientAction.SERVICE));
    final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
    final MutableAttributeMap requestScope = mockRequestContext.getRequestScope();
    assertEquals(service, flowScope.get(ClientAction.SERVICE));
    assertEquals(TGT_ID, flowScope.get(TGT_NAME));
    assertEquals(TGT_ID, requestScope.get(TGT_NAME));
}

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

@Test
public void testRedirect() 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   ww w.  j  a v a  2 s.c om*/
    };

    request.setSession(session);
    this.filter.doFilter(request, response, filterChain);

    assertEquals(CAS_LOGIN_URL + "?service=" + URLEncoder.encode(CAS_SERVICE_URL, "UTF-8"),
            response.getRedirectedUrl());
}

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

@Test
public void testAssertion() 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
        }// w w w .  ja v  a2  s .com
    };

    request.setSession(session);
    session.setAttribute(AbstractCasFilter.CONST_CAS_ASSERTION, new AssertionImpl("test"));
    this.filter.doFilter(request, response, filterChain);

    assertNull(response.getRedirectedUrl());
}

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
        }//  www  . j  av  a  2  s  . c  o  m
    };

    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());
}