Search in sources :

Example 1 with PageRevision

use of io.gravitee.repository.management.model.PageRevision in project gravitee-management-rest-api by gravitee-io.

the class PageRevisionServiceTest method shouldCreateRevision_PortalPage.

@Test
public void shouldCreateRevision_PortalPage() throws TechnicalException {
    Date now = new Date();
    Page page = mock(Page.class);
    when(page.getId()).thenReturn(PAGE_ID);
    when(page.getName()).thenReturn("SomeName");
    when(page.getContent()).thenReturn("SomeContent");
    when(page.getUpdatedAt()).thenReturn(now);
    when(page.getLastContributor()).thenReturn("Author");
    when(page.getType()).thenReturn(PageType.MARKDOWN.name());
    PageRevision newRevision = mock(PageRevision.class);
    when(pageRevisionRepository.findLastByPageId(PAGE_ID)).thenReturn(Optional.empty());
    when(pageRevisionRepository.create(any())).thenReturn(newRevision);
    ArgumentCaptor<PageRevision> newRevisionCaptor = ArgumentCaptor.forClass(PageRevision.class);
    PageRevisionEntity createdRevision = pageRevisionService.create(page);
    assertNotNull(createdRevision);
    verify(pageRevisionRepository).findLastByPageId(PAGE_ID);
    verify(pageRevisionRepository).create(newRevisionCaptor.capture());
    PageRevision createdRev = newRevisionCaptor.getValue();
    assertNotNull(createdRev);
    assertEquals(1, createdRev.getRevision());
}
Also used : Page(io.gravitee.repository.management.model.Page) PageRevision(io.gravitee.repository.management.model.PageRevision) Date(java.util.Date) PageRevisionEntity(io.gravitee.rest.api.model.PageRevisionEntity) Test(org.junit.Test)

Example 2 with PageRevision

use of io.gravitee.repository.management.model.PageRevision in project gravitee-management-rest-api by gravitee-io.

the class PageRevisionServiceTest method shouldCreateRevision_NewIncrementedRevision.

@Test
public void shouldCreateRevision_NewIncrementedRevision() throws TechnicalException {
    Date now = new Date();
    Page page = mock(Page.class);
    when(page.getId()).thenReturn(PAGE_ID);
    when(page.getName()).thenReturn("SomeName");
    when(page.getContent()).thenReturn("SomeContent");
    when(page.getUpdatedAt()).thenReturn(now);
    when(page.getLastContributor()).thenReturn("Author");
    when(page.getType()).thenReturn(PageType.MARKDOWN.name());
    PageRevision lastRevision = mock(PageRevision.class);
    when(lastRevision.getRevision()).thenReturn(2);
    PageRevision newRevision = mock(PageRevision.class);
    when(pageRevisionRepository.findLastByPageId(PAGE_ID)).thenReturn(Optional.of(lastRevision));
    when(pageRevisionRepository.create(any())).thenReturn(newRevision);
    ArgumentCaptor<PageRevision> newRevisionCaptor = ArgumentCaptor.forClass(PageRevision.class);
    PageRevisionEntity createdRevision = pageRevisionService.create(page);
    assertNotNull(createdRevision);
    verify(pageRevisionRepository).findLastByPageId(PAGE_ID);
    verify(pageRevisionRepository).create(newRevisionCaptor.capture());
    PageRevision createdRev = newRevisionCaptor.getValue();
    assertNotNull(createdRev);
    assertEquals(lastRevision.getRevision() + 1, createdRev.getRevision());
}
Also used : Page(io.gravitee.repository.management.model.Page) PageRevision(io.gravitee.repository.management.model.PageRevision) Date(java.util.Date) PageRevisionEntity(io.gravitee.rest.api.model.PageRevisionEntity) Test(org.junit.Test)

Example 3 with PageRevision

use of io.gravitee.repository.management.model.PageRevision in project gravitee-management-rest-api by gravitee-io.

the class PageRevisionServiceImpl method create.

@Override
public PageRevisionEntity create(Page page) {
    try {
        logger.debug("Create page revision for page {}", page.getId());
        PageType type = PageType.valueOf(page.getType());
        if (!(type == PageType.MARKDOWN || type == PageType.SWAGGER || type == PageType.TRANSLATION)) {
            throw new TechnicalManagementException("Invalid page type for revision");
        }
        PageRevision revision = pageRevisionRepository.create(convert(page));
        return convert(revision);
    } catch (TechnicalException e) {
        logger.warn("An error occurs while trying to create a revision for page {}", page.getId(), e);
        throw new TechnicalManagementException("An error occurs while trying to create a page revision", e);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) PageRevision(io.gravitee.repository.management.model.PageRevision) PageType(io.gravitee.rest.api.model.PageType) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with PageRevision

use of io.gravitee.repository.management.model.PageRevision in project gravitee-management-rest-api by gravitee-io.

the class PageRevisionServiceImpl method findAll.

@Override
public io.gravitee.common.data.domain.Page<PageRevisionEntity> findAll(Pageable pageable) {
    logger.debug("get all page revisions with pageable {}", pageable);
    try {
        io.gravitee.common.data.domain.Page<PageRevision> revisions = pageRevisionRepository.findAll(pageable);
        List<PageRevisionEntity> revisionEntities = revisions.getContent().stream().map(this::convert).collect(Collectors.toList());
        return new io.gravitee.common.data.domain.Page<PageRevisionEntity>(revisionEntities, revisions.getPageNumber(), revisionEntities.size(), revisions.getTotalElements());
    } catch (TechnicalException e) {
        logger.warn("An error occurs while trying to get the page revisions {}", pageable, e);
        throw new TechnicalManagementException("An error occurs while trying to get all page revisions", e);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Page(io.gravitee.repository.management.model.Page) PageRevision(io.gravitee.repository.management.model.PageRevision) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) PageRevisionEntity(io.gravitee.rest.api.model.PageRevisionEntity)

Example 5 with PageRevision

use of io.gravitee.repository.management.model.PageRevision in project gravitee-management-rest-api by gravitee-io.

the class PageRevisionServiceImpl method convert.

private PageRevision convert(Page page) {
    PageRevision revision = new PageRevision();
    revision.setPageId(page.getId());
    revision.setRevision(findLastByPageId(page.getId()).map(rev -> rev.getRevision() + 1).orElse(1));
    revision.setName(page.getName());
    revision.setContent(page.getContent());
    revision.setContributor(page.getLastContributor());
    revision.setCreatedAt(page.getUpdatedAt());
    revision.setHash(computeHash(revision));
    return revision;
}
Also used : PageRevision(io.gravitee.repository.management.model.PageRevision)

Aggregations

PageRevision (io.gravitee.repository.management.model.PageRevision)6 Page (io.gravitee.repository.management.model.Page)4 PageRevisionEntity (io.gravitee.rest.api.model.PageRevisionEntity)4 Date (java.util.Date)3 Test (org.junit.Test)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 PageType (io.gravitee.rest.api.model.PageType)1