Search in sources :

Example 56 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class GatewaysApiServiceImplTestCase method gatewaysRegisterPostTest.

@Test
public void gatewaysRegisterPostTest() throws Exception {
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    RegistrationDTO registrationDTO = Mockito.mock(RegistrationDTO.class);
    LabelInfoDTO labelInfoDTO = Mockito.mock(LabelInfoDTO.class);
    Mockito.when(registrationDTO.getLabelInfo()).thenReturn(labelInfoDTO);
    RegistrationSummary registrationSummary = Mockito.mock(RegistrationSummary.class);
    Mockito.when(adminService.getRegistrationSummary()).thenReturn(registrationSummary);
    RegistrationSummary.AnalyticsInfo analyticsInfo = Mockito.mock(RegistrationSummary.AnalyticsInfo.class);
    Mockito.when(registrationSummary.getAnalyticsInfo()).thenReturn(analyticsInfo);
    RegistrationSummary.Credentials dssCredentials = Mockito.mock(RegistrationSummary.Credentials.class);
    Mockito.when(registrationSummary.getAnalyticsInfo().getDasServerCredentials()).thenReturn(dssCredentials);
    Mockito.when(dssCredentials.getUsername()).thenReturn(DSS_USERNAME);
    Mockito.when(dssCredentials.getPassword()).thenReturn(DSS_PASSWORD);
    RegistrationSummary.JWTInfo jwtInfo = Mockito.mock(RegistrationSummary.JWTInfo.class);
    Mockito.when(registrationSummary.getJwtInfo()).thenReturn(jwtInfo);
    RegistrationSummary.KeyManagerInfo keyManagerInfo = Mockito.mock(RegistrationSummary.KeyManagerInfo.class);
    Mockito.when(registrationSummary.getKeyManagerInfo()).thenReturn(keyManagerInfo);
    RegistrationSummary.Credentials keyManagerCredentials = Mockito.mock(RegistrationSummary.Credentials.class);
    Mockito.when(registrationSummary.getKeyManagerInfo().getCredentials()).thenReturn(keyManagerCredentials);
    Mockito.when(keyManagerCredentials.getUsername()).thenReturn(KEY_MANAGER_USERNAME);
    Mockito.when(keyManagerCredentials.getPassword()).thenReturn(KEY_MANAGER_PASSWORD);
    RegistrationSummary.ThrottlingInfo throttlingInfo = Mockito.mock(RegistrationSummary.ThrottlingInfo.class);
    Mockito.when(registrationSummary.getThrottlingInfo()).thenReturn(throttlingInfo);
    RegistrationSummary.ThrottlingInfo.DataPublisher dataPublisher = Mockito.mock(RegistrationSummary.ThrottlingInfo.DataPublisher.class);
    Mockito.when(registrationSummary.getThrottlingInfo().getDataPublisher()).thenReturn(dataPublisher);
    RegistrationSummary.Credentials throttlingServerCredentials = Mockito.mock(RegistrationSummary.Credentials.class);
    Mockito.when(registrationSummary.getThrottlingInfo().getDataPublisher().getCredentials()).thenReturn(throttlingServerCredentials);
    Mockito.when(throttlingServerCredentials.getUsername()).thenReturn(THROTTLE_SERVER_USERNAME);
    Mockito.when(throttlingServerCredentials.getPassword()).thenReturn(THROTTLE_SERVER_PASSWORD);
    RegistrationSummary.GoogleAnalyticsTrackingInfo googleAnalyticsTrackingInfo = Mockito.mock(RegistrationSummary.GoogleAnalyticsTrackingInfo.class);
    Mockito.when(registrationSummary.getGoogleAnalyticsTrackingInfo()).thenReturn(googleAnalyticsTrackingInfo);
    GatewaysApiServiceImpl gatewaysApiService = new GatewaysApiServiceImpl();
    Response response = gatewaysApiService.gatewaysRegisterPost(registrationDTO, "application/json", getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    CredentialsDTO analyticsInfoResponseCredentials = ((RegistrationSummaryDTO) response.getEntity()).getAnalyticsInfo().getCredentials();
    CredentialsDTO keyManagerInfoResponseCredentials = ((RegistrationSummaryDTO) response.getEntity()).getKeyManagerInfo().getCredentials();
    CredentialsDTO throttleServerInfoResponseCredentials = ((RegistrationSummaryDTO) response.getEntity()).getThrottlingInfo().getCredentials();
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertEquals(analyticsInfoResponseCredentials.getUsername(), DSS_USERNAME);
    Assert.assertEquals(analyticsInfoResponseCredentials.getPassword(), DSS_PASSWORD);
    Assert.assertEquals(keyManagerInfoResponseCredentials.getUsername(), KEY_MANAGER_USERNAME);
    Assert.assertEquals(keyManagerInfoResponseCredentials.getPassword(), KEY_MANAGER_PASSWORD);
    Assert.assertEquals(throttleServerInfoResponseCredentials.getUsername(), THROTTLE_SERVER_USERNAME);
    Assert.assertEquals(throttleServerInfoResponseCredentials.getPassword(), THROTTLE_SERVER_PASSWORD);
}
Also used : APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) LabelInfoDTO(org.wso2.carbon.apimgt.rest.api.core.dto.LabelInfoDTO) Response(javax.ws.rs.core.Response) CredentialsDTO(org.wso2.carbon.apimgt.rest.api.core.dto.CredentialsDTO) RegistrationDTO(org.wso2.carbon.apimgt.rest.api.core.dto.RegistrationDTO) RegistrationSummary(org.wso2.carbon.apimgt.core.models.RegistrationSummary) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 57 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class MappingUtilTestCase method convertToApplicationDtoListTest.

@Test
public void convertToApplicationDtoListTest() {
    List<Application> applicationList = new ArrayList<>();
    applicationList.add(SampleTestObjectCreator.createRandomApplication());
    applicationList.add(SampleTestObjectCreator.createRandomApplication());
    applicationList.add(SampleTestObjectCreator.createRandomApplication());
    List<ApplicationDTO> applicationDTOList = MappingUtil.convertToApplicationDtoList(applicationList);
    Assert.assertEquals(applicationList.size(), applicationDTOList.size());
    for (int i = 0; i < applicationList.size(); i++) {
        Assert.assertEquals(applicationList.get(i).getName(), applicationDTOList.get(i).getName());
        Assert.assertEquals(applicationList.get(i).getId(), applicationDTOList.get(i).getApplicationId());
        Assert.assertEquals(applicationList.get(i).getPolicy().getUuid(), applicationDTOList.get(i).getThrottlingTier());
        Assert.assertEquals(applicationList.get(i).getCreatedUser(), applicationDTOList.get(i).getSubscriber());
    }
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationDTO) ArrayList(java.util.ArrayList) Application(org.wso2.carbon.apimgt.core.models.Application) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 58 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsPost.

/**
 * Adds a new subscription
 *
 * @param body        Subscription details to be added
 * @param request     msf4j request object
 * @return Newly added subscription as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsPost(SubscriptionDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    SubscriptionDTO subscriptionDTO = null;
    URI location = null;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String applicationId = body.getApplicationId();
        String apiId = body.getApiIdentifier();
        String tier = body.getPolicy();
        Application application = apiStore.getApplicationByUuid(applicationId);
        if (application != null && !ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
            String errorMessage = "Application " + applicationId + " is not active";
            ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
            APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
            Map<String, String> paramList = new HashMap<>();
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        if (application != null) {
            SubscriptionResponse addSubResponse = apiStore.addApiSubscription(apiId, applicationId, tier);
            String subscriptionId = addSubResponse.getSubscriptionUUID();
            Subscription subscription = apiStore.getSubscriptionByUUID(subscriptionId);
            location = new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTION + "/" + subscriptionId);
            subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscription);
            // be in either pending or approved state) send back the workflow response
            if (SubscriptionStatus.ON_HOLD == subscription.getStatus()) {
                WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(addSubResponse.getWorkflowResponse());
                return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
            }
        } else {
            String errorMessage = null;
            ExceptionCodes exceptionCode = null;
            exceptionCode = ExceptionCodes.APPLICATION_NOT_FOUND;
            errorMessage = "Application not found";
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, exceptionCode);
            Map<String, String> paramList = new HashMap<>();
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (GatewayException e) {
        String errorMessage = "Failed to add subscription of API : " + body.getApiIdentifier() + " to gateway";
        log.error(errorMessage, e);
        return Response.status(Response.Status.ACCEPTED).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding subscriptions";
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiIdentifier());
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, body.getApplicationId());
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER, body.getPolicy());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while adding location header in response for subscription : " + body.getSubscriptionId();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, body.getSubscriptionId());
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(subscriptionDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) ExceptionCodes(org.wso2.carbon.apimgt.core.exception.ExceptionCodes) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO) Application(org.wso2.carbon.apimgt.core.models.Application) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 59 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class ApplicationImportExportManager method updateApplication.

/**
 * Update details of an existing Application when imported
 *
 * @param importedApplication
 * @param username
 * @throws APIManagementException
 */
public Application updateApplication(Application importedApplication, String username) throws APIManagementException {
    Application updatedApp = null;
    try {
        if (getApplicationDAO().isApplicationNameExists(importedApplication.getName())) {
            Application existingApplication = apiStore.getApplicationByName(importedApplication.getName(), username);
            apiStore.updateApplication(existingApplication.getUuid(), importedApplication);
            updatedApp = apiStore.getApplication(existingApplication.getUuid(), username);
        }
        return updatedApp;
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while finding application matching the provided name";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 60 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class FileBasedApplicationImportExportManager method importApplication.

/**
 * Import a given Application from an InputStream
 *
 * @param uploadedAppArchiveInputStream Content stream of the zip file which contains exported Application
 * @return the imported application
 * @throws APIMgtEntityImportExportException
 */
public Application importApplication(InputStream uploadedAppArchiveInputStream) throws APIMgtEntityImportExportException {
    String appArchiveLocation = path + File.separator + IMPORTED_APPLICATIONS_DIRECTORY_NAME + ".zip";
    String archiveExtractLocation = null;
    try {
        archiveExtractLocation = extractUploadedArchiveApplication(uploadedAppArchiveInputStream, IMPORTED_APPLICATIONS_DIRECTORY_NAME, appArchiveLocation, path);
    } catch (APIManagementException e) {
        String errorMsg = "Error in accessing uploaded Application archive" + appArchiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.APPLICATION_IMPORT_ERROR);
    }
    Application applicationDetails = parseApplicationFile(archiveExtractLocation);
    return applicationDetails;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

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