use of io.gravitee.rest.api.model.documentation.PageQuery in project gravitee-management-rest-api by gravitee-io.
the class ApiResourceTest method shouldGetApiLinks.
@Test
public void shouldGetApiLinks() {
resetAllMocks();
PageEntity sysFolder = new PageEntity();
sysFolder.setId("SYS_FOLDER");
sysFolder.setType("SYSTEM_FOLDER");
sysFolder.setName("SYSTEM_FOLDER");
sysFolder.setPublished(true);
PageEntity linkSysFolder = new PageEntity();
linkSysFolder.setId("LINK_SYS_FOLDER");
linkSysFolder.setParentId("SYS_FOLDER");
linkSysFolder.setType("LINK");
linkSysFolder.setName("LINK");
linkSysFolder.setContent("LINK_RES_REF");
linkSysFolder.setPublished(true);
Map<String, String> linkConf = new HashMap<>();
linkConf.put(PageConfigurationKeys.LINK_RESOURCE_TYPE, "external");
linkSysFolder.setConfiguration(linkConf);
PageEntity swaggerSysFolder = new PageEntity();
swaggerSysFolder.setId("SWAGGER_SYS_FOLDER");
swaggerSysFolder.setParentId("SYS_FOLDER");
swaggerSysFolder.setType("SWAGGER");
swaggerSysFolder.setName("SWAGGER");
swaggerSysFolder.setPublished(true);
PageEntity folderSysFolder = new PageEntity();
folderSysFolder.setId("FOLDER_SYS_FOLDER");
folderSysFolder.setParentId("SYS_FOLDER");
folderSysFolder.setType("FOLDER");
folderSysFolder.setName("FOLDER");
folderSysFolder.setPublished(true);
PageEntity markdownFolderSysFolder = new PageEntity();
markdownFolderSysFolder.setId("MARKDOWN_FOLDER_SYS_FOLDER");
markdownFolderSysFolder.setParentId("FOLDER_SYS_FOLDER");
markdownFolderSysFolder.setType("MARKDOWN");
markdownFolderSysFolder.setName("MARKDOWN");
markdownFolderSysFolder.setPublished(true);
PageEntity markdownTemplate = new PageEntity();
markdownTemplate.setId("MARKDOWN_TEMPLATE");
markdownTemplate.setParentId("SYS_FOLDER");
markdownTemplate.setType("MARKDOWN_TEMPLATE");
markdownTemplate.setName("MARKDOWN_TEMPLATE");
markdownTemplate.setPublished(true);
when(pageService.search(any(PageQuery.class), isNull(), eq(GraviteeContext.getCurrentEnvironment()))).thenAnswer((Answer<List<PageEntity>>) invocation -> {
PageQuery pq = invocation.getArgument(0);
if (PageType.SYSTEM_FOLDER.equals(pq.getType()) && API.equals(pq.getApi())) {
return Collections.singletonList(sysFolder);
} else if ("SYS_FOLDER".equals(pq.getParent()) && API.equals(pq.getApi())) {
return Arrays.asList(linkSysFolder, swaggerSysFolder, folderSysFolder, markdownTemplate);
} else if ("FOLDER_SYS_FOLDER".equals(pq.getParent()) && API.equals(pq.getApi())) {
return Collections.singletonList(markdownFolderSysFolder);
}
return null;
});
when(accessControlService.canAccessPageFromPortal(any())).thenReturn(true);
final Response response = target(API).path("links").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
final LinksResponse links = response.readEntity(LinksResponse.class);
assertNotNull(links);
Map<String, List<CategorizedLinks>> slots = links.getSlots();
assertNotNull(slots);
assertEquals(1, slots.size());
List<CategorizedLinks> sysFolderList = slots.get("system_folder");
assertNotNull(sysFolderList);
assertEquals(2, sysFolderList.size());
CategorizedLinks rootCat = sysFolderList.get(0);
assertNotNull(rootCat);
assertTrue(rootCat.getRoot());
assertEquals("SYSTEM_FOLDER", rootCat.getCategory());
List<Link> rootCatLinks = rootCat.getLinks();
assertNotNull(rootCatLinks);
assertEquals(2, rootCatLinks.size());
Link rootCatLink = rootCatLinks.get(0);
assertNotNull(rootCatLink);
assertEquals("LINK", rootCatLink.getName());
assertEquals("LINK_RES_REF", rootCatLink.getResourceRef());
assertEquals(ResourceTypeEnum.EXTERNAL, rootCatLink.getResourceType());
assertNull(rootCatLink.getFolder());
Link rootCatSwagger = rootCatLinks.get(1);
assertNotNull(rootCatSwagger);
assertEquals("SWAGGER", rootCatSwagger.getName());
assertEquals("SWAGGER_SYS_FOLDER", rootCatSwagger.getResourceRef());
assertEquals(ResourceTypeEnum.PAGE, rootCatSwagger.getResourceType());
CategorizedLinks folderCat = sysFolderList.get(1);
assertNotNull(folderCat);
assertFalse(folderCat.getRoot());
assertEquals("FOLDER", folderCat.getCategory());
List<Link> folderCatLinks = folderCat.getLinks();
assertNotNull(folderCatLinks);
assertEquals(1, folderCatLinks.size());
Link folderCatMarkdown = folderCatLinks.get(0);
assertNotNull(folderCatMarkdown);
assertEquals("MARKDOWN", folderCatMarkdown.getName());
assertEquals("MARKDOWN_FOLDER_SYS_FOLDER", folderCatMarkdown.getResourceRef());
assertEquals(ResourceTypeEnum.PAGE, folderCatMarkdown.getResourceType());
}
use of io.gravitee.rest.api.model.documentation.PageQuery in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method createOrUpdateChildrenPages.
private void createOrUpdateChildrenPages(String apiId, String parentId, List<PageServiceImpl.PageEntityTreeNode> children, String environmentId) {
for (final PageServiceImpl.PageEntityTreeNode child : children) {
PageEntity pageEntityToImport = child.data;
pageEntityToImport.setParentId(parentId);
String newPageEntityId = UuidString.generateForEnvironment(environmentId, apiId, pageEntityToImport.getId());
PageEntity createdOrUpdatedPage = null;
if (pageEntityToImport.getId() != null) {
try {
createdOrUpdatedPage = findById(newPageEntityId);
} catch (PageNotFoundException e) {
// Page not found 🤷 Just create a new one
}
} else {
PageQuery query = new PageQuery.Builder().api(apiId).name(pageEntityToImport.getName()).type(PageType.valueOf(pageEntityToImport.getType())).build();
List<PageEntity> foundPages = search(query, environmentId);
if (foundPages.size() == 1) {
createdOrUpdatedPage = foundPages.get(0);
} else if (foundPages.size() > 1) {
logger.error("Not able to identify the page to update: {}. Too much pages with the same name", pageEntityToImport.getName());
throw new TechnicalManagementException("Not able to identify the page to update: " + pageEntityToImport.getName() + ". Too much pages with the same name");
}
}
if (createdOrUpdatedPage == null) {
createdOrUpdatedPage = createPage(apiId, NewPageEntity.from(pageEntityToImport), environmentId, newPageEntityId);
} else {
createdOrUpdatedPage = update(createdOrUpdatedPage.getId(), UpdatePageEntity.from(pageEntityToImport));
}
if (child.children != null && !child.children.isEmpty()) {
this.createOrUpdateChildrenPages(apiId, createdOrUpdatedPage.getId(), child.children, environmentId);
}
}
}
use of io.gravitee.rest.api.model.documentation.PageQuery in project gravitee-management-rest-api by gravitee-io.
the class PageServiceImpl method search.
private List<PageEntity> search(final PageQuery query, String acceptedLocale, boolean withTranslations, boolean withLinks, String environmentId) {
try {
Stream<Page> pagesStream = pageRepository.search(queryToCriteria(query, environmentId)).stream();
if (!withTranslations) {
pagesStream = pagesStream.filter(page -> !PageType.TRANSLATION.name().equals(page.getType()));
}
if (!withLinks) {
pagesStream = pagesStream.filter(page -> !PageType.LINK.name().equals(page.getType()));
}
List<PageEntity> pages = pagesStream.map(this::convert).collect(Collectors.toList());
if (acceptedLocale == null || acceptedLocale.isEmpty()) {
pages.forEach(p -> {
if (!PageType.TRANSLATION.name().equals(p.getType())) {
List<PageEntity> translations = convert(getTranslations(p.getId()));
if (translations != null && !translations.isEmpty()) {
p.setTranslations(translations);
}
}
});
} else {
pages.forEach(p -> {
if (!PageType.TRANSLATION.name().equals(p.getType())) {
Page translation = getTranslation(p, acceptedLocale);
if (translation != null) {
String translationName = translation.getName();
if (translationName != null && !translationName.isEmpty()) {
p.setName(translationName);
}
String inheritContent = translation.getConfiguration().get(PageConfigurationKeys.TRANSLATION_INHERIT_CONTENT);
if (inheritContent != null && "false".equals(inheritContent)) {
p.setContent(translation.getContent());
}
}
}
});
}
if (query != null && query.getPublished() != null && query.getPublished() || !isAuthenticated()) {
// remove child of unpublished folders
return pages.stream().filter(page -> {
if (page.getParentId() != null) {
PageEntity parent = this.findById(page.getParentId());
if (!isAuthenticated()) {
return parent.isPublished() && Visibility.PUBLIC.equals(parent.getVisibility());
} else {
return parent.isPublished();
}
}
return true;
}).collect(toList());
}
return pages;
} catch (TechnicalException ex) {
logger.error("An error occurs while trying to search pages", ex);
throw new TechnicalManagementException("An error occurs while trying to search pages", ex);
}
}
use of io.gravitee.rest.api.model.documentation.PageQuery in project gravitee-management-rest-api by gravitee-io.
the class ConfigurationResourceTest method shouldGetPortalLinks.
@Test
public void shouldGetPortalLinks() {
resetAllMocks();
PageEntity sysFolder = new PageEntity();
sysFolder.setId("SYS_FOLDER");
sysFolder.setType("SYSTEM_FOLDER");
sysFolder.setName("SYSTEM_FOLDER");
sysFolder.setPublished(true);
PageEntity linkSysFolder = new PageEntity();
linkSysFolder.setId("LINK_SYS_FOLDER");
linkSysFolder.setParentId("SYS_FOLDER");
linkSysFolder.setType("LINK");
linkSysFolder.setName("LINK");
linkSysFolder.setContent("LINK_RES_REF");
linkSysFolder.setPublished(true);
Map<String, String> linkConf = new HashMap<>();
linkConf.put(PageConfigurationKeys.LINK_RESOURCE_TYPE, "external");
linkSysFolder.setConfiguration(linkConf);
PageEntity swaggerSysFolder = new PageEntity();
swaggerSysFolder.setId("SWAGGER_SYS_FOLDER");
swaggerSysFolder.setParentId("SYS_FOLDER");
swaggerSysFolder.setType("SWAGGER");
swaggerSysFolder.setName("SWAGGER");
swaggerSysFolder.setPublished(true);
PageEntity folderSysFolder = new PageEntity();
folderSysFolder.setId("FOLDER_SYS_FOLDER");
folderSysFolder.setParentId("SYS_FOLDER");
folderSysFolder.setType("FOLDER");
folderSysFolder.setName("FOLDER");
folderSysFolder.setPublished(true);
PageEntity markdownFolderSysFolder = new PageEntity();
markdownFolderSysFolder.setId("MARKDOWN_FOLDER_SYS_FOLDER");
markdownFolderSysFolder.setParentId("FOLDER_SYS_FOLDER");
markdownFolderSysFolder.setType("MARKDOWN");
markdownFolderSysFolder.setName("MARKDOWN");
markdownFolderSysFolder.setPublished(true);
PageEntity markdownTemplate = new PageEntity();
markdownTemplate.setId("MARKDOWN_TEMPLATE");
markdownTemplate.setParentId("SYS_FOLDER");
markdownTemplate.setType("MARKDOWN_TEMPLATE");
markdownTemplate.setName("MARKDOWN_TEMPLATE");
markdownTemplate.setPublished(true);
when(pageService.search(any(PageQuery.class), isNull(), eq(GraviteeContext.getCurrentEnvironment()))).thenAnswer((Answer<List<PageEntity>>) invocation -> {
PageQuery pq = invocation.getArgument(0);
if (PageType.SYSTEM_FOLDER.equals(pq.getType())) {
return Arrays.asList(sysFolder);
} else if ("SYS_FOLDER".equals(pq.getParent())) {
return Arrays.asList(linkSysFolder, swaggerSysFolder, folderSysFolder, markdownTemplate);
} else if ("FOLDER_SYS_FOLDER".equals(pq.getParent())) {
return Arrays.asList(markdownFolderSysFolder);
}
return null;
});
when(accessControlService.canAccessPageFromPortal(any())).thenReturn(true);
final Response response = target("/links").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
final LinksResponse links = response.readEntity(LinksResponse.class);
assertNotNull(links);
Map<String, List<CategorizedLinks>> slots = links.getSlots();
assertNotNull(slots);
assertEquals(1, slots.size());
List<CategorizedLinks> sysFolderList = slots.get("system_folder");
assertNotNull(sysFolderList);
assertEquals(2, sysFolderList.size());
CategorizedLinks rootCat = sysFolderList.get(0);
assertNotNull(rootCat);
assertTrue(rootCat.getRoot());
assertEquals("SYSTEM_FOLDER", rootCat.getCategory());
List<Link> rootCatLinks = rootCat.getLinks();
assertNotNull(rootCatLinks);
assertEquals(2, rootCatLinks.size());
Link rootCatLink = rootCatLinks.get(0);
assertNotNull(rootCatLink);
assertEquals("LINK", rootCatLink.getName());
assertEquals("LINK_RES_REF", rootCatLink.getResourceRef());
assertEquals(ResourceTypeEnum.EXTERNAL, rootCatLink.getResourceType());
assertNull(rootCatLink.getFolder());
Link rootCatSwagger = rootCatLinks.get(1);
assertNotNull(rootCatSwagger);
assertEquals("SWAGGER", rootCatSwagger.getName());
assertEquals("SWAGGER_SYS_FOLDER", rootCatSwagger.getResourceRef());
assertEquals(ResourceTypeEnum.PAGE, rootCatSwagger.getResourceType());
CategorizedLinks folderCat = sysFolderList.get(1);
assertNotNull(folderCat);
assertFalse(folderCat.getRoot());
assertEquals("FOLDER", folderCat.getCategory());
List<Link> folderCatLinks = folderCat.getLinks();
assertNotNull(folderCatLinks);
assertEquals(1, folderCatLinks.size());
Link folderCatMarkdown = folderCatLinks.get(0);
assertNotNull(folderCatMarkdown);
assertEquals("MARKDOWN", folderCatMarkdown.getName());
assertEquals("MARKDOWN_FOLDER_SYS_FOLDER", folderCatMarkdown.getResourceRef());
assertEquals(ResourceTypeEnum.PAGE, folderCatMarkdown.getResourceType());
}
use of io.gravitee.rest.api.model.documentation.PageQuery in project gravitee-management-rest-api by gravitee-io.
the class DocumentationSystemFolderUpgrader method upgrade.
@Override
public boolean upgrade() {
PageQuery query = new PageQuery.Builder().type(PageType.SYSTEM_FOLDER).build();
// searching for system folders.
if (pageService.search(query, GraviteeContext.getCurrentEnvironment()).isEmpty()) {
logger.info("No system folders found. Add system folders in documentation, for portal and each API.");
// Portal documentation
Map<SystemFolderType, String> systemFolderIds = pageService.initialize(GraviteeContext.getDefaultEnvironment());
String headerSystemFolderId = systemFolderIds.get(SystemFolderType.HEADER);
String topFooterSystemFolderId = systemFolderIds.get(SystemFolderType.TOPFOOTER);
// Create link to existing documentation in footer
List<PageEntity> pagesToLink = pageService.search(new PageQuery.Builder().homepage(false).rootParent(true).build(), GraviteeContext.getCurrentEnvironment()).stream().filter(p -> PageType.SWAGGER.name().equals(p.getType()) || PageType.MARKDOWN.name().equals(p.getType())).collect(Collectors.toList());
if (!pagesToLink.isEmpty()) {
PageEntity docFolder = createFolder(topFooterSystemFolderId, "Documentation");
pagesToLink.forEach(page -> createLink(docFolder.getId(), page.getName(), page.getId(), "page", Boolean.FALSE, Boolean.TRUE));
}
// Create link to root documentation folder in header
createLink(headerSystemFolderId, "Documentation", "root", "page", Boolean.TRUE, Boolean.FALSE);
// Apis documentation
apiService.findAllLight().forEach(api -> pageService.createSystemFolder(api.getId(), SystemFolderType.ASIDE, 0, GraviteeContext.getDefaultEnvironment()));
}
return true;
}
Aggregations