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

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

Introduction

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

Prototype

public void setMethod(@Nullable String method) 

Source Link

Usage

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * This test concept form being submitted with only one name supplied
 * /* ww  w  .ja  va 2 s  .c om*/
 * @throws Exception
 */
@Test
public void shouldAddConceptWithOnlyNameSpecified() throws Exception {
    final String EXPECTED_PREFERRED_NAME = "no such concept";

    ConceptService cs = Context.getConceptService();

    // make sure the concept doesn't already exist
    Concept conceptToAdd = cs.getConceptByName(EXPECTED_PREFERRED_NAME);
    assertNull(conceptToAdd);

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("namesByLocale[en_GB].name", EXPECTED_PREFERRED_NAME);
    mockRequest.setParameter("descriptionsByLocale[en_GB].description", "some description");
    mockRequest.setParameter("concept.datatype", "1");

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, new MockHttpServletResponse());
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    Concept actualConcept = cs.getConceptByName(EXPECTED_PREFERRED_NAME);
    assertNotNull(actualConcept);
    assertEquals(EXPECTED_PREFERRED_NAME, actualConcept.getFullySpecifiedName(britishEn).getName());
    Collection<ConceptName> actualNames = actualConcept.getNames();
    assertEquals(1, actualNames.size());
    assertNull(actualConcept.getShortNameInLocale(britishEn));
    assertNotNull(actualConcept.getDescription(britishEn));
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @see ConceptFormBackingObject#getConceptFromFormData()
 *//*from w  w w.j a va 2  s  .  co m*/
@Test
@Verifies(value = "should set concept on concept answers", method = "getConceptFromFormData()")
public void getConceptFromFormData_shouldSetConceptOnConceptAnswers() throws Exception {
    int conceptId = 21;

    Concept concept = conceptService.getConcept(conceptId);
    assertNotNull(concept);

    int initialCount = concept.getAnswers().size();

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "Save Concept");
    mockRequest.setParameter("conceptId", "21");
    mockRequest.setParameter("namesByLocale[en].name", concept.getName().getName());
    mockRequest.setParameter("concept.datatype", "2");
    mockRequest.setParameter("concept.answers", "7 8 22 5089");

    ConceptFormBackingObject cb = conceptFormController.formBackingObject(mockRequest);

    // Bind the request parameters
    ServletRequestDataBinder srdb = new ServletRequestDataBinder(cb);
    conceptFormController.initBinder(mockRequest, srdb);
    srdb.bind(mockRequest);

    Concept parsedConcept = cb.getConceptFromFormData();

    assertEquals(initialCount + 1, parsedConcept.getAnswers().size());
    for (ConceptAnswer ca : parsedConcept.getAnswers()) {
        assertNotNull(ca.getConcept());
    }
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * This tests a concept form being submitted with also a short name supplied
 * //  w  ww .  ja v a  2 s .c  om
 * @throws Exception
 */
@Test
public void shouldAddConceptWithNameAndShortNameSpecified() throws Exception {
    final String EXPECTED_PREFERRED_NAME = "no such concept";
    final String EXPECTED_SHORT_NAME = "nonesuch";

    ConceptService cs = Context.getConceptService();

    // make sure the concept doesn't already exist
    Concept conceptToAdd = cs.getConceptByName(EXPECTED_PREFERRED_NAME);
    assertNull(conceptToAdd);

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("shortNamesByLocale[en_GB].name", EXPECTED_SHORT_NAME);
    mockRequest.setParameter("namesByLocale[en_GB].name", EXPECTED_PREFERRED_NAME);
    mockRequest.setParameter("descriptionsByLocale[en_GB].description", "some description");
    mockRequest.setParameter("concept.datatype", "1");

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, new MockHttpServletResponse());
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    Concept actualConcept = cs.getConceptByName(EXPECTED_PREFERRED_NAME);
    assertNotNull(actualConcept);
    Collection<ConceptName> actualNames = actualConcept.getNames();
    assertEquals(2, actualNames.size());
    assertEquals(EXPECTED_PREFERRED_NAME, actualConcept.getFullySpecifiedName(britishEn).getName());
    assertEquals(1, actualConcept.getShortNames().size());
    assertNotNull(actualConcept.getShortNameInLocale(britishEn));
    assertEquals(EXPECTED_SHORT_NAME, actualConcept.getShortNameInLocale(britishEn).getName());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @verifies not void or change attributeList if the attribute values are same
 */// www.  j  a v a 2  s  .  co m
@Test
public void shouldNotVoidOrChangeAttributeListIfTheAttributeValuesAreSame() throws Exception {
    executeDataSet(CONCEPT_ATTRIBUTES_XML);
    Concept concept = Context.getConceptService().getConcept(3);
    final int existingConceptAttributeId = 1;
    ConceptAttributeType conceptAttributeType = Context.getConceptService()
            .getConceptAttributeType(existingConceptAttributeId);
    conceptAttributeType.setName("concept joined date");

    //assert there is one concept attribute
    assertEquals(1, concept.getAttributes().size());
    assertEquals("2011-04-25", concept.getAttributes().iterator().next().getValueReference());

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setMethod("POST");
    mockHttpServletRequest.setParameter("action", "");
    mockHttpServletRequest.setParameter("conceptId", "3");
    mockHttpServletRequest.setParameter(
            "attribute." + conceptAttributeType.getId() + ".existing[" + existingConceptAttributeId + "]",
            "2011-04-25");
    BindException errors = new BindException(concept, "concept");

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");
    conceptFormController.handleRequest(mockHttpServletRequest, new MockHttpServletResponse());

    Assert.assertEquals(1, concept.getAttributes().size());
    Assert.assertFalse(((ConceptAttribute) (concept.getAttributes().toArray()[0])).getVoided());
    Assert.assertFalse(errors.hasErrors());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @see ConceptFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 *//* ww  w. java 2  s.c  om*/
@Test
@Verifies(value = "should copy numeric values into numeric concepts", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)")
public void onSubmit_shouldCopyNumericValuesIntoNumericConcepts() throws Exception {
    final Double EXPECTED_LOW_ABSOLUTE = 100.0;
    final Double EXPECTED_LOW_CRITICAL = 103.0;
    final Double EXPECTED_LOW_NORMAL = 105.0;
    final Double EXPECTED_HI_NORMAL = 110.0;
    final Double EXPECTED_HI_CRITICAL = 117.0;
    final Double EXPECTED_HI_ABSOLUTE = 120.0;

    ConceptService cs = Context.getConceptService();

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("namesByLocale[en_GB].name", "WEIGHT (KG)");
    mockRequest.setParameter("conceptId", "5089");
    mockRequest.setParameter("concept.datatype", "1");
    mockRequest.setParameter("lowAbsolute", EXPECTED_LOW_ABSOLUTE.toString());
    mockRequest.setParameter("lowCritical", EXPECTED_LOW_CRITICAL.toString());
    mockRequest.setParameter("lowNormal", EXPECTED_LOW_NORMAL.toString());
    mockRequest.setParameter("hiNormal", EXPECTED_HI_NORMAL.toString());
    mockRequest.setParameter("hiCritical", EXPECTED_HI_CRITICAL.toString());
    mockRequest.setParameter("hiAbsolute", EXPECTED_HI_ABSOLUTE.toString());

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, new MockHttpServletResponse());
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    ConceptNumeric concept = (ConceptNumeric) cs.getConcept(5089);
    Assert.assertEquals(EXPECTED_LOW_NORMAL, concept.getLowNormal());
    Assert.assertEquals(EXPECTED_HI_NORMAL, concept.getHiNormal());
    Assert.assertEquals(EXPECTED_LOW_ABSOLUTE, concept.getLowAbsolute());
    Assert.assertEquals(EXPECTED_HI_ABSOLUTE, concept.getHiAbsolute());
    Assert.assertEquals(EXPECTED_LOW_CRITICAL, concept.getLowCritical());
    Assert.assertEquals(EXPECTED_HI_CRITICAL, concept.getHiCritical());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @verifies set attributes to void if the values is not set
 *///from  ww w  .ja  va  2s.  c  om
@Test
public void shouldSetAttributesToVoidIfTheValueIsNotSet() throws Exception {
    executeDataSet(CONCEPT_ATTRIBUTES_XML);
    Concept concept = Context.getConceptService().getConcept(3);
    final int existingConceptAttributeId = 1;
    ConceptAttributeType conceptAttributeType = Context.getConceptService()
            .getConceptAttributeType(existingConceptAttributeId);
    conceptAttributeType.setName("concept type");
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    //If value is not set then void all the attributes.
    mockHttpServletRequest.setMethod("POST");
    mockHttpServletRequest.setParameter("action", "");
    mockHttpServletRequest.setParameter("conceptId", "3");
    mockHttpServletRequest.setParameter(
            "attribute." + conceptAttributeType.getId() + ".existing[" + existingConceptAttributeId + "]", "");
    BindException errors = new BindException(concept, "concept");
    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");
    conceptFormController.handleRequest(mockHttpServletRequest, new MockHttpServletResponse());

    Assert.assertEquals(1, concept.getAttributes().size());
    Assert.assertTrue(((ConceptAttribute) (concept.getAttributes().toArray()[0])).getVoided());
    Assert.assertFalse(errors.hasErrors());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @see ConceptFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 *///w ww .ja va  2  s.co  m
@Test
@Verifies(value = "should add a new Concept map to an existing concept", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)")
public void onSubmit_shouldAddANewConceptMapToAnExistingConcept() throws Exception {
    ConceptService cs = Context.getConceptService();
    int conceptId = 3;

    // make sure the concept already exists
    Concept concept = cs.getConcept(conceptId);
    assertNotNull(concept);
    int initialConceptMappingCount = concept.getConceptMappings().size();

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("conceptId", concept.getConceptId().toString());
    mockRequest.setParameter("conceptMappings[0].conceptReferenceTerm", "1");
    mockRequest.setParameter("conceptMappings[0].conceptMapType", "3");

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, response);
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    assertEquals(initialConceptMappingCount + 1, cs.getConcept(conceptId).getConceptMappings().size());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @see ConceptFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 *///from  w w  w. j  a v  a  2 s. c  om
@Test
@Verifies(value = "should ignore new concept map row if the user did not select a term", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)")
public void onSubmit_shouldIgnoreNewConceptMapRowIfTheUserDidNotSelectATerm() throws Exception {
    ConceptService cs = Context.getConceptService();
    int conceptId = 3;

    // make sure the concept already exists
    Concept concept = cs.getConcept(conceptId);
    assertNotNull(concept);
    int initialConceptMappingCount = concept.getConceptMappings().size();

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("conceptId", concept.getConceptId().toString());
    mockRequest.setParameter("conceptMappings[0].conceptReferenceTerm", "");
    mockRequest.setParameter("conceptMappings[0].conceptMapType", "");

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, response);
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    assertEquals(initialConceptMappingCount, cs.getConcept(conceptId).getConceptMappings().size());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * Test updating a concept by adding a name
 * //w  w w  .  jav  a 2s  .c  om
 * @throws Exception
 */
@Test
public void shouldUpdateConceptByAddingName() throws Exception {
    ConceptService cs = Context.getConceptService();

    // make sure the concept already exists
    Concept concept = cs.getConcept(3);
    assertNotNull(concept);

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("conceptId", concept.getConceptId().toString());
    mockRequest.setParameter("namesByLocale[en_GB].name", "new name");

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, response);
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    updateSearchIndex();

    Concept actualConcept = cs.getConceptByName("new name");
    assertNotNull(actualConcept);
    assertEquals(concept.getConceptId(), actualConcept.getConceptId());
}

From source file:org.openmrs.web.controller.ConceptFormControllerTest.java

/**
 * @see ConceptFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 *//*from   w w  w  .  ja  v a 2 s . c o m*/
@Test
@Verifies(value = "should remove a concept map from an existing concept", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)")
public void onSubmit_shouldRemoveAConceptMapFromAnExistingConcept() throws Exception {
    ConceptService cs = Context.getConceptService();
    int conceptId = 5089;

    // make sure the concept already exists and has some concept mappings
    Concept concept = cs.getConcept(conceptId);
    assertNotNull(concept);
    Collection<ConceptMap> maps = concept.getConceptMappings();
    int initialConceptMappingCount = maps.size();
    assertTrue(initialConceptMappingCount > 0);

    ConceptFormController conceptFormController = (ConceptFormController) applicationContext
            .getBean("conceptForm");
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    mockRequest.setMethod("POST");
    mockRequest.setParameter("action", "");
    mockRequest.setParameter("conceptId", concept.getConceptId().toString());
    //remove the first row
    mockRequest.setParameter("conceptMappings[0].conceptReferenceTerm", "");

    ModelAndView mav = conceptFormController.handleRequest(mockRequest, response);
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    assertEquals(initialConceptMappingCount - 1, cs.getConcept(conceptId).getConceptMappings().size());
}