Search in sources :

Example 91 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class AbstractAPIManager method getDocumentationContent.

/**
 * This method used to get the content of a documentation
 *
 * @param docId Document ID
 * @return {@link InputStream} Input stream for document content
 * @throws APIManagementException if the requested documentation content is not available
 */
public DocumentContent getDocumentationContent(String docId) throws APIManagementException {
    try {
        DocumentInfo documentInfo = getDocumentationSummary(docId);
        DocumentContent.Builder documentContentBuilder = new DocumentContent.Builder();
        if (documentInfo != null) {
            documentContentBuilder.documentInfo(documentInfo);
            if (DocumentInfo.SourceType.FILE.equals(documentInfo.getSourceType())) {
                InputStream inputStream = getApiDAO().getDocumentFileContent(docId);
                if (inputStream != null) {
                    documentContentBuilder = documentContentBuilder.fileContent(inputStream);
                } else {
                    throw new APIManagementException("Couldn't find file content of  document", ExceptionCodes.DOCUMENT_CONTENT_NOT_FOUND);
                }
            } else if (documentInfo.getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                String inlineContent = getApiDAO().getDocumentInlineContent(docId);
                if (inlineContent != null) {
                    documentContentBuilder = documentContentBuilder.inlineContent(inlineContent);
                } else {
                    throw new APIManagementException("Couldn't find inline content of  document", ExceptionCodes.DOCUMENT_CONTENT_NOT_FOUND);
                }
            }
        } else {
            throw new APIManagementException("Couldn't fnd document", ExceptionCodes.DOCUMENT_NOT_FOUND);
        }
        return documentContentBuilder.build();
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while retrieving document content";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) InputStream(java.io.InputStream) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 92 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateDocumentationDocNotExists.

@Test(description = "Documentation does not exists error when updating Documentation Info", expectedExceptions = APIManagementException.class)
public void testUpdateDocumentationDocNotExists() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo();
    Mockito.when(apiDAO.isDocumentExist(API_ID, documentInfo)).thenReturn(false);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    apiPublisher.updateDocumentation(API_ID, documentInfo);
}
Also used : ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 93 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddDocumentationInfo.

@Test(description = "Add Documentation Info")
public void testAddDocumentationInfo() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    DocumentInfo documentInfo = new DocumentInfo.Builder().fileName("sample_doc.pdf").name("howto_guide").id(DOC_ID).permission("[{\"groupId\": \"testGroup\",\"permission\":[\"READ\",\"UPDATE\",\"DELETE\"]}]").build();
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    apiPublisher.addDocumentationInfo(API_ID, documentInfo);
    Mockito.verify(apiDAO, Mockito.times(1)).addDocumentInfo(API_ID, documentInfo);
}
Also used : ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 94 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsGet.

@Test
public void testApisApiIdDocumentsGet() throws APIManagementException, NotFoundException {
    printTestMethodName();
    String apiId = UUID.randomUUID().toString();
    String documentId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    DocumentInfo documentInfo1 = TestUtil.createAPIDoc(UUID.randomUUID().toString(), "documentInfo1", "", "API1 documentation 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.PRIVATE);
    DocumentInfo documentInfo2 = TestUtil.createAPIDoc(UUID.randomUUID().toString(), "documentInfo2", "", "API1 documentation 2", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.PRIVATE);
    List<DocumentInfo> documentInfoList = new ArrayList<>();
    documentInfoList.add(documentInfo1);
    documentInfoList.add(documentInfo2);
    Mockito.when(apiStore.getAllDocumentation(apiId, 0, 10)).thenReturn(documentInfoList);
    Response response = apisApiService.apisApiIdDocumentsGet(apiId, 10, 0, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 95 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUnableToAddDocumentationException.

@Test(description = "Unable to add documentation info", expectedExceptions = APIManagementException.class)
public void testUnableToAddDocumentationException() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo();
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    Mockito.doThrow(new APIMgtDAOException("Unable to add documentation")).when(apiDAO).addDocumentInfo(API_ID, documentInfo);
    apiPublisher.addDocumentationInfo(API_ID, documentInfo);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Aggregations

Documentation (org.wso2.carbon.apimgt.api.model.Documentation)56 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Test (org.testng.annotations.Test)38 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)34 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)33 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)32 ArrayList (java.util.ArrayList)29 Resource (org.wso2.carbon.registry.core.Resource)27 HashMap (java.util.HashMap)26 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)26 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)24 API (org.wso2.carbon.apimgt.api.model.API)23 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)23 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)18 Registry (org.wso2.carbon.registry.core.Registry)18 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)17