Search in sources :

Example 76 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testRemoveSubscription.

@Test
public void testRemoveSubscription() throws APIManagementException, WorkflowException, APIPersistenceException {
    String uuid = UUID.randomUUID().toString();
    String apiUUID = UUID.randomUUID().toString();
    Subscriber subscriber = new Subscriber("sub1");
    Application application = new Application("app1", subscriber);
    application.setId(1);
    APIIdentifier identifier = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
    identifier.setUuid(apiUUID);
    SubscribedAPI subscribedAPIOld = new SubscribedAPI(subscriber, identifier);
    subscribedAPIOld.setApplication(application);
    Mockito.when(apiMgtDAO.isAppAllowed(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    Mockito.when(apiMgtDAO.checkAPIUUIDIsARevisionUUID(Mockito.anyString())).thenReturn(null);
    DevPortalAPI devPortalAPI = Mockito.mock(DevPortalAPI.class);
    Mockito.when(apiPersistenceInstance.getDevPortalAPI(any(Organization.class), any(String.class))).thenReturn(devPortalAPI);
    SubscribedAPI subscribedAPINew = new SubscribedAPI(subscriber, identifier);
    subscribedAPINew.setUUID(uuid);
    subscribedAPINew.setApplication(application);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, apiPersistenceInstance);
    apiConsumer.removeSubscription(subscribedAPINew, "org1");
    Mockito.verify(apiMgtDAO, Mockito.times(1)).getApplicationNameFromId(Mockito.anyInt());
    String workflowExtRef = "test_wf_ref";
    String workflowExtRef1 = "complete_wf_ref";
    Mockito.when(apiMgtDAO.getExternalWorkflowReferenceForSubscription((APIIdentifier) Mockito.any(), Mockito.anyInt(), Mockito.anyString())).thenReturn(workflowExtRef, workflowExtRef1);
    SubscriptionWorkflowDTO subscriptionWorkflowDTO = new SubscriptionWorkflowDTO();
    subscriptionWorkflowDTO.setWorkflowReference("1");
    Mockito.when(apiMgtDAO.retrieveWorkflow(workflowExtRef)).thenReturn(subscriptionWorkflowDTO);
    SubscribedAPI subscribedAPI = new SubscribedAPI("api1");
    subscribedAPI.setTier(new Tier("Gold"));
    Mockito.when(apiMgtDAO.getSubscriptionById(Mockito.anyInt())).thenReturn(subscribedAPI);
    Mockito.when(apiMgtDAO.getSubscriptionStatus(uuid, 1)).thenReturn(APIConstants.SubscriptionStatus.ON_HOLD);
    apiConsumer.removeSubscription(subscribedAPINew, "org1");
    Mockito.when(apiMgtDAO.retrieveWorkflow(workflowExtRef1)).thenReturn(subscriptionWorkflowDTO);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
    apiConsumer.removeSubscription(subscribedAPINew, "org1");
    Mockito.verify(apiMgtDAO, Mockito.times(2)).retrieveWorkflow(Mockito.anyString());
}
Also used : DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Matchers.anyString(org.mockito.Matchers.anyString) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method updateAPIforStateChange.

public boolean updateAPIforStateChange(API api, String currentStatus, String newStatus) throws APIManagementException, FaultGatewaysException {
    boolean isSuccess = false;
    String provider = api.getId().getProviderName();
    String providerTenantMode = api.getId().getProviderName();
    provider = APIUtil.replaceEmailDomain(provider);
    String name = api.getId().getApiName();
    String version = api.getId().getVersion();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        // API api = getAPI(apiId);
        if (api != null) {
            if (!currentStatus.equals(newStatus)) {
                api.setStatus(newStatus);
                // If API status changed to publish we should add it to recently added APIs list
                // this should happen in store-publisher cluster domain if deployment is distributed
                // IF new API published we will add it to recently added APIs
                Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.RECENTLY_ADDED_API_CACHE_NAME).removeAll();
                api.setAsPublishedDefaultVersion(api.getId().getVersion().equals(apiMgtDAO.getPublishedDefaultVersion(api.getId())));
                if (APIConstants.RETIRED.equals(newStatus)) {
                    cleanUpPendingSubscriptionCreationProcessesByAPI(api.getUuid());
                    apiMgtDAO.removeAllSubscriptions(api.getUuid());
                    deleteAPIRevisions(api.getUuid(), tenantDomain);
                }
                // updateApiArtifactNew(api, false, false);
                PublisherAPI publisherAPI = APIMapper.INSTANCE.toPublisherApi(api);
                try {
                    apiPersistenceInstance.updateAPI(new Organization(api.getOrganization()), publisherAPI);
                } catch (APIPersistenceException e) {
                    handleException("Error while persisting the updated API ", e);
                }
            }
            isSuccess = true;
        } else {
            handleException("Couldn't find an API with the name-" + name + "version-" + version);
        }
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return isSuccess;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)

Example 78 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method searchPaginatedAPIs.

@Override
public Map<String, Object> searchPaginatedAPIs(String searchQuery, String organization, int start, int end, String sortBy, String sortOrder) throws APIManagementException {
    Map<String, Object> result = new HashMap<String, Object>();
    if (log.isDebugEnabled()) {
        log.debug("Original search query received : " + searchQuery);
    }
    Organization org = new Organization(organization);
    String[] roles = APIUtil.getFilteredUserRoles(userNameWithoutChange);
    Map<String, Object> properties = APIUtil.getUserProperties(userNameWithoutChange);
    UserContext userCtx = new UserContext(userNameWithoutChange, org, properties, roles);
    try {
        PublisherAPISearchResult searchAPIs = apiPersistenceInstance.searchAPIsForPublisher(org, searchQuery, start, end, userCtx, sortBy, sortOrder);
        if (log.isDebugEnabled()) {
            log.debug("searched APIs for query : " + searchQuery + " :-->: " + searchAPIs.toString());
        }
        Set<Object> apiSet = new LinkedHashSet<>();
        if (searchAPIs != null) {
            List<PublisherAPIInfo> list = searchAPIs.getPublisherAPIInfoList();
            List<Object> apiList = new ArrayList<>();
            for (PublisherAPIInfo publisherAPIInfo : list) {
                API mappedAPI = APIMapper.INSTANCE.toApi(publisherAPIInfo);
                populateAPIStatus(mappedAPI);
                populateDefaultVersion(mappedAPI);
                apiList.add(mappedAPI);
            }
            apiSet.addAll(apiList);
            result.put("apis", apiSet);
            result.put("length", searchAPIs.getTotalAPIsCount());
            result.put("isMore", true);
        } else {
            result.put("apis", apiSet);
            result.put("length", 0);
            result.put("isMore", false);
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while searching the api ", e);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) ArrayList(java.util.ArrayList) PublisherAPIInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPISearchResult) JSONObject(org.json.simple.JSONObject) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)

Example 79 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class APIConsumerImpl method getAPIorAPIProductByUUID.

@Override
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String organization) throws APIManagementException {
    try {
        Organization org = new Organization(organization);
        DevPortalAPI devPortalApi = apiPersistenceInstance.getDevPortalAPI(org, uuid);
        if (devPortalApi != null) {
            checkVisibilityPermission(userNameWithoutChange, devPortalApi.getVisibility(), devPortalApi.getVisibleRoles());
            if (APIConstants.API_PRODUCT.equalsIgnoreCase(devPortalApi.getType())) {
                APIProduct apiProduct = APIMapper.INSTANCE.toApiProduct(devPortalApi);
                apiProduct.setID(new APIProductIdentifier(devPortalApi.getProviderName(), devPortalApi.getApiName(), devPortalApi.getVersion()));
                populateAPIProductInformation(uuid, organization, apiProduct);
                populateAPIStatus(apiProduct);
                apiProduct = addTiersToAPI(apiProduct, organization);
                return new ApiTypeWrapper(apiProduct);
            } else {
                API api = APIMapper.INSTANCE.toApi(devPortalApi);
                populateDevPortalAPIInformation(uuid, organization, api);
                populateDefaultVersion(api);
                populateAPIStatus(api);
                api = addTiersToAPI(api, organization);
                return new ApiTypeWrapper(api);
            }
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException | OASPersistenceException | ParseException e) {
        String msg = "Failed to get API";
        throw new APIManagementException(msg, e);
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) ParseException(org.json.simple.parser.ParseException)

Example 80 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class APIConsumerImpl method getLightweightAPIByUUID.

/**
 * Get minimal details of API by registry artifact id
 *
 * @param uuid Registry artifact id
 * @param organization identifier of the organization
 * @return API of the provided artifact id
 * @throws APIManagementException
 */
@Override
public API getLightweightAPIByUUID(String uuid, String organization) throws APIManagementException {
    try {
        Organization org = new Organization(organization);
        DevPortalAPI devPortalApi = apiPersistenceInstance.getDevPortalAPI(org, uuid);
        if (devPortalApi != null) {
            checkVisibilityPermission(userNameWithoutChange, devPortalApi.getVisibility(), devPortalApi.getVisibleRoles());
            API api = APIMapper.INSTANCE.toApi(devPortalApi);
            // / populate relavant external info
            // environment
            String environmentString = null;
            if (api.getEnvironments() != null) {
                environmentString = String.join(",", api.getEnvironments());
            }
            api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
            // CORS . if null is returned, set default config from the configuration
            if (api.getCorsConfiguration() == null) {
                api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
            }
            return api;
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException e) {
        String msg = "Failed to get API with uuid " + uuid;
        throw new APIManagementException(msg, e);
    }
}
Also used : DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)83 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)55 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)49 Registry (org.wso2.carbon.registry.core.Registry)49 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)47 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 Resource (org.wso2.carbon.registry.core.Resource)40 API (org.wso2.carbon.apimgt.api.model.API)35 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)34 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)32 Test (org.junit.Test)29 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)27 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 ArrayList (java.util.ArrayList)21 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)19 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)18 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)16