Search in sources :

Example 6 with ApiStoreSdkGenerationException

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

the class ApiStoreSdkGenerationManager method generateSdkForApi.

/*
        * This method generates the client side SDK for the API with API ID (apiID) and SDK language (language)
        *
        * @param apiId ID for the specific API
        * @param language preferred language to generate the SDK
        * @throws ApiStoreSdkGenerationException if failed to generate the SDK
        * */
public String generateSdkForApi(String apiId, String language, String userName) throws ApiStoreSdkGenerationException, APIManagementException {
    APIStore apiStore = APIManagerFactory.getInstance().getAPIConsumer(userName);
    API api = apiStore.getAPIbyUUID(apiId);
    if (api == null) {
        String errorMessage = "Cannot find API for specified API ID";
        throw new APIManagementException(errorMessage, ExceptionCodes.API_NOT_FOUND);
    }
    String apiName = api.getName();
    String apiVersion = api.getVersion();
    String swaggerDefinitionForApi = null;
    try {
        swaggerDefinitionForApi = apiStore.getApiSwaggerDefinition(apiId);
    } catch (APIManagementException e) {
        handleSdkGenException("Error retrieving swagger definition for API " + apiId + " from database.", e);
    }
    Swagger swaggerDoc = new SwaggerParser().parse(swaggerDefinitionForApi);
    if (swaggerDoc == null) {
        handleSdkGenException("Error while parsing retrieved swagger definition");
    }
    // Format the swagger definition as a string before writing to the file.
    String formattedSwaggerDefinitionForSdk = Json.pretty(swaggerDoc);
    Path tempSdkGenDir = null;
    File swaggerDefJsonFile = null;
    try {
        // Create a temporary directory to store the API files
        tempSdkGenDir = Files.createTempDirectory(apiName + "_" + language + "_" + apiVersion);
        // Create a temporary file to store the swagger definition
        swaggerDefJsonFile = Files.createTempFile(tempSdkGenDir, apiId + "_" + language, APIMgtConstants.APIFileUtilConstants.JSON_EXTENSION).toFile();
    } catch (IOException e) {
        handleSdkGenException("Error creating temporary directory or json file for swagger definition!", e);
    }
    String tempZipFilePath = "";
    if (swaggerDefJsonFile.exists()) {
        try (Writer swaggerFileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(swaggerDefJsonFile.getAbsoluteFile()), "UTF-8"))) {
            swaggerFileWriter.write(formattedSwaggerDefinitionForSdk);
            log.debug("Writing the swagger definition was sucessful to file {}", swaggerDefJsonFile.getAbsolutePath());
        } catch (IOException e) {
            handleSdkGenException("Error writing swagger definition to file in " + tempSdkGenDir, e);
        }
        // Generate the SDK for the specified language
        generateSdkForSwaggerDef(language, swaggerDefJsonFile.getAbsolutePath(), tempSdkGenDir.toString());
        log.debug("Generating SDK for the swagger definition {} was successful.", swaggerDefJsonFile.getAbsolutePath());
        String archiveName = apiName + "_" + language + "_" + apiVersion;
        tempZipFilePath = tempSdkGenDir + File.separator + archiveName + ".zip";
        APIFileUtils.archiveDirectory(tempSdkGenDir.toString(), tempSdkGenDir.toString(), archiveName);
        log.debug("Generating the archive was successful for directory {}.", tempSdkGenDir.toString());
    } else {
        handleSdkGenException("Swagger definition file not found!");
    }
    try {
        // Set deleteOnExit property to generated SDK directory, all sub directories and files.
        recursiveDeleteOnExit(tempSdkGenDir);
    } catch (IOException e) {
        handleSdkGenException("Error while deleting temporary directory " + tempSdkGenDir, e);
    }
    return tempZipFilePath;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) SwaggerParser(io.swagger.parser.SwaggerParser) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Swagger(io.swagger.models.Swagger) FileOutputStream(java.io.FileOutputStream) API(org.wso2.carbon.apimgt.core.models.API) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 7 with ApiStoreSdkGenerationException

use of org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException 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 8 with ApiStoreSdkGenerationException

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

the class ApisApiServiceImplTestCase method apisApiIdSdksLanguageGet.

@Test
public void apisApiIdSdksLanguageGet() throws APIManagementException, ApiStoreSdkGenerationException, NotFoundException {
    String apiId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIConsumer(USER)).thenReturn(apiStore);
    PowerMockito.mockStatic(RestApiUtil.class);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    Endpoint api1SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api1ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api = TestUtil.createApi("provider1", apiId, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", TestUtil.createEndpointTypeToIdMap(api1SandBoxEndpointId, api1ProdEndpointId)).build();
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(api);
    Mockito.when(apiStore.getApiSwaggerDefinition(apiId)).thenReturn(swaggerPetStoreCorrect);
    Response response = apisApiService.apisApiIdSdksLanguageGet(apiId, correctLanguage, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Request(org.wso2.msf4j.Request) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with ApiStoreSdkGenerationException

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

the class ApisApiServiceImplTestCase method apisApiIdSdksLanguageGetIncorrectApiId.

@Test
public void apisApiIdSdksLanguageGetIncorrectApiId() throws APIManagementException, ApiStoreSdkGenerationException, NotFoundException {
    String apiId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIConsumer(USER)).thenReturn(apiStore);
    PowerMockito.mockStatic(RestApiUtil.class);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    Endpoint api1SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api1ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api = TestUtil.createApi("provider1", apiId, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", TestUtil.createEndpointTypeToIdMap(api1SandBoxEndpointId, api1ProdEndpointId)).build();
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(api);
    Mockito.when(apiStore.getApiSwaggerDefinition(apiId)).thenReturn(swaggerPetStoreCorrect);
    Response response = apisApiService.apisApiIdSdksLanguageGet(apiId + "Error-Part", correctLanguage, request);
    Assert.assertEquals(404, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Request(org.wso2.msf4j.Request) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with ApiStoreSdkGenerationException

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

the class ApisApiServiceImplTestCase method apisApiIdSdksLanguageGetIncorrectSwagger.

@Test
public void apisApiIdSdksLanguageGetIncorrectSwagger() throws APIManagementException, ApiStoreSdkGenerationException, NotFoundException {
    String apiId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIConsumer(USER)).thenReturn(apiStore);
    PowerMockito.mockStatic(RestApiUtil.class);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    Endpoint api1SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api1ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api = TestUtil.createApi("provider1", apiId, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", TestUtil.createEndpointTypeToIdMap(api1SandBoxEndpointId, api1ProdEndpointId)).build();
    Mockito.when(apiStore.getAPIbyUUID(apiId)).thenReturn(api);
    Mockito.when(apiStore.getApiSwaggerDefinition(apiId)).thenReturn(swaggerPetStoreIncorrect);
    Response response = apisApiService.apisApiIdSdksLanguageGet(apiId, correctLanguage, request);
    Assert.assertEquals(500, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Request(org.wso2.msf4j.Request) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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