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:apiserver.services.image.controllers.filters.ImageBoxBlurTests.java

@Test
public void testBoxBlurByIdRESTPost() 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 + "/image/filter/boxblur.jpg").file(file).param("hRadius", "2")
                    .param("vRadius", "2").param("iterations", "1").param("preMultiplyAlpha", "true"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(962397, result.getResponse().getContentLength());
    saveFileToLocalDisk("boxblur-post.jpg", result.getResponse().getContentAsByteArray());
}

From source file:com.cognitivabrasil.repositorio.web.FileControllerTest.java

@Test
public void testUploadFile() throws IOException, ServletException, FileUploadException {
    HttpServletResponse response = new MockHttpServletResponse();

    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    assertThat(request.getFileNames().hasNext(), equalTo(false));
    assertThat(request.getFile("file1"), equalTo(null));
    assertThat(request.getFile("file2"), equalTo(null));
    assertThat(request.getFileMap().isEmpty(), equalTo(true));

    request.setContentType("multipart/form-data; boundary=-----1234");
    request.setCharacterEncoding("text/plain");
    request.setContent("algo".getBytes());
    request.setMethod("POST");

    MockMultipartFile mockMultipartFile = new MockMultipartFile("content", "test.txt", "text/plain",
            "HelloWorld".getBytes());
    request.addFile(mockMultipartFile);//from www .  java 2s  .c om

    FileController fileController = mockFiles();

    String result = fileController.upload(request, response);

    assertThat(result, equalTo("{\"jsonrpc\" : \"2.0\", \"result\" : \"success\", \"id\" : \"id\"}"));
    //testes

}

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

@Test
public void testGlowByIdRESTPost() 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 + "/image/filter/glow.jpg").file(file).param("amount", "2"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(508774, result.getResponse().getContentLength());
    saveFileToLocalDisk("glow-post.jpg", result.getResponse().getContentAsByteArray());
}

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

@Test
public void testGaussianByIdRESTPost() 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 + "/image/filter/gaussian.jpg").file(file).param("radius", "2"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(1243666, result.getResponse().getContentLength());
    saveFileToLocalDisk("gaussian-post.jpg", result.getResponse().getContentAsByteArray());
}

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

@Test
public void testGrayscaleByIdRESTPost() 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 + "/image/filter/grayscale.jpg").file(file).param("amount", "2"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(1374871, result.getResponse().getContentLength());
    saveFileToLocalDisk("grayscale-post.jpg", result.getResponse().getContentAsByteArray());
    ;/*from  w w  w  .j a v a 2s.  co m*/
}

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

@Test
public void testOilByIdRESTPost() 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 + "/image/filter/motionblur.jpg").file(file).param("level", "3")
                    .param("range", "256"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(1507837, result.getResponse().getContentLength());
    saveFileToLocalDisk("oil-post.jpg", result.getResponse().getContentAsByteArray());
}

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

@Test
public void testBumpByIdRESTPost() 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 + "/image/filter/bump.jpg").file(file).param("edgeAction", "1")
                    .param("useAlpha", "true").param("matrix", "-1.0,-1.0,0.0,-1.0,1.0,1.0,0.0,1.0,1.0"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

    Assert.assertEquals(2631227, result.getResponse().getContentLength());
    saveFileToLocalDisk("bump-post.jpg", result.getResponse().getContentAsByteArray());
}

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

private MultipartFile multipartFile() {
    MultipartFile mf = new MockMultipartFile("upload", "upload.jpg", MediaType.IMAGE_JPEG.toString(),
            new byte[] {});
    return mf;// w w  w.ja v  a2  s . c o  m
}

From source file:org.jtalks.common.web.controller.UserControllerTest.java

@BeforeClass
public void mockAvatar() throws IOException {
    avatar = new MockMultipartFile("test_avatar.jpg", "test_avatar.jpg", "image/jpeg", new byte[10]);
}

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

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

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