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));
}
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());
}
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());
}
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());
}
Aggregations