Search in sources :

Example 16 with Documentation

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

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdContentGet.

@Test
public void testApisApiIdDocumentsDocumentIdContentGet() throws APIManagementException, NotFoundException {
    printTestMethodName();
    String apiId = 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);
    String documentIdFile = UUID.randomUUID().toString();
    String documentIdInline = UUID.randomUUID().toString();
    DocumentInfo documentInfoFile = TestUtil.createAPIDoc(documentIdFile, "documentInfoFile", "", "API1 documentation file", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.PRIVATE);
    DocumentInfo documentInfoInline = TestUtil.createAPIDoc(documentIdInline, "documentInfoInline", "", "API1 documentation inline", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    DocumentContent documentContentFIle = TestUtil.createDocContent(documentInfoFile, "Sample inline content for API1 DOC 1", null);
    DocumentContent documentContentInline = TestUtil.createDocContent(documentInfoInline, "Sample inline content for API1 DOC 2", null);
    Mockito.when(apiStore.getDocumentationContent(documentIdFile)).thenReturn(documentContentFIle);
    Mockito.when(apiStore.getDocumentationContent(documentIdInline)).thenReturn(documentContentInline);
    Response responseFile = apisApiService.apisApiIdDocumentsDocumentIdContentGet(apiId, documentIdFile, null, null, request);
    Response responseInline = apisApiService.apisApiIdDocumentsDocumentIdContentGet(apiId, documentIdInline, null, null, request);
    Assert.assertEquals(200, responseFile.getStatus());
    Assert.assertEquals(200, responseInline.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) Request(org.wso2.msf4j.Request) 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 17 with Documentation

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

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdGet.

@Test
public void testApisApiIdDocumentsDocumentIdGet() 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 documentInfoFile = TestUtil.createAPIDoc(documentId, "documentInfo", "", "API1 documentation file", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.PRIVATE);
    Mockito.when(apiStore.getDocumentationSummary(documentId)).thenReturn(documentInfoFile);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdGet(apiId, documentId, null, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) 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 18 with Documentation

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

the class APIPublisherImpl method addDocumentationInfo.

/**
 * Attach Documentation (without content) to an API
 *
 * @param apiId        UUID of API
 * @param documentInfo Document Summary
 * @return UUID of document
 * @throws APIManagementException if failed to add documentation
 */
@Override
public String addDocumentationInfo(String apiId, DocumentInfo documentInfo) throws APIManagementException {
    try {
        LocalDateTime localDateTime = LocalDateTime.now();
        DocumentInfo document;
        DocumentInfo.Builder docBuilder = new DocumentInfo.Builder(documentInfo);
        docBuilder.createdBy(getUsername());
        docBuilder.updatedBy(getUsername());
        docBuilder.createdTime(localDateTime);
        docBuilder.lastUpdatedTime(localDateTime);
        if (StringUtils.isEmpty(docBuilder.getId())) {
            docBuilder = docBuilder.id(UUID.randomUUID().toString());
        }
        if (documentInfo.getPermission() != null && !("").equals(documentInfo.getPermission())) {
            HashMap roleNamePermissionList;
            roleNamePermissionList = APIUtils.getAPIPermissionArray(documentInfo.getPermission());
            docBuilder.permissionMap(roleNamePermissionList);
        }
        document = docBuilder.build();
        if (!getApiDAO().isDocumentExist(apiId, document)) {
            getApiDAO().addDocumentInfo(apiId, document);
            return document.getId();
        } else {
            String msg = "Document already exist for the api " + apiId;
            log.error(msg);
            throw new APIManagementException(msg, ExceptionCodes.DOCUMENT_ALREADY_EXISTS);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to add documentation";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (ParseException e) {
        String errorMsg = "Unable to add documentation due to json parse error";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.JSON_PARSE_ERROR);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ParseException(org.json.simple.parser.ParseException) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 19 with Documentation

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

the class AbstractAPIManagerTestCase method testGetDocumentationContentInlineWithNullContent.

@Test(description = "Getting Documentation content when source type is INLINE and inline content is null", expectedExceptions = APIManagementException.class)
public void testGetDocumentationContentInlineWithNullContent() throws APIManagementException {
    ApiDAO apiDAO = mock(ApiDAO.class);
    AbstractAPIManager apiPublisher = getApiPublisherImpl(apiDAO);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo();
    when(apiDAO.getDocumentInfo(DOC_ID)).thenReturn(documentInfo);
    when(apiDAO.getDocumentInlineContent(DOC_ID)).thenReturn(null);
    apiPublisher.getDocumentationContent(DOC_ID);
    verify(apiDAO, times(0)).getDocumentInlineContent(DOC_ID);
}
Also used : ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 20 with Documentation

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

the class AbstractAPIManagerTestCase method testGetDocumentationSummaryException.

@Test(description = "Exception when retrieving documentation summary given the id", expectedExceptions = APIManagementException.class)
public void testGetDocumentationSummaryException() throws APIManagementException {
    ApiDAO apiDAO = mock(ApiDAO.class);
    AbstractAPIManager apiStore = getAPIStoreImpl(apiDAO);
    when(apiDAO.getDocumentInfo(UUID)).thenThrow(new APIMgtDAOException("Error occurred while retrieving documents", new SQLException()));
    apiStore.getDocumentationSummary(UUID);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) 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