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

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

Introduction

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

Prototype

MockHttpServletResponse

Source Link

Usage

From source file:org.openmrs.module.webservices.rest19ext.web.v1_0.controller.LocationAttributeTypeControllerTest.java

/**
 * @see// w w w  .j a  v a  2  s.c om
 * LocationAttributeTypeController#createLocationAttributeType(SimpleObject,WebRequest)
 * @verifies create a new LocationAttributeType
 */
@Test
public void createLocationAttributeType_shouldCreateANewLocationAttributeType() throws Exception {
    int before = Context.getLocationService().getAllLocationAttributeTypes().size();
    String json = "{ \"name\":\"Some attributeType\",\"description\":\"Attribute Type for location\",\"datatypeClassname\":\"org.openmrs.customdatatype.datatype.FreeTextDatatype\"}";
    SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
    Object locationAttributeType = new LocationAttributeTypeController().create(post, emptyRequest(),
            new MockHttpServletResponse());
    Util.log("Created location attribute type", locationAttributeType);
    Assert.assertEquals(before + 1, Context.getLocationService().getAllLocationAttributeTypes().size());
}

From source file:org.openmrs.module.webservices.rest19ext.web.v1_0.controller.ProviderAttributeTypeControllerTest.java

/**
 * @see//from www.  jav  a 2  s.  c  o m
 * ProviderAttributeTypeController#createProviderAttributeType(SimpleObject,WebRequest)
 * @verifies create a new ProviderAttributeType
 */
@Test
public void createProviderAttributeType_shouldCreateANewProviderAttributeType() throws Exception {
    int before = Context.getProviderService().getAllProviderAttributeTypes().size();
    String json = "{ \"name\":\"Some attributeType\",\"description\":\"Attribute Type for provider\",\"datatypeClassname\":\"org.openmrs.customdatatype.datatype.FreeTextDatatype\"}";
    SimpleObject post = new ObjectMapper().readValue(json, SimpleObject.class);
    Object providerAttributeType = new ProviderAttributeTypeController().create(post, emptyRequest(),
            new MockHttpServletResponse());
    Util.log("Created provider attribute type", providerAttributeType);
    Assert.assertEquals(before + 1, Context.getProviderService().getAllProviderAttributeTypes().size());
}

From source file:org.openmrs.module.feedback.web.PredefinedSubjectFormControllerTest.java

@Before
public void setUp() throws Exception {

    /* executed before the test is run */
    this.service = Context.getService(FeedbackService.class);
    this.controller = new PredefinedSubjectFormController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();

    /* this file is in the same folder of test resources where the hibernate mapping file is located */
    initializeInMemoryDatabase();//from   w  w w.ja  v a 2  s .  com
    executeDataSet("PredefinedSubjectDataset.xml");

    /* Sample data is loaded into the system */
    authenticate();
}

From source file:org.openmrs.module.owa.filter.OwaFilterTest.java

/**
 * Test that the OwaFilter class actually services from the base path defined in the global
 * property and ignore any other requests.
 */// ww w . j  a  v  a2  s  . c  o m
@Test
public void testOwaFilterUsesGlobalProperty() throws Exception {
    OwaFilter owaFilter = new OwaFilter();
    owaFilter.init(filterConfig);

    MockFilterChain mockFilterChain = new MockFilterChain();
    MockHttpServletResponse rsp = new MockHttpServletResponse();

    // First make sure that it works with the default base URL
    Context.getAdministrationService()
            .saveGlobalProperty(new GlobalProperty(AppManager.KEY_APP_BASE_URL, DEFAULT_APP_BASE_URL));

    MockHttpServletRequest req = new MockHttpServletRequest("GET", DEFAULT_APP_BASE_URI + SOME_PATH_IN_APP);
    owaFilter.doFilter(req, rsp, mockFilterChain);
    Assert.assertEquals(rsp.getStatus(), 200);
    Assert.assertEquals(FILE_SERVLET_REDIRECT_URL + SOME_PATH_IN_APP, rsp.getForwardedUrl());

    // Now try a custom base URL
    Context.getAdministrationService()
            .saveGlobalProperty(new GlobalProperty(AppManager.KEY_APP_BASE_URL, SOME_RANDOM_BASE_URL));
    mockFilterChain = new MockFilterChain();
    req = new MockHttpServletRequest("GET", SOME_RANDOM_BASE_URI + SOME_PATH_IN_APP);
    rsp = new MockHttpServletResponse();
    owaFilter.doFilter(req, rsp, mockFilterChain);
    Assert.assertEquals(rsp.getStatus(), 200);
    Assert.assertEquals(FILE_SERVLET_REDIRECT_URL + SOME_PATH_IN_APP, rsp.getForwardedUrl());

    // Ensure non-OWA base URLs are ignored
    req = new MockHttpServletRequest("GET", DEFAULT_APP_BASE_URI + SOME_PATH_IN_APP); // we can reuse this URL because the global property has been reset
    rsp = new MockHttpServletResponse();
    owaFilter.doFilter(req, rsp, mockFilterChain);
    Assert.assertEquals(rsp.getStatus(), 200);
    Assert.assertNull(rsp.getForwardedUrl());
}

From source file:org.jasig.cas.web.flow.GenerateServiceTicketActionTests.java

@Test
public void testServiceTicketFromCookie() throws Exception {
    MockRequestContext context = new MockRequestContext();
    context.getFlowScope().put("service", TestUtils.getService());
    context.getFlowScope().put("ticketGrantingTicketId", this.ticketGrantingTicket);
    MockHttpServletRequest request = new MockHttpServletRequest();
    context.setExternalContext(//w  ww .  j av a  2 s .c  om
            new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    request.addParameter("service", "service");
    request.setCookies(new Cookie[] { new Cookie("TGT", this.ticketGrantingTicket) });

    this.action.execute(context);

    assertNotNull(WebUtils.getServiceTicketFromRequestScope(context));
}

From source file:org.craftercms.security.processors.impl.CurrentAuthenticationResolvingProcessorTest.java

@Test
public void testGetAuthentication() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    Date profileLastModified = new Date();
    Cookie ticketCookie = new Cookie(SecurityUtils.TICKET_COOKIE_NAME, TICKET);
    Cookie profileLastModifiedCookie = new Cookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME,
            String.valueOf(profileLastModified.getTime()));

    request.setCookies(ticketCookie, profileLastModifiedCookie);

    Profile profile = new Profile();
    profile.setLastModified(profileLastModified);

    Authentication auth = new DefaultAuthentication(TICKET, profile);

    when(authenticationManager.getAuthentication(TICKET, false)).thenReturn(auth);

    processor.processRequest(context, chain);

    verify(chain).processRequest(context);

    Authentication newAuth = SecurityUtils.getAuthentication(request);

    assertNotNull(newAuth);//  w  w w.  j  a  va  2 s . c  o m
    assertEquals(auth.getTicket(), newAuth.getTicket());
    assertEquals(auth.getProfile().getLastModified(), newAuth.getProfile().getLastModified());
}

From source file:org.jasig.cas.web.ProxyControllerTests.java

@Test
public void testNonExistentPGT() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("pgt", "TestService");
    request.addParameter("targetService", "testDefault");

    assertTrue(this.proxyController.handleRequestInternal(request, new MockHttpServletResponse()).getModel()
            .containsKey("code"));
}

From source file:org.jasig.cas.client.session.SingleSignOutHandlerTests.java

@Before
public void setUp() throws Exception {
    handler = new SingleSignOutHandler();
    handler.setLogoutParameterName(LOGOUT_PARAMETER_NAME);
    handler.setFrontLogoutParameterName(FRONT_LOGOUT_PARAMETER_NAME);
    handler.setRelayStateParameterName(RELAY_STATE_PARAMETER_NAME);
    handler.setArtifactParameterName(ARTIFACT_PARAMETER_NAME);
    handler.setCasServerUrlPrefix(URL);
    handler.init();//from ww w.java 2s  .co m
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
}

From source file:nl.surfnet.coin.teams.service.interceptor.LoginInterceptorTest.java

@Test
public void testPreHandle() throws Exception {
    String remoteUser = "urn:collab:person:surfnet.nl:hansz";

    LoginInterceptor interceptor = new LoginInterceptor();

    OpenConextOAuthClient apiClient = mock(OpenConextOAuthClient.class);
    Person person = new Person();
    person.setId(remoteUser);//from w w  w. ja  v  a2  s .co m
    when(apiClient.getPerson(remoteUser, null)).thenReturn(person);
    MemberAttributeService memberAttributeService = mock(MemberAttributeService.class);
    when(memberAttributeService.findAttributesForMemberId(person.getId()))
            .thenReturn(new ArrayList<MemberAttribute>());
    interceptor.setMemberAttributeService(memberAttributeService);

    interceptor.setApiClient(apiClient);
    interceptor.setTeamEnvironment(new TeamEnvironment());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("REMOTE_USER", remoteUser);
    request.addHeader("coin-user-status", "member");
    MockHttpServletResponse response = new MockHttpServletResponse();
    boolean loggedIn = interceptor.preHandle(request, response, null);
    assertTrue(loggedIn);
    Assert.assertNotNull(request.getSession().getAttribute("person"));
}

From source file:org.craftercms.security.processors.impl.LogoutProcessorTest.java

@Test
public void testLogout() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(LogoutProcessor.DEFAULT_LOGOUT_METHOD,
            LogoutProcessor.DEFAULT_LOGOUT_URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);

    Profile profile = new Profile();
    profile.setUsername(USERNAME);/* ww w . j a  v a2  s . c o m*/

    Authentication auth = new DefaultAuthentication(new ObjectId().toString(), profile);

    SecurityUtils.setAuthentication(request, auth);

    processor.processRequest(context, chain);

    verify(chain, never()).processRequest(context);

    assertNull(SecurityUtils.getAuthentication(request));

    verify(logoutSuccessHandler).handle(context, auth);
}