Search in sources :

Example 11 with MockMultipartFile

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

the class MultipartControllerTests method multipartRequest.

@Test
public void multipartRequest() throws Exception {
    byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
    MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);
    byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
    MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
    standaloneSetup(new MultipartController()).build().perform(multipart("/test-multipartfile").file(filePart).file(jsonPart)).andExpect(status().isFound()).andExpect(model().attribute("fileContent", fileContent)).andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) Test(org.junit.Test)

Example 12 with MockMultipartFile

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

the class StudyFileServiceTest method testUploadWithEmptyFile.

@Test(expected = FileUploadException.class)
public void testUploadWithEmptyFile() 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", "".getBytes());
    studyFileService.upload(file, STUDY_ID);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) Test(org.junit.Test)

Example 13 with MockMultipartFile

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

the class AssociationUploadServiceTest method uploadBlankFile.

@Test(expected = FileUploadException.class)
public void uploadBlankFile() throws Exception {
    MockMultipartFile blankFile = new MockMultipartFile("data", "filename.txt", "text/plain", "".getBytes());
    doThrow(new FileUploadException()).when(studyFileService).upload(blankFile, STUDY.getId());
    associationUploadService.upload(blankFile, STUDY, SECURE_USER);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException) Test(org.junit.Test)

Example 14 with MockMultipartFile

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

the class AssociationUploadServiceTest method uploadFileValidationErrors.

@Test
public void uploadFileValidationErrors() 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_ASS_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 15 with MockMultipartFile

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

the class AssociationUploadServiceTest method uploadFileNoErrors.

@Test
public void uploadFileNoErrors() 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_NO_ERRORS);
    // Test and verify
    assertThat(associationUploadService.upload(uploadedFile, STUDY, SECURE_USER)).hasSize(0);
    verify(studyFileService, times(1)).getFileFromFileName(STUDY.getId(), uploadedFile.getOriginalFilename());
    verify(associationFileUploadService, times(1)).processAndValidateAssociationFile(file, "full");
    // Verify batch upload event
    verify(trackingOperationService, times(1)).update(STUDY, SECURE_USER, "ASSOCIATION_BATCH_UPLOAD", "1 associations created from upload of 'filename.txt'");
    verify(studyRepository, times(1)).save(STUDY);
    verify(associationOperationsService, times(1)).saveAssociation(ASSOCIATION, STUDY, ASSOCIATION_SUMMARY_NO_ERROR.getErrors());
    verify(associationOperationsService, times(1)).createAssociationCreationEvent(ASSOCIATION, SECURE_USER);
//verify(associationOperationsService, times(1)).runMapping(STUDY.getHousekeeping().getCurator(),
//                                                          ASSOCIATION,
//                                                          SECURE_USER);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) File(java.io.File) 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