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