use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.
the class TestMappingUtilTestCase method testDocumentInfoToDocumentDTOInfoMappingAndViceVersa.
@Test(description = "DocumentInfo to DocumentDTO mapping and vice versa")
void testDocumentInfoToDocumentDTOInfoMappingAndViceVersa() {
DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().build();
DocumentDTO documentDTO = MappingUtil.toDocumentDTO(documentInfo);
// Test DocumentInfo to DocumentDTO mapping
assertEquals(documentInfo.getName(), documentDTO.getName());
assertEquals(documentInfo.getId(), documentDTO.getDocumentId());
assertEquals(documentInfo.getOtherType(), documentDTO.getOtherTypeName());
assertEquals(documentInfo.getSourceType().getType(), documentDTO.getSourceType().name());
assertEquals(documentInfo.getSourceURL(), documentDTO.getSourceUrl());
assertEquals(documentInfo.getFileName(), documentDTO.getFileName());
assertEquals(documentInfo.getSummary(), documentDTO.getSummary());
assertEquals(documentInfo.getVisibility().toString(), documentDTO.getVisibility().name());
assertEquals(documentInfo.getType().toString(), documentDTO.getType().name());
// Test DocumentDTO to DocumentInfo mapping
DocumentInfo mappedDocumentInfo = MappingUtil.toDocumentInfo(documentDTO);
assertEquals(mappedDocumentInfo.getName(), documentDTO.getName());
assertEquals(mappedDocumentInfo.getId(), documentDTO.getDocumentId());
assertEquals(mappedDocumentInfo.getOtherType(), documentDTO.getOtherTypeName());
assertEquals(mappedDocumentInfo.getSourceType().getType(), documentDTO.getSourceType().name());
assertEquals(mappedDocumentInfo.getSourceURL(), documentDTO.getSourceUrl());
assertEquals(mappedDocumentInfo.getFileName(), documentDTO.getFileName());
assertEquals(mappedDocumentInfo.getSummary(), documentDTO.getSummary());
assertEquals(mappedDocumentInfo.getVisibility().toString(), documentDTO.getVisibility().name());
assertEquals(mappedDocumentInfo.getType().toString(), documentDTO.getType().name());
}
use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdContentPostFileWrongSource.
@Test
public void testApisApiIdDocumentsDocumentIdContentPostFileWrongSource() throws Exception {
String fileName = "swagger.json";
String contentType = "text/json";
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
FileInfo fileDetail = new FileInfo();
fileDetail.setFileName(fileName);
fileDetail.setContentType(contentType);
FileInputStream fis = null;
fis = new FileInputStream(file);
printTestMethodName();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String api1Id = UUID.randomUUID().toString();
String documentId = UUID.randomUUID().toString();
DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().fileName(fileName).sourceType(DocumentInfo.SourceType.INLINE).build();
Mockito.doReturn(documentInfo).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiPublisher).uploadDocumentationFile(documentId, fis, contentType);
Response response = apisApiService.apisApiIdDocumentsDocumentIdContentPost(api1Id, documentId, fis, fileDetail, null, null, null, getRequest());
fis.close();
assertEquals(response.getStatus(), 400);
assertTrue(response.getEntity().toString().contains("is not FILE"));
}
use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method decodeApiInformationFromDirectoryStructure.
/**
* Reads and decodes APIs and relevant information from the given set of paths
*
* @param apiArtifactsBasePath path to the directory with API related artifacts
* @param newApiProvider API newApiProvider to be updated
* @return Set of {@link APIDetails} objects
* @throws APIMgtEntityImportExportException if any error occurs while decoding the APIs
*/
public Set<APIDetails> decodeApiInformationFromDirectoryStructure(String apiArtifactsBasePath, String newApiProvider) throws APIMgtEntityImportExportException {
Set<String> apiDefinitionsRootDirectoryPaths = null;
try {
apiDefinitionsRootDirectoryPaths = APIFileUtils.getDirectoryList(apiArtifactsBasePath);
} catch (APIMgtDAOException e) {
String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
}
if (apiDefinitionsRootDirectoryPaths.isEmpty()) {
try {
APIFileUtils.deleteDirectory(path);
} catch (APIMgtDAOException e) {
log.warn("Unable to remove directory " + path);
}
String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
}
Set<APIDetails> apiDetailsSet = new HashSet<>();
for (String apiDefinitionDirectoryPath : apiDefinitionsRootDirectoryPaths) {
File apiDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX);
File swaggerDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.SWAGGER_DEFINITION_FILE_PREFIX);
API api;
String swaggerDefinition, gatewayConfiguration;
Set<Endpoint> endpoints;
try {
api = getApiDefinitionFromExtractedArchive(apiDefinitionFile.getPath());
swaggerDefinition = getSwaggerDefinitionFromExtractedArchive(swaggerDefinitionFile.getPath());
gatewayConfiguration = getGatewayConfigurationFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.GATEWAY_CONFIGURATION_DEFINITION_FILE);
endpoints = getEndpointsFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + ENDPOINTS_ROOT_DIRECTORY, api.getName(), api.getVersion());
} catch (APIManagementException e) {
log.error("Error occurred while importing api from path: " + apiDefinitionDirectoryPath, e);
// skip this API
continue;
}
if (newApiProvider != null && !newApiProvider.isEmpty()) {
// update the newApiProvider
api = new API.APIBuilder(api).provider(newApiProvider).build();
}
String documentsRootDirectory = apiDefinitionDirectoryPath + File.separator + DOCUMENTS_ROOT_DIRECTORY;
Set<DocumentInfo> documentInfoSet = getDocumentInfoFromExtractedArchive(documentsRootDirectory, api.getName(), api.getVersion());
Set<DocumentContent> documentContents = new HashSet<>();
for (DocumentInfo aDocumentInfo : documentInfoSet) {
DocumentContent aDocumentContent = getDocumentContentFromExtractedArchive(aDocumentInfo, documentsRootDirectory + File.separator + aDocumentInfo.getId());
if (aDocumentContent != null) {
documentContents.add(aDocumentContent);
}
}
InputStream thumbnailStream = null;
try {
thumbnailStream = APIFileUtils.getThumbnailImage(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME);
} catch (APIMgtDAOException e) {
// log and ignore
log.error("Error occurred while reading thumbnail image.", e);
}
APIDetails apiDetails = new APIDetails(api, swaggerDefinition);
apiDetails.setGatewayConfiguration(gatewayConfiguration);
apiDetails.setEndpoints(endpoints);
if (!documentInfoSet.isEmpty()) {
apiDetails.addDocumentInformation(documentInfoSet);
}
if (!documentContents.isEmpty()) {
apiDetails.addDocumentContents(documentContents);
}
if (thumbnailStream != null) {
apiDetails.setThumbnailStream(thumbnailStream);
}
apiDetailsSet.add(apiDetails);
}
return apiDetailsSet;
}
use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method getDocumentInfoFromExtractedArchive.
/**
* Retrieves {@link DocumentInfo} instance from the directory containing docs
*
* @param documentImportLocation path to the directory containing docs
* @param apiName API name
* @param version API version
* @return Set of {@link DocumentInfo} insjtaces
*/
private Set<DocumentInfo> getDocumentInfoFromExtractedArchive(String documentImportLocation, String apiName, String version) {
Set<DocumentInfo> documents = new HashSet<>();
File rootDocumentationDirectoryForAPI = new File(documentImportLocation);
if (!rootDocumentationDirectoryForAPI.isDirectory()) {
// no Docs!
log.debug("No documentation found for API name: " + apiName + ", version: " + version);
return documents;
}
File[] documentationDirectories = rootDocumentationDirectoryForAPI.listFiles(File::isDirectory);
if (documentationDirectories == null) {
// do docs!
log.debug("No documents found at " + documentImportLocation);
return documents;
}
for (File docDir : documentationDirectories) {
// read the 'doc.json'
String content;
try {
content = APIFileUtils.readFileContentAsText(docDir.getPath() + File.separator + DOCUMENTATION_DEFINITION_FILE);
Gson gson = new GsonBuilder().create();
documents.add(gson.fromJson(content, DocumentInfo.class));
// add the doc
} catch (APIManagementException e) {
// no need to throw, log and continue
log.error("Error in importing documentation from file: " + docDir.getPath() + " for API: " + apiName + ", version: " + version);
}
}
return documents;
}
use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.
the class MappingUtil method toDocumentDTO.
/**
* this method convert Model object into Dto
*
* @param documentInfo object containing document information
* @return DTO object containing document data
*/
public static DocumentDTO toDocumentDTO(DocumentInfo documentInfo) {
DocumentDTO documentDTO = new DocumentDTO();
documentDTO.setName(documentInfo.getName());
documentDTO.setDocumentId(documentInfo.getId());
documentDTO.setOtherTypeName(documentInfo.getOtherType());
documentDTO.setSourceType(DocumentDTO.SourceTypeEnum.fromValue(documentInfo.getSourceType().getType()));
documentDTO.setSourceUrl(documentInfo.getSourceURL());
documentDTO.setFileName(documentInfo.getFileName());
documentDTO.setSummary(documentInfo.getSummary());
documentDTO.setVisibility(DocumentDTO.VisibilityEnum.fromValue(documentInfo.getVisibility().toString()));
documentDTO.setType(DocumentDTO.TypeEnum.fromValue(documentInfo.getType().toString()));
return documentDTO;
}
Aggregations