Search in sources :

Example 46 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project mots by motech-implementations.

the class JasperTemplateServiceTest method shouldValidateFileAndSetDataIfDefaultValueExpressionIsNull.

@Test
public void shouldValidateFileAndSetDataIfDefaultValueExpressionIsNull() throws Exception {
    MultipartFile file = mock(MultipartFile.class);
    when(file.getOriginalFilename()).thenReturn(NAME_OF_FILE);
    mockStatic(JasperCompileManager.class);
    JasperReport report = mock(JasperReport.class);
    InputStream inputStream = mock(InputStream.class);
    when(file.getInputStream()).thenReturn(inputStream);
    JRParameter param1 = mock(JRParameter.class);
    JRParameter param2 = mock(JRParameter.class);
    JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
    JRExpression jrExpression = mock(JRExpression.class);
    String[] propertyNames = { DISPLAY_NAME };
    when(report.getParameters()).thenReturn(new JRParameter[] { param1, param2 });
    when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);
    when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
    when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(PARAM_DISPLAY_NAME);
    when(param1.getPropertiesMap()).thenReturn(propertiesMap);
    when(param1.getValueClassName()).thenReturn("java.lang.String");
    when(param1.isForPrompting()).thenReturn(true);
    when(param1.getDefaultValueExpression()).thenReturn(jrExpression);
    when(jrExpression.getText()).thenReturn("text");
    when(param2.getPropertiesMap()).thenReturn(propertiesMap);
    when(param2.getValueClassName()).thenReturn("java.lang.Integer");
    when(param2.isForPrompting()).thenReturn(true);
    when(param2.getDefaultValueExpression()).thenReturn(null);
    ByteArrayOutputStream byteOutputStream = mock(ByteArrayOutputStream.class);
    whenNew(ByteArrayOutputStream.class).withAnyArguments().thenReturn(byteOutputStream);
    ObjectOutputStream objectOutputStream = spy(new ObjectOutputStream(byteOutputStream));
    whenNew(ObjectOutputStream.class).withArguments(byteOutputStream).thenReturn(objectOutputStream);
    doNothing().when(objectOutputStream).writeObject(report);
    byte[] byteData = new byte[1];
    when(byteOutputStream.toByteArray()).thenReturn(byteData);
    JasperTemplate jasperTemplate = new JasperTemplate();
    jasperTemplateService.validateFileAndInsertTemplate(jasperTemplate, file);
    verify(jasperTemplateRepository).save(jasperTemplate);
    assertThat(jasperTemplate.getTemplateParameters().get(0).getDisplayName(), is(PARAM_DISPLAY_NAME));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) JRParameter(net.sf.jasperreports.engine.JRParameter) InputStream(java.io.InputStream) JRExpression(net.sf.jasperreports.engine.JRExpression) JasperReport(net.sf.jasperreports.engine.JasperReport) JRPropertiesMap(net.sf.jasperreports.engine.JRPropertiesMap) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 47 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project mots by motech-implementations.

the class JasperTemplateServiceTest method testSaveTemplate.

private JasperTemplate testSaveTemplate(String name) throws ReportingException {
    JasperTemplateService service = spy(jasperTemplateService);
    MultipartFile file = mock(MultipartFile.class);
    String description = "description";
    // validating and saving file is checked by other tests
    doNothing().when(service).validateFileAndSaveTemplate(any(JasperTemplate.class), eq(file));
    // when
    JasperTemplate resultTemplate = service.saveTemplate(file, name, description);
    // then
    assertEquals(name, resultTemplate.getName());
    assertEquals(description, resultTemplate.getDescription());
    return resultTemplate;
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Matchers.anyString(org.mockito.Matchers.anyString) JasperTemplate(org.motechproject.mots.domain.JasperTemplate)

Example 48 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project lavagna by digitalfondue.

the class CardDataControllerTest method uploadFiles.

@Test
public void uploadFiles() throws NoSuchAlgorithmException, IOException {
    MultipartFile f = mock(MultipartFile.class);
    when(f.getOriginalFilename()).thenReturn("fileName");
    when(f.getSize()).thenReturn(7L);
    when(f.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[] { 42, 42, 42, 42, 84, 84, 84 }));
    when(cardDataService.createFile(any(String.class), any(String.class), any(Long.class), any(Integer.class), any(InputStream.class), any(String.class), any(User.class), any(Date.class))).thenReturn(ImmutablePair.of(true, mock(CardData.class)));
    List<MultipartFile> files = Arrays.asList(f);
    cardDataController.uploadFiles(cardId, files, user, new MockHttpServletResponse());
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 49 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project tutorials by eugenp.

the class FileUploadController method uploadFile.

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView uploadFile(MultipartFile file) throws IOException {
    ModelAndView modelAndView = new ModelAndView("file");
    InputStream in = file.getInputStream();
    File currDir = new File(".");
    String path = currDir.getAbsolutePath();
    FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + file.getOriginalFilename());
    int ch = 0;
    while ((ch = in.read()) != -1) {
        f.write(ch);
    }
    f.flush();
    f.close();
    modelAndView.getModel().put("message", "File uploaded successfully!");
    return modelAndView;
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ModelAndView(org.springframework.web.servlet.ModelAndView) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 50 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project tutorials by eugenp.

the class SimplePostController method postMultipart.

@RequestMapping(value = "/users/upload", method = RequestMethod.POST)
public String postMultipart(@RequestParam("file") final MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss");
            final String fileName = dateFormat.format(new Date());
            final File fileServer = new File(fileName);
            fileServer.createNewFile();
            final byte[] bytes = file.getBytes();
            final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded ";
        } catch (final Exception e) {
            return "You failed to upload " + e.getMessage();
        }
    } else {
        return "You failed to upload because the file was empty.";
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) BufferedOutputStream(java.io.BufferedOutputStream) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15