Search in sources :

Example 6 with CreateMediaParams

use of com.enonic.xp.content.CreateMediaParams in project xp by enonic.

the class CreateMediaHandlerTest method createMediaAsPDFDocument.

@Test
public void createMediaAsPDFDocument() {
    Mockito.when(this.contentService.create(Mockito.any(CreateMediaParams.class))).thenAnswer(mock -> {
        final CreateMediaParams params = (CreateMediaParams) mock.getArguments()[0];
        final PropertySet attachmentSet = new PropertySet();
        attachmentSet.addString("attachment", params.getName());
        final PropertyTree propertyTree = new PropertyTree();
        propertyTree.addSet("media", attachmentSet);
        return Media.create().id(ContentId.from("dbc077af-fb97-4b17-a567-ad69e85f1010")).name(params.getName()).parentPath(params.getParent()).type(ContentTypeName.documentMedia()).displayName(params.getName()).valid(true).creator(PrincipalKey.ofAnonymous()).data(propertyTree).attachments(Attachments.create().add(Attachment.create().name("documentName.pdf").label("source").mimeType("application/pdf").size(653453).build()).build()).createdTime(Instant.parse("1975-01-08T00:00:00Z")).build();
    });
    runFunction("/test/CreateMediaHandlerTest.js", "createMediaAsPDF");
}
Also used : CreateMediaParams(com.enonic.xp.content.CreateMediaParams) PropertyTree(com.enonic.xp.data.PropertyTree) PropertySet(com.enonic.xp.data.PropertySet) Test(org.junit.jupiter.api.Test)

Example 7 with CreateMediaParams

use of com.enonic.xp.content.CreateMediaParams in project xp by enonic.

the class CreateMediaHandler method createParams.

private CreateMediaParams createParams(final String name) {
    final CreateMediaParams params = new CreateMediaParams();
    params.name(name);
    params.parent(this.parentPath != null ? ContentPath.from(this.parentPath) : null);
    params.mimeType(this.mimeType);
    params.byteSource(this.data);
    params.focalX(this.focalX);
    params.focalY(this.focalY);
    return params;
}
Also used : CreateMediaParams(com.enonic.xp.content.CreateMediaParams)

Example 8 with CreateMediaParams

use of com.enonic.xp.content.CreateMediaParams in project xp by enonic.

the class CreateMediaHandler method doExecute.

@Override
protected Object doExecute() {
    String name = this.name;
    Content result = null;
    final ContentPath parent = this.parentPath != null ? ContentPath.from(this.parentPath) : ContentPath.ROOT;
    while (result == null) {
        final CreateMediaParams params = createParams(name);
        try {
            result = this.contentService.create(params);
        } catch (ContentAlreadyExistsException e) {
            name = generateUniqueContentName(this.idGenerator, parent, this.name);
        }
    }
    return new ContentMapper(result);
}
Also used : CreateMediaParams(com.enonic.xp.content.CreateMediaParams) Content(com.enonic.xp.content.Content) ContentAlreadyExistsException(com.enonic.xp.content.ContentAlreadyExistsException) ContentMapper(com.enonic.xp.lib.content.mapper.ContentMapper) ContentPath(com.enonic.xp.content.ContentPath)

Example 9 with CreateMediaParams

use of com.enonic.xp.content.CreateMediaParams in project xp by enonic.

the class ContentServiceImplTest_media method create_media_document.

@Test
public void create_media_document() throws Exception {
    final CreateMediaParams createMediaParams = new CreateMediaParams();
    createMediaParams.byteSource(loadImage("document.pdf")).name("document.pdf").mimeType("application/pdf").parent(ContentPath.ROOT);
    Mockito.when(this.xDataService.getFromContentType(Mockito.any(ContentType.class))).thenReturn(XDatas.empty());
    final Content content = this.contentService.create(createMediaParams);
    final Content storedContent = this.contentService.getById(content.getId());
    assertNotNull(storedContent.getName());
    assertNotNull(storedContent.getCreatedTime());
    assertNotNull(storedContent.getCreator());
    assertNotNull(storedContent.getModifiedTime());
    assertNotNull(storedContent.getModifier());
    assertNotNull(storedContent.getData().getString(ContentPropertyNames.MEDIA));
    final Attachments attachments = storedContent.getAttachments();
    assertEquals(1, attachments.getSize());
}
Also used : CreateMediaParams(com.enonic.xp.content.CreateMediaParams) ContentType(com.enonic.xp.schema.content.ContentType) Content(com.enonic.xp.content.Content) Attachments(com.enonic.xp.attachment.Attachments) Test(org.junit.jupiter.api.Test)

Example 10 with CreateMediaParams

use of com.enonic.xp.content.CreateMediaParams in project xp by enonic.

the class ContentServiceImplTest_media method audit_data.

@Test
public void audit_data() throws Exception {
    final ArgumentCaptor<LogAuditLogParams> captor = ArgumentCaptor.forClass(LogAuditLogParams.class);
    final CreateMediaParams createMediaParams = new CreateMediaParams();
    createMediaParams.byteSource(loadImage("cat-small.jpg")).name("Small cat").parent(ContentPath.ROOT);
    final Content content = this.contentService.create(createMediaParams);
    Mockito.verify(auditLogService, Mockito.timeout(5000).atLeast(15)).log(captor.capture());
    final PropertySet logResultSet = captor.getAllValues().stream().filter(log -> log.getType().equals("system.content.create")).findFirst().get().getData().getSet("result");
    assertEquals(content.getId().toString(), logResultSet.getString("id"));
    assertEquals(content.getPath().toString(), logResultSet.getString("path"));
}
Also used : CreateMediaParams(com.enonic.xp.content.CreateMediaParams) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) Content(com.enonic.xp.content.Content) PropertySet(com.enonic.xp.data.PropertySet) Test(org.junit.jupiter.api.Test)

Aggregations

CreateMediaParams (com.enonic.xp.content.CreateMediaParams)13 Test (org.junit.jupiter.api.Test)10 Content (com.enonic.xp.content.Content)8 ContentType (com.enonic.xp.schema.content.ContentType)7 Attachments (com.enonic.xp.attachment.Attachments)4 ContentAlreadyExistsException (com.enonic.xp.content.ContentAlreadyExistsException)2 UpdateMediaParams (com.enonic.xp.content.UpdateMediaParams)2 PropertySet (com.enonic.xp.data.PropertySet)2 Attachment (com.enonic.xp.attachment.Attachment)1 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)1 ContentPath (com.enonic.xp.content.ContentPath)1 Media (com.enonic.xp.content.Media)1 ContentConfig (com.enonic.xp.core.impl.content.ContentConfig)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 ContentMapper (com.enonic.xp.lib.content.mapper.ContentMapper)1