Search in sources :

Example 61 with Application

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 62 with Application

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;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 63 with Application

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;
}
Also used : Application(org.wso2.carbon.apimgt.core.models.Application)

Example 64 with 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());
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) Response(javax.ws.rs.core.Response) ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 65 with Application

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());
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) Response(javax.ws.rs.core.Response) ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.testng.annotations.Test)156 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)143 Application (org.wso2.carbon.apimgt.api.model.Application)130 Application (org.wso2.carbon.apimgt.core.models.Application)121 Test (org.junit.Test)102 ArrayList (java.util.ArrayList)98 SQLException (java.sql.SQLException)94 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)94 PreparedStatement (java.sql.PreparedStatement)88 Connection (java.sql.Connection)83 ResultSet (java.sql.ResultSet)73 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)71 HashMap (java.util.HashMap)70 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)63 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)63 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)61 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)60 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)59 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)58 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)57