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:org.openmrs.module.radiology.report.template.web.RadiologyDashboardReportTemplatesTabControllerTest.java

/**
 * @see RadiologyDashboardReportTemplatesTabController#getRadiologyReportTemplatesTab(HttpServletRequest)
 * @verifies return model and view of the radiology report templates tab
 *           page and set tab session attribute to radiology reports tab
 *           page//w w  w .  j  av  a  2  s  .  co  m
 */
@Test
public void getRadiologyReportTemplatesTab_shouldReturnModelAndViewOfTheRadiologyReportTemplatesTabPageAndSetTabSessionAttributeToRadiologyReportsTabPage()
        throws Exception {

    MockHttpSession mockSession = new MockHttpSession();
    request.setSession(mockSession);

    ModelAndView modelAndView = radiologyDashboardReportTemplatesTabController
            .getRadiologyReportTemplatesTab(request);

    verifyZeroInteractions(mrrtReportTemplateService);

    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(),
            is(RadiologyDashboardReportTemplatesTabController.RADIOLOGY_REPORT_TEMPLATES_TAB_VIEW));
    assertThat(mockSession.getAttribute(RadiologyWebConstants.RADIOLOGY_DASHBOARD_TAB_SESSION_ATTRIBUTE),
            is(RadiologyDashboardReportTemplatesTabController.RADIOLOGY_REPORT_TEMPLATES_TAB_REQUEST_MAPPING));
}

From source file:org.openmrs.module.radiology.report.template.web.RadiologyDashboardReportTemplatesTabControllerTest.java

/**
 * @see RadiologyDashboardReportTemplatesTabController#deleteMrrtReportTemplate(HttpServletRequest,
 *      org.openmrs.module.radiology.report.template.MrrtReportTemplate)
 * @verifies return a model and view of the radiology dashboard report
 *           templates page with a status message
 *///from  www  .j a v a 2s  . co m
@Test
public void deleteMrrtReportTemplate_shouldReturnAModelAndViewOfTheRadiologyDashboardReportTemplatesPageWithAStatusMessage() {

    MockHttpSession mockSession = new MockHttpSession();
    MrrtReportTemplate mockTemplate = mock(MrrtReportTemplate.class);
    request.setSession(mockSession);

    ModelAndView modelAndView = radiologyDashboardReportTemplatesTabController.deleteMrrtReportTemplate(request,
            mockTemplate);

    verify(mrrtReportTemplateService).purgeMrrtReportTemplate(mockTemplate);
    verifyNoMoreInteractions(mrrtReportTemplateService);
    assertNotNull(modelAndView);
    assertThat(modelAndView.getViewName(),
            is(RadiologyDashboardReportTemplatesTabController.RADIOLOGY_REPORT_TEMPLATES_TAB_VIEW));
    assertThat(mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR),
            is("radiology.MrrtReportTemplate.deleted"));
}

From source file:org.openmrs.module.sync.web.controller.ConfigServerFormControllerTest.java

/**
 * Test of onSaveParent method, of class ConfigServerFormController.
 * It tests if the parameters passed as arguments are correctly saved
 * @throws Exception /*from   w  ww .j a v a 2  s.co m*/
 */
@Test
public void onSaveParent_shouldSaveParentServerParameters() throws Exception {
    RemoteServer server = new RemoteServer();
    String nickname = "testnickname";
    String address = "testaddress";
    String username = "testusername";
    String password = "Testpass123";
    Boolean started = true;
    Integer repeatInterval = 60;
    HttpSession session = new MockHttpSession();

    Set<SyncServerClass> classes = Collections.emptySet();
    server.setServerClasses(classes);

    ConfigServerFormController instance = new ConfigServerFormController();
    instance.onSaveParent(nickname, address, username, password, started, repeatInterval, session, server,
            new BeanPropertyBindingResult(null, null), null, null);

    // Retrieve parent server and check its parameters
    SyncService service = Context.getService(SyncService.class);
    RemoteServer parent = service.getParentServer();

    Assert.assertNotNull(parent.getServerId());
    Assert.assertEquals(nickname, parent.getNickname());
    Assert.assertEquals(address, parent.getAddress());
    Assert.assertEquals(username, parent.getUsername());
    Assert.assertEquals(password, parent.getPassword());

    boolean taskScheduled = false;
    for (TaskDefinition task : Context.getSchedulerService().getScheduledTasks()) {
        if (task.getTaskClass().equals(SyncConstants.SCHEDULED_TASK_CLASS) && parent.getServerId().toString()
                .equals(task.getProperty(SyncConstants.SCHEDULED_TASK_PROPERTY_SERVER_ID))) {
            taskScheduled = true;
            break;
        }
    }
    Assert.assertTrue(taskScheduled);
}

From source file:org.springframework.security.web.authentication.www.BasicAuthenticationFilterTests.java

@Test
public void testInvalidBasicAuthorizationTokenIsIgnored() throws Exception {
    String token = "NOT_A_VALID_TOKEN_AS_MISSING_COLON";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
    request.setServletPath("/some_file.html");
    request.setSession(new MockHttpSession());
    final MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, response, chain);

    verify(chain, never()).doFilter(any(ServletRequest.class), any(ServletResponse.class));
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
    assertThat(response.getStatus()).isEqualTo(401);
}

From source file:org.springframework.security.web.authentication.www.BasicAuthenticationFilterTests.java

@Test
public void invalidBase64IsIgnored() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Authorization", "Basic NOT_VALID_BASE64");
    request.setServletPath("/some_file.html");
    request.setSession(new MockHttpSession());
    final MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, response, chain);
    // The filter chain shouldn't proceed
    verify(chain, never()).doFilter(any(ServletRequest.class), any(ServletResponse.class));
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
    assertThat(response.getStatus()).isEqualTo(401);
}

From source file:org.springframework.security.web.authentication.www.BasicAuthenticationFilterTests.java

@Test
public void testWrongPasswordContinuesFilterChainIfIgnoreFailureIsTrue() throws Exception {
    String token = "rod:WRONG_PASSWORD";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
    request.setServletPath("/some_file.html");
    request.setSession(new MockHttpSession());

    filter = new BasicAuthenticationFilter(manager);
    assertThat(filter.isIgnoreFailure()).isTrue();
    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, new MockHttpServletResponse(), chain);

    verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));

    // Test - the filter chain will be invoked, as we've set ignoreFailure = true
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}

From source file:org.springframework.security.web.authentication.www.BasicAuthenticationFilterTests.java

@Test
public void testWrongPasswordReturnsForbiddenIfIgnoreFailureIsFalse() throws Exception {
    String token = "rod:WRONG_PASSWORD";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
    request.setServletPath("/some_file.html");
    request.setSession(new MockHttpSession());
    assertThat(filter.isIgnoreFailure()).isFalse();
    final MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, response, chain);

    // Test - the filter chain will not be invoked, as we get a 401 forbidden response
    verify(chain, never()).doFilter(any(ServletRequest.class), any(ServletResponse.class));
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
    assertThat(response.getStatus()).isEqualTo(401);
}