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, InputStream contentStream) throws IOException 

Source Link

Document

Create a new MockMultipartFile with the given content.

Usage

From source file:org.unidle.form.QuestionFormTest.java

@Test
public void testSetAttachments() throws Exception {
    final List<? extends MultipartFile> attachments = Lists.newArrayList(
            new MockMultipartFile("empty", new byte[0]), new MockMultipartFile("empty", new byte[0]));

    subject.setAttachments(attachments);

    final Object result = getField(subject, "attachments");
    assertThat(result).isEqualTo(attachments);
}

From source file:business.SmallExcerptListTests.java

@Test(groups = "request", dependsOnMethods = "approveRequest")
public void uploadExcerptList() throws IOException {
    UserAuthenticationToken palga = getPalga();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(palga);

    RequestRepresentation representation = requestController.getRequestById(palga, processInstanceId);
    log.info("Status: " + representation.getStatus());

    log.info("Activity: " + representation.getActivityId());
    if (representation.getActivityId() == null) {
        for (Task task : taskService.createTaskQuery().list()) {
            log.info("Task " + task.getId() + ", process instance: " + task.getProcessInstanceId() + ", name: "
                    + task.getName() + ", key: " + task.getTaskDefinitionKey());
        }//from  w  w  w  .j  a v  a2s .c  om
    }

    log.info("uploadExcerptList: processInstanceId = " + processInstanceId);

    representation = requestController.claim(palga, processInstanceId, representation);

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Example excerptlist.csv");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), input);

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    int entryCount = requestController.uploadExcerptList(palga, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(3, entryCount);

    SecurityContextHolder.clearContext();
}

From source file:business.LargerExcerptListTests.java

@Test(groups = "request", dependsOnMethods = "approveRequest")
public void uploadExcerptList() throws IOException {
    UserAuthenticationToken palga = getPalga();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(palga);

    RequestRepresentation representation = requestController.getRequestById(palga, processInstanceId);
    log.info("Status: " + representation.getStatus());

    log.info("Activity: " + representation.getActivityId());
    if (representation.getActivityId() == null) {
        for (Task task : taskService.createTaskQuery().list()) {
            log.info("Task " + task.getId() + ", process instance: " + task.getProcessInstanceId() + ", name: "
                    + task.getName() + ", key: " + task.getTaskDefinitionKey());
        }//from   w w w.j a v a  2 s  . c  o  m
    }

    log.info("uploadExcerptList: processInstanceId = " + processInstanceId);

    representation = requestController.claim(palga, processInstanceId, representation);

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Example excerptlist 20150521 v2.csv");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), input);

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    int entryCount = requestController.uploadExcerptList(palga, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(6, entryCount);

    SecurityContextHolder.clearContext();
}

From source file:com.epam.ta.reportportal.ws.controller.impl.FileStorageControllerTest.java

@Test
public void addPhotoExtensionNegative() throws Exception {
    File file = new File("src/test/resources/logback.xml");
    this.mvcMock/*  www .j a v a 2s . com*/
            .perform(MockMvcRequestBuilders.fileUpload("/data/photo")
                    .file(new MockMultipartFile("file", new FileInputStream(file))).principal(authentication()))
            .andExpect(status().is(400));
}

From source file:ch.silviowangler.dox.web.ImportControllerTest.java

@Test
public void importDocument()
        throws DocumentClassNotFoundException, DocumentDuplicationException, ValidationException {

    when(request.getParameterNames()).thenReturn(Lists.asList("a", new String[] { "b", "c" }).iterator());
    when(request.getParameter("a")).thenReturn("A");
    when(request.getParameter("b")).thenReturn("B");
    when(request.getParameter("c")).thenReturn("C");

    when(documentService.importDocument(any(PhysicalDocument.class)))
            .thenReturn(new DocumentReference("test.pdf"));

    ResponseEntity responseEntity = controller.importDocument(
            new MockMultipartFile("test.pdf", "this is just a test".getBytes()), request, "Client");
    assertThat(responseEntity.getStatusCode(), is(CREATED));

    verify(documentService).importDocument(any(PhysicalDocument.class));
}

From source file:com.carlos.projects.billing.ExcelToMySQLImporterTest.java

@Test
public void shouldImportNComponentsAndNFamiliesFromFileWithNComponentsAndNFamiliesAllOfThemWithFamilyCodeValue()
        throws Exception {
    //given//from   w  w w  .j av  a 2s  .c  om
    ExcelToMySQLImporter importer = new ExcelToMySQLImporter(familyDAO, componentDAO);
    MultipartFile file = new MockMultipartFile("data.xlsx", getClass().getResourceAsStream("/data.xlsx"));

    Family family1 = createFamily("36", "CONTADORES");
    Component component1 = createComponent("KITAZUL162212", "KIT AZUL BATERIA 16-2-21/2 BAHISA", 33.0, 0.0,
            383.2065, family1);
    Family family2 = createFamily("33", "ACCES. FONTANERIA");
    Component component2 = createComponent("000636", "LATIGUILLO FLEX.RIVER MH 2 300", 40.0, 0.0, 38.274,
            family2);

    //when
    Long importedComponents = importer.importData(file);

    //then
    assertThat("The number of components imported is not 2", importedComponents, is(2L));
    InOrder inOrder = inOrder(familyDAO, componentDAO);

    inOrder.verify(familyDAO).getById(Family.class, family1.getCode());
    inOrder.verify(componentDAO).getById(Component.class, component1.getCode());
    inOrder.verify(familyDAO).save(family1);

    inOrder.verify(familyDAO).getById(Family.class, family2.getCode());
    inOrder.verify(componentDAO).getById(Component.class, component2.getCode());
    inOrder.verify(familyDAO).save(family2);

    verifyNoMoreInteractions(familyDAO, componentDAO);
}

From source file:org.tonguetied.web.ImportValidatorTest.java

/**
 * Create a new instance of ImportValidatorTest.
 * /*from ww w.ja va  2s  .co m*/
 * @param fileName
 * @param formatType
 * @param translationState
 * @param data
 * @param fieldName
 */
public ImportValidatorTest(final String fileName, final FormatType formatType,
        final TranslationState translationState, final byte[] data, final String fieldName) {
    ImportParameters parameters = new ImportParameters();
    parameters.setFileName(fileName);
    parameters.setFormatType(formatType);
    parameters.setTranslationState(translationState);
    this.importBean = new ImportBean();
    this.importBean.setParameters(parameters);
    MultipartFile file = new MockMultipartFile(fileName, data);
    FileUploadBean fileUploadBean = new FileUploadBean();
    fileUploadBean.setFile(file);
    this.importBean.setFileUploadBean(fileUploadBean);
    this.fieldName = fieldName;
}

From source file:com.epam.ta.reportportal.ws.controller.impl.FileStorageControllerTest.java

@Test
public void addPhotoSizeNegative() throws Exception {
    File file = new File("src/test/resources/locators.pdf");
    this.mvcMock/*ww  w  .ja va 2  s.  c  o  m*/
            .perform(MockMvcRequestBuilders.fileUpload("/data/photo")
                    .file(new MockMultipartFile("file", new FileInputStream(file))).principal(authentication()))
            .andExpect(status().is(400));
}

From source file:org.openmrs.module.idgen.web.controller.IdentifierSourceControllerTest.java

@Test
public void importIdentifiers_shouldAcceptJson() throws Exception {
    Mockito.doNothing().when(iss).addIdentifiersToPool(Mockito.any(IdentifierPool.class),
            (List<String>) Mockito.anyCollectionOf(String.class));

    IdentifierPool identifierPool = new IdentifierPool();
    String identifiers = "{\"identifiers\":[\"1\",\"2\",\"3\"]}";
    InputStream inputStream = new ByteArrayInputStream(identifiers.getBytes());

    MockMultipartFile mockMultipartFile = new MockMultipartFile("inputFile", inputStream);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    controller.addIdentifiersFromFile(null, mockRequest, mockResponse, identifierPool, mockMultipartFile);
    String response = (String) mockRequest.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR);
    Assert.assertEquals(response, "Success: Identifiers successfully uploaded.");
}

From source file:com.epam.ta.reportportal.ws.controller.impl.FileStorageControllerTest.java

@Test
public void addPhotoPositive() throws Exception {
    File file = new File("src/test/resources/picture.png");
    this.mvcMock/*from   ww  w  .ja  va2 s.c om*/
            .perform(MockMvcRequestBuilders.fileUpload("/data/photo")
                    .file(new MockMultipartFile("file", new FileInputStream(file))).principal(authentication()))
            .andExpect(status().is(200));
}