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:de.prokimedo.test.IcdTest.java

@Test
public void saveVersion() throws FileNotFoundException, IOException, Throwable {
    File file = new File("src/test/resources/icdTest.csv");
    FileInputStream input = new FileInputStream(file);
    MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
            IOUtils.toByteArray(input));

    HashMap hash = this.service.saveVersion(multipartFile, "test");
    List list = (List) hash.get("new");
    assertNotEquals(0, list.size());//from w ww  .  j ava  2  s .co m
}

From source file:org.sakaiproject.imagegallery.test.ImageGalleryServiceTest.java

private MultipartFile getMockMultipartFile(String seed) {
    return new MockMultipartFile(seed, seed + ".jpg", "image/jpeg", seed.getBytes());
}

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

@Test
public void should_respond_400_when_file_content_type_is_not_supported() throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "text/html", "foo".getBytes());

    mockMvc.perform(fileUpload("/import/page").file(file)).andExpect(status().isBadRequest())
            .andExpect(jsonPath("type").value("IllegalArgumentException"))
            .andExpect(jsonPath("message").value("Only zip files are allowed when importing a component"));
}

From source file:org.openlmis.fulfillment.service.TemplateServiceTest.java

@Test
public void shouldThrowErrorIfFileIsInvalid() throws Exception {
    expectedException.expect(ReportingException.class);
    expectedException.expectMessage(REPORTING_FILE_INVALID);

    templateService.validateFileAndInsertTemplate(new Template(),
            new MockMultipartFile(NAME_OF_FILE, NAME_OF_FILE, "", new byte[1]));
}

From source file:alpha.portal.webapp.controller.CardFileUploadControllerTest.java

/**
 * Test on cancel.//  w w  w . ja  va2 s  .  co m
 */
@Test
public void testOnCancel() {
    final String caseId = "550e4713-e22b-11d4-a716-446655440000";
    final String cardId = "440e4816-e01b-74d4-a716-449955440092";
    final String fileName = "doesnotcompute.file";
    final String mimeType = "text/plain";
    final byte[] content = "roflcopter".getBytes();

    final MockHttpServletRequest request = this.newGet("/cardfileupload");
    request.setRemoteUser("admin");
    request.addParameter("case", caseId);
    request.addParameter("card", cardId);
    final FileUpload fileUpload = this.ctrl.showForm(request);
    fileUpload.setFile(content);

    final MockMultipartHttpServletRequest upload = new MockMultipartHttpServletRequest();
    upload.setRemoteUser("admin");
    final MockMultipartFile file = new MockMultipartFile("file", fileName, mimeType, content);
    upload.addFile(file);
    upload.addParameter("case", caseId);
    upload.addParameter("card", cardId);
    upload.addParameter("cancel", "Abbrechen");

    final BindingResult errors = new DataBinder(fileUpload).getBindingResult();
    String result = "";
    try {
        result = this.ctrl.onSubmit(fileUpload, errors, upload);
    } catch (final IOException e) {
        Assert.fail("Should not fail on fail upload");
    }
    Assert.assertFalse(errors.hasErrors());
    Assert.assertNull(upload.getSession().getAttribute("successMessages"));

    Assert.assertEquals("redirect:/caseform?caseId=" + caseId + "&activeCardId=" + cardId, result);
}

From source file:org.bonitasoft.web.designer.controller.asset.AssetServiceTest.java

@Test
public void should_upload_newfile_and_save_new_asset() throws Exception {
    Page page = aPage().withId("aPage").build();
    byte[] fileContent = "function(){}".getBytes();
    MockMultipartFile file = new MockMultipartFile("fileName.js", "originalFileName.js",
            "application/javascript", fileContent);
    Asset expectedAsset = anAsset().withName("originalFileName.js").withType(AssetType.JAVASCRIPT).withOrder(1)
            .build();//  ww w  . ja  v  a2  s .  c  o  m

    Asset asset = assetService.upload(file, page, "js");

    verify(assetRepository).save("aPage", asset, fileContent);
    verify(repository).updateLastUpdateAndSave(page);
    assertThat(page.getAssets()).contains(asset);
    assertThat(asset.getId()).isNotNull();
    assertThat(asset).isEqualToIgnoringGivenFields(expectedAsset, "id");
}

From source file:de.prokimedo.test.IcdTest.java

@Test
public void readVersions() throws FileNotFoundException, IOException, Throwable {
    Icd icd = new Icd("123", "diagnose", "type");
    this.service.save(icd);
    File file = new File("src/test/resources/icdTest.csv");
    FileInputStream input = new FileInputStream(file);
    MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
            IOUtils.toByteArray(input));

    this.service.saveVersion(multipartFile, "test");
    List<IcdVersion> response = this.service.readVersions();
    assertEquals(2, response.size());//from   ww w . j a v a 2 s . c  om
}

From source file:com.ethercamp.harmony.service.ContractsTests.java

@Test
public void contracts_shouldDetectContractName_whenFileUpload() throws Exception {
    when(repository.getCode(any())).thenReturn(Hex.decode(
            "60606040525b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c010000000000000000000000009081020402179055505b5b610181806100526000396000f360606040523615610044576000357c0100000000000000000000000000000000000000000000000000000000900480639937232114610086578063a6f9dae1146100a3575b6100845b60405180807f7465737400000000000000000000000000000000000000000000000000000000815260200150600401905060405180910390a05b565b005b34610000576100a160048080359060200190919050506100c0565b005b34610000576100be60048080359060200190919050506100e8565b005b6000600082604051808260001916815260200191505060405180910390a0600091505b505050565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561017d5780600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c010000000000000000000000009081020402179055505b5b5b5056"));

    final byte[] contract1 = ("contract owned {\n" + "  address owner;\n" + "  function owned() {\n"
            + "    owner = msg.sender;\n" + "  }\n" + "  function changeOwner(address newOwner) onlyowner {\n"
            + "    owner = newOwner;\n" + "  }\n" + "  modifier onlyowner() {\n"
            + "    if (msg.sender==owner) _;\n" + "  }\n" + "}\n").getBytes("UTF-8");

    final byte[] contract2 = ("import \"std.sol\";" + "contract Contract is owned {\n" + "  enum E { E1, E2 }\n"
            + "  function test(bytes32 str) {\n" + "    log0(str);\n" + "    E e = E.E1;\n" + "    uint b;\n"
            + "  }\n" + "  function () payable {\n" + "    log0(\"test\");\n" + "  }\n" + "}\n")
                    .getBytes("UTF-8");

    MockMultipartFile[] files = { new MockMultipartFile("std.sol", "std.sol", "plain/text", contract1),
            new MockMultipartFile("contract.sol", "contract.sol", "plain/text", contract2)

    };/*from  w  w  w. java  2s  .  com*/
    ContractInfoDTO addedContract = contractsService.uploadContract(ADDRESS, files);

    assertThat(addedContract.getName(), is("Contract"));
}

From source file:org.ngrinder.script.controller.FileEntryControllerTest.java

@SuppressWarnings("unchecked")
@Test//w  w  w .  ja v  a 2  s.  co m
public void testUploadFiles() {
    ModelMap model = new ModelMap();
    String path = "test-upload-path";
    scriptController.addFolder(getTestUser(), "", path, model);

    String upFileName = "Uploaded";
    MultipartFile upFile = new MockMultipartFile("Uploaded.py", "Uploaded.py", null,
            "#test content...".getBytes());
    path = path + "/" + upFileName;
    scriptController.upload(getTestUser(), path, "Uploaded file desc.", upFile, model);
    model.clear();
    scriptController.search(getTestUser(), "Uploaded", model);
    Collection<FileEntry> searchResult = (Collection<FileEntry>) model.get("files");
    assertThat(searchResult.size(), is(1));
}

From source file:com.github.cherimojava.orchidae.controller._PictureController.java

@Test
public void pictureUploadCheck() throws Exception {
    // check that there's no picture yet
    getLatest(10).andExpect(content().string("[]"));

    // upload one picture
    createPicture("test", "jpg");

    // make sure the picture is there
    getLatest(10).andExpect(jsonPath("$[0].title", is("test")))
            .andExpect(jsonPath("$[0].originalName", is("test.jpg")));

    // Upload another picture
    createPicture("b", "png");

    // verify we have two pictures now
    getLatest(10).andExpect(jsonPath("$[0].title", is("test")))
            .andExpect(jsonPath("$[0].originalName", is("test.jpg"))).andExpect(jsonPath("$[1].title", is("b")))
            .andExpect(jsonPath("$[1].originalName", is("b.png")));

    // check that if some IOException happens we get it returned appropriate
    MockMultipartFile file = new MockMultipartFile("b", "b.png", "image/png", "nonsensecontent".getBytes());
    mvc.perform(fileUpload("/picture").file(file).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andExpect(content().string(containsString("b.png")));
}