Search in sources :

Example 1 with EditableRepository

use of com.enonic.xp.repository.EditableRepository in project xp by enonic.

the class ModifyRepositoryHandlerTest method modifyScoped.

@Test
void modifyScoped() throws Exception {
    runScript("/lib/xp/examples/repo/modifyScoped.js");
    ArgumentCaptor<UpdateRepositoryParams> captor = ArgumentCaptor.forClass(UpdateRepositoryParams.class);
    Mockito.verify(this.repositoryService, Mockito.times(1)).updateRepository(captor.capture());
    final UpdateRepositoryParams capturedParams = captor.getValue();
    final EditableRepository edited = new EditableRepository(MOCK_REPO);
    capturedParams.getEditor().accept(edited);
    assertEquals("toBeModified", edited.source.getData().getString("myScopedObject.myScopedString"), "Test is invalid");
    assertEquals("modified", edited.data.getString("myScopedObject.myScopedString"));
    assertEquals("toBeKeptValue", edited.data.getString("toBeKept"));
    final BinaryAttachment attachment = edited.binaryAttachments.get(0);
    assertEquals(BinaryReference.from("myFile"), attachment.getReference());
    assertTrue(attachment.getByteSource().contentEquals(ByteSource.wrap("Hello World".getBytes())));
}
Also used : UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) EditableRepository(com.enonic.xp.repository.EditableRepository) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Test(org.junit.jupiter.api.Test)

Example 2 with EditableRepository

use of com.enonic.xp.repository.EditableRepository in project xp by enonic.

the class ModifyRepositoryHandlerTest method modifyRemoveProperty.

@Test
void modifyRemoveProperty() {
    runScript("/lib/xp/examples/repo/modifyRemoveProperty.js");
    ArgumentCaptor<UpdateRepositoryParams> captor = ArgumentCaptor.forClass(UpdateRepositoryParams.class);
    Mockito.verify(this.repositoryService, Mockito.times(1)).updateRepository(captor.capture());
    final UpdateRepositoryParams capturedParams = captor.getValue();
    final EditableRepository edited = new EditableRepository(MOCK_REPO);
    capturedParams.getEditor().accept(edited);
    assertTrue(edited.source.getData().hasProperty("myScopedObject"), "Test is invalid");
    assertFalse(edited.data.hasProperty("myScopedObject"));
}
Also used : UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) EditableRepository(com.enonic.xp.repository.EditableRepository) Test(org.junit.jupiter.api.Test)

Example 3 with EditableRepository

use of com.enonic.xp.repository.EditableRepository in project xp by enonic.

the class RepositoryServiceImpl method doUpdateRepository.

private Repository doUpdateRepository(final UpdateRepositoryParams updateRepositoryParams, Repository previousRepository) {
    RepositoryId repositoryId = updateRepositoryParams.getRepositoryId();
    previousRepository = previousRepository == null ? repositoryEntryService.getRepositoryEntry(repositoryId) : previousRepository;
    if (previousRepository == null) {
        throw new RepositoryNotFoundException(repositoryId);
    }
    final EditableRepository editableRepository = new EditableRepository(previousRepository);
    updateRepositoryParams.getEditor().accept(editableRepository);
    UpdateRepositoryEntryParams params = UpdateRepositoryEntryParams.create().repositoryId(repositoryId).repositoryData(editableRepository.data).attachments(ImmutableList.copyOf(editableRepository.binaryAttachments)).build();
    return repositoryEntryService.updateRepositoryEntry(params);
}
Also used : RepositoryNotFoundException(com.enonic.xp.repository.RepositoryNotFoundException) RepositoryId(com.enonic.xp.repository.RepositoryId) EditableRepository(com.enonic.xp.repository.EditableRepository)

Example 4 with EditableRepository

use of com.enonic.xp.repository.EditableRepository in project xp by enonic.

the class ModifyRepositoryHandlerTest method modify.

@Test
void modify() throws Exception {
    runScript("/lib/xp/examples/repo/modify.js");
    ArgumentCaptor<UpdateRepositoryParams> captor = ArgumentCaptor.forClass(UpdateRepositoryParams.class);
    Mockito.verify(this.repositoryService, Mockito.times(1)).updateRepository(captor.capture());
    final UpdateRepositoryParams capturedParams = captor.getValue();
    assertEquals(RepositoryId.from("my-repo"), capturedParams.getRepositoryId());
    final EditableRepository edited = new EditableRepository(MOCK_REPO);
    capturedParams.getEditor().accept(edited);
    assertSame(MOCK_REPO, edited.source);
    assertEquals("modified", edited.data.getString("myString"));
    assertTrue(edited.source.getData().hasProperty("toBeRemoved"));
    assertFalse(edited.data.hasProperty("toBeRemoved"));
    final BinaryAttachment attachment = edited.binaryAttachments.get(0);
    assertEquals(BinaryReference.from("myFile"), attachment.getReference());
    assertTrue(attachment.getByteSource().contentEquals(ByteSource.wrap("Hello World".getBytes())));
}
Also used : UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) EditableRepository(com.enonic.xp.repository.EditableRepository) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Test(org.junit.jupiter.api.Test)

Aggregations

EditableRepository (com.enonic.xp.repository.EditableRepository)4 UpdateRepositoryParams (com.enonic.xp.repository.UpdateRepositoryParams)3 Test (org.junit.jupiter.api.Test)3 BinaryAttachment (com.enonic.xp.node.BinaryAttachment)2 RepositoryId (com.enonic.xp.repository.RepositoryId)1 RepositoryNotFoundException (com.enonic.xp.repository.RepositoryNotFoundException)1