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.bonitasoft.web.designer.controller.ImportControllerTest.java

@Test
public void should_respond_an_error_with_ok_code_when_import_exception_occurs_while_importing_a_widget()
        throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    when(pathImporter.importFromPath(unzipedPath, widgetImporter))
            .thenThrow(new ImportException(Type.SERVER_ERROR, "an error messge"));

    mockMvc.perform(fileUpload("/import/widget").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("SERVER_ERROR"))
            .andExpect(jsonPath("message").value("an error messge"));
}

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

@Test
public void should_respond_an_error_with_ok_code_when_import_exception_occurs_while_zip_file_could_not_be_opened()
        throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    when(unzipper.unzipInTempDir(any(InputStream.class), anyString())).thenThrow(ZipException.class);

    mockMvc.perform(fileUpload("/import/widget").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("CANNOT_OPEN_ZIP"))
            .andExpect(jsonPath("message").value("Cannot open zip file"));

    mockMvc.perform(fileUpload("/import/page").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("CANNOT_OPEN_ZIP"))
            .andExpect(jsonPath("message").value("Cannot open zip file"));
}

From source file:com.trenako.web.controllers.RollingStocksControllerTests.java

private MultipartFile buildFile(MediaType mediaType) {
    byte[] content = "file content".getBytes();
    return new MockMultipartFile("image.jpg", "image.jpg", mediaType.toString(), content);
}

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

@Test
public void should_respond_an_error_with_ok_code_when_import_exception_occurs_while_unzipping()
        throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    when(unzipper.unzipInTempDir(any(InputStream.class), anyString())).thenThrow(IOException.class);

    mockMvc.perform(fileUpload("/import/widget").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("SERVER_ERROR"))
            .andExpect(jsonPath("message").value("Error while unzipping zip file"));

    mockMvc.perform(fileUpload("/import/page").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("SERVER_ERROR"))
            .andExpect(jsonPath("message").value("Error while unzipping zip file"));
}

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

@Test
public void should_upload_a_local_asset() 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());
    Page page = mockPageOfId("my-page");
    Asset expectedAsset = anAsset().withId("assetId").active().withName("myfile.js").withOrder(2)
            .withScope(PAGE).withType(AssetType.JAVASCRIPT).build();
    when(pageAssetService.upload(file, page, "js")).thenReturn(expectedAsset);

    mockMvc.perform(fileUpload("/rest/pages/my-page/assets/js").file(file)).andExpect(status().isCreated())
            .andExpect(jsonPath("$.id").value("assetId")).andExpect(jsonPath("$.name").value("myfile.js"))
            .andExpect(jsonPath("$.scope").value(PAGE.toString())).andExpect(jsonPath("$.type").value("js"))
            .andExpect(jsonPath("$.order").value(2));

    verify(pageAssetService).upload(file, page, "js");
}

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

@Test
public void should_import_an_artifact() 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.zip", "application/zip", "foo".getBytes());
    ImportReport expectedReport = anImportReportFor(aWidget().id("aWidget").name("myWidgetName")).build();
    doReturn(widgetImporter).when(importerResolver).getImporter(unzipedPath);
    when(pathImporter.importFromPath(unzipedPath, widgetImporter)).thenReturn(expectedReport);

    mockMvc.perform(fileUpload("/import/artifact").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isCreated())
            .andExpect(jsonPath("element.id").value("aWidget"))
            .andExpect(jsonPath("element.name").value("myWidgetName"));
}

From source file:fr.treeptik.cloudunit.modules.AbstractModuleControllerTestIT.java

@Test
public void test_runScript() throws Exception {
    requestAddModule();/*w  w  w .  jav a  2 s  .  c o m*/
    String filename = FilenameUtils.getName(testScriptPath);
    if (filename == null) {
        logger.info("No script found - test escape");
    } else {
        MockMultipartFile file = new MockMultipartFile("file", filename, "application/sql",
                new FileInputStream(testScriptPath));

        String genericModule = cuInstanceName.toLowerCase() + "-johndoe-" + applicationName.toLowerCase() + "-"
                + module;

        ResultActions result = mockMvc.perform(fileUpload("/module/{moduleName}/run-script", genericModule)
                .file(file).session(session).contentType(MediaType.MULTIPART_FORM_DATA)).andDo(print());
        result.andExpect(status().isOk());
    }
}

From source file:cn.org.once.cstack.modules.AbstractModuleControllerTestIT.java

@Test
public void test_runScript() throws Exception {
    requestAddModule();/*  ww w  .j  a  va2  s.  c o  m*/
    String filename = FilenameUtils.getName(testScriptPath);
    if (filename == null) {
        logger.info("No script found - test escape");
    } else {
        MockMultipartFile file = new MockMultipartFile("file", filename, "application/sql",
                new FileInputStream(testScriptPath));

        String genericModule = NamingUtils.getContainerName(applicationName, module, "johndoe");

        ResultActions result = mockMvc.perform(fileUpload("/module/{moduleName}/run-script", genericModule)
                .file(file).session(session).contentType(MediaType.MULTIPART_FORM_DATA)).andDo(print());
        result.andExpect(status().isOk());
    }
}

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

@Test
public void should_respond_an_error_with_ok_code_when_model_file_is_not_found_while_importing_an_artifact()
        throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    Files.createDirectory(unzipedPath.resolve("resources"));

    mockMvc.perform(fileUpload("/import/artifact").file(file)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("MODEL_NOT_FOUND"))
            .andExpect(jsonPath("message").value("Could not load component, artifact model file not found"))
            .andExpect(jsonPath("infos.modelfiles", containsInAnyOrder("page.json", "widget.json")));
}

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

private MockMultipartFile aJsonFileWithContent(byte[] content) {
    return new MockMultipartFile("file", "myfile.js", "application/json", content);
}