Search in sources :

Example 21 with DocumentDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdPutUrlEmpty.

@Test
public void testApisApiIdDocumentsDocumentIdPutUrlEmpty() throws Exception {
    printTestMethodName();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().sourceType(DocumentInfo.SourceType.URL).build();
    DocumentDTO documentDTO = MappingUtil.toDocumentDTO(documentInfo);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String documentId = UUID.randomUUID().toString();
    String apiId = UUID.randomUUID().toString();
    Mockito.doReturn(documentInfo).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdPut(apiId, documentId, documentDTO, null, null, getRequest());
    assertEquals(response.getStatus(), 400);
    assertTrue(response.getEntity().toString().contains("Invalid document sourceUrl Format"));
}
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 22 with DocumentDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsPostException.

@Test
public void testApisApiIdDocumentsPostException() 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 apiId = UUID.randomUUID().toString();
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().sourceType(DocumentInfo.SourceType.INLINE).build();
    DocumentDTO documentDTO = MappingUtil.toDocumentDTO(documentInfo);
    Mockito.doThrow(new APIManagementException("Error occurred", ExceptionCodes.INVALID_DOCUMENT_CONTENT_DATA)).when(apiPublisher).addDocumentationInfo(apiId, documentInfo);
    Response response = apisApiService.apisApiIdDocumentsPost(apiId, 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 23 with DocumentDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdPutNotFound.

@Test
public void testApisApiIdDocumentsDocumentIdPutNotFound() throws Exception {
    printTestMethodName();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().build();
    DocumentDTO documentDTO = MappingUtil.toDocumentDTO(documentInfo);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String documentId = UUID.randomUUID().toString();
    String apiId = UUID.randomUUID().toString();
    Mockito.doReturn(null).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdPut(apiId, documentId, documentDTO, null, null, getRequest());
    assertEquals(response.getStatus(), 404);
    assertTrue(response.getEntity().toString().contains("Error while getting document"));
}
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 24 with DocumentDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method getAPIProductDocument.

@Override
public Response getAPIProductDocument(String apiProductId, String documentId, String accept, String ifNoneMatch, MessageContext messageContext) {
    Documentation documentation;
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        documentation = apiProvider.getDocumentation(apiProductId, documentId, organization);
        APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
        if (documentation == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
        }
        DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
        return Response.ok().entity(documentDTO).build();
    } catch (APIManagementException e) {
        // Auth failure occurs when cross tenant accessing API Products. Sends 404, since we don't need to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving document : " + documentId + " of API Product " + apiProductId, e, log);
        } else {
            String errorMessage = "Error while retrieving document : " + documentId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 25 with DocumentDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentDTO in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method addAPIProductDocument.

@Override
public Response addAPIProductDocument(String apiProductId, DocumentDTO body, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        if (body.getType() == null) {
            throw new BadRequestException();
        }
        if (body.getType() == DocumentDTO.TypeEnum.OTHER && org.apache.commons.lang3.StringUtils.isBlank(body.getOtherTypeName())) {
            // check otherTypeName for not null if doc type is OTHER
            RestApiUtil.handleBadRequest("otherTypeName cannot be empty if type is OTHER.", log);
        }
        String sourceUrl = body.getSourceUrl();
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (org.apache.commons.lang3.StringUtils.isBlank(sourceUrl) || !RestApiCommonUtil.isURL(sourceUrl))) {
            RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
        }
        Documentation documentation = DocumentationMappingUtil.fromDTOtoDocumentation(body);
        String documentName = body.getName();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // this will fail if user does not have access to the API Product or the API Product does not exist
        APIProductIdentifier productIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
        if (apiProvider.isDocumentationExist(apiProductId, documentName, organization)) {
            String errorMessage = "Requested document '" + documentName + "' already exists";
            RestApiUtil.handleResourceAlreadyExistsError(errorMessage, log);
        }
        documentation = apiProvider.addDocumentation(apiProductId, documentation, organization);
        DocumentDTO newDocumentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
        String uriString = RestApiConstants.RESOURCE_PATH_PRODUCT_DOCUMENTS_DOCUMENT_ID.replace(RestApiConstants.APIPRODUCTID_PARAM, apiProductId).replace(RestApiConstants.DOCUMENTID_PARAM, documentation.getId());
        URI uri = new URI(uriString);
        return Response.created(uri).entity(newDocumentDTO).build();
    } catch (APIManagementException e) {
        // Auth failure occurs when cross tenant accessing API Products. Sends 404, since we don't need to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while adding documents of API : " + apiProductId, e, log);
        } else {
            String errorMessage = "Error while adding the document for API : " + apiProductId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving location for document " + body.getName() + " of API " + apiProductId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO) BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Aggregations

DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)13 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)12 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)11 Response (javax.ws.rs.core.Response)9 Test (org.junit.Test)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)9 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)9 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)9 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.DocumentDTO)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)4 HashMap (java.util.HashMap)3 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 DocumentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.DocumentDTO)3