Example usage for org.springframework.mock.web MockMultipartFile MockMultipartFile

List of usage examples for org.springframework.mock.web MockMultipartFile MockMultipartFile

Introduction

In this page you can find the example usage for org.springframework.mock.web MockMultipartFile MockMultipartFile.

Prototype

public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType,
        InputStream contentStream) throws IOException 

Source Link

Document

Create a new MockMultipartFile with the given content.

Usage

From source file:io.onedecision.engine.decisions.test.MockMultipartFileUtil.java

public static MultipartFile newInstance(String dmnResource) {
    File baseDir = new File("target" + File.separator + "test-classes");
    File dmnToUpload = new File(baseDir, dmnResource);
    assertTrue("Cannot find DMN file to use as test input", dmnToUpload.exists());

    Path path = Paths.get(dmnToUpload.getAbsolutePath());
    String name = "file.dmn";
    String originalFileName = "file.dmn";
    String contentType = "application/xml";
    byte[] content = null;
    try {/*from   w ww . jav  a2  s . com*/
        content = Files.readAllBytes(path);
    } catch (final IOException e) {
    }
    MultipartFile mpf = new MockMultipartFile(name, originalFileName, contentType, content);
    return mpf;
}

From source file:org.jtalks.common.web.dto.user.UserViewDto.java

/**
 * Constructor which fill DTO fields from User entity
 *
 * @param user User entity/* ww  w. j  ava  2 s.  co m*/
 */
public UserViewDto(User user) {
    firstName = user.getFirstName();
    lastName = user.getLastName();
    username = user.getUsername();
    encodedUsername = user.getEncodedUsername();
    email = user.getEmail();
    lastLogin = user.getLastLogin();
    avatar = new MockMultipartFile("avatar", "", ImageFormats.JPG.getContentType(), user.getAvatar());
}

From source file:com.trenako.web.images.ThumbnailatorServiceTests.java

private MultipartFile mockFile(byte[] content, MediaType type) {
    return new MockMultipartFile("file.jpg", "file.jpg", type.toString(), content);
}

From source file:org.jtalks.common.web.validation.ImageSizeValidatorTest.java

@Test
public void testValidatorSuccess() {
    Set<ConstraintViolation<TestObject>> constraintViolations = validator.validate(
            new TestObject(new MockMultipartFile("test_avatar", "test_avatar", "image/jpeg", new byte[1024])));

    Assert.assertEquals(constraintViolations.size(), 0, "Validation errors");
}

From source file:org.jtalks.common.web.validation.ImageDimensionValidatorTest.java

@Test
public void testValidatorNormalDimension() {
    Set<ConstraintViolation<TestObject>> constraintViolations = validator.validate(new TestObject(
            new MockMultipartFile("test_avatar", "test_avatar", "image/png", normalAvatarByteArray)));

    Assert.assertEquals(constraintViolations.size(), 0, "Validation errors");

}

From source file:nl.surfnet.coin.teams.service.impl.InvitationFormValidatorTest.java

@Test
public void testValidateForCSVInput() throws Exception {
    InvitationForm form = new InvitationForm();
    String mails = "test@example.com,test@example.net,john.doe@example.org";
    MultipartFile mockFile = new MockMultipartFile("mockFile", "test.csv", "text/csv", mails.getBytes("utf-8"));
    form.setCsvFile(mockFile);/*from   www .  j a v a 2 s .  com*/
    Errors errors = new BeanPropertyBindingResult(form, "invitationForm");
    validator.validate(form, errors);
    assertEquals(0, errors.getErrorCount());
}

From source file:org.jtalks.common.web.validation.ImageFormatValidatorTest.java

@Test(dataProvider = "allowableFormats")
public void testValidatorSuccess(String contentType) {
    Set<ConstraintViolation<TestObject>> constraintViolations = validator.validate(
            new TestObject(new MockMultipartFile("test_avatar", "test_avatar", contentType, new byte[10])));

    Assert.assertEquals(constraintViolations.size(), 0, "Validation errors");
}

From source file:org.unidle.service.AttachmentServiceImplTest.java

@Test
public void testCreateAttachment() throws Exception {
    final Attachment result = subject.createAttachment(
            new MockMultipartFile("test.txt", "test.txt", "text/plain", "this is a text file".getBytes()));

    assertThat(attachmentRepository.count()).isEqualTo(1L);
    assertThat(result.getContent()).isEqualTo("this is a text file".getBytes());
    assertThat(result.getContentType()).isEqualTo("text/plain");
    assertThat(result.getTitle()).isEqualTo("test.txt");
}

From source file:fr.treeptik.cloudunit.utils.TestUtils.java

/**
 * Download from github binaries and deploy file
 *
 * @param path/*www  .  jav  a 2s .c  om*/
 * @return
 * @throws IOException
 */
public static MockMultipartFile downloadAndPrepareFileToDeploy(String remoteFile, String path)
        throws IOException {
    URL url;
    OutputStream outputStream = null;
    File file = new File(remoteFile);
    try {
        url = new URL(path);
        InputStream input = url.openStream();

        outputStream = new FileOutputStream(file);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = input.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        StringBuilder msgError = new StringBuilder(512);
        msgError.append(remoteFile);
        msgError.append(",");
        msgError.append(path);
        logger.debug(msgError.toString(), e);
    } finally {
        outputStream.close();
    }
    return new MockMultipartFile("file", file.getName(), "multipart/form-data", new FileInputStream(file));

}

From source file:feign.form.feign.spring.FeignClientAnnotatedInterfaceTest.java

@Test
public void uploadFileNameAndContentTypeTest() throws Exception {
    MultipartFile file = new MockMultipartFile("file", "hello.dat", "application/octet-stream",
            "test".getBytes(UTF_8));
    String response = client.upload3(file, "test folder", "message text");

    Assert.assertEquals("hello.dat:application/octet-stream", response);
}