use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class APIProviderImplTest method testSaveGraphqlSchemaDefinition.
@Test
public void testSaveGraphqlSchemaDefinition() throws Exception {
Resource resource = new ResourceImpl();
String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + "admin" + RegistryConstants.PATH_SEPARATOR + "API1" + RegistryConstants.PATH_SEPARATOR + "1.0.0" + RegistryConstants.PATH_SEPARATOR;
String schemaContent = "sample schema";
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
Mockito.when(APIUtil.getGraphqlDefinitionFilePath("API1", "1.0.0", "admin")).thenReturn(resourcePath);
Resource resourceMock = Mockito.mock(Resource.class);
resourceMock.setContent(schemaContent);
resourceMock.setMediaType(String.valueOf(ContentType.TEXT_PLAIN));
ServiceReferenceHolder sh = PowerMockito.mock(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(sh);
RegistryService registryService = Mockito.mock(RegistryService.class);
PowerMockito.when(sh.getRegistryService()).thenReturn(registryService);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
Mockito.when(userRegistry.newResource()).thenReturn(resource);
PowerMockito.doNothing().when(APIUtil.class, "clearResourcePermissions", Mockito.any(), Mockito.any(), Mockito.anyInt());
PowerMockito.doNothing().when(APIUtil.class, "setResourcePermissions", Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
GraphQLSchemaDefinition graphQLSchemaDefinition = Mockito.mock(GraphQLSchemaDefinition.class);
PowerMockito.doCallRealMethod().when(graphQLSchemaDefinition).saveGraphQLSchemaDefinition(api, schemaContent, userRegistry);
// org.wso2.carbon.registry.api.RegistryException
Mockito.doThrow(RegistryException.class).when(registry).put(Matchers.anyString(), any(Resource.class));
try {
graphQLSchemaDefinition.saveGraphQLSchemaDefinition(api, schemaContent, registry);
} catch (APIManagementException e) {
String msg = "Error while adding Graphql Definition for API1-1.0.0";
Assert.assertEquals(msg, e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class ExportUtils method addDocumentationToArchive.
/**
* Retrieve documentation for the exporting API or API Product and store it in the archive directory.
* FILE, INLINE, MARKDOWN and URL documentations are handled.
*
* @param archivePath File path to export the documents
* @param identifier ID of the requesting API or API Product
* @param exportFormat Format for export
* @param apiProvider API Provider
* @param type Type of the Project (whether an API or an API Product)
* @throws APIImportExportException If an error occurs while retrieving documents from the
* registry or storing in the archive directory
* @throws APIManagementException If an error occurs while retrieving document details
*/
public static void addDocumentationToArchive(String archivePath, Identifier identifier, ExportFormat exportFormat, APIProvider apiProvider, String type) throws APIImportExportException, APIManagementException {
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
List<Documentation> docList = StringUtils.equals(type, APIConstants.API_IDENTIFIER_TYPE) ? apiProvider.getAllDocumentation(identifier.getUUID(), tenantDomain) : apiProvider.getAllDocumentation(identifier);
if (!docList.isEmpty()) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String docDirectoryPath = archivePath + File.separator + ImportExportConstants.DOCUMENT_DIRECTORY;
CommonUtil.createDirectory(docDirectoryPath);
try {
for (Documentation doc : docList) {
// Retrieving the document again since objects in docList might have missing fields
Documentation individualDocument = apiProvider.getDocumentation(identifier.getUUID(), doc.getId(), tenantDomain);
String sourceType = individualDocument.getSourceType().name();
String resourcePath = null;
InputStream inputStream = null;
String localFileName = null;
String individualDocDirectoryPath = docDirectoryPath + File.separator + cleanFolderName(individualDocument.getName());
CommonUtil.createDirectory(individualDocDirectoryPath);
DocumentationContent documentationContent = apiProvider.getDocumentationContent(identifier.getUUID(), doc.getId(), tenantDomain);
if (documentationContent != null) {
if (Documentation.DocumentSourceType.FILE.toString().equalsIgnoreCase(sourceType)) {
localFileName = individualDocument.getFilePath().substring(individualDocument.getFilePath().lastIndexOf(RegistryConstants.PATH_SEPARATOR) + 1);
inputStream = documentationContent.getResourceFile().getContent();
individualDocument.setFilePath(localFileName);
} else if (Documentation.DocumentSourceType.INLINE.toString().equalsIgnoreCase(sourceType) || Documentation.DocumentSourceType.MARKDOWN.toString().equalsIgnoreCase(sourceType)) {
// Inline/Markdown content file name would be same as the documentation name
localFileName = individualDocument.getName();
inputStream = new ByteArrayInputStream(documentationContent.getTextContent().getBytes());
}
}
CommonUtil.writeDtoToFile(individualDocDirectoryPath + ImportExportConstants.DOCUMENT_FILE_NAME, exportFormat, ImportExportConstants.TYPE_DOCUMENTS, DocumentationMappingUtil.fromDocumentationToDTO(individualDocument));
if (inputStream != null) {
// Check whether resource exists in the registry
try (OutputStream outputStream = new FileOutputStream(individualDocDirectoryPath + File.separator + localFileName)) {
IOUtils.copy(inputStream, outputStream);
}
} else {
// Log error and avoid throwing as we give capability to export document artifact without
// the content if does not exists
log.error("Documentation resource for API/API Product: " + identifier.getName() + " not found in " + resourcePath);
}
}
if (log.isDebugEnabled()) {
log.debug("Documentation retrieved successfully for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion());
}
} catch (IOException e) {
throw new APIImportExportException("I/O error while writing documentation to file for API/API Product: " + identifier.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + identifier.getVersion(), e);
}
} else if (log.isDebugEnabled()) {
log.debug("No documentation found for API/API Product: " + identifier + ". Skipping documentation export.");
}
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class APIMappingUtil method fromResourcePathListToDTO.
/**
* Converts a List object of API resource paths into a DTO.
*
* @param resourcePathList List of API resource paths
* @param limit maximum number of API resource paths to be returned
* @param offset starting index
* @return ResourcePathListDTO object containing ResourcePathDTOs
*/
public static ResourcePathListDTO fromResourcePathListToDTO(List<ResourcePath> resourcePathList, int limit, int offset) {
ResourcePathListDTO resourcePathListDTO = new ResourcePathListDTO();
List<ResourcePathDTO> resourcePathDTOs = new ArrayList<ResourcePathDTO>();
// identifying the proper start and end indexes
int size = resourcePathList.size();
int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
for (int i = start; i <= end; i++) {
ResourcePath path = resourcePathList.get(i);
ResourcePathDTO dto = new ResourcePathDTO();
dto.setId(path.getId());
dto.setResourcePath(path.getResourcePath());
dto.setHttpVerb(path.getHttpVerb());
resourcePathDTOs.add(dto);
}
resourcePathListDTO.setCount(resourcePathDTOs.size());
resourcePathListDTO.setList(resourcePathDTOs);
return resourcePathListDTO;
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class AbstractAPIManager method uploadWsdl.
/**
* Create a wsdl in the path specified.
*
* @param resourcePath Registry path of the resource
* @param wsdlDefinition wsdl content
*/
@Override
public void uploadWsdl(String resourcePath, String wsdlDefinition) throws APIManagementException {
try {
Resource resource = registry.newResource();
resource.setContent(wsdlDefinition);
resource.setMediaType(String.valueOf(ContentType.APPLICATION_XML));
registry.put(resourcePath, resource);
} catch (RegistryException e) {
String msg = "Error while uploading wsdl to from the registry ";
throw new APIManagementException(msg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.ResourcePath in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAllWsdls.
/**
* Returns list of wsdls
*
* @return list of wsdl objects or null
* @throws APIManagementException If unable to return satisfied wsdl object list
*/
@Override
public List<Wsdl> getAllWsdls() throws APIManagementException {
List<Wsdl> wsdlList = new ArrayList<Wsdl>();
String resourcePath = APIConstants.API_WSDL_RESOURCE;
try {
if (registry.resourceExists(resourcePath)) {
Resource wsdlResource = registry.get(resourcePath);
if (wsdlResource instanceof Collection) {
String[] wsdlCollection = ((Collection) wsdlResource).getChildren();
if (wsdlCollection.length > 0) {
for (String wsdlFile : wsdlCollection) {
Resource wsdlResourceFile = registry.get(wsdlFile);
String uuid = wsdlResourceFile.getUUID();
Wsdl wsdl = new Wsdl();
String wsdlName = wsdlFile.substring(wsdlFile.lastIndexOf("/") + 1);
wsdl.setUuid(uuid);
wsdl.setName(wsdlName);
wsdlList.add(wsdl);
}
}
}
}
} catch (RegistryException e) {
String msg = "Failed to get wsdl list";
throw new APIManagementException(msg, e);
}
return wsdlList;
}
Aggregations