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