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:org.jtalks.common.web.validation.ImageSizeValidatorTest.java

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

    Assert.assertEquals(constraintViolations.size(), 1, "Validation without errors");
    Assert.assertNotNull(constraintViolations.iterator().next().getMessage());
}

From source file:cn.org.once.cstack.utils.TestUtils.java

/**
 * Download from github binaries and deploy file.
 *
 * @param path//from  ww w. j  ava  2s  .c  o m
 * @return
 * @throws IOException
 */
public static MockMultipartFile downloadAndPrepareFileToDeploy(String remoteFile, String path)
        throws IOException {
    URL url;
    File file = new File(remoteFile);
    try (OutputStream outputStream = new FileOutputStream(file)) {
        url = new URL(path);
        InputStream input = url.openStream();

        int read;
        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);
    }
    return new MockMultipartFile("file", file.getName(), "multipart/form-data", new FileInputStream(file));

}

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

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

    Assert.assertEquals(constraintViolations.size(), 1, "Validation without errors");
    Assert.assertNotNull(constraintViolations.iterator().next().getMessage());
}

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

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

    Assert.assertEquals(constraintViolations.size(), 1, "Validation without errors");
    Assert.assertNotNull(constraintViolations.iterator().next().getMessage());
}

From source file:apiserver.services.image.controllers.filters.ImageDespeckleTests.java

@Test
public void testDespeckleByIdRESTPost() throws Exception {
    InputStream fileStream = this.getClass().getClassLoader().getResourceAsStream("IMG_5932.JPG");

    MockMultipartFile file = new MockMultipartFile("file", "IMG_5932.JPG", "image/jpeg", fileStream);

    MvcResult result = MockMvcBuilders.webAppContextSetup((WebApplicationContext) context).build()
            .perform(fileUpload(rootUrl + "/api/image/filter/despeckle").file(file).param("format", "jpg"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(1203634, result.getResponse().getContentLength());
    FileUtils.writeByteArrayToFile(new File("/Users/mnimer/Desktop/despeckle-post.jpg"),
            result.getResponse().getContentAsByteArray());
}