Search in sources :

Example 6 with MockMultipartFile

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

the class AssociationUploadServiceTest method uploadFileRowErrors.

@Test
public void uploadFileRowErrors() throws Exception {
    MockMultipartFile uploadedFile = new MockMultipartFile("data", "filename.txt", "text/plain", "Test file".getBytes());
    File file = new File(uploadedFile.getOriginalFilename());
    // Return validation summary with no errors
    when(studyFileService.getFileFromFileName(STUDY.getId(), uploadedFile.getOriginalFilename())).thenReturn(file);
    when(associationFileUploadService.processAndValidateAssociationFile(file, "full")).thenReturn(VALIDATION_SUMMARY_ROW_ERRORS);
    // Test and verify
    assertThat(associationUploadService.upload(uploadedFile, STUDY, SECURE_USER)).hasSize(1);
    verify(studyFileService, times(1)).getFileFromFileName(STUDY.getId(), uploadedFile.getOriginalFilename());
    verify(associationFileUploadService, times(1)).processAndValidateAssociationFile(file, "full");
    verify(studyFileService, times(1)).deleteFile(STUDY.getId(), uploadedFile.getOriginalFilename());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) File(java.io.File) Test(org.junit.Test)

Example 7 with MockMultipartFile

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

the class AssociationUploadServiceTest method uploadIncorrectlyFormattedFile.

@Test(expected = SheetProcessingException.class)
public void uploadIncorrectlyFormattedFile() throws Exception {
    MockMultipartFile incorrectlyFormattedFile = new MockMultipartFile("data", "filename.txt", "text/plain", "Not an Excel file".getBytes());
    File file = new File(incorrectlyFormattedFile.getOriginalFilename());
    // Stubbing
    when(studyFileService.getFileFromFileName(STUDY.getId(), incorrectlyFormattedFile.getOriginalFilename())).thenReturn(file);
    when(associationFileUploadService.processAndValidateAssociationFile(file, "full")).thenThrow(new SheetProcessingException());
    associationUploadService.upload(incorrectlyFormattedFile, STUDY, SECURE_USER);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) File(java.io.File) Test(org.junit.Test)

Example 8 with MockMultipartFile

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

the class ArtifactsControllerIntegrationTest method postFile.

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);
    return artifactsController.postArtifact(pipelineName, pipeline.getLabel(), "stage", "LATEST", "build", buildId, requestFilename, null, multipartRequest);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) StubMultipartHttpServletRequest(com.thoughtworks.go.helper.StubMultipartHttpServletRequest)

Example 9 with MockMultipartFile

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

the class ArtifactsControllerTest method shouldReturnHttpErrorCodeWhenChecksumFileSaveFails.

@Test
public void shouldReturnHttpErrorCodeWhenChecksumFileSaveFails() throws Exception {
    File artifactFile = new File("junk");
    when(artifactService.findArtifact(any(JobIdentifier.class), eq("some-path"))).thenReturn(artifactFile);
    when(artifactService.saveFile(any(File.class), any(InputStream.class), eq(false), eq(1))).thenReturn(true);
    when(artifactService.saveOrAppendFile(any(File.class), any(InputStream.class))).thenReturn(false);
    MockMultipartHttpServletRequest mockMultipartHttpServletRequest = new MockMultipartHttpServletRequest();
    mockMultipartHttpServletRequest.addFile(new MockMultipartFile(REGULAR_MULTIPART_FILENAME, "content".getBytes()));
    mockMultipartHttpServletRequest.addFile(new MockMultipartFile(CHECKSUM_MULTIPART_FILENAME, "checksum-content".getBytes()));
    ModelAndView modelAndView = artifactsController.postArtifact("pipeline-1", "1", "stage-1", "2", "job-1", 122L, "some-path", 1, mockMultipartHttpServletRequest);
    ResponseCodeView view = (ResponseCodeView) modelAndView.getView();
    assertThat(view.getStatusCode(), is(SC_INTERNAL_SERVER_ERROR));
    assertThat(view.getContent(), is("Error saving checksum file for the artifact at path 'some-path'"));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) ResponseCodeView(com.thoughtworks.go.server.web.ResponseCodeView) InputStream(java.io.InputStream) ModelAndView(org.springframework.web.servlet.ModelAndView) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) File(java.io.File) Test(org.junit.Test)

Example 10 with MockMultipartFile

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

the class FileUploadControllerTests method readString.

@Test
public void readString() throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes());
    webAppContextSetup(this.wac).build().perform(fileUpload("/fileupload").file(file)).andExpect(model().attribute("message", "File 'orig' uploaded successfully"));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) 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