Search in sources :

Example 1 with PageNotFoundException

use of io.gravitee.rest.api.service.exceptions.PageNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class PageService_CreateOrUpdatePagesTest method shouldCreateOrUpdatePages.

@Test
public void shouldCreateOrUpdatePages() throws TechnicalException {
    PageEntity page1 = new PageEntity();
    page1.setId(UuidString.generateRandom());
    page1.setName("Page 1");
    page1.setType("SWAGGER");
    page1.setReferenceType("API");
    page1.setReferenceId(API_ID);
    PageEntity page2 = new PageEntity();
    page2.setId(UuidString.generateRandom());
    page2.setName("Page 2");
    page2.setType("MARKDOWN");
    page2.setReferenceType("API");
    page2.setReferenceId(API_ID);
    PageEntity page3 = new PageEntity();
    page3.setId(UuidString.generateRandom());
    page3.setName("Sub Page 3");
    page3.setType("ASCIIDOC");
    page3.setParentId(page1.getId());
    page3.setReferenceType("API");
    page3.setReferenceId(API_ID);
    when(pageRepository.create(any(Page.class))).thenAnswer(returnsFirstArg());
    when(pageRepository.update(any(Page.class))).thenAnswer(returnsFirstArg());
    String page1NewId = UuidString.generateForEnvironment(ENVIRONMENT_ID, API_ID, page1.getId());
    Page page = new Page();
    page.setId(page1NewId);
    page.setName(page1.getName());
    page.setType(page1.getType());
    page.setReferenceType(PageReferenceType.valueOf(page1.getReferenceType()));
    page.setReferenceId(page1.getReferenceId());
    page.setVisibility("PUBLIC");
    // Simulate the fact that page 1 is already created
    when(pageRepository.findById(page1NewId)).thenReturn(Optional.of(page));
    when(pageRepository.findById(argThat(id -> !id.equals(page1NewId)))).thenThrow(new PageNotFoundException(""));
    when(planService.findByApi(anyString())).thenReturn(Collections.emptySet());
    pageService.createOrUpdatePages(List.of(page1, page2, page3), ENVIRONMENT_ID, API_ID);
    ArgumentCaptor<Page> createdPagesCaptor = ArgumentCaptor.forClass(Page.class);
    verify(pageRepository, times(2)).create(createdPagesCaptor.capture());
    List<Page> createdPages = createdPagesCaptor.getAllValues();
    assertThat(createdPages.size()).isEqualTo(2);
    assertThat(createdPages).extracting(Page::getName).containsExactly("Sub Page 3", "Page 2");
    // New parent id of page 3 must be new id of page 1
    assertThat(createdPages.get(0)).extracting(Page::getParentId).isEqualTo(page1NewId);
    assertThat(createdPages).extracting(Page::getId).doesNotContain(page2.getId(), page3.getId());
    ArgumentCaptor<Page> updatedPageCaptor = ArgumentCaptor.forClass(Page.class);
    verify(pageRepository, times(1)).update(updatedPageCaptor.capture());
    List<Page> updatedPages = updatedPageCaptor.getAllValues();
    assertThat(updatedPages.size()).isEqualTo(1);
    assertThat(updatedPages).extracting(Page::getName, Page::getId).containsExactly(tuple("Page 1", page1NewId));
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) PageEntity(io.gravitee.rest.api.model.PageEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) PageServiceImpl(io.gravitee.rest.api.service.impl.PageServiceImpl) org.mockito(org.mockito) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) AssertionsForClassTypes.tuple(org.assertj.core.api.AssertionsForClassTypes.tuple) AdditionalAnswers.returnsFirstArg(org.mockito.AdditionalAnswers.returnsFirstArg) PageReferenceType(io.gravitee.repository.management.model.PageReferenceType) Mockito(org.mockito.Mockito) List(java.util.List) Page(io.gravitee.repository.management.model.Page) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) PageRepository(io.gravitee.repository.management.api.PageRepository) Optional(java.util.Optional) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) Page(io.gravitee.repository.management.model.Page) UuidString(io.gravitee.rest.api.service.common.UuidString) Test(org.junit.Test)

Example 2 with PageNotFoundException

use of io.gravitee.rest.api.service.exceptions.PageNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiPageResourceTest method shouldNotFoundPageWhileGettingApiPageContent.

@Test
public void shouldNotFoundPageWhileGettingApiPageContent() {
    doThrow(new PageNotFoundException(UNKNOWN_PAGE)).when(pageService).findById(UNKNOWN_PAGE, null);
    final Response response = target(API).path("pages").path(UNKNOWN_PAGE).path("content").request().get();
    assertEquals(NOT_FOUND_404, response.getStatus());
    ErrorResponse errorResponse = response.readEntity(ErrorResponse.class);
    List<Error> errors = errorResponse.getErrors();
    assertNotNull(errors);
    assertEquals(1, errors.size());
    Error error = errors.get(0);
    assertEquals("errors.page.notFound", error.getCode());
    assertEquals("404", error.getStatus());
    assertEquals("Page [" + UNKNOWN_PAGE + "] can not be found.", error.getMessage());
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) Error(io.gravitee.rest.api.portal.rest.model.Error) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Test(org.junit.Test)

Example 3 with PageNotFoundException

use of io.gravitee.rest.api.service.exceptions.PageNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class PageResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    PageEntity publishedPage = new PageEntity();
    publishedPage.setPublished(true);
    publishedPage.setVisibility(Visibility.PUBLIC);
    publishedPage.setContent(PAGE_CONTENT);
    doReturn(publishedPage).when(pageService).findById(PUBLISHED_PAGE, null);
    PageEntity unPublishedPage = new PageEntity();
    unPublishedPage.setPublished(false);
    publishedPage.setVisibility(Visibility.PUBLIC);
    unPublishedPage.setContent(PAGE_CONTENT);
    doReturn(unPublishedPage).when(pageService).findById(UNPUBLISHED_PAGE, null);
    mockAnotherPage = new PageEntity();
    mockAnotherPage.setPublished(true);
    publishedPage.setVisibility(Visibility.PUBLIC);
    Map<String, String> metadataMap = new HashMap<>();
    metadataMap.put(ANOTHER_PAGE, ANOTHER_PAGE);
    mockAnotherPage.setMetadata(metadataMap);
    doReturn(mockAnotherPage).when(pageService).findById(ANOTHER_PAGE, null);
    doThrow(new PageNotFoundException(UNKNOWN_PAGE)).when(pageService).findById(UNKNOWN_PAGE, null);
    doReturn(new Page()).when(pageMapper).convert(any(), any(), any());
    doReturn(new PageLinks()).when(pageMapper).computePageLinks(any(), any());
    doReturn(true).when(accessControlService).canAccessApiFromPortal(anyString());
    doReturn(true).when(accessControlService).canAccessPageFromPortal(any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) HashMap(java.util.HashMap) Page(io.gravitee.rest.api.portal.rest.model.Page) PageLinks(io.gravitee.rest.api.portal.rest.model.PageLinks) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Before(org.junit.Before)

Example 4 with PageNotFoundException

use of io.gravitee.rest.api.service.exceptions.PageNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiPageResourceTest method shouldNotFoundPageWhileGettingApiPage.

@Test
public void shouldNotFoundPageWhileGettingApiPage() {
    doThrow(new PageNotFoundException(UNKNOWN_PAGE)).when(pageService).findById(UNKNOWN_PAGE, null);
    final Response response = target(API).path("pages").path(UNKNOWN_PAGE).request().get();
    assertEquals(NOT_FOUND_404, response.getStatus());
    ErrorResponse errorResponse = response.readEntity(ErrorResponse.class);
    List<Error> errors = errorResponse.getErrors();
    assertNotNull(errors);
    assertEquals(1, errors.size());
    Error error = errors.get(0);
    assertEquals("errors.page.notFound", error.getCode());
    assertEquals("404", error.getStatus());
    assertEquals("Page [" + UNKNOWN_PAGE + "] can not be found.", error.getMessage());
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) Error(io.gravitee.rest.api.portal.rest.model.Error) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Test(org.junit.Test)

Aggregations

PageNotFoundException (io.gravitee.rest.api.service.exceptions.PageNotFoundException)4 Test (org.junit.Test)3 PageEntity (io.gravitee.rest.api.model.PageEntity)2 Error (io.gravitee.rest.api.portal.rest.model.Error)2 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)2 Response (javax.ws.rs.core.Response)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 PageRepository (io.gravitee.repository.management.api.PageRepository)1 Page (io.gravitee.repository.management.model.Page)1 PageReferenceType (io.gravitee.repository.management.model.PageReferenceType)1 Page (io.gravitee.rest.api.portal.rest.model.Page)1 PageLinks (io.gravitee.rest.api.portal.rest.model.PageLinks)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 PageServiceImpl (io.gravitee.rest.api.service.impl.PageServiceImpl)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 AssertionsForClassTypes.tuple (org.assertj.core.api.AssertionsForClassTypes.tuple)1