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_8.PersonController1_8Test.java

@SuppressWarnings("unchecked")
@Test//from www. j  a v a  2 s.  c  o m
public void shouldNotShowVoidedNamesInFullRepresentation() throws Exception {
    executeDataSet("PersonControllerTest-otherPersonData.xml");
    Person person = service.getPersonByUuid(getUuid());
    assertEquals(2, person.getNames().size());
    PersonName nameToVoid = person.getNames().iterator().next();
    String nameToVoidUuid = nameToVoid.getUuid();
    if (!nameToVoid.isVoided()) {
        //void the Name
        handle(newDeleteRequest("person/" + getUuid() + "/name/" + nameToVoidUuid, new Parameter("!purge", ""),
                new Parameter("reason", "none")));
    }
    assertTrue(nameToVoid.isVoided());

    MockHttpServletRequest req = newGetRequest(getURI() + "/" + getUuid(), new Parameter(
            RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL));
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);

    SimpleObject result = deserialize(handle(req));

    List<SimpleObject> names = (List<SimpleObject>) PropertyUtils.getProperty(result, "names");
    assertEquals(1, names.size());
    assertFalse(nameToVoidUuid.equals(PropertyUtils.getProperty(names.get(0), "uuid")));
}

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

@SuppressWarnings("unchecked")
@Test/*from w  w  w  .ja  va2s  .  com*/
public void shouldNotShowVoidedAttributesInFullRepresentation() throws Exception {
    Person person = service.getPersonByUuid(getUuid());
    PersonAttribute attributeToVoid = person.getActiveAttributes().get(0);
    String attributeToVoidUuid = attributeToVoid.getUuid();
    assertEquals(3, person.getActiveAttributes().size());
    if (!attributeToVoid.isVoided()) {
        //void the attribute
        handle(newDeleteRequest("person/" + getUuid() + "/attribute/" + attributeToVoidUuid,
                new Parameter("!purge", ""), new Parameter("reason", "none")));
    }
    assertTrue(attributeToVoid.isVoided());

    MockHttpServletRequest req = newGetRequest(getURI() + "/" + getUuid(), new Parameter(
            RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL));
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);

    SimpleObject result = deserialize(handle(req));

    List<SimpleObject> attributes = (List<SimpleObject>) PropertyUtils.getProperty(result, "attributes");
    assertEquals(2, attributes.size());
    List<Object> uuids = Arrays.asList(PropertyUtils.getProperty(attributes.get(0), "uuid"),
            PropertyUtils.getProperty(attributes.get(1), "uuid"));
    assertFalse(uuids.contains(attributeToVoidUuid));
}

From source file:org.araneaframework.tests.FormConstraintTest.java

public void testFormRangeConstraint() throws Exception {

    FormWidget testForm = new FormWidget();
    testForm._getComponent().init(new MockEnviroment());

    //Adding elements to form
    FormElement lo = testForm.createElement("my date and time", new DateTimeControl(), new DateData(), false);
    FormElement hi = testForm.createElement("my date and time", new DateTimeControl(), new DateData(), false);
    FormWidget date = testForm.addSubForm("date");
    date.addElement("myDateLo", lo);
    date.addElement("myDateHi", hi);

    MockHttpServletRequest request = new MockHttpServletRequest();

    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

    //valid//from  w  w  w .  ja  va  2  s .c o m

    request.addParameter("testForm.__present", "true");
    request.addParameter("testForm.date.myDateLo.date",
            sdf.format(new java.sql.Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24)));
    request.addParameter("testForm.date.myDateHi.date", sdf.format(new java.util.Date()));

    testForm.getElement("date").setConstraint(
            new RangeConstraint((FormElement) testForm.getGenericElementByFullName("date.myDateLo"),
                    (FormElement) testForm.getGenericElementByFullName("date.myDateHi"), true));

    StandardServletInputData input = new StandardServletInputData(request);
    input.pushScope("testForm");
    testForm._getWidget().update(input);
    input.popScope();

    assertTrue("Test form must be valid after reading from request", testForm.convertAndValidate());
}

From source file:org.openmrs.module.htmlformentry19ext.IntegrationTest.java

@Test
public void encounterProviderAndRole_testSpecifyingASingleProviderForTagThatAcceptsTwo() throws Exception {
    final Date date = new Date();
    new RegressionTestHelper() {

        @Override/* w ww.j a va  2s  .  c o m*/
        protected String getXmlDatasetPath() {
            return "org/openmrs/module/htmlformentry19ext/include/";
        }

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

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

        @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("Doctors:"), "2"); // Doctor Bob
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertLocation(2);

            Map<EncounterRole, Set<Provider>> byRoles = results.getEncounterCreated().getProvidersByRoles();
            Assert.assertEquals(1, byRoles.size());

            Set<Provider> doctors = byRoles.get(Context.getEncounterService().getEncounterRole(3));
            Assert.assertEquals(1, doctors.size());
            Assert.assertEquals(new Integer(2), doctors.iterator().next().getId());
        }

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

        @Override
        public void testViewingEncounter(Encounter encounter, String html) {
            TestUtil.assertFuzzyContains("Doctor Bob", html);
            TestUtil.assertFuzzyDoesNotContain("Super User", html);
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry19ext.IntegrationTest.java

@Test
public void encounterProviderAndRole_testTagSpecifyingEncounterProviderTwiceWithDifferentRoles()
        throws Exception {
    final Date date = new Date();
    new RegressionTestHelper() {

        @Override//from  w  ww .j  a v  a  2s .c o  m
        protected String getXmlDatasetPath() {
            return "org/openmrs/module/htmlformentry19ext/include/";
        }

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

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

        @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("Doctor:"), "2"); // Doctor Bob
            request.addParameter(widgets.get("Nurse:"), "1"); // Superuser
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertLocation(2);
            Map<EncounterRole, Set<Provider>> byRoles = results.getEncounterCreated().getProvidersByRoles();
            Assert.assertEquals(2, byRoles.size());
            Set<Provider> doctors = byRoles.get(Context.getEncounterService().getEncounterRole(3));
            Set<Provider> nurses = byRoles.get(Context.getEncounterService().getEncounterRole(2));
            Assert.assertEquals(1, doctors.size());
            Assert.assertEquals(1, nurses.size());
            Assert.assertEquals(2, (int) doctors.iterator().next().getProviderId());
            Assert.assertEquals(1, (int) nurses.iterator().next().getProviderId());
        }

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

        @Override
        public void testViewingEncounter(Encounter encounter, String html) {
            TestUtil.assertFuzzyEquals("Date:" + Context.getDateFormat().format(date)
                    + " Location:Xanadu Doctor:Doctor Bob, M.D. Nurse:Super User", html);
        }

    }.run();
}

From source file:org.openmrs.module.htmlformentry19ext.IntegrationTest.java

@Test
public void encounterProviderAndRole_testTagSpecifyingEncounterProviderTwiceWithSameRole() throws Exception {
    final Date date = new Date();
    new RegressionTestHelper() {

        @Override/*www.ja  v a2  s. co  m*/
        protected String getXmlDatasetPath() {
            return "org/openmrs/module/htmlformentry19ext/include/";
        }

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

        @Override
        public String[] widgetLabels() {
            return new String[] { "Date:", "Location:", "Doctors:", "Doctors:!!1" };
        }

        @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("Doctors:"), "2"); // Doctor Bob
            request.addParameter(widgets.get("Doctors:!!1"), "1"); // Superuser  (hack to reference by widget id, but no label for this widget
        }

        @Override
        public void testResults(SubmissionResults results) {
            results.assertNoErrors();
            results.assertEncounterCreated();
            results.assertLocation(2);

            Map<EncounterRole, Set<Provider>> byRoles = results.getEncounterCreated().getProvidersByRoles();
            Assert.assertEquals(1, byRoles.size());

            Set<Provider> doctors = byRoles.get(Context.getEncounterService().getEncounterRole(3));
            Assert.assertEquals(2, doctors.size());

            // we can't guarantee the order of providers, but make sure both providers are now present
            Set<Integer> providerIds = new HashSet<Integer>();
            for (Provider doctor : doctors) {
                providerIds.add(doctor.getId());
            }
            Assert.assertEquals(2, providerIds.size());
            Assert.assertTrue(providerIds.contains(1));
            Assert.assertTrue(providerIds.contains(2));
        }

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

        @Override
        public void testViewingEncounter(Encounter encounter, String html) {
            TestUtil.assertFuzzyContains("Doctor Bob", html);
            TestUtil.assertFuzzyContains("Super User", html);
        }

    }.run();
}

From source file:org.cloudfoundry.identity.uaa.security.web.UaaRequestMatcherTests.java

private MockHttpServletRequest request(String path, String accept, String... parameters) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/ctx");
    request.setRequestURI("/ctx" + path);
    if (accept != null) {
        request.addHeader("Accept", accept);
    }//from   w w  w  .  java2  s  . co m
    for (int i = 0; i < parameters.length; i += 2) {
        String key = parameters[i];
        String value = parameters[i + 1];
        request.addParameter(key, value);
    }
    return request;
}

From source file:org.openmrs.module.htmlformentryui.fragment.controller.htmlform.EnterHtmlFormFragmentControllerComponentTest.java

@Test
public void testSubmittingHtmlFormDefinedInUiResourceShouldCreateOpenVisit() throws Exception {
    // first, ensure the form is created and persisted, by calling the controller display method
    testDefiningAnHtmlFormInUiResource();
    HtmlForm hf = htmlFormEntryService.getHtmlFormByForm(formService.getFormByUuid("form-uuid"));

    // make "Hippocrates" a provider
    Provider provider = new Provider();
    provider.setPerson(personService.getPerson(502));
    providerService.saveProvider(provider);

    Patient patient = patientService.getPatient(8);
    assertThat(encounterService.getEncountersByPatient(patient).size(), is(0));

    Date date = new DateMidnight().toDate();
    String dateString = new SimpleDateFormat("yyyy-MM-dd").format(date);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("w2", "70"); // weight in kg
    request.addParameter("w5", dateString); // date
    request.addParameter("w3hours", "0");
    request.addParameter("w3minutes", "0");
    request.addParameter("w3seconds", "0");
    request.addParameter("w7", "2"); // location = Xanadu
    request.addParameter("w9", "502"); // provider = Hippocrates

    SimpleObject result = controller.submit(sessionContext, patient, hf, null, null, true, null, adtService,
            featureToggles, ui, request);
    assertThat((Boolean) result.get("success"), is(Boolean.TRUE));
    assertThat(encounterService.getEncountersByPatient(patient).size(), is(1));
    Encounter created = encounterService.getEncountersByPatient(patient).get(0);

    assertNotNull(created.getVisit());/*from   ww w . j  a v  a 2s.  c om*/
    assertThat(created.getEncounterDatetime(), DateMatchers.within(1, TimeUnit.SECONDS, new Date()));
    assertThat(created.getVisit().getStartDatetime(), DateMatchers.within(1, TimeUnit.SECONDS, new Date()));
    assertNull(created.getVisit().getStopDatetime());
}

From source file:org.openmrs.module.htmlformentryui.fragment.controller.htmlform.EnterHtmlFormFragmentControllerComponentTest.java

@Test
public void testSubmittingHtmlFormDefinedInUiResourceShouldAssociateWithExistingVisit() throws Exception {
    // first, ensure the form is created and persisted, by calling the controller display method
    testDefiningAnHtmlFormInUiResource();
    HtmlForm hf = htmlFormEntryService.getHtmlFormByForm(formService.getFormByUuid("form-uuid"));

    // make "Hippocrates" a provider
    Provider provider = new Provider();
    provider.setPerson(personService.getPerson(502));
    providerService.saveProvider(provider);

    Patient patient = patientService.getPatient(8);
    assertThat(encounterService.getEncountersByPatient(patient).size(), is(0));

    Visit visit = visitService.getVisit(1001);

    Date date = new DateMidnight(2012, 1, 20).toDate();
    String dateString = new SimpleDateFormat("yyyy-MM-dd").format(date);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("w2", "70"); // weight in kg
    request.addParameter("w5", dateString); // date
    request.addParameter("w3hours", "0");
    request.addParameter("w3minutes", "0");
    request.addParameter("w3seconds", "0");
    request.addParameter("w7", "2"); // location = Xanadu
    request.addParameter("w9", "502"); // provider = Hippocrates

    SimpleObject result = controller.submit(sessionContext, patient, hf, null, visit, true, null, adtService,
            featureToggles, ui, request);
    assertThat((Boolean) result.get("success"), is(Boolean.TRUE));
    assertThat(encounterService.getEncountersByPatient(patient).size(), is(1));
    Encounter created = encounterService.getEncountersByPatient(patient).get(0);

    assertNotNull(created.getVisit());/*from   w w  w .j a v  a2s . c  o m*/
    assertThat(created.getVisit(), is(visit));
    assertThat(created.getEncounterDatetime(), is(visit.getStartDatetime())); // make sure the encounter date has been shifted to match the visit start time of 10:10:10
}

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

@SuppressWarnings("unchecked")
@Test//  w  ww .  ja va  2s.  c  om
public void shouldNotShowVoidedNamesInFullRepresentation() throws Exception {
    executeDataSet("PersonControllerTest-otherPersonData.xml");
    Person person = service.getPersonByUuid(getUuid());
    assertEquals(4, person.getNames().size());
    PersonName nameToVoid = person.getNames().iterator().next();
    String nameToVoidUuid = nameToVoid.getUuid();
    if (!nameToVoid.isVoided()) {
        //void the Name
        handle(newDeleteRequest("person/" + getUuid() + "/name/" + nameToVoidUuid, new Parameter("!purge", ""),
                new Parameter("reason", "none")));
    }
    assertTrue(nameToVoid.isVoided());

    MockHttpServletRequest req = newGetRequest(getURI() + "/" + getUuid(), new Parameter(
            RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL));
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);

    SimpleObject result = deserialize(handle(req));

    List<SimpleObject> names = (List<SimpleObject>) PropertyUtils.getProperty(result, "names");
    assertEquals(3, names.size());
    assertFalse(nameToVoidUuid.equals(PropertyUtils.getProperty(names.get(0), "uuid")));
}