Search in sources :

Example 1 with MockMultipartFile

use of org.springframework.mock.web.MockMultipartFile in project spring-framework by spring-projects.

the class MultipartControllerTests method multipartRequestWrapped.

// SPR-13317
@Test
public void multipartRequestWrapped() throws Exception {
    byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
    MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
    Filter filter = new RequestWrappingFilter();
    MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build();
    Map<String, String> jsonMap = Collections.singletonMap("name", "yeeeah");
    mockMvc.perform(multipart("/test-json").file(jsonPart)).andExpect(model().attribute("json", jsonMap));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) Filter(javax.servlet.Filter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 2 with MockMultipartFile

use of org.springframework.mock.web.MockMultipartFile in project gocd by gocd.

the class ArtifactsControllerIntegrationTest method shouldSaveChecksumFileInTheCruiseOutputFolder.

@Test
public void shouldSaveChecksumFileInTheCruiseOutputFolder() throws Exception {
    File fooFile = createFile(artifactsRoot, "/tmp/foobar.html");
    FileUtils.writeStringToFile(fooFile, "FooBarBaz...");
    File checksumFile = createFile(artifactsRoot, "/tmp/foobar.html.checksum");
    FileUtils.writeStringToFile(checksumFile, "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());
    assertThat(FileUtils.readLines(uploadedChecksumFile).get(0).toString(), is("baz/foobar.html:FooMD5"));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) StubMultipartHttpServletRequest(com.thoughtworks.go.helper.StubMultipartHttpServletRequest) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) Test(org.junit.Test)

Example 3 with MockMultipartFile

use of org.springframework.mock.web.MockMultipartFile in project gocd by gocd.

the class ArtifactsControllerIntegrationTest method shouldAppendChecksumInTheCruiseOutputFolder.

@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);
    assertThat(list.size(), is(2));
    assertThat(list.get(0).toString(), is("oldbaz/foobar.html:BazMD5"));
    assertThat(list.get(1).toString(), is("baz/foobar.html:FooMD5"));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) StubMultipartHttpServletRequest(com.thoughtworks.go.helper.StubMultipartHttpServletRequest) List(java.util.List) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) Test(org.junit.Test)

Example 4 with MockMultipartFile

use of org.springframework.mock.web.MockMultipartFile in project goci by EBISPOT.

the class StudyFileServiceTest method testUploadWithFile.

@Test
public void testUploadWithFile() throws Exception {
    // Create our study dir , it should be empty
    studyFileService.createStudyDir(STUDY_ID);
    assertThat(studyFileService.getStudyFiles(STUDY_ID)).isEmpty();
    // Mock a file coming in via the controller
    MockMultipartFile file = new MockMultipartFile("data", "filename.txt", "text/plain", "Some study details".getBytes());
    studyFileService.upload(file, STUDY_ID);
    assertThat(studyFileService.getStudyFiles(STUDY_ID)).isNotEmpty();
    assertThat(studyFileService.getStudyFiles(STUDY_ID)).hasSize(1);
    assertThat(studyFileService.getStudyFiles(STUDY_ID)).hasOnlyElementsOfType(StudyFileSummary.class);
    assertThat(studyFileService.getStudyFiles(STUDY_ID)).extracting("fileName").contains("filename.txt");
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) Test(org.junit.Test)

Example 5 with MockMultipartFile

use of org.springframework.mock.web.MockMultipartFile in project goci by EBISPOT.

the class AssociationUploadServiceTest method uploadMissingFile.

@Test(expected = IOException.class)
public void uploadMissingFile() throws Exception {
    MockMultipartFile file = new MockMultipartFile("data", "filename.txt", "text/plain", "Blank file".getBytes());
    // Stubbing
    when(studyFileService.getFileFromFileName(STUDY.getId(), file.getOriginalFilename())).thenThrow(new FileNotFoundException());
    associationUploadService.upload(file, STUDY, SECURE_USER);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

MockMultipartFile (org.springframework.mock.web.MockMultipartFile)15 Test (org.junit.Test)14 File (java.io.File)5 StubMultipartHttpServletRequest (com.thoughtworks.go.helper.StubMultipartHttpServletRequest)3 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)1 ResponseCodeView (com.thoughtworks.go.server.web.ResponseCodeView)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Filter (javax.servlet.Filter)1 MockMultipartHttpServletRequest (org.springframework.mock.web.MockMultipartHttpServletRequest)1 MockMvc (org.springframework.test.web.servlet.MockMvc)1 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 FileUploadException (uk.ac.ebi.spot.goci.curation.exception.FileUploadException)1 SheetProcessingException (uk.ac.ebi.spot.goci.exception.SheetProcessingException)1