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:com.cisco.ca.cstg.pdi.controllers.license.LicenseControllerTest.java

@Test
public void licenseUpload_returnTypeCheck() throws Exception {
    MockMultipartFile file = new MockMultipartFile("data", "filename.txt", "text/plain", "some xml".getBytes());
    ModelAndView mv = licenseController.licenseUpload(file);
    assertEquals("license/license", mv.getViewName());
}

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

@Test
public void testBlurByIdRESTPost() 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/blur.jpg").file(file)).andExpect(status().is(200))
            .andExpect(content().contentType("image/jpeg")).andReturn();

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

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

@Test
public void testValidatorImageNull() {
    Set<ConstraintViolation<TestObject>> constraintViolations = validator.validate(
            new TestObject(new MockMultipartFile("test_avatar", "", "application/octet-stream", new byte[0])));

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

From source file:com.ccserver.digital.controller.CreditCardApplicationDocumentControllerTest.java

@Test
public void save() throws IOException {
    File ifile = new File("./src/main/resources/sample");
    Path idDocPath = FileSystems.getDefault().getPath(ifile.getAbsolutePath(), "IdDoc.pdf");
    byte[] idDocByteArray = Files.readAllBytes(idDocPath);
    MockMultipartFile idDocMultipartFile = new MockMultipartFile("IdDoc", "IdDoc.pdf", "application/pdf",
            idDocByteArray);//from www. j ava2s  . co m
    ResponseEntity<?> fileUploadResponse = mockDocController.saveAll(
            new MockMultipartFile[] { idDocMultipartFile }, new String[] { "front" }, Long.valueOf(1),
            Long.valueOf(1), request);

    Assert.assertNotNull(fileUploadResponse);
}

From source file:apiserver.services.image.controllers.ImageSizeTests.java

@Test
public void testSizeByIdRESTPost() 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/info/size").file(file)).andExpect(status().is(200))
            .andExpect(jsonPath("$.width").value(4000)).andExpect(jsonPath("$.height").value(3000)).andReturn();

    //Map asyncResult = (Map)result.getAsyncResult();
    //Assert.assertTrue(asyncResult instanceof Map);
    //Assert.assertEquals(3000, asyncResult.get("height") );
    //Assert.assertEquals(4000, asyncResult.get("width") );
}

From source file:com.cisco.ca.cstg.pdi.controllers.license.LicenseControllerTest.java

@Test
public void licenseUpload_nullCheck() throws Exception {
    MockMultipartFile file = new MockMultipartFile("data", null, "text/plain", "some xml".getBytes());
    ModelAndView mv = licenseController.licenseUpload(file);
    assertEquals("license/license", mv.getViewName());
    assertNotNull(mv.getModel());/*from  www .j  av  a  2s  .c o  m*/
    String expected = "No File Uploaded. Please select a file and upload.";
    assertEquals(expected, mv.getModel().get("errorMessage"));
}

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

@Test
public void testLensBlurByIdRESTPost() 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/maximum").file(file).param("format", "jpg"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

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

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

@Test
public void testLensBlurByIdRESTPost() 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/minimum").file(file).param("format", "jpg"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

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

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

@Test
public void testMedianByIdRESTPost() 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/median").file(file).param("format", "jpg"))
            .andExpect(status().is(200)).andExpect(content().contentType("image/jpeg")).andReturn();

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

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

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

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