use of org.wso2.carbon.apimgt.rest.api.publisher.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"));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.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"));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.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"));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.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;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.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;
}
Aggregations