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.kenyaemr.rest.AccessReportsByRestWebServiceTest.java

@Test
public void shouldListDataSetDefinitionsByWebService() throws Exception {
    // equivalent to doing "GET .../datasetdefinition"
    SimpleObject result = dsdController.getAll(new MockHttpServletRequest(), new MockHttpServletResponse());

    List<SimpleObject> simpleDSDs = (List<SimpleObject>) result.get("results");
    Assert.assertNotNull(simpleDSDs);//from w  w w.  ja  v a2  s  . c  om

    for (SimpleObject simpleDSD : simpleDSDs) {
        Assert.assertNotNull(simpleDSD);
        Assert.assertNotNull(simpleDSD.get("uuid"));
        Assert.assertNotNull(simpleDSD.get("display"));
        Assert.assertNotNull(simpleDSD.get("links"));
    }

    //printJson(result);
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.DrugControllerTest.java

@Before
public void before() {
    this.service = Context.getConceptService();
    this.controller = new DrugController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
}

From source file:org.openmrs.web.controller.encounter.EncounterTypeFormControllerTest.java

@Test
public void shouldNotDeleteEncounterTypeWhenEncounterTypesAreLocked() throws Exception {
    // dataset to lock encounter types
    executeDataSet("org/openmrs/web/encounter/include/EncounterTypeFormControllerTest.xml");

    EncounterService es = Context.getEncounterService();

    EncounterTypeFormController controller = (EncounterTypeFormController) applicationContext
            .getBean("encounterTypeForm");
    controller.setApplicationContext(applicationContext);
    controller.setSuccessView("index.htm");
    controller.setFormView("EncounterType.form");

    // setting up the request and doing an initial "get" equivalent to the user loading the page for the first time
    MockHttpServletRequest request = new MockHttpServletRequest("GET",
            "/admin/encounters/encounterType.form?encounterTypeId=1");
    request.setSession(new MockHttpSession(null));
    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);

    // set this to be a page submission
    request.setMethod("POST");

    request.addParameter("action", "Delete EncounterType"); // so that the form is processed

    // send the parameters to the controller
    ModelAndView mav = controller.handleRequest(request, response);

    Assert.assertEquals("The purge attempt should have failed!", "EncounterType.form", mav.getViewName());
    Assert.assertSame(controller.getFormView(), mav.getViewName());
    Assert.assertNotNull(es.getEncounterType(1));
}

From source file:com.surevine.alfresco.audit.integration.ViewDiscussionTopicTest.java

/**
 * Test sunny day scenario./*from   www  .  j  a v a  2  s.  c  o  m*/
 */
@Test
public void testSuccessfulViewing() {

    String discussionTopic = "post-1288169634961_839";
    try {

        mockRequest.setRequestURI("/alfresco/s/api/forum/post/site/mytestsite/discussions/" + discussionTopic);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();
        FilterChain mockChain = new MockFilterChain();

        springAuditFilterBean.doFilter(mockRequest, mockResponse, mockChain);

        Auditable audited = getSingleAuditedEvent();

        verifyGenericAuditMetadata(audited);
        assertEquals(discussionTopic, audited.getSource());
        assertEquals(AbstractAuditEventListener.NO_VERSION_STRING, audited.getVersion());

    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }

}

From source file:org.openmrs.web.controller.form.FormFormControllerTest.java

@Test
public void shouldNotSaveAFormWhenFormsAreLocked() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/admin/forms/formEdit.form?formId=1");
    request.setSession(new MockHttpSession(null));
    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);

    request.addParameter("name", "TRUNK");
    request.addParameter("version", "1");
    request.addParameter("action", "Form.save");
    request.setContentType("application/x-www-form-urlencoded");

    ModelAndView mav = controller.handleRequest(request, response);
    Assert.assertEquals("The save attempt should have failed!", "index.htm", mav.getViewName());
    Assert.assertNotEquals("formEdit.form", mav.getViewName());
    Assert.assertSame(controller.getFormView(), mav.getViewName());
    Assert.assertNotNull(formService.getForm(1));
}

From source file:com.thoughtworks.go.server.web.FileViewTest.java

@Before
public void setUp() throws Exception {
    temporaryFolder.create();//from   w  ww.java2  s .  c  o  m
    mockRequest = new MockHttpServletRequest();
    mockResponse = new MockHttpServletResponse();
    mockServletContext = mock(ServletContext.class);
    view = new FileView();
    view.setServletContext(mockServletContext);
    file = temporaryFolder.newFile("file.txt");
    FileUtils.writeStringToFile(file, "hello", UTF_8);
}

From source file:org.jasig.cas.web.view.Cas10ResponseViewTests.java

public void testSuccessView() throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.view.setSuccessResponse(true);
    this.view.render(this.model, new MockHttpServletRequest(), response);
    assertEquals("yes\ntest\n", response.getContentAsString());
}

From source file:ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilderTest.java

@SuppressWarnings("unchecked")
@Test//from www .  j  a v a 2 s . c  o m
public void testBuilder() {

    MockHttpServletRequest request = createRequest();

    MockHttpServletResponse servletResponse = new MockHttpServletResponse();
    ExtDirectResponseBuilder.create(request, servletResponse).addResultProperty("additionalProperty", 11)
            .buildAndWrite();

    ExtDirectResponse response = ControllerUtil.readDirectResponse(servletResponse.getContentAsByteArray());
    assertThat(response.getAction()).isEqualTo("action");
    assertThat(response.getMethod()).isEqualTo("method");
    assertThat(response.getType()).isEqualTo("type");
    assertThat(response.getTid()).isEqualTo(1);

    assertThat(response.getResult()).isNotNull();
    assertThat(response.getWhere()).isNull();
    assertThat(response.getMessage()).isNull();

    Map<String, Object> data = (Map<String, Object>) response.getResult();
    assertThat(data).hasSize(2);
    assertThat(data.get("additionalProperty")).isEqualTo(11);
    assertThat(data.get("success")).isEqualTo(Boolean.TRUE);

    servletResponse = new MockHttpServletResponse();
    ExtDirectResponseBuilder.create(request, servletResponse).unsuccessful()
            .addResultProperty("additionalProperty", 9).buildAndWrite();
    response = ControllerUtil.readDirectResponse(servletResponse.getContentAsByteArray());
    data = (Map<String, Object>) response.getResult();
    assertThat(data).hasSize(2);
    assertThat(data.get("additionalProperty")).isEqualTo(9);
    assertThat(data.get("success")).isEqualTo(Boolean.FALSE);
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonAddressControllerTest.java

@Before
public void before() {
    this.service = Context.getPersonService();
    this.controller = new PersonAddressController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.LocationControllerTest.java

@Before
public void before() {
    this.service = Context.getLocationService();
    this.controller = new LocationController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
}