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:fragment.web.AbstractConnectorControllerTest.java

@Test
public void testUploadServiceInstanceLogoInvalidFile() throws Exception {

    Configuration configuration = configurationService
            .locateConfigurationByName(Names.com_citrix_cpbm_portal_settings_images_uploadPath);
    configuration.setValue("src\\test\\resources");
    configurationService.update(configuration);
    ServiceInstance serviceInstance = serviceInstanceDao.find(1L);
    Assert.assertEquals(null, serviceInstance.getImagePath());
    MultipartFile logo = new MockMultipartFile("ServiceInstanceLogo.jpeg", "ServiceInstanceLogo.jpe", "byte",
            "ServiceInstance".getBytes());
    ServiceInstanceLogoForm form = new ServiceInstanceLogoForm(serviceInstance);
    form.setLogo(logo);/*  w  w  w  .  j  av  a2  s  . c  om*/
    BindingResult result = validate(form);
    HttpServletRequest request = new MockHttpServletRequest();
    map = new ModelMap();
    String resultString = controller.uploadServiceInstanceLogo(form, result, request, map);
    Assert.assertNotNull(resultString);
    String Error = messageSource.getMessage(result.getFieldError("logo").getCode(), null, request.getLocale());
    Assert.assertEquals(Error, resultString);
    serviceInstance = serviceInstanceDao.find(1L);
    Assert.assertEquals(null, serviceInstance.getImagePath());
}

From source file:org.bonitasoft.web.designer.controller.WidgetResourceTest.java

@Test
public void should_not_upload_an_asset_for_custom_widget() throws Exception {
    //We construct a mockfile (the first arg is the name of the property expected in the controller
    MockMultipartFile file = new MockMultipartFile("file", "myfile.js", "application/javascript",
            "foo".getBytes());
    Widget widget = aWidget().id("my-widget").build();
    when(widgetRepository.get("my-widget")).thenReturn(widget);
    doThrow(IllegalArgumentException.class).when(widgetAssetService).upload(file, widget, "js");

    mockMvc.perform(fileUpload("/rest/widgets/my-widget/assets/js").file(file))
            .andExpect(status().isBadRequest());
}

From source file:fragment.web.AbstractConnectorControllerTest.java

@Test
public void testUploadServiceInstanceLogoNullDirectoryPath() throws Exception {

    Configuration configuration = configurationService
            .locateConfigurationByName(Names.com_citrix_cpbm_portal_settings_images_uploadPath);
    configuration.setValue(null);/*w  ww.j  a  v a 2  s . c  o m*/
    configurationService.update(configuration);

    ServiceInstance serviceInstance = serviceInstanceDao.find(1L);
    Assert.assertEquals(null, serviceInstance.getImagePath());
    MultipartFile logo = new MockMultipartFile("ServiceInstanceLogo.jpeg", "ServiceInstanceLogo.jpeg", "byte",
            "ServiceInstance".getBytes());
    ServiceInstanceLogoForm form = new ServiceInstanceLogoForm(serviceInstance);
    form.setLogo(logo);
    BindingResult result = validate(form);
    HttpServletRequest request = new MockHttpServletRequest();
    map = new ModelMap();
    String resultString = controller.uploadServiceInstanceLogo(form, result, request, map);
    Assert.assertNotNull(resultString);
    String Error = messageSource.getMessage(result.getFieldError("logo").getCode(), null, request.getLocale());
    Assert.assertEquals(Error, resultString);
    serviceInstance = serviceInstanceDao.find(1L);
    Assert.assertEquals(null, serviceInstance.getImagePath());
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTest.java

/**
 * Test to make sure we can submit a job with attachments.
 *
 * @throws Exception on any error/* w  ww.  j  a  v  a2 s. com*/
 */
@Test
public void canSubmitJobWithAttachments() throws Exception {
    final List<String> commandArgs = Lists.newArrayList("-c", "'echo hello world'");

    final List<ClusterCriteria> clusterCriteriaList = Lists
            .newArrayList(new ClusterCriteria(Sets.newHashSet(LOCALHOST_CLUSTER_TAG)));

    final String setUpFile = this.resourceLoader.getResource(BASE_DIR + "job" + FILE_DELIMITER + "jobsetupfile")
            .getFile().getAbsolutePath();

    final File attachment1File = this.resourceLoader.getResource(BASE_DIR + "job/query.sql").getFile();

    final MockMultipartFile attachment1;
    try (InputStream is = new FileInputStream(attachment1File)) {
        attachment1 = new MockMultipartFile("attachment", attachment1File.getName(),
                MediaType.APPLICATION_OCTET_STREAM_VALUE, is);
    }

    final File attachment2File = this.resourceLoader.getResource(BASE_DIR + "job/query2.sql").getFile();

    final MockMultipartFile attachment2;
    try (InputStream is = new FileInputStream(attachment2File)) {
        attachment2 = new MockMultipartFile("attachment", attachment2File.getName(),
                MediaType.APPLICATION_OCTET_STREAM_VALUE, is);
    }
    final Set<String> commandCriteria = Sets.newHashSet(BASH_COMMAND_TAG);
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, clusterCriteriaList,
            commandCriteria).withCommandArgs(commandArgs).withDisableLogArchival(true).withSetupFile(setUpFile)
                    .withDescription(JOB_DESCRIPTION).build();

    this.waitForDone(this.submitJob(4, jobRequest, Lists.newArrayList(attachment1, attachment2)));
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTests.java

/**
 * Test to make sure we can submit a job with attachments.
 *
 * @throws Exception on any error//w  w  w. j ava 2  s.  c o m
 */
@Test
public void canSubmitJobWithAttachments() throws Exception {
    final String commandArgs = "-c 'echo hello world'";

    final String clusterTag = "localhost";
    final List<ClusterCriteria> clusterCriteriaList = Lists
            .newArrayList(new ClusterCriteria(Sets.newHashSet(clusterTag)));

    final String setUpFile = this.resourceLoader.getResource(BASE_DIR + "job" + FILE_DELIMITER + "jobsetupfile")
            .getFile().getAbsolutePath();

    final File attachment1File = this.resourceLoader.getResource(BASE_DIR + "job/query.sql").getFile();

    final MockMultipartFile attachment1;
    try (final InputStream is = new FileInputStream(attachment1File)) {
        attachment1 = new MockMultipartFile("attachment", attachment1File.getName(),
                MediaType.APPLICATION_OCTET_STREAM_VALUE, is);
    }

    final File attachment2File = this.resourceLoader.getResource(BASE_DIR + "job/query2.sql").getFile();

    final MockMultipartFile attachment2;
    try (final InputStream is = new FileInputStream(attachment2File)) {
        attachment2 = new MockMultipartFile("attachment", attachment2File.getName(),
                MediaType.APPLICATION_OCTET_STREAM_VALUE, is);
    }
    final Set<String> commandCriteria = Sets.newHashSet("bash");
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, commandArgs,
            clusterCriteriaList, commandCriteria).withDisableLogArchival(true).withSetupFile(setUpFile)
                    .withDescription(JOB_DESCRIPTION).build();

    this.waitForDone(this.submitJob(4, jobRequest, Lists.newArrayList(attachment1, attachment2)));
}

From source file:fragment.web.AbstractProductsControllerTest.java

@Test
public void testEditProductLogoValid() throws Exception {

    Product product = productDAO.find(1L);
    ProductLogoForm form = new ProductLogoForm(product);
    MultipartFile logo = new MockMultipartFile("Product.jpeg", "Product.jpeg", "bytes",
            "ProductLogo".getBytes());
    form.setLogo(logo);//from w  w w .  j  av  a 2  s.  co m
    BindingResult result = validate(form);
    String resultString = productsController.editProductLogo(form, result, request, map);
    Assert.assertNotNull(resultString);
    String osName = System.getProperty("os.name");
    if (osName.startsWith("Windows")) {
        // The default config for saving product images in db is '/tmp'. Since '/' is not for Windows
        // saving the product logo fails. So skipping this check for Windows. Following assertion will
        // still happen on Hudson but will not fail on windows dev setup
        return;
    }
    Assert.assertTrue(resultString.contains("\"id\":" + product.getId() + ",\"name\":\"" + product.getName()
            + "\",\"code\":\"" + product.getCode() + "\""));
}

From source file:fragment.web.AbstractProductsControllerTest.java

@Test
public void testEditProductLogoInValid() throws Exception {

    Product product = productDAO.find(1L);
    ProductLogoForm form = new ProductLogoForm(product);
    MultipartFile logo = new MockMultipartFile("Product.jpe", "Product.jpe", "bytes", "ProductLogo".getBytes());
    form.setLogo(logo);//from  w  ww.  j a v  a  2  s .c  o  m
    BindingResult result = validate(form);
    String resultString = productsController.editProductLogo(form, result, request, map);
    Assert.assertNotNull(resultString);
    Assert.assertEquals("{\"errormessage\":\"File should have either .jpeg/.jpg/.png/.gif/.bmp extension\"}",
            resultString);
}