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.eclipse.hawkbit.rest.mgmt.documentation.SoftwaremodulesDocumentationTest.java

@Test
@Description("Handles POST request for artifact upload. Required Permission: " + SpPermission.CREATE_REPOSITORY)
public void postArtifact() throws Exception {
    final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();

    // create test file
    final byte random[] = RandomStringUtils.random(5).getBytes();
    final MockMultipartFile file = new MockMultipartFile("file", "origFilename", null, random);

    mockMvc.perform(//from   w  ww  .  jav a 2s .co  m
            fileUpload(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts",
                    sm.getId()).file(file))
            .andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated())
            .andExpect(content().contentType(MediaTypes.HAL_JSON_UTF8))
            .andDo(this.document.document(
                    pathParameters(parameterWithName("softwareModuleId")
                            .description(ApiModelPropertiesGeneric.ITEM_ID)),
                    responseFields(fieldWithPath("size").description(MgmtApiModelProperties.ARTIFACT_SIZE),
                            fieldWithPath("id").description(ApiModelPropertiesGeneric.ITEM_ID),
                            fieldWithPath("createdBy").description(ApiModelPropertiesGeneric.CREATED_BY),
                            fieldWithPath("createdAt").description(ApiModelPropertiesGeneric.CREATED_AT),
                            fieldWithPath("lastModifiedBy")
                                    .description(ApiModelPropertiesGeneric.LAST_MODIFIED_BY),
                            fieldWithPath("lastModifiedAt")
                                    .description(ApiModelPropertiesGeneric.LAST_MODIFIED_AT),
                            fieldWithPath("_links.self").ignored(),
                            fieldWithPath("_links.download")
                                    .description(MgmtApiModelProperties.ARTIFACT_DOWNLOAD_LINK),
                            fieldWithPath("hashes.md5").description(MgmtApiModelProperties.ARTIFACT_HASHES_MD5),
                            fieldWithPath("hashes.sha1")
                                    .description(MgmtApiModelProperties.ARTIFACT_HASHES_SHA1),
                            fieldWithPath("providedFilename")
                                    .description(MgmtApiModelProperties.ARTIFACT_PROVIDED_FILENAME)))

    );
}

From source file:org.eclipse.hawkbit.rest.mgmt.documentation.SoftwaremodulesDocumentationTest.java

@Test
@Description("Handles POST request for artifact upload. Required Permission: " + SpPermission.CREATE_REPOSITORY)
public void postArtifactWithParameters() throws Exception {

    final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();

    final byte random[] = RandomStringUtils.random(5).getBytes();
    final MockMultipartFile file = new MockMultipartFile("file", "origFilename", null, random);

    mockMvc.perform(// w  ww . j  a  v a2  s  .  co  m
            fileUpload(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts",
                    sm.getId()).file(file).param("filename", "filename").param("file", "s")
                            .param("md5sum", "md5sum").param("sha1sum", "sha1sum"))
            .andDo(MockMvcResultPrinter.print()).andExpect(status().isBadRequest())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(this.document.document(requestParameters(
                    parameterWithName("filename")
                            .description(MgmtApiModelProperties.ARTIFACT_PROVIDED_FILENAME),
                    parameterWithName("file").description(MgmtApiModelProperties.ARTIFACT_PROVIDED_FILE),
                    parameterWithName("md5sum").description(MgmtApiModelProperties.ARTIFACT_HASHES_MD5),
                    parameterWithName("sha1sum").description(MgmtApiModelProperties.ARTIFACT_HASHES_SHA1))));
}

From source file:org.jgrades.rest.client.BaseTest.java

public void installTestbedBaseLicence() throws Exception {
    if (!licenceCheckServiceClient.checkForProduct("JG-BASE").isValid()) {
        File licenceResource = new File("src/test/resources/0.4-DEV-SNAPSHOT.lic");
        FileInputStream input = new FileInputStream(licenceResource);
        MultipartFile licenceMultipartFile = new MockMultipartFile("licence", licenceResource.getName(),
                "text/plain", IOUtils.toByteArray(input));

        File signatureResource = new File("src/test/resources/0.4-DEV-SNAPSHOT.lic.sign");
        FileInputStream input2 = new FileInputStream(signatureResource);
        MultipartFile signatureMultipartFile = new MockMultipartFile("signature", licenceResource.getName(),
                "text/plain", IOUtils.toByteArray(input2));

        licenceManagerServiceClient.uploadAndInstall(licenceMultipartFile, signatureMultipartFile);
    }// www .j  a  v  a 2s. co m
}

From source file:org.jgrades.rest.client.lic.LicenceServicesClientTest.java

@Test
public void shouldInstall() throws Exception {
    loginServiceClient.logIn("admin", "admin");

    File licenceResource = new File("src/test/resources/0.4-DEV-SNAPSHOT.lic");
    FileInputStream input = new FileInputStream(licenceResource);
    MultipartFile licenceMultipartFile = new MockMultipartFile("licence", licenceResource.getName(),
            "text/plain", IOUtils.toByteArray(input));

    File signatureResource = new File("src/test/resources/0.4-DEV-SNAPSHOT.lic.sign");
    FileInputStream input2 = new FileInputStream(signatureResource);
    MultipartFile signatureMultipartFile = new MockMultipartFile("signature", licenceResource.getName(),
            "text/plain", IOUtils.toByteArray(input2));

    licenceManagerServiceClient.uploadAndInstall(licenceMultipartFile, signatureMultipartFile);

    System.out.println(licenceManagerServiceClient.getAll());
    System.out.println(licenceManagerServiceClient.get(1992L));
    System.out.println(licenceCheckServiceClient.check(1992L).isValid());
    System.out.println(licenceCheckServiceClient.checkForProduct("JG-BASE").isValid());

    licenceManagerServiceClient.uninstall(1992L);
    System.out.println(licenceManagerServiceClient.getAll());

    System.out.println(licenceCheckServiceClient.checkForProduct("JG-BASE").isValid());

}

From source file:org.jgrades.rest.lic.LicenceManagerServiceTest.java

@Test
public void shouldProcessMultipartFiles_andCorrectlyInstallLicence() throws Exception {
    // given/*from   ww w . j  av  a2 s  .com*/
    Licence licence = getExampleLicence();

    File licenceTargetFile = tempFolder.newFile();
    File signatureTargetFile = tempFolder.newFile();

    when(incomingFilesNameResolverMock.getLicenceFile()).thenReturn(licenceTargetFile);
    when(incomingFilesNameResolverMock.getSignatureFile()).thenReturn(signatureTargetFile);

    byte[] licenceFileContent = "LIC_CONTENT".getBytes();
    byte[] signatureFileContent = "SIGNATURE_CONTENT".getBytes();

    MockMultipartFile licenceInputFile = new MockMultipartFile("licence", "jg.lic", "text/plain",
            licenceFileContent);
    MockMultipartFile signatureInputFile = new MockMultipartFile("signature", "jg.lic.sign", "text/plain",
            signatureFileContent);

    when(licenceManagingServiceMock.installLicence(eq(licenceTargetFile.getAbsolutePath()),
            eq(signatureTargetFile.getAbsolutePath()))).thenReturn(licence);

    // when
    mockMvc.perform(fileUpload("/licence").file(licenceInputFile).file(signatureInputFile))
            .andExpect(status().isOk()).andExpect(jsonPath("$.uid", is(licence.getUid().intValue())))
            .andExpect(jsonPath("$.customer.name", is(licence.getCustomer().getName())))
            .andExpect(jsonPath("$.product.version", is(licence.getProduct().getVersion())));

    // then
    assertThat(FileUtils.readFileToByteArray(licenceTargetFile)).isEqualTo(licenceFileContent);
    assertThat(FileUtils.readFileToByteArray(signatureTargetFile)).isEqualTo(signatureFileContent);

    verify(licenceManagingServiceMock, times(1)).installLicence(licenceTargetFile.getAbsolutePath(),
            signatureTargetFile.getAbsolutePath());
}

From source file:org.jgrades.rest.lic.LicenceManagerServiceTest.java

@Test
public void shouldThrowException_whenLicenceFileIsEmpty() throws Exception {
    // given/*from w ww  . j  a  va 2 s. c o m*/
    byte[] licenceFileContent = StringUtils.EMPTY.getBytes();
    byte[] signatureFileContent = "SIGNATURE_CONTENT".getBytes();

    MockMultipartFile licenceInputFile = new MockMultipartFile("licence", "jg.lic", "text/plain",
            licenceFileContent);
    MockMultipartFile signatureInputFile = new MockMultipartFile("signature", "jg.lic.sign", "text/plain",
            signatureFileContent);

    // when then
    mockMvc.perform(fileUpload("/licence").file(licenceInputFile).file(signatureInputFile))
            .andExpect(status().isInternalServerError())
            .andExpect(jsonPath("$.title", is(IllegalArgumentException.class.getSimpleName())));
}

From source file:org.jgrades.rest.lic.LicenceManagerServiceTest.java

@Test
public void shouldThrowException_whenSignatureFileIsEmpty() throws Exception {
    // given//ww w.  j a va 2s  .com
    byte[] licenceFileContent = "LIC_CONTENT".getBytes();
    byte[] signatureFileContent = StringUtils.EMPTY.getBytes();

    MockMultipartFile licenceInputFile = new MockMultipartFile("licence", "jg.lic", "text/plain",
            licenceFileContent);
    MockMultipartFile signatureInputFile = new MockMultipartFile("signature", "jg.lic.sign", "text/plain",
            signatureFileContent);

    // when then
    mockMvc.perform(fileUpload("/licence").file(licenceInputFile).file(signatureInputFile))
            .andExpect(status().isInternalServerError())
            .andExpect(jsonPath("$.title", is(IllegalArgumentException.class.getSimpleName())));
}

From source file:org.jgrades.rest.lic.LicenceManagerServiceTest.java

@Test
public void shouldRemoveReceivedFilesFromFileSystem_whenSomethingWrongDuringInstallationHappened()
        throws Exception {
    // given/*from w w w. j  av  a2s . c o  m*/
    File licenceTargetFile = tempFolder.newFile();
    File signatureTargetFile = tempFolder.newFile();

    when(incomingFilesNameResolverMock.getLicenceFile()).thenReturn(licenceTargetFile);
    when(incomingFilesNameResolverMock.getSignatureFile()).thenReturn(signatureTargetFile);

    byte[] licenceFileContent = "LIC_CONTENT".getBytes();
    byte[] signatureFileContent = "SIGNATURE_CONTENT".getBytes();

    MockMultipartFile licenceInputFile = new MockMultipartFile("licence", "jg.lic", "text/plain",
            licenceFileContent);
    MockMultipartFile signatureInputFile = new MockMultipartFile("signature", "jg.lic.sign", "text/plain",
            signatureFileContent);

    when(licenceManagingServiceMock.installLicence(eq(licenceTargetFile.getAbsolutePath()),
            eq(signatureTargetFile.getAbsolutePath()))).thenThrow(UnreliableLicenceException.class);

    // when
    mockMvc.perform(fileUpload("/licence").file(licenceInputFile).file(signatureInputFile))
            .andExpect(status().isInternalServerError())
            .andExpect(jsonPath("$.title", is(UnreliableLicenceException.class.getSimpleName())));

    // then
    assertThat(licenceTargetFile).doesNotExist();
    assertThat(signatureTargetFile).doesNotExist();
}

From source file:org.jtalks.jcommune.service.nontransactional.ImageConverterTest.java

@Test(dataProvider = "parameterResizeImage")
public void testResizeImage(int maxWidth, int maxHeight, int imageType, String format) throws IOException {
    int expectedWidth = 4;
    int expectedHeight = 4;
    imageConverter = new ImageConverter(format, imageType, maxWidth, maxHeight);
    BufferedImage originalImage = ImageIO
            .read(new MockMultipartFile("test_image", "test_image", "image/png", originalImageByteArray)
                    .getInputStream());//from  ww w  .j  ava  2  s .  com
    Image modifiedImage = imageConverter.resizeImage(originalImage, imageType);
    assertEquals(modifiedImage.getWidth(null), expectedWidth);
    assertEquals(modifiedImage.getHeight(null), expectedHeight);
}

From source file:org.jtalks.jcommune.service.nontransactional.ImageServiceTest.java

@DataProvider
public Object[][] invalidFormatValues() {
    return new Object[][] { { new MockMultipartFile("test_image", "test_image", "image/bmp", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "image/tiff", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "text/plain", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "audio/mpeg", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "audio/x-wav", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "text/plain", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "text/html", new byte[10]) },
            { new MockMultipartFile("test_image", "test_image", "video/mpeg", new byte[10]) } };
}