List of usage examples for org.springframework.mock.web MockHttpServletRequest addParameter
public void addParameter(String name, String... values)
From source file:no.dusken.aranea.control.TestImageController.java
@Test public void testGetImage() throws Exception { //creating image object. Image image = new Image(); image.setID(123L);/*from w w w .j a va2 s.c o m*/ //mocking imageSernice.getEntity method to return image object I just created. when(imageService.getEntity(123L)).thenReturn(image); //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(Image) when(imageUtils.getImageFile(image)).thenReturn(imageFile); ModelAndView mav = imgController.handleRequestInternal(request, response); assertEquals("Did not return correct file.", mav.getModel().get("file"), imageFile); }
From source file:org.jasig.cas.web.flow.GenerateServiceTicketActionTests.java
@Test public void testServiceTicketFromCookie() throws Exception { MockRequestContext context = new MockRequestContext(); context.getFlowScope().put("service", TestUtils.getService()); context.getFlowScope().put("ticketGrantingTicketId", this.ticketGrantingTicket); MockHttpServletRequest request = new MockHttpServletRequest(); context.setExternalContext(//from w ww .j ava2s .c o m new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse())); request.addParameter("service", "service"); request.setCookies(new Cookie[] { new Cookie("TGT", this.ticketGrantingTicket) }); this.action.execute(context); assertNotNull(WebUtils.getServiceTicketFromRequestScope(context)); }
From source file:org.openmrs.web.controller.concept.ConceptProposalFormControllerTest.java
/** * @see ConceptProposalFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException) *///from www .ja v a2 s.c o m @Test @Verifies(value = "should create a single unique synonym and obs for all similar proposals", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)") public void onSubmit_shouldCreateASingleUniqueSynonymAndObsForAllSimilarProposals() throws Exception { executeDataSet("org/openmrs/api/include/ConceptServiceTest-proposals.xml"); ConceptService cs = Context.getConceptService(); ObsService os = Context.getObsService(); final Integer conceptproposalId = 5; ConceptProposal cp = cs.getConceptProposal(conceptproposalId); Concept obsConcept = cp.getObsConcept(); Concept conceptToMap = cs.getConcept(5); Locale locale = Locale.ENGLISH; //sanity checks Assert.assertFalse(conceptToMap.hasName(cp.getOriginalText(), locale)); Assert.assertEquals(0, os.getObservationsByPersonAndConcept(cp.getEncounter().getPatient(), obsConcept).size()); List<ConceptProposal> proposals = cs.getConceptProposals(cp.getOriginalText()); Assert.assertEquals(5, proposals.size()); for (ConceptProposal conceptProposal : proposals) { Assert.assertNull(conceptProposal.getObs()); } // set up the controller 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)); Assert.assertNotNull(cp.getObs()); //Obs should have been created for the 2 proposals with same text, obsConcept but different encounters Assert.assertEquals(2, os.getObservationsByPersonAndConcept(cp.getEncounter().getPatient(), obsConcept).size()); //The proposal with a different obs concept should have been skipped proposals = cs.getConceptProposals(cp.getFinalText()); Assert.assertEquals(1, proposals.size()); Assert.assertEquals(21, proposals.get(0).getObsConcept().getConceptId().intValue()); }
From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java
/** * Test on submit./*from w w w .j a v a 2 s.c o m*/ */ @Test public void testOnSubmit() { 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); /* * Sadly enough we would need a flush() within the * PayloadManagerImpl.saveNewPayload() function for this test to succeed * since we moved to saving the payload via its own manager/dao. */ // BindingResult errors = new DataBinder(fileUpload).getBindingResult(); // String result = ""; // try { // result = ctrl.onSubmit(fileUpload, errors, upload); // } catch (IOException e) { // fail("Should not fail on fail upload"); // } // // assertFalse(errors.hasErrors()); // assertNotNull(upload.getSession().getAttribute("successMessages")); // // AlphaCard myCard = alphaCardManager.get(new // AlphaCardIdentifier(caseId, cardId)); // assertNotNull(myCard); // assertNotNull(myCard.getPayload()); // Assert.assertArrayEquals(content, myCard.getPayload().getContent()); // Assert.assertEquals(fileName, myCard.getPayload().getFilename()); // Assert.assertEquals(mimeType, myCard.getPayload().getMimeType()); // Assert.assertEquals("redirect:/caseform?caseId=" + caseId + // "&activeCardId=" + cardId, result); }
From source file:no.dusken.aranea.control.TestImageController.java
@Test public void testResizeImage_Width() throws Exception { //creating image object. Image image = new Image(); image.setID(123L);/* w w w. j a v a 2 s . c o m*/ //mocking imageSernice.getEntity method to return image object I just created. when(imageService.getEntity(123L)).thenReturn(image); //mocking imageFile. File imageFile = mock(File.class); //mocking imageFile.exists method to return true. when(imageFile.exists()).thenReturn(true); //mocking resizedImageFile File resizedImageFile = mock(File.class); when(resizedImageFile.toString()).thenReturn("resized"); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "123"); request.addParameter("maxwidth", "30"); //mocking imageUtils.getImageFile(Image) when(imageUtils.getImageFile(image)).thenReturn(imageFile); //mocking imageUtils.resizeImage(imageFile, maxWidth, maxHeight) when(imageUtils.resizeImage(imageFile, 30, 0)).thenReturn(resizedImageFile); ModelAndView mav = imgController.handleRequestInternal(request, response); assertEquals("Resize image method is wrong.", mav.getModel().get("file"), resizedImageFile); assertEquals("Resized image is wrong", "resized", resizedImageFile.toString()); }
From source file:no.dusken.aranea.control.TestImageController.java
@Test public void testResizeImage_Height() throws Exception { //creating image object. Image image = new Image(); image.setID(123L);//from www . j a v a2 s.c o m //mocking imageSernice.getEntity method to return image object I just created. when(imageService.getEntity(123L)).thenReturn(image); //mocking imageFile. File imageFile = mock(File.class); //mocking imageFile.exists method to return true. when(imageFile.exists()).thenReturn(true); //mocking resizedImageFile File resizedImageFile = mock(File.class); when(resizedImageFile.toString()).thenReturn("resized"); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.addParameter("ID", "123"); request.addParameter("maxheight", "30"); //mocking imageUtils.getImageFile(Image) when(imageUtils.getImageFile(image)).thenReturn(imageFile); //mocking imageUtils.resizeImage(imageFile, maxWidth, maxHeight) when(imageUtils.resizeImage(imageFile, 0, 30)).thenReturn(resizedImageFile); ModelAndView mav = imgController.handleRequestInternal(request, response); assertEquals("Resize image method is wrong.", mav.getModel().get("file"), resizedImageFile); assertEquals("Resized image is wrong", "resized", resizedImageFile.toString()); }
From source file:nl.surfnet.coin.teams.control.EditTeamControllerTest.java
@Test(expected = RuntimeException.class) public void testEditTeamNoPrivileges() throws Exception { MockHttpServletRequest request = getRequest(); String token = TokenUtil.generateSessionToken(); // Add the teamId, team name, description & token request.addParameter("teamName", "Team 1"); request.addParameter("team", "team-1"); request.addParameter("description", "description"); request.addParameter("token", token); GrouperTeamService grouperTeamService = mock(GrouperTeamService.class); when(grouperTeamService.findTeamById("team-1")).thenReturn(mockTeam); when(grouperTeamService.findMember("team-1", "member-1")).thenReturn(mockMember); autoWireMock(editTeamController, grouperTeamService, GrouperTeamService.class); autoWireMock(editTeamController, new Returns(false), ControllerUtil.class); autoWireRemainingResources(editTeamController); editTeamController.editTeam(getModelMap(), request, token, token, new SimpleSessionStatus()); }
From source file:nl.surfnet.coin.teams.control.EditTeamControllerTest.java
@Test(expected = RuntimeException.class) public void testEditTeamNoMember() throws Exception { MockHttpServletRequest request = getRequest(); String token = TokenUtil.generateSessionToken(); // Add the teamId, team name, description & token request.addParameter("teamName", "Team 1"); request.addParameter("team", "team-1"); request.addParameter("description", "description"); request.addParameter("token", token); GrouperTeamService grouperTeamService = mock(GrouperTeamService.class); when(grouperTeamService.findTeamById("team-1")).thenReturn(null); when(grouperTeamService.findMember("team-1", "member-1")).thenReturn(mockAdminMember); autoWireMock(editTeamController, grouperTeamService, GrouperTeamService.class); autoWireMock(editTeamController, new Returns(true), ControllerUtil.class); autoWireRemainingResources(editTeamController); editTeamController.editTeam(getModelMap(), request, token, token, new SimpleSessionStatus()); }
From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.PersonAttributeTypeController1_8Test.java
/** * @see PersonAttributeTypeController#retirePersonAttributeType(PersonAttributeType,String,WebRequest) * @throws Exception // www . jav a2 s . co m * @verifies void a person attribute type */ @Test public void retirePersonAttributeType_shouldRetireAPersonAttributeType() throws Exception { final String nonRetiredAttribute = "a0f5521c-dbbd-4c10-81b2-1b7ab18330df"; PersonAttributeType obj = service.getPersonAttributeTypeByUuid(nonRetiredAttribute); Assert.assertNotNull(obj); Assert.assertFalse(obj.isRetired()); MockHttpServletRequest delRequest = request(RequestMethod.DELETE, getURI() + "/" + nonRetiredAttribute); delRequest.addParameter("!purge", ""); delRequest.addParameter("reason", "unit test"); handle(delRequest); obj = service.getPersonAttributeTypeByUuid(nonRetiredAttribute); Assert.assertNotNull(obj); Assert.assertTrue(obj.isRetired()); Assert.assertTrue("unit test".equals(obj.getRetireReason())); }