List of usage examples for org.springframework.mock.web MockHttpServletRequest addParameter
public void addParameter(String name, String... values)
From source file:org.openmrs.web.controller.concept.ConceptProposalFormControllerTest.java
/** * @see ConceptProposalFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException) *///w ww . j a va2s .c om @Test @Verifies(value = "should work properly for country locales", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)") public void onSubmit_shouldWorkProperlyForCountryLocales() throws Exception { executeDataSet("org/openmrs/api/include/ConceptServiceTest-proposals.xml"); ConceptService cs = Context.getConceptService(); final Integer conceptproposalId = 5; ConceptProposal cp = cs.getConceptProposal(conceptproposalId); Concept conceptToMap = cs.getConcept(4); Locale locale = new Locale("en", "GB"); Assert.assertFalse(conceptToMap.hasName(cp.getOriginalText(), locale)); ConceptProposalFormController controller = (ConceptProposalFormController) applicationContext .getBean("conceptProposalForm"); controller.setApplicationContext(applicationContext); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(new MockHttpSession(null)); request.setMethod("POST"); request.addParameter("conceptProposalId", conceptproposalId.toString()); request.addParameter("finalText", cp.getOriginalText()); request.addParameter("conceptId", conceptToMap.getConceptId().toString()); request.addParameter("conceptNamelocale", locale.toString()); request.addParameter("action", ""); request.addParameter("actionToTake", "saveAsSynonym"); HttpServletResponse response = new MockHttpServletResponse(); ModelAndView mav = controller.handleRequest(request, response); assertNotNull(mav); assertTrue(mav.getModel().isEmpty()); Assert.assertEquals(cp.getOriginalText(), cp.getFinalText()); Assert.assertTrue(conceptToMap.hasName(cp.getOriginalText(), locale)); }
From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java
/** * Test on cancel.//from w ww . j ava2s . co m */ @Test public void testOnCancel() { final String caseId = "550e4713-e22b-11d4-a716-446655440000"; final String cardId = "440e4816-e01b-74d4-a716-449955440092"; final String fileName = "doesnotcompute.file"; final String mimeType = "text/plain"; final byte[] content = "roflcopter".getBytes(); final MockHttpServletRequest request = this.newGet("/cardfileupload"); request.setRemoteUser("admin"); request.addParameter("case", caseId); request.addParameter("card", cardId); final FileUpload fileUpload = this.ctrl.showForm(request); fileUpload.setFile(content); final MockMultipartHttpServletRequest upload = new MockMultipartHttpServletRequest(); upload.setRemoteUser("admin"); final MockMultipartFile file = new MockMultipartFile("file", fileName, mimeType, content); upload.addFile(file); upload.addParameter("case", caseId); upload.addParameter("card", cardId); upload.addParameter("cancel", "Abbrechen"); final BindingResult errors = new DataBinder(fileUpload).getBindingResult(); String result = ""; try { result = this.ctrl.onSubmit(fileUpload, errors, upload); } catch (final IOException e) { Assert.fail("Should not fail on fail upload"); } Assert.assertFalse(errors.hasErrors()); Assert.assertNull(upload.getSession().getAttribute("successMessages")); Assert.assertEquals("redirect:/caseform?caseId=" + caseId + "&activeCardId=" + cardId, result); }
From source file:org.openmrs.module.logmanager.web.util.WebUtilsTest.java
@Test public void getSessionedIntParameter() { MockHttpServletRequest request = new MockHttpServletRequest(); // Should sue default when parameter doesn't exist Assert.assertEquals(123, WebUtils.getSessionedIntParameter(request, "test1", 123, "test.")); // Should use request value when it exists request.addParameter("test1", "" + 456); Assert.assertEquals(456, WebUtils.getSessionedIntParameter(request, "test1", 123, "test.")); // Should remember in session when request doesn't exist request.removeParameter("test1"); Assert.assertEquals(456, WebUtils.getSessionedIntParameter(request, "test1", 123, "test.")); }
From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java
/** * Test zero file.// ww w . j ava2s .c om */ @Test public void testZeroFile() { final String caseId = "550e4713-e22b-11d4-a716-446655440000"; final String cardId = "440e4816-e01b-74d4-a716-449955440092"; final String fileName = "doesnotcompute.file"; final String mimeType = "text/plain"; final byte[] content = "".getBytes(); final MockHttpServletRequest request = this.newGet("/cardfileupload"); request.setRemoteUser("admin"); request.addParameter("case", caseId); request.addParameter("card", cardId); final FileUpload fileUpload = this.ctrl.showForm(request); fileUpload.setFile(content); final MockMultipartHttpServletRequest upload = new MockMultipartHttpServletRequest(); upload.setRemoteUser("user"); final MockMultipartFile file = new MockMultipartFile("file", fileName, mimeType, content); upload.addFile(file); upload.addParameter("case", caseId); upload.addParameter("card", cardId); final BindingResult errors = new DataBinder(fileUpload).getBindingResult(); String result = ""; try { result = this.ctrl.onSubmit(fileUpload, errors, upload); } catch (final IOException e) { Assert.fail("Should not fail on fail upload"); } Assert.assertTrue(errors.hasErrors()); final List<ObjectError> errorList = errors.getAllErrors(); Assert.assertEquals(1, errorList.size()); Assert.assertEquals("errors.required", errorList.get(0).getCode()); Assert.assertNull(upload.getSession().getAttribute("successMessages")); Assert.assertEquals("redirect:/cardfileupload?card=" + cardId + "&case=" + caseId, result); }
From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.PersonAttributeTypeController1_8Test.java
/** * @see PersonAttributeTypeController#getPersonAttributeType(String,WebRequest) * @throws Exception// ww w.j av a2s . co m * @verifies get a full representation of a person attribute type */ @Test public void getPersonAttributeType_shouldGetAFullRepresentationOfAPersonAttributeType() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL); SimpleObject result = deserialize(handle(req)); Assert.assertNotNull(result); Util.log("Person fetched (full)", result); Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); Assert.assertNotNull(PropertyUtils.getProperty(result, "name")); Assert.assertNotNull(PropertyUtils.getProperty(result, "auditInfo")); }
From source file:org.bahmni.module.bahmnicore.web.v1_0.search.BahmniMainResourceControllerTest.java
public MockHttpServletRequest newRequest(RequestMethod method, String requestURI, Parameter... parameters) { MockHttpServletRequest request = request(method, requestURI); for (Parameter parameter : parameters) { request.addParameter(parameter.name, parameter.value); }//from ww w. j a v a2s .c o m return request; }
From source file:org.jasig.cas.web.flow.GenerateServiceTicketActionTests.java
@Test public void testTicketGrantingTicketNoTgt() throws Exception { MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(//from w ww .ja v a2 s . c om new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); WebUtils.putTicketGrantingTicketInRequestScope(context, "bleh"); assertEquals("error", this.action.execute(context).getId()); }
From source file:org.jasig.cas.web.flow.GenerateServiceTicketActionTests.java
@Test public void testTicketGrantingTicketNotTgtButGateway() throws Exception { MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(/* ww w. ja v a 2 s . co m*/ new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); request.addParameter("gateway", "true"); WebUtils.putTicketGrantingTicketInRequestScope(context, "bleh"); assertEquals("gateway", this.action.execute(context).getId()); }
From source file:no.dusken.aranea.control.TestImageController.java
@Test(expected = PageNotFoundException.class) public void testGetImage_Null() throws Exception { //mocking imageSernice.getEntity method to return null. when(imageService.getEntity(0L)).thenReturn(null); //mocking imageFile. File imageFile = mock(File.class); //mocking imageFile.exists method to return true. when(imageFile.exists()).thenReturn(true); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "123"); //mocking imageUtils.getImageFile(null) //this method should not be called, since imageService.getEntity() returned null when(imageUtils.getImageFile(null)).thenReturn(imageFile); ModelAndView mav = imgController.handleRequestInternal(request, response); assertEquals("Was supposed to return null.", null, imageFile); }
From source file:org.jasig.cas.web.flow.GenerateServiceTicketActionTests.java
@Test public void testTicketGrantingTicketFromRequest() throws Exception { MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(/*from w w w. j a v a 2s . c o m*/ new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); WebUtils.putTicketGrantingTicketInRequestScope(context, this.ticketGrantingTicket); this.action.execute(context); assertNotNull(WebUtils.getServiceTicketFromRequestScope(context)); }