List of usage examples for org.springframework.mock.web MockMultipartFile MockMultipartFile
public MockMultipartFile(String name, InputStream contentStream) throws IOException
From source file:gateway.test.DataTests.java
/** * Test POST /data/file endpoint//from www . j a v a 2 s. c om * * @throws Exception */ @Test public void testAddFile() throws Exception { // Mock an Ingest Job request, containing some sample data we want to // ingest. Also mock a file. IngestJob mockJob = new IngestJob(); mockJob.data = mockData; mockJob.host = false; // This will cause a failure initially MockMultipartFile file = new MockMultipartFile("test.tif", "Content".getBytes()); // Generate a UUID that we can reproduce. when(gatewayUtil.sendJobRequest(any(PiazzaJobRequest.class), anyString())).thenReturn("123456"); // Test the request ResponseEntity<PiazzaResponse> entity = dataController .ingestDataFile(new ObjectMapper().writeValueAsString(mockJob), file, user); PiazzaResponse response = entity.getBody(); // Verify the results. This request should fail since the host flag is // set to false. assertTrue(response instanceof ErrorResponse == true); mockJob.host = true; // Resubmit the Job. Now it should fail because it is a TextResource. entity = dataController.ingestDataFile(new ObjectMapper().writeValueAsString(mockJob), file, user); response = entity.getBody(); assertTrue(response instanceof ErrorResponse == true); // Change to a File that should succeed. mockJob.data = new DataResource(); mockJob.data.dataType = new GeoJsonDataType(); // Resubmit the Job. It should now succeed with the message successfully // being sent to Kafka. entity = dataController.ingestDataFile(new ObjectMapper().writeValueAsString(mockJob), file, user); response = entity.getBody(); assertTrue(response instanceof ErrorResponse == false); assertTrue(((JobResponse) response).data.getJobId().equalsIgnoreCase("123456")); assertTrue(entity.getStatusCode().equals(HttpStatus.CREATED)); }
From source file:org.kew.rmf.reconciliation.ws.GeneralReconciliationServiceStepdefs.java
@When("^I make a bulk match query with a file containing these rows:$") public void i_make_a_bulk_match_query_with_a_file_containing_these_rows(String allRows) throws Throwable { MockMultipartFile multipartFile = new MockMultipartFile("file", allRows.getBytes()); result = mockMvc.perform(MockMvcRequestBuilders.fileUpload("/filematch/generalTest/").file(multipartFile)) .andExpect(status().is(200)).andReturn(); }
From source file:com.thoughtworks.go.server.controller.ArtifactsControllerIntegrationTest.java
@Test public void shouldSaveChecksumFileInTheCruiseOutputFolder() throws Exception { File fooFile = createFile(artifactsRoot, "/tmp/foobar.html"); FileUtils.writeStringToFile(fooFile, "FooBarBaz...", UTF_8); File checksumFile = createFile(artifactsRoot, "/tmp/foobar.html.checksum"); FileUtils.writeStringToFile(checksumFile, "baz/foobar.html:FooMD5\n", UTF_8); MockMultipartFile artifactMultipart = new MockMultipartFile("file", new FileInputStream(fooFile)); MockMultipartFile checksumMultipart = new MockMultipartFile("file_checksum", new FileInputStream(checksumFile)); request.addHeader("Confirm", "true"); StubMultipartHttpServletRequest multipartRequest = new StubMultipartHttpServletRequest(request, artifactMultipart, checksumMultipart); postFileWithChecksum("baz/foobar.html", multipartRequest); assertThat(file(artifactsRoot, "baz/foobar.html"), exists()); File uploadedChecksumFile = file(artifactsRoot, "cruise-output/md5.checksum"); assertThat(uploadedChecksumFile, exists()); assertThat(FileUtils.readLines(uploadedChecksumFile, UTF_8).get(0).toString(), is("baz/foobar.html:FooMD5")); }
From source file:com.thoughtworks.go.server.controller.ArtifactsControllerIntegrationTest.java
@Test public void shouldAppendChecksumInTheCruiseOutputFolder() throws Exception { File fooFile = createFileWithContent(artifactsRoot, "/tmp/foobar.html", "FooBarBaz..."); createFileWithContent(artifactsRoot, "cruise-output/md5.checksum", "oldbaz/foobar.html:BazMD5\n"); File checksumFile = createFileWithContent(artifactsRoot, "/tmp/foobar.html.checksum", "baz/foobar.html:FooMD5\n"); MockMultipartFile artifactMultipart = new MockMultipartFile("file", new FileInputStream(fooFile)); MockMultipartFile checksumMultipart = new MockMultipartFile("file_checksum", new FileInputStream(checksumFile)); request.addHeader("Confirm", "true"); StubMultipartHttpServletRequest multipartRequest = new StubMultipartHttpServletRequest(request, artifactMultipart, checksumMultipart); postFileWithChecksum("baz/foobar.html", multipartRequest); assertThat(file(artifactsRoot, "baz/foobar.html"), exists()); File uploadedChecksumFile = file(artifactsRoot, "cruise-output/md5.checksum"); assertThat(uploadedChecksumFile, exists()); List list = FileUtils.readLines(uploadedChecksumFile, UTF_8); assertThat(list.size(), is(2));/*from w ww . j a va 2 s .c o m*/ assertThat(list.get(0).toString(), is("oldbaz/foobar.html:BazMD5")); assertThat(list.get(1).toString(), is("baz/foobar.html:FooMD5")); }
From source file:com.thoughtworks.go.server.controller.ArtifactsControllerIntegrationTest.java
private ModelAndView postFile(String requestFilename, String multipartFilename, InputStream stream, MockHttpServletResponse response) throws Exception { MockMultipartFile multipartFile = new MockMultipartFile(multipartFilename, stream); request.addHeader("Confirm", "true"); StubMultipartHttpServletRequest multipartRequest = new StubMultipartHttpServletRequest(request, multipartFile);/* w w w. j a va 2 s . c o m*/ return artifactsController.postArtifact(pipelineName, Integer.toString(pipeline.getCounter()), "stage", "LATEST", "build", buildId, requestFilename, null, multipartRequest); }
From source file:business.controllers.RequestController.java
@Profile("dev") @PreAuthorize("isAuthenticated() and hasRole('palga') and hasPermission(#id, 'requestAssignedToUser')") @RequestMapping(value = "/requests/{id}/excerptList/useExample", method = RequestMethod.POST) public Integer useExampleExcerptList(UserAuthenticationToken user, @PathVariable String id) { ClassLoader classLoader = getClass().getClassLoader(); String filename = "Example excerptlist groot bestand.csv"; URL resource = classLoader.getResource("test/" + filename); try {//from w w w .j av a2 s. co m InputStream input = resource.openStream(); MultipartFile file = new MockMultipartFile(resource.getFile(), input); Integer flowTotalChunks = 1; Integer flowChunkNumber = 1; String flowIdentifier = "flow_" + UUID.randomUUID().toString(); return this.uploadExcerptList(user, id, filename, flowTotalChunks, flowChunkNumber, flowIdentifier, file); } catch (IOException e) { log.error("Error while uploading example file: " + e.getMessage()); throw new FileUploadError(e.getMessage()); } }
From source file:org.geogig.geoserver.functional.GeoServerTestSupport.java
/** * Issue a POST request to the provided URL with the given file passed as form data. * * @param resourceUri the url to issue the request to * @param formFieldName the form field name for the file to be posted * @param file the file to post//from ww w. jav a 2 s .c o m * * @return the response to the request */ public MockHttpServletResponse postFile(String resourceUri, String formFieldName, File file) throws Exception { try (FileInputStream fis = new FileInputStream(file)) { MockMultipartFile mFile = new MockMultipartFile(formFieldName, fis); MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders .fileUpload(new URI(resourceUri)).file(mFile); MockHttpServletRequest request = requestBuilder.buildRequest(applicationContext.getServletContext()); /** * Duplicated from GeoServerSystemTestSupport#createRequest to do the same work on the * MockMultipartHttpServletRequest */ request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(8080); request.setContextPath("/geoserver"); request.setRequestURI( ResponseUtils.stripQueryString(ResponseUtils.appendPath("/geoserver/", resourceUri))); // request.setRequestURL(ResponseUtils.appendPath("http://localhost:8080/geoserver", // path ) ); request.setQueryString(ResponseUtils.getQueryString(resourceUri)); request.setRemoteAddr("127.0.0.1"); request.setServletPath(ResponseUtils.makePathAbsolute(ResponseUtils.stripRemainingPath(resourceUri))); request.setPathInfo(ResponseUtils.makePathAbsolute( ResponseUtils.stripBeginningPath(ResponseUtils.stripQueryString(resourceUri)))); request.addHeader("Host", "localhost:8080"); // deal with authentication if (username != null) { String token = username + ":"; if (password != null) { token += password; } request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes()))); } kvp(request, resourceUri); request.setUserPrincipal(null); /** * End duplication */ return dispatch(request); } }
From source file:org.geowebcache.sqlite.OperationsRestTest.java
@Test public void testMultipleFilesUploadReplace() throws Exception { // creates some database files for the tests Tuple<File, Tuple<String, String>> testFiles = createTestFiles(); File rootDirectory = testFiles.first; try (FileInputStream fileA = new FileInputStream(new File(rootDirectory, testFiles.second.first)); FileInputStream fileB = new FileInputStream(new File(rootDirectory, testFiles.second.second))) { // perform the rest request MockMultipartFile fileUploadA = new MockMultipartFile("file", fileA); MockMultipartFile fileUploadB = new MockMultipartFile("file", fileB); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); // first request mockMvc.perform(MockMvcRequestBuilders.fileUpload("/sqlite/replace").file(fileUploadA) .param("layer", "europe").param("destination", testFiles.second.first)) .andExpect(status().is(200)); // second request mockMvc.perform(MockMvcRequestBuilders.fileUpload("/sqlite/replace").file(fileUploadB) .param("layer", "europe").param("destination", testFiles.second.second)) .andExpect(status().is(200)); // check that files were replaced checkThatStoreContainsReplacedTiles(testFiles.second.first, testFiles.second.second); }/* w ww. ja v a 2s. c om*/ }
From source file:org.geowebcache.sqlite.OperationsRestTest.java
@Test public void testZipFileUploadReplace() throws Exception { // creates some database files for the tests Tuple<File, Tuple<String, String>> testFiles = createTestFiles(); File rootDirectory = testFiles.first; // zip store files File tempDirectory = Files.createTempDirectory("gwc-").toFile(); addFilesToDelete(tempDirectory);// w ww .ja v a 2 s . c om File zipFile = new File(tempDirectory, "replace.zip"); zipDirectory(Paths.get(rootDirectory.getPath()), zipFile); try (FileInputStream zipFileInputStream = new FileInputStream(zipFile)) { // perform the rest request MockMultipartFile zipUpload = new MockMultipartFile("file", zipFileInputStream); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); // execute request mockMvc.perform( MockMvcRequestBuilders.fileUpload("/sqlite/replace").file(zipUpload).param("layer", "europe")) .andExpect(status().is(200)); // check that files were replaced checkThatStoreContainsReplacedTiles(testFiles.second.first, testFiles.second.second); } }