Search in sources :

Example 1 with ApiStoreSdkGenerationException

use of org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdSdksLanguageGet.

/**
 * Generates an SDK for a API with provided ID and language
 *
 * @param apiId   API ID
 * @param language   SDK language
 * @param request msf4j request object
 * @return ZIP file for the generated SDK
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdSdksLanguageGet(String apiId, String language, Request request) throws NotFoundException {
    if (StringUtils.isBlank(apiId) || StringUtils.isBlank(language)) {
        String errorMessage = "API ID or language is not valid";
        log.error(errorMessage);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    String userName = RestApiUtil.getLoggedInUsername(request);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    if (!sdkGenerationManager.getSdkGenLanguages().containsKey(language)) {
        String errorMessage = "Specified language parameter doesn't exist";
        log.error(errorMessage);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    String tempZipFilePath;
    try {
        tempZipFilePath = sdkGenerationManager.generateSdkForApi(apiId, language, userName);
    } catch (ApiStoreSdkGenerationException e) {
        String errorMessage = "Error while generating SDK for requested language";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API for SDK generation " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    File sdkZipFile = new File(tempZipFilePath);
    return Response.ok().entity(sdkZipFile).header("Content-Disposition", "attachment; filename=\"" + sdkZipFile.getName() + "\"").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) ApiStoreSdkGenerationManager(org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager) File(java.io.File) ApiStoreSdkGenerationException(org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException)

Example 2 with ApiStoreSdkGenerationException

use of org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method apisApiIdSdksLanguageGetNullApiId.

@Test
public void apisApiIdSdksLanguageGetNullApiId() throws APIManagementException, ApiStoreSdkGenerationException, NotFoundException {
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    Request request = getRequest();
    Response response = apisApiService.apisApiIdSdksLanguageGet(null, correctLanguage, request);
    Assert.assertEquals(400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ApiStoreSdkGenerationException

use of org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method apisApiIdSdksLanguageGetIncorrectLanguage.

@Test
public void apisApiIdSdksLanguageGetIncorrectLanguage() throws APIManagementException, ApiStoreSdkGenerationException, NotFoundException {
    String apiId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    Request request = getRequest();
    Response response = apisApiService.apisApiIdSdksLanguageGet(apiId, incorrectLanguage, request);
    Assert.assertEquals(400, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ApiStoreSdkGenerationException

use of org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException in project carbon-apimgt by wso2.

the class ApiStoreSdkGenerationManagerTestCase method testGenerateSdkForApi.

@Test
public void testGenerateSdkForApi() throws APIManagementException, ApiStoreSdkGenerationException {
    String apiId = UUID.randomUUID().toString();
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    Mockito.when(instance.getAPIConsumer(USER)).thenReturn(apiStore);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(api);
    Mockito.when(apiStore.getApiSwaggerDefinition(apiId)).thenReturn(swaggerPetStore);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    String pathToZip = sdkGenerationManager.generateSdkForApi(apiId, LANGUAGE, USER);
    File sdkZipFile = new File(pathToZip);
    Assert.assertTrue(sdkZipFile.exists() && sdkZipFile.length() > MIN_SDK_SIZE);
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ApiStoreSdkGenerationException

use of org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException in project carbon-apimgt by wso2.

the class ApiStoreSdkGenerationManagerTestCase method testGenerateSdkForApiIncorrectSwagger.

@Test(expected = NullPointerException.class)
public void testGenerateSdkForApiIncorrectSwagger() throws APIManagementException, ApiStoreSdkGenerationException {
    String apiId = UUID.randomUUID().toString();
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    Mockito.when(instance.getAPIConsumer(USER)).thenReturn(apiStore);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(api);
    Mockito.when(apiStore.getApiSwaggerDefinition(apiId)).thenReturn(null);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    sdkGenerationManager.generateSdkForApi(apiId, LANGUAGE, USER);
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 API (org.wso2.carbon.apimgt.core.models.API)6 Response (javax.ws.rs.core.Response)5 Request (org.wso2.msf4j.Request)5 File (java.io.File)3 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)3 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 Swagger (io.swagger.models.Swagger)1 SwaggerParser (io.swagger.parser.SwaggerParser)1 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 ApiStoreSdkGenerationException (org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException)1