Search in sources :

Example 6 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class MultipartFormImplTest method testGetWithIndex.

@Test
public void testGetWithIndex() {
    final MultipartItem item1 = this.form.get("upload1", 0);
    assertNotNull(item1);
    assertEquals("upload1", item1.getName());
    assertEquals("hello1", item1.getAsString());
    final MultipartItem item1B = this.form.get("upload1", 1);
    assertNotNull(item1B);
    assertEquals("upload1", item1B.getName());
    assertEquals("bye1", item1B.getAsString());
    final MultipartItem item1C = this.form.get("upload1", 2);
    assertNull(item1C);
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) Test(org.junit.jupiter.api.Test)

Example 7 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class ApplicationResourceTest method install_exception.

@Test
public void install_exception() throws Exception {
    ApplicationResource resource = getResourceInstance();
    MultipartItem multipartItem = Mockito.mock(MultipartItem.class);
    ByteSource byteSource = ByteSource.wrap("bytes".getBytes());
    String fileName = "app-name";
    Mockito.when(multipartItem.getBytes()).thenReturn(byteSource);
    Mockito.when(multipartItem.getFileName()).thenReturn(fileName);
    MultipartForm multipartForm = Mockito.mock(MultipartForm.class);
    Mockito.when(this.applicationService.installGlobalApplication(Mockito.isA(ByteSource.class), eq("app-name"))).thenThrow(new RuntimeException());
    Mockito.when(multipartForm.get("file")).thenReturn(multipartItem);
    ApplicationInstallResultJson result = resource.install(multipartForm);
    assertEquals("Failed to process application app-name", result.getFailure());
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) ApplicationInstallResultJson(com.enonic.xp.impl.server.rest.model.ApplicationInstallResultJson) ByteSource(com.google.common.io.ByteSource) MultipartForm(com.enonic.xp.web.multipart.MultipartForm) Test(org.junit.jupiter.api.Test)

Example 8 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class ApplicationResource method install.

@POST
@Path("install")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public ApplicationInstallResultJson install(final MultipartForm form) {
    final MultipartItem appFile = form.get("file");
    if (appFile == null) {
        throw new RuntimeException("Missing file item");
    }
    final ByteSource byteSource = appFile.getBytes();
    return installApplication(byteSource, appFile.getFileName());
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) ByteSource(com.google.common.io.ByteSource) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 9 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class GetMultipartScriptTest method createItem.

private MultipartItem createItem(final String name, final String fileName, final long size, final String ext, final String type) {
    final MultipartItem item = Mockito.mock(MultipartItem.class);
    Mockito.when(item.getName()).thenReturn(name);
    Mockito.when(item.getFileName()).thenReturn(fileName + "." + ext);
    Mockito.when(item.getContentType()).thenReturn(MediaType.parse(type));
    Mockito.when(item.getSize()).thenReturn(size);
    Mockito.when(item.getBytes()).thenReturn(ByteSource.wrap(name.getBytes()));
    return item;
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem)

Example 10 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class GetMultipartScriptTest method createTextForm.

private void createTextForm() {
    final MultipartForm form = Mockito.mock(MultipartForm.class);
    final MultipartItem item1 = createItem("item1", 10, "txt", "text/plain");
    Mockito.when(item1.getAsString()).thenReturn("Some text");
    Mockito.when(form.iterator()).thenReturn(List.of(item1).iterator());
    Mockito.when(form.get("item1", 0)).thenReturn(item1);
    Mockito.when(this.multipartService.parse(Mockito.any())).thenReturn(form);
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) MultipartForm(com.enonic.xp.web.multipart.MultipartForm)

Aggregations

MultipartItem (com.enonic.xp.web.multipart.MultipartItem)12 MultipartForm (com.enonic.xp.web.multipart.MultipartForm)6 Test (org.junit.jupiter.api.Test)4 ByteSource (com.google.common.io.ByteSource)3 ApplicationInstallResultJson (com.enonic.xp.impl.server.rest.model.ApplicationInstallResultJson)2 Application (com.enonic.xp.app.Application)1 ApplicationInstalledJson (com.enonic.xp.impl.server.rest.model.ApplicationInstalledJson)1 MediaType (com.google.common.net.MediaType)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1