Search in sources :

Example 16 with DocumentInfo

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());
}
Also used : DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 17 with DocumentInfo

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"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) FileInfo(org.wso2.msf4j.formparam.FileInfo) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) File(java.io.File) FileInputStream(java.io.FileInputStream) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with DocumentInfo

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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) InputStream(java.io.InputStream) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) HashSet(java.util.HashSet) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 19 with DocumentInfo

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) File(java.io.File) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) HashSet(java.util.HashSet)

Example 20 with DocumentInfo

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;
}
Also used : DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO)

Aggregations

DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)71 Test (org.testng.annotations.Test)28 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)28 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 Response (javax.ws.rs.core.Response)21 Test (org.junit.Test)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)20 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)18 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)18 API (org.wso2.carbon.apimgt.core.models.API)17 DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)16 HashMap (java.util.HashMap)14 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO)12 ArrayList (java.util.ArrayList)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)9 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)9 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)9 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)8 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)7