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

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

Introduction

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

Prototype

public void addParameter(String name, String... values) 

Source Link

Document

Add an array of values for the specified HTTP parameter.

Usage

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

@Test
public void shouldSearchAndReturnConceptsThatContainsNamePartInRequest() throws Exception {
    service.updateConceptIndex(service.getConceptByUuid("15f83cd6-64e9-4e06-a5f9-364d3b14a43d"));
    MockHttpServletRequest req = request(RequestMethod.GET, getURI());
    SimpleObject result;/*from   ww  w.  ja  v a2  s. co m*/
    List<Object> hits;

    String conceptClassUuid = "3d065ed4-b0b9-4710-9a17-6d8c4fd259b7"; // DRUG
    String name = "Asp"; //ASPIRIN
    String searchType = "fuzzy";

    req.addParameter("class", conceptClassUuid);
    req.addParameter("name", name);
    req.addParameter("searchType", searchType);

    result = deserialize(handle(req));
    hits = result.get("results");

    assertThat(hits, contains(hasUuid("15f83cd6-64e9-4e06-a5f9-364d3b14a43d")));
}

From source file:org.openmrs.module.htmlformentry.ObsTagTest.java

@Test
public void shouldNotDisplayDefaultValueForEmptyTextValueInViewAndEdit() throws Exception {
    new RegressionTestHelper() {

        @Override//from  ww w.  j ava  2  s  . c om
        public String getFormName() {
            return "singleObsFormWithDefaultTextValue";
        }

        public Patient getPatient() {
            return patient;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:", "Text:" };
        }

        public void testBlankFormHtml(String html) {
            assertTrue("Should contain default text: " + html, html.contains("default text"));
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.addParameter(widgets.get("Date:"), dateAsString(new Date()));
            request.addParameter(widgets.get("Location:"), "2");
            request.addParameter(widgets.get("Provider:"), "502");
            request.addParameter(widgets.get("Text:"), "");
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertProvider(502);
            results.assertLocation(2);
            results.assertObsCreatedCount(0);
        }

        public boolean doViewEncounter() {
            return true;
        }

        public void testViewingEncounter(Encounter encounter, String html) {
            Assert.assertFalse("Should not contain default text: " + html, html.contains("default text"));
        }

        public boolean doEditEncounter() {
            return true;
        }

        public void testEditFormHtml(String html) {
            Assert.assertFalse("Should not contain default text: " + html, html.contains("default text"));
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry.ObsTagTest.java

@Test
public void shouldNotDisplayDefaultValueForEmptyDateValueInViewAndEdit() throws Exception {
    new RegressionTestHelper() {

        @Override//from  w w w . ja v  a 2 s.  co  m
        public String getFormName() {
            return "singleObsFormWithDefaultDateValue";
        }

        public Patient getPatient() {
            return patient;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:", "Obs date:" };
        }

        public void testBlankFormHtml(String html) {
            assertTrue("Should contain default date: " + html, html.contains("2011-06-10"));
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.addParameter(widgets.get("Date:"), dateAsString(new Date()));
            request.addParameter(widgets.get("Location:"), "2");
            request.addParameter(widgets.get("Provider:"), "502");
            request.addParameter(widgets.get("Obs date:"), "");
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertProvider(502);
            results.assertLocation(2);
            results.assertObsCreatedCount(0);
        }

        public boolean doViewEncounter() {
            return true;
        }

        public void testViewingEncounter(Encounter encounter, String html) {
            Assert.assertFalse("Should not contain default date: " + html, html.contains("2011-06-10"));
        }

        public boolean doEditEncounter() {
            return true;
        }

        public void testEditFormHtml(String html) {
            Assert.assertFalse("Should not contain default date: " + html, html.contains("2011-06-10"));
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry.ObsTagTest.java

@Test
public void dynamicAutocomplete_shouldVoidAllExistingObsIfEmpty() throws Exception {
    new RegressionTestHelper() {

        @Override//from w  w w .j  a va 2s .  com
        public String getFormName() {
            return "singleObsFormWithMultiAutocomplete";
        }

        public Patient getPatient() {
            return patient;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:", "Coded:" };
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.addParameter(widgets.get("Date:"), dateAsString(new Date()));
            request.addParameter(widgets.get("Location:"), "2");
            request.addParameter(widgets.get("Provider:"), "502");
            request.addParameter(widgets.get("Coded:"), "2"); // in the dynamic autocomplete, the widget value is just the count of the number of entries
            request.addParameter("w8span_0_hid", "1001");
            request.addParameter("w8span_1_hid", "1002");
        }

        @Override
        public boolean doEditEncounter() {
            return true;
        }

        @Override
        public String[] widgetLabelsForEdit() {
            return new String[] { "Coded:" };
        }

        @Override
        public void setupEditRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.setParameter(widgets.get("Coded:"), "0"); // in the dynamic autocomplete, the widget value is just the count of the number of entries
            request.removeParameter("w8span_0_hid");
            request.removeParameter("w8span_1_hid");
        }

        @Override
        public void testEditedResults(SubmissionResults results) {

            results.assertNoErrors();
            Encounter encounter = results.getEncounterCreated();

            assertThat(encounter.getAllObs(false).size(), is(0)); // no none-voided obs
            assertThat(encounter.getAllObs(true).size(), is(2)); // the existing obs should have been voided
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry.ObsTagTest.java

@Test
public void shouldNotDisplayDefaultValueForEmptyBooleanValueInViewAndEdit() throws Exception {
    new RegressionTestHelper() {

        @Override/*from w  w  w. j  a  v a2 s.c o  m*/
        public String getFormName() {
            return "singleObsFormWithDefaultBooleanValue";
        }

        public Patient getPatient() {
            return patient;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:", "Boolean:" };
        }

        public void testBlankFormHtml(String html) {
            assertTrue("Should contain default boolean: " + html, html.contains(
                    "<input type=\"checkbox\" id=\"w8\" name=\"w8\" value=\"true\" checked=\"true\"/>"));
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.addParameter(widgets.get("Date:"), dateAsString(new Date()));
            request.addParameter(widgets.get("Location:"), "2");
            request.addParameter(widgets.get("Provider:"), "502");
            request.addParameter(widgets.get("Boolean:"), "");
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertProvider(502);
            results.assertLocation(2);
            results.assertObsCreatedCount(0);
        }

        public boolean doViewEncounter() {
            return true;
        }

        public void testViewingEncounter(Encounter encounter, String html) {
            assertTrue("View should not contain default boolean: " + html,
                    html.contains("Boolean: <span class=\"emptyValue\">"));
        }

        public boolean doEditEncounter() {
            return true;
        }

        public void testEditFormHtml(String html) {
            assertTrue("Edit should not contain default boolean: " + html,
                    html.contains("<input type=\"checkbox\" id=\"w8\" name=\"w8\" value=\"true\"/>"));
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry.ObsTagTest.java

@Test
public void shouldNotDisplayDefaultValueForEmptyCodedValueInViewAndEdit() throws Exception {
    new RegressionTestHelper() {

        @Override/*  w ww .  j  a  v  a 2  s .  c o m*/
        public String getFormName() {
            return "singleObsFormWithDefaultCodedValue";
        }

        public Patient getPatient() {
            return patient;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:", "Coded:" };
        }

        public void testBlankFormHtml(String html) {
            assertTrue("Should contain default coded value: " + html,
                    html.contains("value=\"1002\" checked=\"true\""));
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.addParameter(widgets.get("Date:"), dateAsString(new Date()));
            request.addParameter(widgets.get("Location:"), "2");
            request.addParameter(widgets.get("Provider:"), "502");
            request.addParameter(widgets.get("Coded:"), "");
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertProvider(502);
            results.assertLocation(2);
            results.assertObsCreatedCount(0);
        }

        public boolean doViewEncounter() {
            return true;
        }

        public void testViewingEncounter(Encounter encounter, String html) {
            assertTrue("View should not contain default coded value: " + html,
                    html.contains("Coded: <span class=\"emptyValue\">"));
        }

        public boolean doEditEncounter() {
            return true;
        }

        public void testEditFormHtml(String html) {
            Assert.assertFalse("Edit should not contain default coded value: " + html,
                    html.contains("checked=\"true\""));
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry.ObsTagTest.java

/**
 * verifies whether the previous obs is correctly voided when a new obs created, with changing the numeric value
 * of checkbox, tests the changing of numeric value too.
 * @throws Exception/*from   w ww .ja va  2  s . c om*/
 */
@Test
public void shouldVoidPreviousObsWhenEditNumericValueCheckbox() throws Exception {
    new RegressionTestHelper() {

        Date date = new Date();

        @Override
        public String getFormName() {
            return "SingleObsFormWithNumericCheckbox";
        }

        public Patient getPatient() {
            return patient;
        }

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Provider:", "NumericValue:" };
        }

        @Override
        public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.addParameter(widgets.get("Date:"), dateAsString(date));
            request.addParameter(widgets.get("Location:"), "2");
            request.addParameter(widgets.get("Provider:"), "502");
            request.addParameter(widgets.get("NumericValue:"), "8");
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertProvider(502);
            results.assertLocation(2);
            results.assertObsCreatedCount(1);
            results.assertObsCreated(1, "8.0");
        }

        @Override
        public boolean doEditEncounter() {
            return true;
        }

        @Override
        public String[] widgetLabelsForEdit() {
            return new String[] { "Date:", "NumericValue:" };
        }

        @Override
        public void setupEditRequest(MockHttpServletRequest request, Map<String, String> widgets) {
            request.setParameter(widgets.get("NumericValue:"), "4");
        }

        @Override
        public void testEditedResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertObsCreatedCount(1);
            results.assertObsVoided(1, "8.0");
            results.assertObsCreated(1, "4.0");

        }
    }.run();
}

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

@Test
public void shouldSearchAndReturnConceptsThatEqualsToClassAndName() throws Exception {
    service.updateConceptIndex(service.getConceptByUuid("15f83cd6-64e9-4e06-a5f9-364d3b14a43d"));
    MockHttpServletRequest req = request(RequestMethod.GET, getURI());
    SimpleObject result;/* w w  w  . j  a va 2  s .c o m*/
    List<Object> hits;

    String conceptClassUuid = "3d065ed4-b0b9-4710-9a17-6d8c4fd259b7"; // DRUG
    String name = "Aspirin"; //ASPIRIN
    String searchType = "equals";

    req.addParameter("class", conceptClassUuid);
    req.addParameter("name", name);
    req.addParameter("searchType", searchType);

    result = deserialize(handle(req));
    hits = result.get("results");

    assertThat(hits, contains(hasUuid("15f83cd6-64e9-4e06-a5f9-364d3b14a43d")));

    //Should not find it when it has partial name:
    name = "Asp";
    req.setParameter("name", name);

    result = deserialize(handle(req));
    hits = result.get("results");

    assertThat(hits, is(empty()));
}

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

@Test
public void shouldNotReturnAnythingWhenConceptDoesntMatchClass() throws Exception {
    service.updateConceptIndex(service.getConceptByUuid("a09ab2c5-878e-4905-b25d-5784167d0216"));
    MockHttpServletRequest req = request(RequestMethod.GET, getURI());
    SimpleObject result;/* w  w w  .  j a  va2  s. com*/
    List<Object> hits;

    String conceptClassUuid = "97097dd9-b092-4b68-a2dc-e5e5be961d42"; // TEST
    String name = "CD4 COU"; //CD4 COUNT
    String searchType = "fuzzy";

    req.addParameter("class", conceptClassUuid);
    req.addParameter("name", name);
    req.addParameter("searchType", searchType);

    result = deserialize(handle(req));
    hits = result.get("results");

    assertThat(hits, contains(hasUuid("a09ab2c5-878e-4905-b25d-5784167d0216")));

    //Should not find it when it has partial name:
    conceptClassUuid = "3d065ed4-b0b9-4710-9a17-6d8c4fd259b7"; // DRUG
    req.setParameter("class", conceptClassUuid);

    result = deserialize(handle(req));
    hits = result.get("results");

    assertThat(hits, is(empty()));
}

From source file:nl.surfnet.coin.selfservice.interceptor.AuthorityScopeInterceptorTest.java

@Test
public void token_session_does_not_equal_request_param_token() throws Exception {
    ModelAndView modelAndView = buildSecurityContext(ROLE_DASHBOARD_ADMIN);

    MockHttpServletRequest request = new MockHttpServletRequest();
    interceptor.postHandle(request, null, null, modelAndView);

    // first check if the token is generated and stored in session and modelMap
    String token = (String) modelAndView.getModelMap().get(TOKEN_CHECK);
    assertNotNull(token);/*from  w w  w  .java 2 s .com*/

    String sessionToken = (String) request.getSession(false).getAttribute(TOKEN_CHECK);
    assertNotNull(token);
    assertEquals(token, sessionToken);

    // now check if the prehandle checks the token if the method is a POST
    request = new MockHttpServletRequest();
    request.setMethod(RequestMethod.POST.name());
    try {
        interceptor.preHandle(request, null, null);
        fail("Expected security exception");
    } catch (Exception e) {
    }

    // now check if the prehandle checks the token if the method is a POST
    request = new MockHttpServletRequest();
    request.addParameter(TOKEN_CHECK, sessionToken);
    request.getSession().setAttribute(TOKEN_CHECK, sessionToken);
    request.setMethod(RequestMethod.POST.name());

    assertTrue(interceptor.preHandle(request, null, null));

}