use of io.gravitee.repository.management.api.PageRepository in project gravitee-management-rest-api by gravitee-io.
the class PageService_ImportDirectoryTest method shouldImportDirectory.
@Test
public void shouldImportDirectory() throws Exception {
// We mock the validateSafeContent method because the fetcher keeps sending the same json descriptor which is
// not a swagger valid document (and modify the fetcher mock to produce valid desc is overkill)
when(pageService.validateSafeContent(any(), any())).thenReturn(new ArrayList<>());
PageSourceEntity pageSource = new PageSourceEntity();
pageSource.setType("type");
pageSource.setConfiguration(mapper.readTree("{}"));
ImportPageEntity pageEntity = new ImportPageEntity();
pageEntity.setSource(pageSource);
pageEntity.setPublished(true);
FetcherPlugin fetcherPlugin = mock(FetcherPlugin.class);
when(fetcherPlugin.clazz()).thenReturn("io.gravitee.rest.api.service.PageService_ImportDirectoryMockFetcher");
when(fetcherPlugin.configuration()).thenReturn(PageService_MockFilesFetcherConfiguration.class);
when(fetcherPluginManager.get(any())).thenReturn(fetcherPlugin);
Class<PageService_ImportDirectoryMockFetcher> mockFetcherClass = PageService_ImportDirectoryMockFetcher.class;
when(fetcherPlugin.fetcher()).thenReturn(mockFetcherClass);
PageService_MockFilesFetcherConfiguration fetcherConfiguration = new PageService_MockFilesFetcherConfiguration();
when(fetcherConfigurationFactory.create(eq(PageService_MockFilesFetcherConfiguration.class), anyString())).thenReturn(fetcherConfiguration);
AutowireCapableBeanFactory mockAutowireCapableBeanFactory = mock(AutowireCapableBeanFactory.class);
when(applicationContext.getAutowireCapableBeanFactory()).thenReturn(mockAutowireCapableBeanFactory);
Page newPage = mock(Page.class);
PageSource ps = new PageSource();
ps.setType(pageSource.getType());
ps.setConfiguration(pageSource.getConfiguration());
when(newPage.getId()).thenReturn(UuidString.generateRandom());
when(newPage.isPublished()).thenReturn(Boolean.TRUE);
when(newPage.getSource()).thenReturn(ps);
when(newPage.getType()).thenReturn("MARKDOWN");
when(newPage.getReferenceType()).thenReturn(PageReferenceType.ENVIRONMENT);
when(newPage.getReferenceId()).thenReturn("envId");
when(newPage.getVisibility()).thenReturn("PUBLIC");
when(pageRepository.create(any())).thenReturn(newPage);
when(graviteeDescriptorService.descriptorName()).thenReturn(".gravitee.json");
List<PageEntity> pageEntities = pageService.importFiles(pageEntity, GraviteeContext.getCurrentEnvironment());
assertNotNull(pageEntities);
assertEquals(8, pageEntities.size());
verify(searchEngineService, times(8)).index(any(), eq(false));
// //////////////////////
// check Folder creation
// //////////////////////
verify(pageRepository, times(3)).create(argThat(pageToCreate -> "FOLDER".equals(pageToCreate.getType())));
// /src
verify(pageRepository).create(argThat(pageToCreate -> "src".equals(pageToCreate.getName()) && "FOLDER".equals(pageToCreate.getType()) && null == pageToCreate.getParentId()));
// /src/doc
verify(pageRepository).create(argThat(pageToCreate -> "doc".equals(pageToCreate.getName()) && "FOLDER".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
// /src/folder.with.dot/
verify(pageRepository).create(argThat(pageToCreate -> "folder.with.dot".equals(pageToCreate.getName()) && "FOLDER".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
// //////////////////////
// verify files creation
// //////////////////////
verify(pageRepository, times(5)).create(argThat(pageToCreate -> pageToCreate.getType() != null && !"FOLDER".equals(pageToCreate.getType())));
// /src/doc/m1.md
verify(pageRepository).create(argThat(pageToCreate -> "m1".equals(pageToCreate.getName()) && "MARKDOWN".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
// /swagger.json
verify(pageRepository).create(argThat(pageToCreate -> "swagger".equals(pageToCreate.getName()) && "SWAGGER".equals(pageToCreate.getType()) && null == pageToCreate.getParentId()));
// /src/doc/sub.m11.md
verify(pageRepository).create(argThat(pageToCreate -> "sub.m11".equals(pageToCreate.getName()) && "MARKDOWN".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
// /src/doc/m2.yaml
verify(pageRepository).create(argThat(pageToCreate -> "m2".equals(pageToCreate.getName()) && "SWAGGER".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
// /src/folder.with.dot/m2.MD
verify(pageRepository).create(argThat(pageToCreate -> "m2".equals(pageToCreate.getName()) && "MARKDOWN".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
verify(pageRevisionService, times(5)).create(any());
}
use of io.gravitee.repository.management.api.PageRepository in project gravitee-management-rest-api by gravitee-io.
the class PageService_UpdateTest method shouldUpdateOrderIncrement.
@Test
public void shouldUpdateOrderIncrement() throws TechnicalException {
final Page pageOrder1 = new Page();
pageOrder1.setId(PAGE_ID);
pageOrder1.setOrder(1);
pageOrder1.setReferenceId(API_ID);
pageOrder1.setReferenceType(PageReferenceType.API);
pageOrder1.setPublished(true);
pageOrder1.setVisibility("PUBLIC");
final Page pageOrder2 = new Page();
pageOrder2.setId("2");
pageOrder2.setOrder(2);
pageOrder2.setReferenceId(API_ID);
pageOrder2.setReferenceType(PageReferenceType.API);
pageOrder2.setVisibility("PUBLIC");
final Page pageOrder3 = new Page();
pageOrder3.setId("3");
pageOrder3.setOrder(3);
pageOrder3.setReferenceId(API_ID);
pageOrder3.setReferenceType(PageReferenceType.API);
pageOrder3.setVisibility("PUBLIC");
when(pageRepository.findById(PAGE_ID)).thenReturn(Optional.of(pageOrder1));
when(pageRepository.search(argThat(o -> o == null || "LINK".equals(o.getType())))).thenReturn(Collections.emptyList());
when(pageRepository.search(argThat(o -> o == null || API_ID.equals(o.getReferenceId())))).thenReturn(asList(pageOrder1, pageOrder2, pageOrder3));
when(pageRepository.update(any(Page.class))).thenReturn(pageOrder1);
final UpdatePageEntity updatePageEntity = new UpdatePageEntity();
updatePageEntity.setOrder(2);
updatePageEntity.setVisibility(Visibility.PUBLIC);
pageService.update(PAGE_ID, updatePageEntity);
verify(pageRepository, times(3)).update(argThat(pageToUpdate -> {
if (PAGE_ID.equals(pageToUpdate.getId())) {
return pageToUpdate.getOrder() == 2;
}
if ("2".equals(pageToUpdate.getId())) {
return pageToUpdate.getOrder() == 1;
}
if ("3".equals(pageToUpdate.getId())) {
return pageToUpdate.getOrder() == 3;
}
return false;
}));
// neither content nor name are updated
verify(pageRevisionService, times(0)).create(any());
}
use of io.gravitee.repository.management.api.PageRepository 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.repository.management.api.PageRepository in project gravitee-management-rest-api by gravitee-io.
the class PageService_UpdateTest method shouldUpdateRelatedPagesPublicationStatus.
@Test
public void shouldUpdateRelatedPagesPublicationStatus() throws TechnicalException {
Page unpublishedPage = new Page();
unpublishedPage.setId(PAGE_ID);
unpublishedPage.setOrder(1);
unpublishedPage.setReferenceId("DEFAULT");
unpublishedPage.setReferenceType(PageReferenceType.ENVIRONMENT);
unpublishedPage.setType("MARKDOWN");
unpublishedPage.setPublished(false);
unpublishedPage.setVisibility("PUBLIC");
doReturn(Optional.of(unpublishedPage)).when(pageRepository).findById(PAGE_ID);
Page linkPage = new Page();
linkPage.setId("LINK_ID");
linkPage.setOrder(1);
linkPage.setPublished(false);
linkPage.setReferenceId("DEFAULT");
linkPage.setReferenceType(PageReferenceType.ENVIRONMENT);
linkPage.setType("LINK");
Map<String, String> conf = new HashMap<String, String>();
conf.put(PageConfigurationKeys.LINK_RESOURCE_TYPE, "page");
linkPage.setConfiguration(conf);
linkPage.setContent(PAGE_ID);
linkPage.setVisibility("PUBLIC");
Map<String, String> translationConf = new HashMap<String, String>();
translationConf.put(PageConfigurationKeys.TRANSLATION_LANG, "fr");
Page translationPage = new Page();
translationPage.setId("TRANSLATION_ID");
translationPage.setParentId(PAGE_ID);
translationPage.setOrder(1);
translationPage.setPublished(false);
translationPage.setReferenceId("DEFAULT");
translationPage.setReferenceType(PageReferenceType.ENVIRONMENT);
translationPage.setType("TRANSLATION");
translationPage.setConfiguration(translationConf);
translationPage.setVisibility("PUBLIC");
Page linkTranslationPage = new Page();
linkTranslationPage.setId("LINK_TRANSLATION_ID");
linkTranslationPage.setParentId("LINK_ID");
linkTranslationPage.setOrder(2);
linkTranslationPage.setPublished(false);
linkTranslationPage.setReferenceId("DEFAULT");
linkTranslationPage.setReferenceType(PageReferenceType.ENVIRONMENT);
linkTranslationPage.setType("TRANSLATION");
linkTranslationPage.setConfiguration(translationConf);
linkTranslationPage.setVisibility("PUBLIC");
doReturn(asList(linkPage)).when(pageRepository).search(argThat(p -> PageType.LINK.name().equals(p.getType())));
doReturn(asList(translationPage)).when(pageRepository).search(argThat(p -> PageType.TRANSLATION.name().equals(p.getType()) && PAGE_ID.equals(p.getParent())));
doReturn(asList(linkTranslationPage)).when(pageRepository).search(argThat(p -> PageType.TRANSLATION.name().equals(p.getType()) && "LINK_ID".equals(p.getParent())));
UpdatePageEntity updatePageEntity = new UpdatePageEntity();
updatePageEntity.setPublished(true);
updatePageEntity.setContent("");
updatePageEntity.setOrder(1);
updatePageEntity.setConfiguration(conf);
updatePageEntity.setVisibility(Visibility.PUBLIC);
Page updatedPage = new Page();
updatedPage.setId(PAGE_ID);
updatedPage.setOrder(1);
updatedPage.setReferenceId("DEFAULT");
updatedPage.setReferenceType(PageReferenceType.ENVIRONMENT);
updatedPage.setType("TRANSLATION");
updatedPage.setPublished(true);
updatedPage.setVisibility("PUBLIC");
doReturn(updatedPage).when(pageRepository).update(argThat(p -> p.getId().equals(PAGE_ID)));
pageService.update(PAGE_ID, updatePageEntity);
// neither content nor name are updated
verify(pageRevisionService, times(0)).create(any());
verify(pageRepository).update(argThat(p -> p.getId().equals(PAGE_ID) && p.isPublished()));
verify(pageRepository).update(argThat(p -> p.getId().equals("LINK_ID") && p.isPublished()));
verify(pageRepository).update(argThat(p -> p.getId().equals("TRANSLATION_ID") && p.isPublished()));
verify(pageRepository).update(argThat(p -> p.getId().equals("LINK_TRANSLATION_ID") && p.isPublished()));
}
use of io.gravitee.repository.management.api.PageRepository in project gravitee-management-rest-api by gravitee-io.
the class PageService_UpdateTest method shouldNotUnpublishPage.
private void shouldNotUnpublishPage(PlanStatus planStatus) throws TechnicalException {
final String API_ID = "API_ID_TEST";
Page unpublishedPage = new Page();
unpublishedPage.setId(PAGE_ID);
unpublishedPage.setOrder(1);
unpublishedPage.setReferenceId(API_ID);
unpublishedPage.setReferenceType(PageReferenceType.API);
unpublishedPage.setType("MARKDOWN");
unpublishedPage.setPublished(true);
unpublishedPage.setVisibility("PUBLIC");
doReturn(Optional.of(unpublishedPage)).when(pageRepository).findById(PAGE_ID);
UpdatePageEntity updatePageEntity = new UpdatePageEntity();
updatePageEntity.setPublished(false);
updatePageEntity.setOrder(1);
updatePageEntity.setVisibility(Visibility.PUBLIC);
PlanEntity plan = mock(PlanEntity.class);
when(plan.getGeneralConditions()).thenReturn(PAGE_ID);
when(plan.getStatus()).thenReturn(planStatus);
when(planService.findByApi(API_ID)).thenReturn(Sets.newSet(plan));
pageService.update(PAGE_ID, updatePageEntity);
verify(planService).findByApi(argThat(p -> p.equals(API_ID)));
}
Aggregations