Search in sources :

Example 21 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentPost.

/**
 * Uploads a document's content and attach to particular document
 *
 * @param apiId             UUID of API
 * @param documentId        UUID of the document
 * @param fileInputStream   file content stream
 * @param fileDetail        meta infomation about the file
 * @param inlineContent     inline documentation content
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return updated document meta information
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsDocumentIdContentPost(String apiId, String documentId, InputStream fileInputStream, FileInfo fileDetail, String inlineContent, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiProvider = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        if (fileInputStream != null && inlineContent != null) {
            String msg = "Only one of 'file' and 'inlineContent' should be specified";
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        // retrieves the document and send 404 if not found
        DocumentInfo documentation = apiProvider.getDocumentationSummary(documentId);
        if (documentation == null) {
            String msg = "Documentation not found " + documentId;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
        // add content depending on the availability of either input stream or inline content
        if (fileInputStream != null) {
            if (!documentation.getSourceType().equals(DocumentInfo.SourceType.FILE)) {
                String msg = "Source type of document " + documentId + " is not FILE";
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
                log.error(msg);
                return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
            }
            apiProvider.uploadDocumentationFile(documentId, fileInputStream, fileDetail.getContentType());
        } else if (inlineContent != null) {
            if (!documentation.getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                String msg = "Source type of document " + documentId + " is not INLINE";
                log.error(msg);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900976L, msg);
                return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
            }
            apiProvider.addDocumentationContent(documentId, inlineContent);
        } else {
            String msg = "Either 'file' or 'inlineContent' should be specified";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900976L, msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        String newFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, null, null, request);
        return Response.status(Response.Status.CREATED).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding content to document" + documentId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.DOC_ID, documentId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 22 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdImplementationPut.

@Test
public void testCompositeApisApiIdImplementationPut() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String apiID = UUID.randomUUID().toString();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    InputStream implmentation = null;
    FileInfo fileInfo = new FileInfo();
    fileInfo.setFileName("sample.txt");
    fileInfo.setContentType(contentType);
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiStore).updateCompositeApiImplementation(apiID, implmentation);
    Response response = compositeApisApiService.compositeApisApiIdImplementationPut(apiID, implmentation, fileInfo, null, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) FileInfo(org.wso2.msf4j.formparam.FileInfo) InputStream(java.io.InputStream) Request(org.wso2.msf4j.Request) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdWsdlPutFile.

@Test
public void testApisApiIdWsdlPutFile() throws Exception {
    printTestMethodName();
    File file = new File(getClass().getClassLoader().getResource(WSDL_FILE_LOCATION).getFile());
    FileInfo fileInfo = new FileInfo();
    fileInfo.setFileName(WSDL_FILE);
    InputStream inputStream = new FileInputStream(file);
    String fileContent = IOUtils.toString(inputStream);
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = powerMockDefaultAPIPublisher();
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Mockito.doReturn(fileContent).when(apiPublisher).updateAPIWSDL(api.getId(), inputStream);
    Response response = apisApiService.apisApiIdWsdlPut(api.getId(), inputStream, fileInfo, null, null, getRequest());
    assertEquals(response.getStatus(), 200);
    assertTrue(response.getEntity().toString().contains("StockQuote"));
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) FileInfo(org.wso2.msf4j.formparam.FileInfo) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdContentPostFile.

@Test
public void testApisApiIdDocumentsDocumentIdContentPostFile() throws Exception {
    String fileName = "swagger.json";
    String contentType = "text/json";
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource(fileName).getFile());
    FileInfo fileDetail = new FileInfo();
    fileDetail.setFileName(fileName);
    fileDetail.setContentType(contentType);
    FileInputStream fis = null;
    fis = new FileInputStream(file);
    printTestMethodName();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
    String api1Id = UUID.randomUUID().toString();
    String documentId = UUID.randomUUID().toString();
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().fileName(fileName).sourceType(DocumentInfo.SourceType.FILE).build();
    Mockito.doReturn(documentInfo).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiPublisher).uploadDocumentationFile(documentId, fis, contentType);
    Response response = apisApiService.apisApiIdDocumentsDocumentIdContentPost(api1Id, documentId, fis, fileDetail, null, null, null, getRequest());
    fis.close();
    assertEquals(response.getStatus(), 201);
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) FileInfo(org.wso2.msf4j.formparam.FileInfo) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) File(java.io.File) FileInputStream(java.io.FileInputStream) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisValidateDefinitionPostWSDLFileInvalidExtension.

@Test
public void testApisValidateDefinitionPostWSDLFileInvalidExtension() throws Exception {
    printTestMethodName();
    File file = new File(getClass().getClassLoader().getResource("swagger.json").getFile());
    FileInputStream fis = new FileInputStream(file);
    FileInfo fileInfo = new FileInfo();
    fileInfo.setFileName("swagger.json");
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    powerMockDefaultAPIPublisher();
    Response response = apisApiService.apisValidateDefinitionPost(WSDL, fis, fileInfo, null, getRequest());
    fis.close();
    assertEquals(response.getStatus(), 400);
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) FileInfo(org.wso2.msf4j.formparam.FileInfo) File(java.io.File) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Response (javax.ws.rs.core.Response)24 Test (org.junit.Test)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 FileInfo (org.wso2.msf4j.formparam.FileInfo)22 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)21 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)21 File (java.io.File)19 FileInputStream (java.io.FileInputStream)19 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)17 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 API (org.wso2.carbon.apimgt.core.models.API)10 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)6 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDTO)6 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)3 Request (org.wso2.msf4j.Request)3 IOException (java.io.IOException)2