Search in sources :

Example 1 with ApiStoreSdkGenerationManager

use of org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager 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 ApiStoreSdkGenerationManager

use of org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager 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 3 with ApiStoreSdkGenerationManager

use of org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager 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)

Example 4 with ApiStoreSdkGenerationManager

use of org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager in project carbon-apimgt by wso2.

the class ApiStoreSdkGenerationManagerTestCase method testGenerateSdkForApiNullApi.

@Test(expected = APIManagementException.class)
public void testGenerateSdkForApiNullApi() 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);
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(null);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    sdkGenerationManager.generateSdkForApi(apiId, LANGUAGE, USER);
}
Also used : APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ApiStoreSdkGenerationManager

use of org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager in project carbon-apimgt by wso2.

the class SdkGenApiServiceImpl method sdkGenLanguagesGet.

/**
 * Retrieve a list of languages that support SDK generation.
 *
 * @param request     msf4j request object
 * @return A list of supported languages
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response sdkGenLanguagesGet(Request request) throws NotFoundException {
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    Set<String> languageList = sdkGenerationManager.getSdkGenLanguages().keySet();
    return Response.ok().entity(languageList).build();
}
Also used : ApiStoreSdkGenerationManager(org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager)

Aggregations

Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 File (java.io.File)2 ApiStoreSdkGenerationManager (org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager)2 API (org.wso2.carbon.apimgt.core.models.API)2 HashMap (java.util.HashMap)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 ApiStoreSdkGenerationException (org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1