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;
}
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);
}
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());
}
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());
}
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());
}
Aggregations