Search in sources :

Example 21 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApiImportExportManager method addAPIDetails.

/**
 * Adds the API details
 *
 * @param apiDetails {@link APIDetails} instance
 * @throws APIManagementException if an error occurs while adding API details
 */
public void addAPIDetails(APIDetails apiDetails) throws APIManagementException {
    // update everything
    String swaggerDefinition = apiDetails.getSwaggerDefinition();
    String gatewayConfig = apiDetails.getGatewayConfiguration();
    Map<String, Endpoint> endpointTypeToIdMap = apiDetails.getApi().getEndpoint();
    Map<String, UriTemplate> uriTemplateMap = apiDetails.getApi().getUriTemplates();
    // endpoints
    for (Endpoint endpoint : apiDetails.getEndpoints()) {
        try {
            Endpoint existingEndpoint = apiPublisher.getEndpointByName(endpoint.getName());
            String endpointId;
            if (existingEndpoint == null) {
                // no endpoint by that name, add it
                endpointId = apiPublisher.addEndpoint(endpoint);
            } else {
                endpointId = existingEndpoint.getId();
                if (log.isDebugEnabled()) {
                    log.debug("Endpoint with id " + endpoint.getId() + " already exists, not adding again");
                }
            // endpoint with same name exists, add to endpointTypeToIdMap
            // endpointTypeToIdMap.put(endpoint.getType(), existingEndpoint.getId());
            }
            endpointTypeToIdMap.forEach((String k, Endpoint v) -> {
                if (endpoint.getName().equals(v.getName())) {
                    Endpoint replacedEndpoint = new Endpoint.Builder(v).id(endpointId).build();
                    endpointTypeToIdMap.replace(k, replacedEndpoint);
                }
            });
            uriTemplateMap.forEach(((String templateId, UriTemplate uriTemplate) -> {
                UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
                Map<String, Endpoint> uriEndpointMap = uriTemplateBuilder.getEndpoint();
                uriEndpointMap.forEach((String type, Endpoint endpoint1) -> {
                    if (endpoint.getName().equals(endpoint1.getName())) {
                        Endpoint replacedEndpoint = new Endpoint.Builder(endpoint1).id(endpointId).build();
                        uriEndpointMap.replace(type, replacedEndpoint);
                    }
                });
                uriTemplateMap.replace(templateId, uriTemplateBuilder.endpoint(uriEndpointMap).build());
            }));
        } catch (APIManagementException e) {
            // skip adding this API; log and continue
            log.error("Error while adding the endpoint with id: " + endpoint.getId() + ", type: " + endpoint.getType() + " for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
        }
    }
    API.APIBuilder apiBuilder = new API.APIBuilder(apiDetails.getApi());
    apiPublisher.addAPI(apiBuilder.apiDefinition(swaggerDefinition).gatewayConfig(gatewayConfig).endpoint(endpointTypeToIdMap).uriTemplates(uriTemplateMap));
    // docs
    try {
        Set<DocumentInfo> documentInfo = apiDetails.getAllDocumentInformation();
        for (DocumentInfo aDocInfo : documentInfo) {
            apiPublisher.addDocumentationInfo(apiDetails.getApi().getId(), aDocInfo);
        }
        for (DocumentContent aDocContent : apiDetails.getDocumentContents()) {
            // add documentation
            if (aDocContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.FILE)) {
                apiPublisher.uploadDocumentationFile(aDocContent.getDocumentInfo().getId(), aDocContent.getFileContent(), URLConnection.guessContentTypeFromStream(aDocContent.getFileContent()));
            } else if (aDocContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                apiPublisher.addDocumentationContent(aDocContent.getDocumentInfo().getId(), aDocContent.getInlineContent());
            }
        }
    } catch (APIManagementException e) {
        // no need to throw, log and continue
        log.error("Error while adding Document details for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    } catch (IOException e) {
        // no need to throw, log and continue
        log.error("Error while retrieving content type of the File documentation of API : " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    }
    // add thumbnail
    try {
        apiPublisher.saveThumbnailImage(apiDetails.getApi().getId(), apiDetails.getThumbnailStream(), "thumbnail");
    } catch (APIManagementException e) {
        // no need to throw, log and continue
        log.error("Error while adding thumbnail for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    }
}
Also used : IOException(java.io.IOException) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) 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) HashMap(java.util.HashMap) Map(java.util.Map) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 22 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdPut.

@Test
public void testApisApiIdDocumentsDocumentIdPut() throws Exception {
    printTestMethodName();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    DocumentInfo documentInfo1 = SampleTestObjectCreator.createDefaultDocumentationInfo().build();
    DocumentInfo documentInfo2 = SampleTestObjectCreator.createDefaultDocumentationInfo().id(documentInfo1.getId()).summary("My new summary").build();
    DocumentDTO documentDTO = MappingUtil.toDocumentDTO(documentInfo2);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String documentId = UUID.randomUUID().toString();
    String apiId = UUID.randomUUID().toString();
    Mockito.doReturn(documentInfo1).doReturn(documentInfo2).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
    Mockito.doReturn("updated").doThrow(new IllegalArgumentException()).when(apiPublisher).updateDocumentation(apiId, documentInfo1);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdPut(apiId, documentId, documentDTO, null, null, getRequest());
    assertEquals(response.getStatus(), 200);
    assertTrue(response.getEntity().toString().contains("My new summary"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdGet.

@Test
public void testApisApiIdDocumentsDocumentIdGet() throws Exception {
    printTestMethodName();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String documentId = UUID.randomUUID().toString();
    String apiId = UUID.randomUUID().toString();
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().build();
    Mockito.doReturn(documentInfo).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdGet(apiId, documentId, null, null, getRequest());
    assertEquals(response.getStatus(), 200);
    assertTrue(response.getEntity().toString().contains("Summary of Calculator Documentation"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdPutException.

@Test
public void testApisApiIdDocumentsDocumentIdPutException() throws Exception {
    printTestMethodName();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    DocumentInfo documentInfo1 = SampleTestObjectCreator.createDefaultDocumentationInfo().build();
    DocumentInfo documentInfo2 = SampleTestObjectCreator.createDefaultDocumentationInfo().id(documentInfo1.getId()).summary("My new summary").build();
    DocumentDTO documentDTO = MappingUtil.toDocumentDTO(documentInfo2);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String documentId = UUID.randomUUID().toString();
    String apiId = UUID.randomUUID().toString();
    Mockito.doReturn(documentInfo1).doReturn(documentInfo2).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
    Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.INVALID_DOCUMENT_CONTENT_DATA)).when(apiPublisher).updateDocumentation(apiId, documentInfo1);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdPut(apiId, documentId, documentDTO, null, null, getRequest());
    assertEquals(response.getStatus(), 400);
    assertTrue(response.getEntity().toString().contains("Invalid document content data provided"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsGet.

@Test
public void testApisApiIdDocumentsGet() throws Exception {
    printTestMethodName();
    Integer offset = 0;
    Integer limit = 10;
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String apiId = UUID.randomUUID().toString();
    List<DocumentInfo> documentInfos = new ArrayList<>();
    documentInfos.add(SampleTestObjectCreator.createDefaultDocumentationInfo().name("NewName1").build());
    documentInfos.add(SampleTestObjectCreator.createDefaultDocumentationInfo().name("NewName2").build());
    Mockito.doReturn(documentInfos).doThrow(new IllegalArgumentException()).when(apiPublisher).getAllDocumentation(apiId, offset, limit);
    Response response = apisApiService.apisApiIdDocumentsGet(apiId, 10, 0, null, getRequest());
    assertEquals(response.getStatus(), 200);
    assertTrue(response.getEntity().toString().contains("NewName1"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) ArrayList(java.util.ArrayList) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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