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:us.fatehi.schemacrawler.webapp.SchemaCrawlerControllerTest.java

@Test
public void formWithNoParameters() throws Exception {
    final MockMultipartFile multipartFile = new MockMultipartFile("file", "test.db", "application/octet-stream",
            RandomUtils.nextBytes(5));//w ww  . jav  a2 s  . c  om
    mvc.perform(fileUpload("/schemacrawler").file(multipartFile)).andExpect(model().errorCount(2))
            .andExpect(model().attributeHasFieldErrors("diagramRequest", "name", "email"))
            .andExpect(status().is2xxSuccessful());
}

From source file:us.fatehi.schemacrawler.webapp.SchemaCrawlerControllerTest.java

@Test
public void formWithUpload() throws Exception {
    final MockMultipartFile multipartFile = new MockMultipartFile("file", "test.db", "application/octet-stream",
            RandomUtils.nextBytes(5));/*from  www. jav a 2  s  .  c om*/

    when(storageService.resolve(any(), eq(SQLITE_DB))).thenReturn(Optional.ofNullable(Paths.get("/")));
    when(scService.createSchemaCrawlerDiagram(any(), eq("png"))).thenReturn(Paths.get("/"));

    mvc.perform(fileUpload("/schemacrawler").file(multipartFile).param("name", "Sualeh").param("email",
            "sualeh@hotmail.com")).andExpect(view().name("SchemaCrawlerDiagramResult"))
            .andExpect(status().is2xxSuccessful());

    then(storageService).should().store(eq(multipartFile), any(), eq(SQLITE_DB));
}

From source file:us.fatehi.schemacrawler.webapp.SchemaCrawlerControllerTest.java

@Test
public void formWithUploadWithErrors() throws Exception {
    final MockMultipartFile multipartFile = new MockMultipartFile("file", "test.db", "application/octet-stream",
            RandomUtils.nextBytes(5));//from   w  w w. j a  va 2s.c om

    when(storageService.resolve(any(), eq(SQLITE_DB))).thenReturn(Optional.ofNullable(null)); // Do not "find" the
                                                                                              // SQLite database
    when(scService.createSchemaCrawlerDiagram(any(), eq("png"))).thenReturn(Paths.get("/"));

    mvc.perform(fileUpload("/schemacrawler").file(multipartFile).param("name", "Sualeh").param("email",
            "sualeh@hotmail.com"))
            // TODO: Check for the correct exception
            .andExpect(view().name("redirect:error")).andExpect(status().is3xxRedirection());

    then(storageService).should().store(eq(multipartFile), any(), eq(SQLITE_DB));
}

From source file:xyz.codevomit.bootlog.blog.BackupControllerImportTest.java

@Test
@WithMockUser(username = "merka", password = "merka")
public void testImport() throws Exception {
    assertTrue(postRepo.count() == 0);/* w w  w .j  a  v  a  2  s.  co  m*/

    InputStream stream = getClass().getClassLoader().getResourceAsStream("json/bootlog-export.json");
    byte[] jsonContent = IOUtils.toByteArray(stream);
    MockMultipartFile part = new MockMultipartFile("jsonContent", "bootlog-export.json", "octet/stream",
            jsonContent);

    mockMvc.perform(fileUpload("/backup/import").file(part).with(csrf())) // TODO set multipart content and input file
            .andExpect(status().is3xxRedirection()).andExpect(redirectedUrlPattern("/backup*"))
            .andExpect(flash().attributeExists("message"));

    assertEquals(5, postRepo.count());

    // cleanup
    postRepo.deleteAll();
}