use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.
the class FileBasedApplicationImportExportManager method exportApplication.
/**
* Export a given Application to a file system as zip archive.
* The export root location is given by {@link FileBasedApplicationImportExportManager#path}/exported-application.
*
* @param application Application{@link Application} to be exported
* @param exportDirectoryName Name of directory to be exported
* @return path to the exported directory with exported artifacts
* @throws APIMgtEntityImportExportException
*/
public String exportApplication(Application application, String exportDirectoryName) throws APIMgtEntityImportExportException {
String applicationArtifactBaseDirectoryPath = path + File.separator + exportDirectoryName;
try {
Files.createDirectories(Paths.get(applicationArtifactBaseDirectoryPath));
} catch (IOException e) {
String errorMsg = "Unable to create the directory for export Application at :" + applicationArtifactBaseDirectoryPath;
log.error(errorMsg, e);
throw new APIMgtEntityImportExportException(errorMsg, e);
}
Application exportApplication = application;
String applicationExportDirectory = applicationArtifactBaseDirectoryPath + File.separator + exportApplication.getName();
try {
// create directory per application
Files.createDirectories(Paths.get(applicationExportDirectory));
// export application details
exportApplicationDetailsToFileSystem(exportApplication, applicationExportDirectory);
} catch (IOException e) {
log.error("Error while exporting Application: " + exportApplication.getName(), e);
}
// Check if no application is exported
try {
if (APIFileUtils.getDirectoryList(applicationArtifactBaseDirectoryPath).isEmpty()) {
// cleanup the archive root directory
APIFileUtils.deleteDirectory(path);
}
} catch (APIManagementException e) {
String errorMsg = "Unable to find Application Details at: " + applicationArtifactBaseDirectoryPath;
throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.APPLICATION_EXPORT_ERROR);
}
return applicationArtifactBaseDirectoryPath;
}
use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.
the class FileBasedApplicationImportExportManager method parseApplicationFile.
/**
* Extracts the details of an Application from a json file
*
* @param applicationDetailsFilePath Directory which contains the json file
* @return an application object containing the details extracted from the json file
* @throws APIMgtEntityImportExportException
*/
private Application parseApplicationFile(String applicationDetailsFilePath) throws APIMgtEntityImportExportException {
String applicationDetailsString;
try {
applicationDetailsString = new String(Files.readAllBytes(Paths.get(applicationDetailsFilePath)), StandardCharsets.UTF_8);
} catch (IOException e) {
String errorMsg = "Unable to read application details from file at " + applicationDetailsFilePath;
throw new APIMgtEntityImportExportException(errorMsg, e);
}
// convert to bean
Gson gson = new GsonBuilder().create();
// returns an application object from a json string
Application applicationDetails = gson.fromJson(applicationDetailsString, Application.class);
return applicationDetails;
}
use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultApplication.
public static Application createDefaultApplication() {
// created by admin
Application application = new Application(TEST_APP_1, ADMIN);
application.setId(UUID.randomUUID().toString());
application.setDescription("This is a test application");
application.setStatus(APIMgtConstants.ApplicationStatus.APPLICATION_CREATED);
application.setPolicy(fiftyPerMinApplicationPolicy);
application.setCreatedTime(LocalDateTime.now());
application.setUpdatedUser(ADMIN);
application.setUpdatedTime(LocalDateTime.now());
return application;
}
use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsPost.
@Test
public void testApplicationsPost() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
Application application = getSampleApplication(applicationId);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
applicationKeysDTOList.add(applicationKeysDTO);
ApplicationDTO applicationDTO = new ApplicationDTO();
applicationDTO.setApplicationId(applicationId);
applicationDTO.setDescription("sample application");
applicationDTO.setName("app1");
applicationDTO.setSubscriber("subscriber");
applicationDTO.setPermission("permission");
applicationDTO.setLifeCycleStatus("APPROVED");
applicationDTO.setThrottlingTier("UNLIMITED");
applicationDTO.setToken(applicationTokenDTO);
applicationDTO.setKeys(applicationKeysDTOList);
Mockito.doThrow(new APIManagementException("Error Occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).addApplication(application);
Response response = applicationsApiService.applicationsPost(applicationDTO, request);
Assert.assertEquals(500, response.getStatus());
}
use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdPutErrorCase.
@Test
public void testApplicationsApplicationIdPutErrorCase() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
applicationKeysDTOList.add(applicationKeysDTO);
ApplicationDTO applicationDTO = new ApplicationDTO();
applicationDTO.setApplicationId(applicationId);
applicationDTO.setDescription("sample application");
applicationDTO.setName("app1");
applicationDTO.setSubscriber("subscriber");
applicationDTO.setPermission("permission");
applicationDTO.setLifeCycleStatus("APPROVED");
applicationDTO.setThrottlingTier("UNLIMITED");
applicationDTO.setToken(applicationTokenDTO);
applicationDTO.setKeys(applicationKeysDTOList);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
Mockito.when(apiStore.getApplication(applicationId, USER)).thenReturn(getSampleApplication(applicationId));
Mockito.doThrow(new APIManagementException("Error Occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).updateApplication(applicationId, getSampleApplication(applicationId));
Response response = applicationsApiService.applicationsApplicationIdPut(applicationId, applicationDTO, null, null, request);
Assert.assertEquals(500, response.getStatus());
}
Aggregations